Coverage Report

Created: 2025-08-11 07:04

/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
8.76M
#define CHARTYPE_BS_ESC         (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
28
29
431k
#define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
30
431k
                  ASN1_STRFLGS_ESC_2254 | \
31
431k
                  ASN1_STRFLGS_ESC_QUOTE | \
32
431k
                  ASN1_STRFLGS_ESC_CTRL | \
33
431k
                  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.2M
{
41
11.2M
    if (!arg)
42
4.90M
        return 1;
43
6.38M
    if (BIO_write(arg, buf, len) != len)
44
0
        return 0;
45
6.38M
    return 1;
46
6.38M
}
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.29M
{
70
9.29M
    unsigned short chflgs;
71
9.29M
    unsigned char chtmp;
72
9.29M
    char tmphex[HEX_SIZE(long) + 3];
73
74
9.29M
    if (c > 0xffffffffL)
75
0
        return -1;
76
9.29M
    if (c > 0xffff) {
77
425k
        BIO_snprintf(tmphex, sizeof(tmphex), "\\W%08lX", c);
78
425k
        if (!io_ch(arg, tmphex, 10))
79
0
            return -1;
80
425k
        return 10;
81
425k
    }
82
8.87M
    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
8.76M
    chtmp = (unsigned char)c;
89
8.76M
    if (chtmp > 0x7f)
90
3.17M
        chflgs = flags & ASN1_STRFLGS_ESC_MSB;
91
5.58M
    else
92
5.58M
        chflgs = char_type[chtmp] & flags;
93
8.76M
    if (chflgs & CHARTYPE_BS_ESC) {
94
        /* If we don't escape with quotes, signal we need quotes */
95
82.0k
        if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
96
76.0k
            if (do_quotes)
97
38.0k
                *do_quotes = 1;
98
76.0k
            if (!io_ch(arg, &chtmp, 1))
99
0
                return -1;
100
76.0k
            return 1;
101
76.0k
        }
102
5.99k
        if (!io_ch(arg, "\\", 1))
103
0
            return -1;
104
5.99k
        if (!io_ch(arg, &chtmp, 1))
105
0
            return -1;
106
5.99k
        return 2;
107
5.99k
    }
108
8.68M
    if (chflgs & (ASN1_STRFLGS_ESC_CTRL
109
8.68M
                  | ASN1_STRFLGS_ESC_MSB
110
8.68M
                  | ASN1_STRFLGS_ESC_2254)) {
111
2.67M
        BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
112
2.67M
        if (!io_ch(arg, tmphex, 3))
113
0
            return -1;
114
2.67M
        return 3;
115
2.67M
    }
116
    /*
117
     * If we get this far and do any escaping at all must escape the escape
118
     * character itself: backslash.
119
     */
120
6.00M
    if (chtmp == '\\' && (flags & ESC_FLAGS)) {
121
0
        if (!io_ch(arg, "\\\\", 2))
122
0
            return -1;
123
0
        return 2;
124
0
    }
125
6.00M
    if (!io_ch(arg, &chtmp, 1))
126
0
        return -1;
127
6.00M
    return 1;
