Coverage Report

Created: 2026-04-29 06:57

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