Coverage Report

Created: 2025-11-16 06:40

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