Coverage Report

Created: 2026-02-14 07:20

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