128
6.00M
}
129
130
570k
#define BUF_TYPE_WIDTH_MASK     0x7
131
7.94M
#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
570k
{
143
570k
    int i, outlen, len, charwidth;
144
570k
    unsigned short orflags;
145
570k
    unsigned char *p, *q;
146
570k
    unsigned long c;
147
148
570k
    p = buf;
149
570k
    q = buf + buflen;
150
570k
    outlen = 0;
151
570k
    charwidth = type & BUF_TYPE_WIDTH_MASK;
152
153
570k
    switch (charwidth) {
154
46.3k
    case 4:
155
46.3k
        if (buflen & 3) {
156
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
157
0
            return -1;
158
0
        }
159
46.3k
        break;
160
116k
    case 2:
161
116k
        if (buflen & 1) {
162
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
163
0
            return -1;
164
0
        }
165
116k
        break;
166
406k
    default:
167
406k
        break;
168
570k
    }
169
170
8.38M
    while (p != q) {
171
7.90M
        if (p == buf && flags & ASN1_STRFLGS_ESC_2253)
172
322k
            orflags = CHARTYPE_FIRST_ESC_2253;
173
7.58M
        else
174
7.58M
            orflags = 0;
175
176
7.90M
        switch (charwidth) {
177
452k
        case 4:
178
452k
            c = ((unsigned long)*p++) << 24;
179
452k
            c |= ((unsigned long)*p++) << 16;
180
452k
            c |= ((unsigned long)*p++) << 8;
181
452k
            c |= *p++;
182
452k
            break;
183
184
814k
        case 2:
185
814k
            c = ((unsigned long)*p++) << 8;
186
814k
            c |= *p++;
187
814k
            break;
188
189
5.07M
        case 1:
190
5.07M
            c = *p++;
191
5.07M
            break;
192
193
1.56M
        case 0:
194
1.56M
            i = UTF8_getc(p, buflen, &c);
195
1.56M
            if (i < 0)
196
89.9k
                return -1;      /* Invalid UTF8String */
197
1.47M
            buflen -= i;
198
1.47M
            p += i;
199
1.47M
            break;
200
0
        default:
201
0
            return -1;          /* invalid width */
202
7.90M
        }
203
7.81M
        if (p == q && flags & ASN1_STRFLGS_ESC_2253)
204
322k
            orflags = CHARTYPE_LAST_ESC_2253;
205
7.81M
        if (type & BUF_TYPE_CONVUTF8) {
206
1.35M
            unsigned char utfbuf[6];
207
1.35M
            int utflen;
208
1.35M
            utflen = UTF8_putc(utfbuf, sizeof(utfbuf), c);
209
4.18M
            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.83M
                len = do_esc_char(utfbuf[i], flags | orflags, quotes,
217
2.83M
                                  io_ch, arg);
218
2.83M
                if (len < 0)
219
0
                    return -1;
220
2.83M
                outlen += len;
221
2.83M
            }
222
6.46M
        } else {
223
6.46M
            len = do_esc_char(c, flags | orflags, quotes,
224
6.46M
                              io_ch, arg);
225
6.46M
            if (len < 0)
226
0
                return -1;
227
6.46M
            outlen += len;
228
6.46M
        }
229
7.81M
    }
230
480k
    return outlen;
231
570k
}
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
91.9k
{
238
91.9k
    static const char hexdig[] = "0123456789ABCDEF";
239
91.9k
    unsigned char *p, *q;
240
91.9k
    char hextmp[2];
241
91.9k
    if (arg) {
242
91.9k
        p = buf;
243
91.9k
        q = buf + buflen;
244
1.14M
        while (p != q) {
245
1.05M
            hextmp[0] = hexdig[*p >> 4];
246
1.05M
            hextmp[1] = hexdig[*p & 0xf];
247
1.05M
            if (!io_ch(arg, hextmp, 2))
248
0
                return -1;
249
1.05M
            p++;
250
1.05M
        }
251
91.9k
    }
252
91.9k
    return buflen << 1;
253
91.9k
}
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
91.9k
{
264
    /*
265
     * Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to
266
     * readily obtained
267
     */
268
91.9k
    ASN1_TYPE t;
269
91.9k
    unsigned char *der_buf, *p;
270
91.9k
    int outlen, der_len;
271
272
91.9k
    if (!io_ch(arg, "#", 1))
273
0
        return -1;
274
    /* If we don't dump DER encoding just dump content octets */
275
91.9k
    if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
276
24.9k
        outlen = do_hex_dump(io_ch, arg, str->data, str->length);
277
24.9k
        if (outlen < 0)
278
0
            return -1;
279
24.9k
        return outlen + 1;
280
24.9k
    }
281
67.0k
    t.type = str->type;
282
67.0k
    t.value.ptr = (char *)str;
283
67.0k
    der_len = i2d_ASN1_TYPE(&t, NULL);
284
67.0k
    if (der_len <= 0)
285
0
        return -1;
286
67.0k
    if ((der_buf = OPENSSL_malloc(der_len)) == NULL)
287
0
        return -1;
288
67.0k
    p = der_buf;
289
67.0k
    i2d_ASN1_TYPE(&t, &p);
290
67.0k
    outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
291
67.0k
    OPENSSL_free(der_buf);
292
67.0k
    if (outlen < 0)
293
0
        return -1;
294
67.0k
    return outlen + 1;
