Coverage Report

Created: 2025-08-28 07:07

/src/openssl32/crypto/asn1/a_strex.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include <string.h>
12
#include "internal/cryptlib.h"
13
#include "internal/sizes.h"
14
#include "crypto/asn1.h"
15
#include <openssl/crypto.h>
16
#include <openssl/x509.h>
17
#include <openssl/asn1.h>
18
19
#include "charmap.h"
20
21
/*
22
 * ASN1_STRING_print_ex() and X509_NAME_print_ex(). Enhanced string and name
23
 * printing routines handling multibyte characters, RFC2253 and a host of
24
 * other options.
25
 */
26
27
9.14M
#define CHARTYPE_BS_ESC         (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
28
29
406k
#define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
30
406k
                  ASN1_STRFLGS_ESC_2254 | \
31
406k
                  ASN1_STRFLGS_ESC_QUOTE | \
32
406k
                  ASN1_STRFLGS_ESC_CTRL | \
33
406k
                  ASN1_STRFLGS_ESC_MSB)
34
35
/*
36
 * Three IO functions for sending data to memory, a BIO and a FILE
37
 * pointer.
38
 */
39
static int send_bio_chars(void *arg, const void *buf, int len)
40
11.6M
{
41
11.6M
    if (!arg)
42
5.02M
        return 1;
43
6.59M
    if (BIO_write(arg, buf, len) != len)
44
0
        return 0;
45
6.59M
    return 1;
46
6.59M
}
47
48
#ifndef OPENSSL_NO_STDIO
49
static int send_fp_chars(void *arg, const void *buf, int len)
50
0
{
51
0
    if (!arg)
52
0
        return 1;
53
0
    if (fwrite(buf, 1, len, arg) != (unsigned int)len)
54
0
        return 0;
55
0
    return 1;
56
0
}
57
#endif
58
59
typedef int char_io (void *arg, const void *buf, int len);
60
61
/*
62
 * This function handles display of strings, one character at a time. It is
63
 * passed an unsigned long for each character because it could come from 2 or
64
 * even 4 byte forms.
65
 */
66
67
static int do_esc_char(unsigned long c, unsigned short flags, char *do_quotes,
68
                       char_io *io_ch, void *arg)
69
9.77M
{
70
9.77M
    unsigned short chflgs;
71
9.77M
    unsigned char chtmp;
72
9.77M
    char tmphex[HEX_SIZE(long) + 3];
73
74
9.77M
    if (c > 0xffffffffL)
75
0
        return -1;
76
9.77M
    if (c > 0xffff) {
77
518k
        BIO_snprintf(tmphex, sizeof(tmphex), "\\W%08lX", c);
78
518k
        if (!io_ch(arg, tmphex, 10))
79
0
            return -1;
80
518k
        return 10;
81
518k
    }
82
9.25M
    if (c > 0xff) {
83
108k
        BIO_snprintf(tmphex, sizeof(tmphex), "\\U%04lX", c);
84
108k
        if (!io_ch(arg, tmphex, 6))
85
0
            return -1;
86
108k
        return 6;
87
108k
    }
88
9.14M
    chtmp = (unsigned char)c;
89
9.14M
    if (chtmp > 0x7f)
90
2.83M
        chflgs = flags & ASN1_STRFLGS_ESC_MSB;
91
6.31M
    else
92
6.31M
        chflgs = char_type[chtmp] & flags;
93
9.14M
    if (chflgs & CHARTYPE_BS_ESC) {
94
        /* If we don't escape with quotes, signal we need quotes */
95
92.4k
        if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
96
86.5k
            if (do_quotes)
97
43.2k
                *do_quotes = 1;
98
86.5k
            if (!io_ch(arg, &chtmp, 1))
99
0
                return -1;
100
86.5k
            return 1;
101
86.5k
        }
102
5.90k
        if (!io_ch(arg, "\\", 1))
103
0
            return -1;
104
5.90k
        if (!io_ch(arg, &chtmp, 1))
105
0
            return -1;
106
5.90k
        return 2;
107
5.90k
    }
