Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/x509/x_name.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 "crypto/ctype.h"
12
#include "internal/cryptlib.h"
13
#include <openssl/asn1t.h>
14
#include <openssl/x509.h>
15
#include "crypto/x509.h"
16
#include "crypto/asn1.h"
17
#include "x509_local.h"
18
19
/*
20
 * Maximum length of X509_NAME: much larger than anything we should
21
 * ever see in practice.
22
 */
23
24
142k
#define X509_NAME_MAX (1024 * 1024)
25
26
static int x509_name_ex_d2i(ASN1_VALUE **val,
27
                            const unsigned char **in, long len,
28
                            const ASN1_ITEM *it,
29
                            int tag, int aclass, char opt, ASN1_TLC *ctx);
30
31
static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
32
                            const ASN1_ITEM *it, int tag, int aclass);
33
static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
34
static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
35
36
static int x509_name_encode(X509_NAME *a);
37
static int x509_name_canon(X509_NAME *a);
38
static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in);
39
static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
40
                          unsigned char **in);
41
42
static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
43
                              int indent,
44
                              const char *fname, const ASN1_PCTX *pctx);
45
46
ASN1_SEQUENCE(X509_NAME_ENTRY) = {
47
        ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
48
        ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
49
} ASN1_SEQUENCE_END(X509_NAME_ENTRY)
50
51
IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
52
IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
53
54
/*
55
 * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
56
 * declare two template wrappers for this
57
 */
58
59
ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
60
        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
61
static_ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
62
63
ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
64
        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
65
static_ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
66
67
/*
68
 * Normally that's where it would end: we'd have two nested STACK structures
69
 * representing the ASN1. Unfortunately X509_NAME uses a completely different
70
 * form and caches encodings so we have to process the internal form and
71
 * convert to the external form.
72
 */
73
74
static const ASN1_EXTERN_FUNCS x509_name_ff = {
75
    NULL,
76
    x509_name_ex_new,
77
    x509_name_ex_free,
78
    0,                          /* Default clear behaviour is OK */
79
    x509_name_ex_d2i,
80
    x509_name_ex_i2d,
81
    x509_name_ex_print
82
};
83
84
IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
85
86
IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
87
88
IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
89
90
static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
91
315k
{
92
315k
    X509_NAME *ret = OPENSSL_zalloc(sizeof(*ret));
93
94
315k
    if (ret == NULL)
95
0
        goto memerr;
96
315k
    if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
97
0
        goto memerr;
98
315k
    if ((ret->bytes = BUF_MEM_new()) == NULL)
99
0
        goto memerr;
100
315k
    ret->modified = 1;
101
315k
    *val = (ASN1_VALUE *)ret;
102
315k
    return 1;
103
104
0
 memerr:
105
0
    ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
106
0
    if (ret) {
107
0
        sk_X509_NAME_ENTRY_free(ret->entries);
108
0
        OPENSSL_free(ret);
109
0
    }
110
0
    return 0;
111
315k
}
112
113
static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
114
315k
{
115
315k
    X509_NAME *a;
116
117
315k
    if (!pval || !*pval)
118
0
        return;
119
315k
    a = (X509_NAME *)*pval;
120
121
315k
    BUF_MEM_free(a->bytes);
122
315k
    sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
123
315k
    OPENSSL_free(a->canon_enc);
124
315k
    OPENSSL_free(a);
125
315k
    *pval = NULL;
126
315k
}
127
128
static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
129
846k
{
130
846k
    sk_X509_NAME_ENTRY_free(ne);
131
846k
}
132
133
static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
134
9.80k
{
135
9.80k
    sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
136
9.80k
}
137
138
static int x509_name_ex_d2i(ASN1_VALUE **val,
139
                            const unsigned char **in, long len,
140
                            const ASN1_ITEM *it, int tag, int aclass,
141
                            char opt, ASN1_TLC *ctx)
