Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
9.92M
#define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
32
33
377k
#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
11.5M
{
43
11.5M
    if (!arg)
44
5.12M
        return 1;
45
6.46M
    if (BIO_write(arg, buf, len) != len)
46
0
        return 0;
47
6.46M
    return 1;
48
6.46M
}
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
10.0M
{
72
10.0M
    unsigned short chflgs;
73
10.0M
    unsigned char chtmp;
74
10.0M
    char tmphex[HEX_SIZE(long) + 3];
75
76
10.0M
    if (c > UNICODE_MAX)
77
83
        return -1;
78
10.0M
    if (c > 0xffff) {
79
10.1k
        snprintf(tmphex, sizeof(tmphex), "\\W%08" PRIX32, c);
80
10.1k
        if (!io_ch(arg, tmphex, 10))
81
0
            return -1;
82
10.1k
        return 10;
83
10.1k
    }
84
10.0M
    if (c > 0xff) {
85
113k
        snprintf(tmphex, sizeof(tmphex), "\\U%04" PRIX32, c);
86
113k
        if (!io_ch(arg, tmphex, 6))
87
0
            return -1;
88
113k
        return 6;
89
113k
    }
90
9.92M
    chtmp = (unsigned char)c;
91
9.92M
    if (chtmp > 0x7f)
92
2.45M
        chflgs = flags & ASN1_STRFLGS_ESC_MSB;
93
7.47M
    else
94
7.47M
        chflgs = char_type[chtmp] & flags;
95
9.92M
    if (chflgs & CHARTYPE_BS_ESC) {
96
        /* If we don't escape with quotes, signal we need quotes */
97
80.3k
        if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
98
75.1k
            if (do_quotes)
99
37.6k
                *do_quotes = 1;
100
75.1k
            if (!io_ch(arg, &chtmp, 1))
101
0
                return -1;
102
75.1k
            return 1;
103
75.1k
        }
104
5.17k
        if (!io_ch(arg, "\\", 1))
105
0
            return -1;
106
5.17k
        if (!io_ch(arg, &chtmp, 1))
107
0
            return -1;
108
5.17k
        return 2;
109
5.17k
    }
110
9.84M
    if (chflgs & (ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB | ASN1_STRFLGS_ESC_2254)) {
111
1.75M
        snprintf(tmphex, 11, "\\%02X", chtmp);
112
1.75M
        if (!io_ch(arg, tmphex, 3))
113
0
            return -1;
114
1.75M
        return 3;
115
1.75M
    }
116
    /*
117
     * If we get this far and do any escaping at all must escape the escape
118
     * character itself: backslash.
119
     */
120
8.09M
    if (chtmp == '\\' && (flags & ESC_FLAGS)) {
121
0
        if (!io_ch(arg, "\\\\", 2))
122
0
            return -1;
123
0
        return 2;
124
0
    }
125
8.09M
    if (!io_ch(arg, &chtmp, 1))
126
0
        return -1;
127
8.09M
    return 1;
128
8.09M
}
129
130
166k
#define BUF_TYPE_WIDTH_MASK 0x7
131
4.09M
#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
208k
{
143
208k
    int i, outlen, len, charwidth;
144
208k
    unsigned short orflags;
145
208k
    const unsigned char *p, *q;
146
208k
    uint32_t c;
147
148
208k
    if (buflen < 0)
149
0
        return -1;
150
208k
    if (buflen == 0)
151
42.6k
        return 0;
152
166k
    p = buf;
153
166k
    q = buf + buflen;
154
166k
    outlen = 0;
155
166k
    charwidth = type & BUF_TYPE_WIDTH_MASK;
156
157
166k
    switch (charwidth) {
158
428
    case 4:
159
428
        if (buflen & 3) {
160
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
161
0
            return -1;
162
0
        }
163
428
        break;
164
6.83k
    case 2:
165
6.83k
        if (buflen & 1) {
166
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);
167
0
            return -1;
168
0
        }
169
6.83k
        break;
170
158k
    default:
171
158k
        break;
172
166k
    }