108
9.05M
    if (chflgs & (ASN1_STRFLGS_ESC_CTRL
109
9.05M
                  | ASN1_STRFLGS_ESC_MSB
110
9.05M
                  | ASN1_STRFLGS_ESC_2254)) {
111
2.49M
        BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
112
2.49M
        if (!io_ch(arg, tmphex, 3))
113
0
            return -1;
114
2.49M
        return 3;
115
2.49M
    }
116
    /*
117
     * If we get this far and do any escaping at all must escape the escape
118
     * character itself: backslash.
119
     */
120
6.55M
    if (chtmp == '\\' && (flags & ESC_FLAGS)) {
121
0
        if (!io_ch(arg, "\\\\", 2))
122
0
            return -1;
123
0
        return 2;
124
0
    }
125
6.55M
    if (!io_ch(arg, &chtmp, 1))
126
0
        return -1;
127
6.55M
    return 1;
128
6.55M
}
129
130
550k
#define BUF_TYPE_WIDTH_MASK     0x7
131
8.61M
#define BUF_TYPE_CONVUTF8       0x8
132
133
/*
134
 * This function sends each character in a buffer to do_esc_char(). It
135
 * interprets the content formats and converts to or from UTF8 as
136
 * appropriate.
137
 */
138
139
static int do_buf(unsigned char *buf, int buflen,
140
                  int type, unsigned short flags, char *quotes, char_io *io_ch,
141
                  void *arg)
142
550k
{
143
550k
    int i, outlen, len, charwidth;
144
550k
    unsigned short orflags;
145
550k
    unsigned char *p, *q;
146
550k
    unsigned long c;
147
148
550k
    p = buf;
149
550k
    q = buf + buflen;
150
550k
    outlen = 0;
151
550k
    charwidth = type & BUF_TYPE_WIDTH_MASK;
152
153
550k
    switch (charwidth) {
154
47.5k
    case 4:
155
47.5k
        if (buflen & 3) {
156
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
157
0
            return -1;
158
0
        }
159
47.5k
        break;
160
111k
    case 2:
161
111k
        if (buflen & 1) {
162
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
163
0
            return -1;
164
0
        }
165
111k
        break;
166
391k
    default:
167
391k
        break;
168
550k
    }
169
170
9.03M
    while (p != q) {
171
8.53M
        if (p == buf && flags & ASN1_STRFLGS_ESC_2253)
172
336k
            orflags = CHARTYPE_FIRST_ESC_2253;
173
8.19M
        else
174
8.19M
            orflags = 0;
175
176
8.53M
        switch (charwidth) {
177
552k
        case 4:
178
552k
            c = ((unsigned long)*p++) << 24;
179
552k
            c |= ((unsigned long)*p++) << 16;
180
552k
            c |= ((unsigned long)*p++) << 8;
181
552k
            c |= *p++;
182
552k
            break;
183
184
713k
        case 2:
185
713k
            c = ((unsigned long)*p++) << 8;
186
713k
            c |= *p++;
187
713k
            break;
188
189
6.06M
        case 1:
190
6.06M
            c = *p++;
191
6.06M
            break;
192
193
1.20M
        case 0:
194
1.20M
            i = UTF8_getc(p, buflen, &c);
195
1.20M
            if (i < 0)
196
49.2k
                return -1;      /* Invalid UTF8String */
197
1.15M
            buflen -= i;
198
1.15M
            p += i;
199
1.15M
            break;
200
0
        default:
201
0
            return -1;          /* invalid width */
202
8.53M
        }
203
8.48M
        if (p == q && flags & ASN1_STRFLGS_ESC_2253)
204
336k
            orflags = CHARTYPE_LAST_ESC_2253;
205
8.48M
        if (type & BUF_TYPE_CONVUTF8) {
206
1.27M
            unsigned char utfbuf[6];
207
1.27M
            int utflen;
208
1.27M
            utflen = UTF8_putc(utfbuf, sizeof(utfbuf), c);
209
3.84M
            for (i = 0; i < utflen; i++) {
210
                /*
211
                 * We don't need to worry about setting orflags correctly
212
                 * because if utflen==1 its value will be correct anyway
213
                 * otherwise each character will be > 0x7f and so the
214
                 * character will never be escaped on first and last.
215
                 */
216
2.56M
                len = do_esc_char(utfbuf[i], flags | orflags, quotes,
217
2.56M
                                  io_ch, arg);
218
2.56M
                if (len < 0)
219
0
                    return -1;
220
2.56M
                outlen += len;
221
2.56M
            }
222
7.20M
        } else {
223
7.20M
            len = do_esc_char(c, flags | orflags, quotes,
224
7.20M
                              io_ch, arg);
225
7.20M
            if (len < 0)
226
0
                return -1;
227
7.20M
            outlen += len;
228
7.20M
        }
229
8.48M
    }
230
501k
    return outlen;
231
550k
}
232
233
/* This function hex dumps a buffer of characters */
234
235
static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
236
                       int buflen)