295
67.0k
}
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
422k
{
333
422k
    int outlen, len;
334
422k
    int type;
335
422k
    char quotes;
336
422k
    unsigned short flags;
337
422k
    quotes = 0;
338
    /* Keep a copy of escape flags */
339
422k
    flags = (unsigned short)(lflags & ESC_FLAGS);
340
341
422k
    type = str->type;
342
343
422k
    outlen = 0;
344
345
422k
    if (lflags & ASN1_STRFLGS_SHOW_TYPE) {
346
24.9k
        const char *tagname;
347
348
24.9k
        tagname = ASN1_tag2str(type);
349
        /* We can directly cast here as tagname will never be too large. */
350
24.9k
        outlen += (int)strlen(tagname);
351
24.9k
        if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
352
0
            return -1;
353
24.9k
        outlen++;
354
24.9k
    }
355
356
    /* Decide what to do with type, either dump content or display it */
357
358
    /* Dump everything */
359
422k
    if (lflags & ASN1_STRFLGS_DUMP_ALL)
360
24.9k
        type = -1;
361
    /* Ignore the string type */
362
397k
    else if (lflags & ASN1_STRFLGS_IGNORE_TYPE)
363
0
        type = 1;
364
397k
    else {
365
        /* Else determine width based on type */
366
397k
        if ((type > 0) && (type < 31))
367
395k
            type = tag2nbyte[type];
368
2.08k
        else
369
2.08k
            type = -1;
370
397k
        if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
371
4.61k
            type = 1;
372
397k
    }
373
374
422k
    if (type == -1) {
375
91.9k
        len = do_dump(lflags, io_ch, arg, str);
376
91.9k
        if (len < 0 || len > INT_MAX - outlen)
377
0
            return -1;
378
91.9k
        outlen += len;
379
91.9k
        return outlen;
380
91.9k
    }
381
382
330k
    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
206k
        if (!type)
389
74.7k
            type = 1;
390
131k
        else
391
131k
            type |= BUF_TYPE_CONVUTF8;
392
206k
    }
393
394
330k
    len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
395
330k
    if (len < 0 || len > INT_MAX - 2 - outlen)
396
89.9k
        return -1;
397
240k
    outlen += len;
398
240k
    if (quotes)
399
35.3k
        outlen += 2;
400
240k
    if (!arg)
401
0
        return outlen;
402
240k
    if (quotes && !io_ch(arg, "\"", 1))
403
0
        return -1;
404
240k
    if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
405
0
        return -1;
406
240k
    if (quotes && !io_ch(arg, "\"", 1))
407
0
        return -1;
408
240k
    return outlen;
409
240k
}
410
411
/* Used for line indenting: print 'indent' spaces */
412
413
static int do_indent(char_io *io_ch, void *arg, int indent)
414
192k
{
415
192k
    int i;
416
197k
    for (i = 0; i < indent; i++)
417
5.53k
        if (!io_ch(arg, " ", 1))
418
0
            return 0;
419
192k
    return 1;
420
192k
}
421
422
0
#define FN_WIDTH_LN     25
423
89.9k
#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
123k
{
428
123k
    int i, prev = -1, orflags, cnt;
429
123k
    int fn_opt, fn_nid;
430
123k
    ASN1_OBJECT *fn;
431
123k
    const ASN1_STRING *val;
432
123k
    const X509_NAME_ENTRY *ent;
433
123k
    char objtmp[80];
434
123k
    const char *objbuf;
435
123k
    int outlen, len;
436
123k
    char *sep_dn, *sep_mv, *sep_eq;
437
123k
    int sep_dn_len, sep_mv_len, sep_eq_len;
438
123k
    if (indent < 0)
439
0
        indent = 0;
440
123k
    outlen = indent;
441
123k
    if (!do_indent(io_ch, arg, indent))
442
0
        return -1;
443
123k
    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
123k
    case XN_FLAG_SEP_CPLUS_SPC:
460
123k
        sep_dn = ", ";
461
123k
        sep_dn_len = 2;
462
123k
        sep_mv = " + ";
463
123k
        sep_mv_len = 3;
464
123k
        indent = 0;
465
123k
        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
123k
    }
