Coverage Report

Created: 2026-05-20 07:05

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