173
174
4.15M
    while (p != q) {
175
4.02M
        uint16_t tmp;
176
177
4.02M
        if (p == buf && flags & ASN1_STRFLGS_ESC_2253)
178
118k
            orflags = CHARTYPE_FIRST_ESC_2253;
179
3.90M
        else
180
3.90M
            orflags = 0;
181
182
4.02M
        switch (charwidth) {
183
1.42k
        case 4:
184
1.42k
            p = OPENSSL_load_u32_be(&c, p);
185
1.42k
            break;
186
187
46.5k
        case 2:
188
46.5k
            p = OPENSSL_load_u16_be(&tmp, p);
189
46.5k
            c = tmp;
190
46.5k
            break;
191
192
3.84M
        case 1:
193
3.84M
            c = *p++;
194
3.84M
            break;
195
196
137k
        case 0:
197
137k
            i = ossl_utf8_getc_internal(p, buflen, &c);
198
137k
            if (i < 0)
199
33.7k
                return -1; /* Invalid UTF8String */
200
103k
            buflen -= i;
201
103k
            p += i;
202
103k
            break;
203
0
        default:
204
0
            return -1; /* invalid width */
205
4.02M
        }
206
3.99M
        if (p == q && flags & ASN1_STRFLGS_ESC_2253)
207
118k
            orflags = CHARTYPE_LAST_ESC_2253;
208
3.99M
        if (type & BUF_TYPE_CONVUTF8) {
209
172k
            unsigned char utfbuf[6];
210
172k
            int utflen = ossl_utf8_putc_internal(utfbuf, sizeof(utfbuf), c);
211
212
172k
            if (utflen < 0)
213
389
                return -1; /* error happened with UTF8 */
214
454k
            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
282k
                len = do_esc_char(utfbuf[i], flags | orflags, quotes,
222
282k
                    io_ch, arg);
223
282k
                if (len < 0)
224
0
                    return -1;
225
282k
                outlen += len;
226
282k
            }
227
3.81M
        } else {
228
3.81M
            len = do_esc_char(c, flags | orflags, quotes,
229
3.81M
                io_ch, arg);
230
3.81M
            if (len < 0)
231
83
                return -1;
232
3.81M
            outlen += len;
233
3.81M
        }
234
3.99M
    }
235
131k
    return outlen;
236
166k
}
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
11.8k
{
243
11.8k
    unsigned char *p, *q;
244
11.8k
    char hextmp[2];
245
246
11.8k
    if (buflen < 0)
247
0
        return -1;
248
11.8k
    if (buflen == 0)
249
3.69k
        return 0;
250
8.15k
    if (arg) {
251
8.15k
        p = buf;
252
8.15k
        q = buf + buflen;
253
135k
        while (p != q) {
254
127k
            ossl_to_hex(hextmp, *p);
255
127k
            if (!io_ch(arg, hextmp, 2))
256
0
                return -1;
257
127k
            p++;
258
127k
        }
259
8.15k
    }
260
8.15k
    return buflen << 1;
261
8.15k
}
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.2k
{
272
    /*
273
     * Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to
274
     * readily obtained
275
     */
276
53.2k
    ASN1_TYPE t;
277
53.2k
    unsigned char *der_buf, *p;
278
53.2k
    int outlen, der_len;
279
280
53.2k
    if (!io_ch(arg, "#", 1))
281
0
        return -1;
282
    /* If we don't dump DER encoding just dump content octets */
283
53.2k
    if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
284
17.9k
        outlen = do_hex_dump(io_ch, arg, str->data, str->length);
285
17.9k
        if (outlen < 0)
286
0
            return -1;
287
17.9k
        return outlen + 1;
288
17.9k
    }
289
35.3k
    t.type = str->type;
290
35.3k
    t.value.ptr = (char *)str;
291
35.3k
    der_len = i2d_ASN1_TYPE(&t, NULL);
292
35.3k
    if (der_len <= 0)
293
0
        return -1;
294
35.3k
    if ((der_buf = OPENSSL_malloc(der_len)) == NULL)
295
0
        return -1;
296
35.3k
    p = der_buf;
297
35.3k
    i2d_ASN1_TYPE(&t, &p);
298
35.3k
    outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
299
35.3k
    OPENSSL_free(der_buf);
300
35.3k
    if (outlen < 0)
301
0
        return -1;
302
35.3k
    return outlen + 1;