237
97.7k
{
238
97.7k
    static const char hexdig[] = "0123456789ABCDEF";
239
97.7k
    unsigned char *p, *q;
240
97.7k
    char hextmp[2];
241
97.7k
    if (arg) {
242
97.7k
        p = buf;
243
97.7k
        q = buf + buflen;
244
947k
        while (p != q) {
245
849k
            hextmp[0] = hexdig[*p >> 4];
246
849k
            hextmp[1] = hexdig[*p & 0xf];
247
849k
            if (!io_ch(arg, hextmp, 2))
248
0
                return -1;
249
849k
            p++;
250
849k
        }
251
97.7k
    }
252
97.7k
    return buflen << 1;
253
97.7k
}
254
255
/*
256
 * "dump" a string. This is done when the type is unknown, or the flags
257
 * request it. We can either dump the content octets or the entire DER
258
 * encoding. This uses the RFC2253 #01234 format.
259
 */
260
261
static int do_dump(unsigned long lflags, char_io *io_ch, void *arg,
262
                   const ASN1_STRING *str)
263
97.7k
{
264
    /*
265
     * Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to
266
     * readily obtained
267
     */
268
97.7k
    ASN1_TYPE t;
269
97.7k
    unsigned char *der_buf, *p;
270
97.7k
    int outlen, der_len;
271
272
97.7k
    if (!io_ch(arg, "#", 1))
273
0
        return -1;
274
    /* If we don't dump DER encoding just dump content octets */
275
97.7k
    if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
276
19.7k
        outlen = do_hex_dump(io_ch, arg, str->data, str->length);
277
19.7k
        if (outlen < 0)
278
0
            return -1;
279
19.7k
        return outlen + 1;
280
19.7k
    }
281
77.9k
    t.type = str->type;
282
77.9k
    t.value.ptr = (char *)str;
283
77.9k
    der_len = i2d_ASN1_TYPE(&t, NULL);
284
77.9k
    if (der_len <= 0)
285
0
        return -1;
286
77.9k
    if ((der_buf = OPENSSL_malloc(der_len)) == NULL)
287
0
        return -1;
288
77.9k
    p = der_buf;
289
77.9k
    i2d_ASN1_TYPE(&t, &p);
290
77.9k
    outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
291
77.9k
    OPENSSL_free(der_buf);
292
77.9k
    if (outlen < 0)
293
0
        return -1;
294
77.9k
    return outlen + 1;
295
77.9k
}
296
297
/*
298
 * Lookup table to convert tags to character widths, 0 = UTF8 encoded, -1 is
299
 * used for non string types otherwise it is the number of bytes per
300
 * character
301
 */
302
303
static const signed char tag2nbyte[] = {
304
    -1, -1, -1, -1, -1,         /* 0-4 */
305
    -1, -1, -1, -1, -1,         /* 5-9 */
306
    -1, -1,                     /* 10-11 */
307
     0,                         /* 12 V_ASN1_UTF8STRING */
308
    -1, -1, -1, -1, -1,         /* 13-17 */
309
     1,                         /* 18 V_ASN1_NUMERICSTRING */
310
     1,                         /* 19 V_ASN1_PRINTABLESTRING */
311
     1,                         /* 20 V_ASN1_T61STRING */
312
    -1,                         /* 21 */
313
     1,                         /* 22 V_ASN1_IA5STRING */
314
     1,                         /* 23 V_ASN1_UTCTIME */
315
     1,                         /* 24 V_ASN1_GENERALIZEDTIME */
316
    -1,                         /* 25 */
317
     1,                         /* 26 V_ASN1_ISO64STRING */
318
    -1,                         /* 27 */
319
     4,                         /* 28 V_ASN1_UNIVERSALSTRING */
320
    -1,                         /* 29 */
321
     2                          /* 30 V_ASN1_BMPSTRING */
322
};
323
324
/*
325
 * This is the main function, print out an ASN1_STRING taking note of various
326
 * escape and display options. Returns number of characters written or -1 if
327
 * an error occurred.
328
 */
