Coverage Report

Created: 2025-12-10 06:24

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