142
142k
{
143
142k
    const unsigned char *p = *in, *q;
144
142k
    union {
145
142k
        STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
146
142k
        ASN1_VALUE *a;
147
142k
    } intname = {
148
142k
        NULL
149
142k
    };
150
142k
    union {
151
142k
        X509_NAME *x;
152
142k
        ASN1_VALUE *a;
153
142k
    } nm = {
154
142k
        NULL
155
142k
    };
156
142k
    int i, j, ret;
157
142k
    STACK_OF(X509_NAME_ENTRY) *entries;
158
142k
    X509_NAME_ENTRY *entry;
159
142k
    if (len > X509_NAME_MAX)
160
0
        len = X509_NAME_MAX;
161
142k
    q = p;
162
163
    /* Get internal representation of Name */
164
142k
    ret = ASN1_item_ex_d2i(&intname.a,
165
142k
                           &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
166
142k
                           tag, aclass, opt, ctx);
167
168
142k
    if (ret <= 0)
169
41.1k
        return ret;
170
171
101k
    if (*val)
172
96.2k
        x509_name_ex_free(val, NULL);
173
101k
    if (!x509_name_ex_new(&nm.a, NULL))
174
0
        goto err;
175
    /* We've decoded it: now cache encoding */
176
101k
    if (!BUF_MEM_grow(nm.x->bytes, p - q))
177
0
        goto err;
178
101k
    memcpy(nm.x->bytes->data, q, p - q);
179
180
    /* Convert internal representation to X509_NAME structure */
181
945k
    for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
182
844k
        entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
183
1.31M
        for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
184
471k
            entry = sk_X509_NAME_ENTRY_value(entries, j);
185
471k
            entry->set = i;
186
471k
            if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
187
0
                goto err;
188
471k
            sk_X509_NAME_ENTRY_set(entries, j, NULL);
189
471k
        }
190
844k
    }
191
101k
    ret = x509_name_canon(nm.x);
192
101k
    if (!ret)
193
486
        goto err;
194
100k
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
195
100k
                                         local_sk_X509_NAME_ENTRY_free);
196
100k
    nm.x->modified = 0;
197
100k
    *val = nm.a;
198
100k
    *in = p;
199
100k
    return ret;
200
201
486
 err:
202
486
    if (nm.x != NULL)
203
486
        X509_NAME_free(nm.x);
204
486
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
205
486
                                         local_sk_X509_NAME_ENTRY_pop_free);
206
486
    ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
207
486
    return 0;
208
101k
}
209
210
static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
211
                            const ASN1_ITEM *it, int tag, int aclass)
212
15.1k
{
213
15.1k
    int ret;
214
15.1k
    X509_NAME *a = (X509_NAME *)*val;
215
15.1k
    if (a->modified) {
216
391
        ret = x509_name_encode(a);
217
391
        if (ret < 0)
218
0
            return ret;
219
391
        ret = x509_name_canon(a);
220
391
        if (!ret)
221
194
            return -1;
222
391
    }
223
14.9k
    ret = a->bytes->length;
224
14.9k
    if (out != NULL) {
225
3.56k
        memcpy(*out, a->bytes->data, ret);
226
3.56k
        *out += ret;
227
3.56k
    }
228
14.9k
    return ret;
229
15.1k
}
230
231
static int x509_name_encode(X509_NAME *a)
232
391
{
233
391
    union {
234
391
        STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
235
391
        ASN1_VALUE *a;
236
391
    } intname = {
237
391
        NULL
238
391
    };
239
391
    int len;
240
391
    unsigned char *p;
241
391
    STACK_OF(X509_NAME_ENTRY) *entries = NULL;
242
391
    X509_NAME_ENTRY *entry;
243
391
    int i, set = -1;
244
391
    intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
245
391
    if (!intname.s)
246
0
        goto memerr;
247
226k
    for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
248
226k
        entry = sk_X509_NAME_ENTRY_value(a->entries, i);
249
226k
        if (entry->set != set) {
250
2.81k
            entries = sk_X509_NAME_ENTRY_new_null();
251
2.81k
            if (!entries)
252
0
                goto memerr;
253
2.81k
            if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries)) {
254
0
                sk_X509_NAME_ENTRY_free(entries);
255
0
                goto memerr;
256
0
            }
257
2.81k
            set = entry->set;
258
2.81k
        }
259
226k
        if (!sk_X509_NAME_ENTRY_push(entries, entry))
260
0
            goto memerr;
261
226k
    }
262
391
    len = ASN1_item_ex_i2d(&intname.a, NULL,
263
391
                           ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
264
391
    if (!BUF_MEM_grow(a->bytes, len))
265
0
        goto memerr;
266
391
    p = (unsigned char *)a->bytes->data;
267
391
    ASN1_item_ex_i2d(&intname.a,
268
391
                     &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
269
391
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
270
391
                                         local_sk_X509_NAME_ENTRY_free);
271
391
    a->modified = 0;
272
391
    return len;
273
0
 memerr:
274
0
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
275
0
                                         local_sk_X509_NAME_ENTRY_free);
276
0
    ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
277
0
    return -1;
278
391
}
279
280
static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
281
                              int indent,
282
                              const char *fname, const ASN1_PCTX *pctx)