478
479
123k
    if (flags & XN_FLAG_SPC_EQ) {
480
114k
        sep_eq = " = ";
481
114k
        sep_eq_len = 3;
482
114k
    } else {
483
8.81k
        sep_eq = "=";
484
8.81k
        sep_eq_len = 1;
485
8.81k
    }
486
487
123k
    fn_opt = flags & XN_FLAG_FN_MASK;
488
489
123k
    cnt = X509_NAME_entry_count(n);
490
396k
    for (i = 0; i < cnt; i++) {
491
273k
        if (flags & XN_FLAG_DN_REV)
492
0
            ent = X509_NAME_get_entry(n, cnt - i - 1);
493
273k
        else
494
273k
            ent = X509_NAME_get_entry(n, i);
495
273k
        if (prev != -1) {
496
167k
            if (prev == X509_NAME_ENTRY_set(ent)) {
497
98.7k
                if (!io_ch(arg, sep_mv, sep_mv_len))
498
0
                    return -1;
499
98.7k
                outlen += sep_mv_len;
500
98.7k
            } else {
501
69.0k
                if (!io_ch(arg, sep_dn, sep_dn_len))
502
0
                    return -1;
503
69.0k
                outlen += sep_dn_len;
504
69.0k
                if (!do_indent(io_ch, arg, indent))
505
0
                    return -1;
506
69.0k
                outlen += indent;
507
69.0k
            }
508
167k
        }
509
273k
        prev = X509_NAME_ENTRY_set(ent);
510
273k
        fn = X509_NAME_ENTRY_get_object(ent);
511
273k
        val = X509_NAME_ENTRY_get_data(ent);
512
273k
        fn_nid = OBJ_obj2nid(fn);
513
273k
        if (fn_opt != XN_FLAG_FN_NONE) {
514
273k
            int objlen, fld_len;
515
273k
            if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
516
183k
                OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1);
517
183k
                fld_len = 0;    /* XXX: what should this be? */
518
183k
                objbuf = objtmp;
519
183k
            } else {
520
89.9k
                if (fn_opt == XN_FLAG_FN_SN) {
521
89.9k
                    fld_len = FN_WIDTH_SN;
522
89.9k
                    objbuf = OBJ_nid2sn(fn_nid);
523
89.9k
                } 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
89.9k
            }
531
273k
            objlen = strlen(objbuf);
532
273k
            if (!io_ch(arg, objbuf, objlen))
533
0
                return -1;
534
273k
            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
273k
            if (!io_ch(arg, sep_eq, sep_eq_len))
540
0
                return -1;
541
273k
            outlen += objlen + sep_eq_len;
542
273k
        }
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
273k
        if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
549
0
            orflags = ASN1_STRFLGS_DUMP_ALL;
550
273k
        else
551
273k
            orflags = 0;
552
553
273k
        len = do_print_ex(io_ch, arg, flags | orflags, val);
554
273k
        if (len < 0)
555
0
            return -1;
556
273k
        outlen += len;
557
273k
    }
558
123k
    return outlen;
559
123k
}
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
282k
{
566
282k
    if (flags == XN_FLAG_COMPAT)
567
159k
        return X509_NAME_print(out, nm, indent);
568
123k
    return do_name_ex(send_bio_chars, out, nm, indent, flags);
569
282k
}
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
148k
{
591
148k
    return do_print_ex(send_bio_chars, out, flags, str);
592
148k
}
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
8.46M
{
608
8.46M
    ASN1_STRING stmp, *str = &stmp;
609
8.46M
    int mbflag, type, ret;
610
8.46M
    if (!in)
611
0
        return -1;
612
8.46M
    type = in->type;
613
8.46M
    if ((type < 0) || (type > 30))
614
0
        return -1;
615
8.46M
    mbflag = tag2nbyte[type];
616
8.46M
    if (mbflag == -1)
617
8
        return -1;
618
8.46M
    mbflag |= MBSTRING_FLAG;
619
8.46M
    stmp.data = NULL;
620
8.46M
    stmp.length = 0;
621
8.46M
    stmp.flags = 0;
622
8.46M
    ret =
623
8.46M
        ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
624
8.46M
                           B_ASN1_UTF8STRING);
625
8.46M
    if (ret < 0)
626
29.6k
        return ret;
627
8.43M
    *out = stmp.data;
628
8.43M
    return stmp.length;
629
8.46M
}