Coverage Report

Created: 2026-05-24 07:14

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