329
330
static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
331
                       const ASN1_STRING *str)
332
397k
{
333
397k
    int outlen, len;
334
397k
    int type;
335
397k
    char quotes;
336
397k
    unsigned short flags;
337
397k
    quotes = 0;
338
    /* Keep a copy of escape flags */
339
397k
    flags = (unsigned short)(lflags & ESC_FLAGS);
340
341
397k
    type = str->type;
342
343
397k
    outlen = 0;
344
345
397k
    if (lflags & ASN1_STRFLGS_SHOW_TYPE) {
346
19.7k
        const char *tagname;
347
348
19.7k
        tagname = ASN1_tag2str(type);
349
        /* We can directly cast here as tagname will never be too large. */
350
19.7k
        outlen += (int)strlen(tagname);
351
19.7k
        if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
352
0
            return -1;
353
19.7k
        outlen++;
354
19.7k
    }
355
356
    /* Decide what to do with type, either dump content or display it */
357
358
    /* Dump everything */
359
397k
    if (lflags & ASN1_STRFLGS_DUMP_ALL)
360
19.7k
        type = -1;
361
    /* Ignore the string type */
362
377k
    else if (lflags & ASN1_STRFLGS_IGNORE_TYPE)
363
0
        type = 1;
364
377k
    else {
365
        /* Else determine width based on type */
366
377k
        if ((type > 0) && (type < 31))
367
375k
            type = tag2nbyte[type];
368
2.22k
        else
369
2.22k
            type = -1;
370
377k
        if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
371
4.89k
            type = 1;
372
377k
    }
373
374
397k
    if (type == -1) {
375
97.7k
        len = do_dump(lflags, io_ch, arg, str);
376
97.7k
        if (len < 0 || len > INT_MAX - outlen)
377
0
            return -1;
378
97.7k
        outlen += len;
379
97.7k
        return outlen;
380
97.7k
    }
381
382
299k
    if (lflags & ASN1_STRFLGS_UTF8_CONVERT) {
383
        /*
384
         * Note: if string is UTF8 and we want to convert to UTF8 then we
385
         * just interpret it as 1 byte per character to avoid converting
386
         * twice.
387
         */
388
215k
        if (!type)
389
81.2k
            type = 1;
390
134k
        else
391
134k
            type |= BUF_TYPE_CONVUTF8;
392
215k
    }
393
394
299k
    len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
395
299k
    if (len < 0 || len > INT_MAX - 2 - outlen)
396
49.2k
        return -1;
397
250k
    outlen += len;
398
250k
    if (quotes)
399
40.3k
        outlen += 2;
400
250k
    if (!arg)
401
0
        return outlen;
402
250k
    if (quotes && !io_ch(arg, "\"", 1))
403
0
        return -1;
404
250k
    if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
405
0
        return -1;
406
250k
    if (quotes && !io_ch(arg, "\"", 1))
407
0
        return -1;
408
250k
    return outlen;
409
250k
}
410
411
/* Used for line indenting: print 'indent' spaces */
412
413
static int do_indent(char_io *io_ch, void *arg, int indent)
414
210k
{
415
210k
    int i;
416
216k
    for (i = 0; i < indent; i++)
417
5.92k
        if (!io_ch(arg, " ", 1))
418
0
            return 0;
419
210k
    return 1;
420
210k
}
421
422
0
#define FN_WIDTH_LN     25
423
99.6k
#define FN_WIDTH_SN     10
424
425
static int do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n,
426
                      int indent, unsigned long flags)