303
35.3k
}
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
357k
{
341
357k
    int outlen, len;
342
357k
    int type;
343
357k
    char quotes;
344
357k
    unsigned short flags;
345
357k
    quotes = 0;
346
    /* Keep a copy of escape flags */
347
357k
    flags = (unsigned short)(lflags & ESC_FLAGS);
348
349
357k
    type = str->type;
350
351
357k
    outlen = 0;
352
353
357k
    if (lflags & ASN1_STRFLGS_SHOW_TYPE) {
354
17.9k
        const char *tagname;
355
356
17.9k
        tagname = ASN1_tag2str(type);
357
        /* We can directly cast here as tagname will never be too large. */
358
17.9k
        outlen += (int)strlen(tagname);
359
17.9k
        if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
360
0
            return -1;
361
17.9k
        outlen++;
362
17.9k
    }
363
364
    /* Decide what to do with type, either dump content or display it */
365
366
    /* Dump everything */
367
357k
    if (lflags & ASN1_STRFLGS_DUMP_ALL)
368
17.9k
        type = -1;
369
    /* Ignore the string type */
370
339k
    else if (lflags & ASN1_STRFLGS_IGNORE_TYPE)
371
0
        type = 1;
372
339k
    else {
373
        /* Else determine width based on type */
374
339k
        if ((type > 0) && (type < 31))
375
337k
            type = tag2nbyte[type];
376
2.06k
        else
377
2.06k
            type = -1;
378
339k
        if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
379
4.07k
            type = 1;
380
339k
    }
381
382
357k
    if (type == -1) {
383
53.2k
        len = do_dump(lflags, io_ch, arg, str);
384
53.2k
        if (len < 0 || len > INT_MAX - outlen)
385
0
            return -1;
386
53.2k
        outlen += len;
387
53.2k
        return outlen;
388
53.2k
    }
389
390
304k
    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
173k
        if (!type)
397
69.0k
            type = 1;
398
104k
        else
399
104k
            type |= BUF_TYPE_CONVUTF8;
400
173k
    }
401
402
304k
    len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
403
304k
    if (len < 0 || len > INT_MAX - 2 - outlen)
404
92.2k
        return -1;
405
211k
    outlen += len;
406
211k
    if (quotes)
407
35.0k
        outlen += 2;
408
211k
    if (!arg)
409
0
        return outlen;
410
211k
    if (quotes && !io_ch(arg, "\"", 1))
411
0
        return -1;
412
211k
    if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
413
0
        return -1;
414
211k
    if (quotes && !io_ch(arg, "\"", 1))
415
0
        return -1;
416
211k
    return outlen;
417
211k
}
418
419
/* Used for line indenting: print 'indent' spaces */
420
421
static int do_indent(char_io *io_ch, void *arg, int indent)
422
168k
{
423
168k
    int i;
424
268k
    for (i = 0; i < indent; i++)
425
99.9k
        if (!io_ch(arg, " ", 1))
426
0
            return 0;
427
168k
    return 1;
428
168k
}
429
430
0
#define FN_WIDTH_LN 25
431
10.1k
#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
29.2k
{
436
29.2k
    int i, prev = -1, orflags, cnt;
437
29.2k
    int fn_opt, fn_nid;
438
29.2k
    const ASN1_OBJECT *fn;
439
29.2k
    const ASN1_STRING *val;
440
29.2k
    const X509_NAME_ENTRY *ent;
441
29.2k
    char objtmp[80];
442
29.2k
    const char *objbuf;
443
29.2k
    int outlen, len;
444
29.2k
    int err = 0;
445
29.2k
    char *sep_dn, *sep_mv, *sep_eq;
446
29.2k
    int sep_dn_len, sep_mv_len, sep_eq_len;
447
29.2k
    if (indent < 0)
448
0
        indent = 0;
449
29.2k
    outlen = indent;
450
29.2k
    if (!do_indent(io_ch, arg, indent))
451
0
        return -1;
452
29.2k
    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
29.2k
    case XN_FLAG_SEP_CPLUS_SPC:
469
29.2k
        sep_dn = ", ";
470
29.2k
        sep_dn_len = 2;
471
29.2k
        sep_mv = " + ";
472
29.2k
        sep_mv_len = 3;
473
29.2k
        indent = 0;
474
29.2k
        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
29.2k
    }
487
488
29.2k
    if (flags & XN_FLAG_SPC_EQ) {
489
24.1k
        sep_eq = " = ";
490
24.1k
        sep_eq_len = 3;
491
24.1k
    } else {
492
5.11k
        sep_eq = "=";
493
5.11k
        sep_eq_len = 1;
494
5.11k
    }
495
496
29.2k
    fn_opt = flags & XN_FLAG_FN_MASK;
497
498
29.2k
    cnt = X509_NAME_entry_count(n);