283
2.13k
{
284
2.13k
    if (X509_NAME_print_ex(out, (const X509_NAME *)*pval,
285
2.13k
                           indent, pctx->nm_flags) <= 0)
286
49
        return 0;
287
2.08k
    return 2;
288
2.13k
}
289
290
/*
291
 * This function generates the canonical encoding of the Name structure. In
292
 * it all strings are converted to UTF8, leading, trailing and multiple
293
 * spaces collapsed, converted to lower case and the leading SEQUENCE header
294
 * removed. In future we could also normalize the UTF8 too. By doing this
295
 * comparison of Name structures can be rapidly performed by just using
296
 * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
297
 * constraints of type dirName can also be checked with a simple memcmp().
298
 */
299
300
static int x509_name_canon(X509_NAME *a)
301
101k
{
302
101k
    unsigned char *p;
303
101k
    STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname;
304
101k
    STACK_OF(X509_NAME_ENTRY) *entries = NULL;
305
101k
    X509_NAME_ENTRY *entry, *tmpentry = NULL;
306
101k
    int i, set = -1, ret = 0, len;
307
308
101k
    OPENSSL_free(a->canon_enc);
309
101k
    a->canon_enc = NULL;
310
    /* Special case: empty X509_NAME => null encoding */
311
101k
    if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
312
98.6k
        a->canon_enclen = 0;
313
98.6k
        return 1;
314
98.6k
    }
315
3.08k
    intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
316
3.08k
    if (intname == NULL) {
317
0
        X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
318
0
        goto err;
319
0
    }
320
700k
    for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
321
697k
        entry = sk_X509_NAME_ENTRY_value(a->entries, i);
322
697k
        if (entry->set != set) {
323
9.13k
            entries = sk_X509_NAME_ENTRY_new_null();
324
9.13k
            if (entries == NULL)
325
0
                goto err;
326
9.13k
            if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
327
0
                sk_X509_NAME_ENTRY_free(entries);
328
0
                X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
329
0
                goto err;
330
0
            }
331
9.13k
            set = entry->set;
332
9.13k
        }
333
697k
        tmpentry = X509_NAME_ENTRY_new();
334
697k
        if (tmpentry == NULL) {
335
0
            X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
336
0
            goto err;
337
0
        }
338
697k
        tmpentry->object = OBJ_dup(entry->object);
339
697k
        if (tmpentry->object == NULL) {
340
0
            X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
341
0
            goto err;
342
0
        }
343
697k
        if (!asn1_string_canon(tmpentry->value, entry->value))
344
680
            goto err;
345
697k
        if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) {
346
0
            X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
347
0
            goto err;
348
0
        }
349
697k
        tmpentry = NULL;
350
697k
    }
351
352
    /* Finally generate encoding */
353
2.40k
    len = i2d_name_canon(intname, NULL);
354
2.40k
    if (len < 0)
355
0
        goto err;
356
2.40k
    a->canon_enclen = len;
357
358
2.40k
    p = OPENSSL_malloc(a->canon_enclen);
359
2.40k
    if (p == NULL) {
360
0
        X509err(X509_F_X509_NAME_CANON, ERR_R_MALLOC_FAILURE);
361
0
        goto err;
362
0
    }
363
364
2.40k
    a->canon_enc = p;
365
366
2.40k
    i2d_name_canon(intname, &p);
367
368
2.40k
    ret = 1;
369
370
3.08k
 err:
371
3.08k
    X509_NAME_ENTRY_free(tmpentry);
372
3.08k
    sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
373
3.08k
                                         local_sk_X509_NAME_ENTRY_pop_free);
374
3.08k
    return ret;
375
2.40k
}
376
377
/* Bitmap of all the types of string that will be canonicalized. */
378
379
#define ASN1_MASK_CANON \
380
697k
        (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
381
697k
        | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
382
697k
        | B_ASN1_VISIBLESTRING)
383
384
static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in)
385
697k
{
386
697k
    unsigned char *to, *from;
387
697k
    int len, i;
388
389
    /* If type not in bitmask just copy string across */
390
697k
    if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
391
163k
        if (!ASN1_STRING_copy(out, in))
392
0
            return 0;
393
163k
        return 1;
394
163k
    }
395
396
534k
    out->type = V_ASN1_UTF8STRING;
397
534k
    out->length = ASN1_STRING_to_UTF8(&out->data, in);
398
534k
    if (out->length == -1)
399
680
        return 0;
400
401
533k
    to = out->data;
402
533k
    from = to;
403
404
533k
    len = out->length;
405
406
    /*
407
     * Convert string in place to canonical form. Ultimately we may need to
408
     * handle a wider range of characters but for now ignore anything with
409
     * MSB set and rely on the ossl_isspace() to fail on bad characters without
410
     * needing isascii or range checks as well.
411
     */