427
132k
{
428
132k
    int i, prev = -1, orflags, cnt;
429
132k
    int fn_opt, fn_nid;
430
132k
    ASN1_OBJECT *fn;
431
132k
    const ASN1_STRING *val;
432
132k
    const X509_NAME_ENTRY *ent;
433
132k
    char objtmp[80];
434
132k
    const char *objbuf;
435
132k
    int outlen, len;
436
132k
    char *sep_dn, *sep_mv, *sep_eq;
437
132k
    int sep_dn_len, sep_mv_len, sep_eq_len;
438
132k
    if (indent < 0)
439
0
        indent = 0;
440
132k
    outlen = indent;
441
132k
    if (!do_indent(io_ch, arg, indent))
442
0
        return -1;
443
132k
    switch (flags & XN_FLAG_SEP_MASK) {
444
0
    case XN_FLAG_SEP_MULTILINE:
445
0
        sep_dn = "\n";
446
0
        sep_dn_len = 1;
447
0
        sep_mv = " + ";
448
0
        sep_mv_len = 3;
449
0
        break;
450
451
0
    case XN_FLAG_SEP_COMMA_PLUS:
452
0
        sep_dn = ",";
453
0
        sep_dn_len = 1;
454
0
        sep_mv = "+";
455
0
        sep_mv_len = 1;
456
0
        indent = 0;
457
0
        break;
458
459
132k
    case XN_FLAG_SEP_CPLUS_SPC:
460
132k
        sep_dn = ", ";
461
132k
        sep_dn_len = 2;
462
132k
        sep_mv = " + ";
463
132k
        sep_mv_len = 3;
464
132k
        indent = 0;
465
132k
        break;
466
467
0
    case XN_FLAG_SEP_SPLUS_SPC:
468
0
        sep_dn = "; ";
469
0
        sep_dn_len = 2;
470
0
        sep_mv = " + ";
471
0
        sep_mv_len = 3;
472
0
        indent = 0;
473
0
        break;
474
475
0
    default:
476
0
        return -1;
477
132k
    }
478
479
132k
    if (flags & XN_FLAG_SPC_EQ) {
480
123k
        sep_eq = " = ";
481
123k
        sep_eq_len = 3;
482
123k
    } else {
483
8.81k
        sep_eq = "=";
484
8.81k
        sep_eq_len = 1;
485
8.81k
    }
486
487
132k
    fn_opt = flags & XN_FLAG_FN_MASK;
488
489
132k
    cnt = X509_NAME_entry_count(n);
490
426k
    for (i = 0; i < cnt; i++) {
491
293k
        if (flags & XN_FLAG_DN_REV)
492
0
            ent = X509_NAME_get_entry(n, cnt - i - 1);
493
293k
        else
494
293k
            ent = X509_NAME_get_entry(n, i);
495
293k
        if (prev != -1) {
496
182k
            if (prev == X509_NAME_ENTRY_set(ent)) {
497
104k
                if (!io_ch(arg, sep_mv, sep_mv_len))
498
0
                    return -1;
499
104k
                outlen += sep_mv_len;
500
104k
            } else {
501
78.6k
                if (!io_ch(arg, sep_dn, sep_dn_len))
502
0
                    return -1;
503
78.6k
                outlen += sep_dn_len;
504
78.6k
                if (!do_indent(io_ch, arg, indent))
505
0
                    return -1;
506
78.6k
                outlen += indent;
507
78.6k
            }
508
182k
        }
509
293k
        prev = X509_NAME_ENTRY_set(ent);
510
293k
        fn = X509_NAME_ENTRY_get_object(ent);
511
293k
        val = X509_NAME_ENTRY_get_data(ent);
512
293k
        fn_nid = OBJ_obj2nid(fn);
513
293k
        if (fn_opt != XN_FLAG_FN_NONE) {
514
293k
            int objlen, fld_len;
515
293k
            if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
516
194k
                OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1);
517
194k
                fld_len = 0;    /* XXX: what should this be? */
518
194k
                objbuf = objtmp;
519
194k
            } else {
520
99.6k
                if (fn_opt == XN_FLAG_FN_SN) {
521
99.6k
                    fld_len = FN_WIDTH_SN;
522
99.6k
                    objbuf = OBJ_nid2sn(fn_nid);
523
99.6k
                } else if (fn_opt == XN_FLAG_FN_LN) {
524
0
                    fld_len = FN_WIDTH_LN;
525
0
                    objbuf = OBJ_nid2ln(fn_nid);
526
0
                } else {
527
0
                    fld_len = 0; /* XXX: what should this be? */
528
0
                    objbuf = "";
529
0
                }
530
99.6k
            }
531
293k
            objlen = strlen(objbuf);
532
293k
            if (!io_ch(arg, objbuf, objlen))
533
0
                return -1;
534
293k
            if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
535
0
                if (!do_indent(io_ch, arg, fld_len - objlen))
536
0
                    return -1;
537
0
                outlen += fld_len - objlen;
538
0
            }
