Coverage Report

Created: 2026-10-28 06:28

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