412
413
    /* Ignore leading spaces */
414
533k
    while (len > 0 && ossl_isspace(*from)) {
415
460
        from++;
416
460
        len--;
417
460
    }
418
419
533k
    to = from + len;
420
421
    /* Ignore trailing spaces */
422
535k
    while (len > 0 && ossl_isspace(to[-1])) {
423
2.29k
        to--;
424
2.29k
        len--;
425
2.29k
    }
426
427
533k
    to = out->data;
428
429
533k
    i = 0;
430
18.5M
    while (i < len) {
431
        /* If not ASCII set just copy across */
432
18.0M
        if (!ossl_isascii(*from)) {
433
17.6M
            *to++ = *from++;
434
17.6M
            i++;
435
17.6M
        }
436
        /* Collapse multiple spaces */
437
391k
        else if (ossl_isspace(*from)) {
438
            /* Copy one space across */
439
10.6k
            *to++ = ' ';
440
            /*
441
             * Ignore subsequent spaces. Note: don't need to check len here
442
             * because we know the last character is a non-space so we can't
443
             * overflow.
444
             */
445
196k
            do {
446
196k
                from++;
447
196k
                i++;
448
196k
            }
449
196k
            while (ossl_isspace(*from));
450
380k
        } else {
451
380k
            *to++ = ossl_tolower(*from);
452
380k
            from++;
453
380k
            i++;
454
380k
        }
455
18.0M
    }
456
457
533k
    out->length = to - out->data;
458
459
533k
    return 1;
460
461
534k
}
462
463
static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
464
                          unsigned char **in)
465
4.81k
{
466
4.81k
    int i, len, ltmp;
467
4.81k
    ASN1_VALUE *v;
468
4.81k
    STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
469
470
4.81k
    len = 0;
471
19.3k
    for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
472
14.4k
        v = sk_ASN1_VALUE_value(intname, i);
473
14.4k
        ltmp = ASN1_item_ex_i2d(&v, in,
474
14.4k
                                ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
475
14.4k
        if (ltmp < 0)
476
0
            return ltmp;
477
14.4k
        len += ltmp;
478
14.4k
    }
479
4.81k
    return len;
480
4.81k
}
481
482
int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
483
0
{
484
0
    if (*xn == name)
485
0
        return *xn != NULL;
486
0
    if ((name = X509_NAME_dup(name)) == NULL)
487
0
        return 0;
488
0
    X509_NAME_free(*xn);
489
0
    *xn = name;
490
0
    return 1;
491
0
}
492
493
int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
494
2.13k
{
495
2.13k
    char *s, *c, *b;
496
2.13k
    int i;
497
498
2.13k
    b = X509_NAME_oneline(name, NULL, 0);
499
2.13k
    if (!b)
500
49
        return 0;
501
2.08k
    if (!*b) {
502
1.13k
        OPENSSL_free(b);
503
1.13k
        return 1;
504
1.13k
    }
505
947
    s = b + 1;                  /* skip the first slash */
506
507
947
    c = s;
508
83.2M
    for (;;) {
509
83.2M
        if (((*s == '/') &&
510
83.2M
             (ossl_isupper(s[1]) && ((s[2] == '=') ||
511
1.50k
                                (ossl_isupper(s[2]) && (s[3] == '='))
512
83.2M
              ))) || (*s == '\0'))
513
1.55k
        {
514
1.55k
            i = s - c;
515
1.55k
            if (BIO_write(bp, c, i) != i)
516
0
                goto err;
517
1.55k
            c = s + 1;          /* skip following slash */
518
1.55k
            if (*s != '\0') {
519
603
                if (BIO_write(bp, ", ", 2) != 2)
520
0
                    goto err;
521
603
            }
522
1.55k
        }
523
83.2M
        if (*s == '\0')
524
947
            break;
525
83.2M
        s++;
526
83.2M
    }
527
528
947
    OPENSSL_free(b);
529
947
    return 1;
530
0
 err:
531
0
    X509err(X509_F_X509_NAME_PRINT, ERR_R_BUF_LIB);
532
0
    OPENSSL_free(b);
533
0
    return 0;
534
947
}
535
536
int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **pder,
537
                       size_t *pderlen)
538
0
{
539
    /* Make sure encoding is valid */
540
0
    if (i2d_X509_NAME(nm, NULL) <= 0)
541
0
        return 0;
542
0
    if (pder != NULL)
543
0
        *pder = (unsigned char *)nm->bytes->data;
544
0
    if (pderlen != NULL)
545
0
        *pderlen = nm->bytes->length;
546
0
    return 1;
547
0
}