539
293k
            if (!io_ch(arg, sep_eq, sep_eq_len))
540
0
                return -1;
541
293k
            outlen += objlen + sep_eq_len;
542
293k
        }
543
        /*
544
         * If the field name is unknown then fix up the DER dump flag. We
545
         * might want to limit this further so it will DER dump on anything
546
         * other than a few 'standard' fields.
547
         */
548
293k
        if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
549
0
            orflags = ASN1_STRFLGS_DUMP_ALL;
550
293k
        else
551
293k
            orflags = 0;
552
553
293k
        len = do_print_ex(io_ch, arg, flags | orflags, val);
554
293k
        if (len < 0)
555
0
            return -1;
556
293k
        outlen += len;
557
293k
    }
558
132k
    return outlen;
559
132k
}
560
561
/* Wrappers round the main functions */
562
563
int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
564
                       unsigned long flags)
565
298k
{
566
298k
    if (flags == XN_FLAG_COMPAT)
567
166k
        return X509_NAME_print(out, nm, indent);
568
132k
    return do_name_ex(send_bio_chars, out, nm, indent, flags);
569
298k
}
570
571
#ifndef OPENSSL_NO_STDIO
572
int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent,
573
                          unsigned long flags)
574
0
{
575
0
    if (flags == XN_FLAG_COMPAT) {
576
0
        BIO *btmp;
577
0
        int ret;
578
0
        btmp = BIO_new_fp(fp, BIO_NOCLOSE);
579
0
        if (!btmp)
580
0
            return -1;
581
0
        ret = X509_NAME_print(btmp, nm, indent);
582
0
        BIO_free(btmp);
583
0
        return ret;
584
0
    }
585
0
    return do_name_ex(send_fp_chars, fp, nm, indent, flags);
586
0
}
587
#endif
588
589
int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags)
590
103k
{
591
103k
    return do_print_ex(send_bio_chars, out, flags, str);
592
103k
}
593
594
#ifndef OPENSSL_NO_STDIO
595
int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags)
596
0
{
597
0
    return do_print_ex(send_fp_chars, fp, flags, str);
598
0
}
599
#endif
600
601
/*
602
 * Utility function: convert any string type to UTF8, returns number of bytes
603
 * in output string or a negative error code
604
 */
605
606
int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
607
9.02M
{
608
9.02M
    ASN1_STRING stmp, *str = &stmp;
609
9.02M
    int mbflag, type, ret;
610
9.02M
    if (!in)
611
0
        return -1;
612
9.02M
    type = in->type;
613
9.02M
    if ((type < 0) || (type > 30))
614
0
        return -1;
615
9.02M
    mbflag = tag2nbyte[type];
616
9.02M
    if (mbflag == -1)
617
10
        return -1;
618
9.02M
    mbflag |= MBSTRING_FLAG;
619
9.02M
    stmp.data = NULL;
620
9.02M
    stmp.length = 0;
621
9.02M
    stmp.flags = 0;
622
9.02M
    ret =
623
9.02M
        ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
624
9.02M
                           B_ASN1_UTF8STRING);
625
9.02M
    if (ret < 0)
626
31.5k
        return ret;
627
8.99M
    *out = stmp.data;
628
8.99M
    return stmp.length;
629
9.02M
}