499
114k
    for (i = 0; i < cnt; i++) {
500
85.6k
        if (flags & XN_FLAG_DN_REV)
501
0
            ent = X509_NAME_get_entry(n, cnt - i - 1);
502
85.6k
        else
503
85.6k
            ent = X509_NAME_get_entry(n, i);
504
85.6k
        if (prev != -1) {
505
64.2k
            if (prev == X509_NAME_ENTRY_set(ent)) {
506
34.4k
                if (!io_ch(arg, sep_mv, sep_mv_len))
507
0
                    return -1;
508
34.4k
                outlen = safe_add_int(outlen, sep_mv_len, &err);
509
34.4k
                if (err != 0)
510
0
                    return -1;
511
34.4k
            } else {
512
29.8k
                if (!io_ch(arg, sep_dn, sep_dn_len))
513
0
                    return -1;
514
29.8k
                outlen = safe_add_int(outlen, sep_dn_len, &err);
515
29.8k
                if (err != 0)
516
0
                    return -1;
517
29.8k
                if (!do_indent(io_ch, arg, indent))
518
0
                    return -1;
519
29.8k
                outlen = safe_add_int(outlen, indent, &err);
520
29.8k
                if (err != 0)
521
0
                    return -1;
522
29.8k
            }
523
64.2k
        }
524
85.6k
        prev = X509_NAME_ENTRY_set(ent);
525
85.6k
        fn = X509_NAME_ENTRY_get_object(ent);
526
85.6k
        val = X509_NAME_ENTRY_get_data(ent);
527
85.6k
        fn_nid = OBJ_obj2nid(fn);
528
85.6k
        if (fn_opt != XN_FLAG_FN_NONE) {
529
85.6k
            int objlen, fld_len;
530
85.6k
            if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
531
75.4k
                OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1);
532
75.4k
                fld_len = 0; /* XXX: what should this be? */
533
75.4k
                objbuf = objtmp;
534
75.4k
            } else {
535
10.1k
                if (fn_opt == XN_FLAG_FN_SN) {
536
10.1k
                    fld_len = FN_WIDTH_SN;
537
10.1k
                    objbuf = OBJ_nid2sn(fn_nid);
538
10.1k
                } 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
10.1k
            }
546
85.6k
            objlen = (int)strlen(objbuf);
547
85.6k
            if (!io_ch(arg, objbuf, objlen))
548
0
                return -1;
549
85.6k
            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
85.6k
            if (!io_ch(arg, sep_eq, sep_eq_len))
557
0
                return -1;
558
85.6k
            outlen = safe_add_int(outlen, objlen, &err);
559
85.6k
            if (err != 0)
560
0
                return -1;
561
85.6k
            outlen = safe_add_int(outlen, sep_eq_len, &err);
562
85.6k
            if (err != 0)
563
0
                return -1;
564
85.6k
        }
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
85.6k
        if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
571
0
            orflags = ASN1_STRFLGS_DUMP_ALL;
572
85.6k
        else
573
85.6k
            orflags = 0;
574
575
85.6k
        len = do_print_ex(io_ch, arg, flags | orflags, val);
576
85.6k
        if (len < 0)
577
389
            return -1;
578
85.2k
        outlen = safe_add_int(outlen, len, &err);
579
85.2k
        if (err != 0)
580
0
            return -1;
581
85.2k
    }
582
28.8k
    return outlen;
583
29.2k
}
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
248k
{
590
248k
    if (flags == XN_FLAG_COMPAT)
591
147k
        return X509_NAME_print(out, nm, indent);
592
101k
    return do_name_ex(send_bio_chars, out, nm, indent, flags);
593
248k
}
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
146k
{
615
146k
    return do_print_ex(send_bio_chars, out, flags, str);
616
146k
}
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
7.78M
{
632
7.78M
    ASN1_STRING stmp, *str = &stmp;
633
7.78M
    int mbflag, type, ret;
634
7.78M
    if (!in)
635
0
        return -1;
636
7.78M
    type = in->type;
637
7.78M
    if ((type < 0) || (type > 30))
638
0
        return -1;
639
7.78M
    mbflag = tag2nbyte[type];
640
7.78M
    if (mbflag == -1)
641
7
        return -1;
642
7.78M
    mbflag |= MBSTRING_FLAG;
643
7.78M
    stmp.data = NULL;
644
7.78M
    stmp.length = 0;
645
7.78M
    stmp.flags = 0;
646
7.78M
    ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
647
7.78M
        B_ASN1_UTF8STRING);
648
7.78M
    if (ret < 0)
649
20.6k
        return ret;
650
    /* ASN1_mbstring_copy() guarantees the data it produced is NUL terminated */
651
7.76M
    *out = stmp.data;
652
7.76M
    return stmp.length;
653
7.78M
}