Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/x509/v3_ncons.c
Line
Count
Source
1
/*
2
 * Copyright 2003-2024 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 "internal/cryptlib.h"
11
#include "internal/numbers.h"
12
#include "internal/safe_math.h"
13
#include <stdio.h>
14
#include "crypto/asn1.h"
15
#include <openssl/asn1t.h>
16
#include <openssl/conf.h>
17
#include <openssl/x509v3.h>
18
#include <openssl/bn.h>
19
20
#include "crypto/x509.h"
21
#include "crypto/punycode.h"
22
#include "ext_dat.h"
23
24
OSSL_SAFE_MATH_SIGNED(int, int)
25
26
static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
27
    X509V3_CTX *ctx,
28
    STACK_OF(CONF_VALUE) *nval);
29
static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
30
    BIO *bp, int ind);
31
static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
32
    STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
33
    int ind, const char *name);
34
static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
35
36
static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
37
static int nc_match_single(int effective_type, GENERAL_NAME *sub,
38
    GENERAL_NAME *gen);
39
static int nc_dn(const X509_NAME *sub, const X509_NAME *nm);
40
static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
41
static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
42
static int nc_email_eai(ASN1_TYPE *emltype, ASN1_IA5STRING *base);
43
static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
44
static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base);
45
46
const X509V3_EXT_METHOD ossl_v3_name_constraints = {
47
    NID_name_constraints, 0,
48
    ASN1_ITEM_ref(NAME_CONSTRAINTS),
49
    0, 0, 0, 0,
50
    0, 0,
51
    0, v2i_NAME_CONSTRAINTS,
52
    i2r_NAME_CONSTRAINTS, 0,
53
    NULL
54
};
55
56
const X509V3_EXT_METHOD ossl_v3_holder_name_constraints = {
57
    NID_holder_name_constraints, 0,
58
    ASN1_ITEM_ref(NAME_CONSTRAINTS),
59
    0, 0, 0, 0,
60
    0, 0,
61
    0, v2i_NAME_CONSTRAINTS,
62
    i2r_NAME_CONSTRAINTS, 0,
63
    NULL
64
};
65
66
const X509V3_EXT_METHOD ossl_v3_delegated_name_constraints = {
67
    NID_delegated_name_constraints, 0,
68
    ASN1_ITEM_ref(NAME_CONSTRAINTS),
69
    0, 0, 0, 0,
70
    0, 0,
71
    0, v2i_NAME_CONSTRAINTS,
72
    i2r_NAME_CONSTRAINTS, 0,
73
    NULL
74
};
75
76
ASN1_SEQUENCE(GENERAL_SUBTREE) = {
77
    ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
78
    ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
79
    ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
80
806k
} ASN1_SEQUENCE_END(GENERAL_SUBTREE)
81
806k
82
806k
ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
83
806k
    ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
84
806k
        GENERAL_SUBTREE, 0),
85
806k
    ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
86
806k
        GENERAL_SUBTREE, 1),
87
2.40M
} ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
88
2.40M
89
2.40M
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
90
2.40M
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
91
2.40M
92
2.40M
#define IA5_OFFSET_LEN(ia5base, offset) \
93
2.40M
    ((ia5base)->length - ((unsigned char *)(offset) - (ia5base)->data))
94
95
/* Like memchr but for ASN1_IA5STRING. Additionally you can specify the
96
 * starting point to search from
97
 */
98
0
#define ia5memchr(str, start, c) memchr(start, c, IA5_OFFSET_LEN(str, start))
99
100
/* Like memrrchr but for ASN1_IA5STRING */
101
static char *ia5memrchr(ASN1_IA5STRING *str, int c)
102
0
{
103
0
    int i;
104
105
0
    for (i = str->length; i > 0 && str->data[i - 1] != c; i--)
106
0
        ;
107
108
0
    if (i == 0)
109
0
        return NULL;
110
111
0
    return (char *)&str->data[i - 1];
112
0
}
113
114
/*
115
 * We cannot use strncasecmp here because that applies locale specific rules. It
116
 * also doesn't work with ASN1_STRINGs that may have embedded NUL characters.
117
 * For example in Turkish 'I' is not the uppercase character for 'i'. We need to
118
 * do a simple ASCII case comparison ignoring the locale (that is why we use
119
 * numeric constants below).
120
 */
121
static int ia5ncasecmp(const char *s1, const char *s2, size_t n)
122
0
{
123
0
    for (; n > 0; n--, s1++, s2++) {
124
0
        if (*s1 != *s2) {
125
0
            unsigned char c1 = (unsigned char)*s1, c2 = (unsigned char)*s2;
126
127
            /* Convert to lower case */
128
0
            if (c1 >= 0x41 /* A */ && c1 <= 0x5A /* Z */)
129
0
                c1 += 0x20;
130
0
            if (c2 >= 0x41 /* A */ && c2 <= 0x5A /* Z */)
131
0
                c2 += 0x20;
132
133
0
            if (c1 == c2)
134
0
                continue;
135
136
0
            if (c1 < c2)
137
0
                return -1;
138
139
            /* c1 > c2 */
140
0
            return 1;
141
0
        }
142
0
    }
143
144
0
    return 0;
145
0
}
146
147
static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
148
    X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
149
0
{
150
0
    int i;
151
0
    CONF_VALUE tval, *val;
152
0
    STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
153
0
    NAME_CONSTRAINTS *ncons = NULL;
154
0
    GENERAL_SUBTREE *sub = NULL;
155
156
0
    ncons = NAME_CONSTRAINTS_new();
157
0
    if (ncons == NULL) {
158
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
159
0
        goto err;
160
0
    }
161
0
    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
162
0
        val = sk_CONF_VALUE_value(nval, i);
163
0
        if (HAS_PREFIX(val->name, "permitted") && val->name[9]) {
164
0
            ptree = &ncons->permittedSubtrees;
165
0
            tval.name = val->name + 10;
166
0
        } else if (HAS_PREFIX(val->name, "excluded") && val->name[8]) {
167
0
            ptree = &ncons->excludedSubtrees;
168
0
            tval.name = val->name + 9;
169
0
        } else {
170
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SYNTAX);
171
0
            goto err;
172
0
        }
173
0
        tval.value = val->value;
174
0
        sub = GENERAL_SUBTREE_new();
175
0
        if (sub == NULL) {
176
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
177
0
            goto err;
178
0
        }
179
0
        if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1)) {
180
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);
181
0
            goto err;
182
0
        }
183
0
        if (*ptree == NULL)
184
0
            *ptree = sk_GENERAL_SUBTREE_new_null();
185
0
        if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub)) {
186
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
187
0
            goto err;
188
0
        }
189
0
        sub = NULL;
190
0
    }
191
192
0
    return ncons;
193
194
0
err:
195
0
    NAME_CONSTRAINTS_free(ncons);
196
0
    GENERAL_SUBTREE_free(sub);
197
198
0
    return NULL;
199
0
}
200
201
static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
202
    BIO *bp, int ind)
203
118k
{
204
118k
    NAME_CONSTRAINTS *ncons = a;
205
118k
    do_i2r_name_constraints(method, ncons->permittedSubtrees,
206
118k
        bp, ind, "Permitted");
207
118k
    if (ncons->permittedSubtrees && ncons->excludedSubtrees)
208
4.50k
        BIO_puts(bp, "\n");
209
118k
    do_i2r_name_constraints(method, ncons->excludedSubtrees,
210
118k
        bp, ind, "Excluded");
211
118k
    return 1;
212
118k
}
213
214
static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
215
    STACK_OF(GENERAL_SUBTREE) *trees,
216
    BIO *bp, int ind, const char *name)
217
236k
{
218
236k
    GENERAL_SUBTREE *tree;
219
236k
    int i;
220
236k
    if (sk_GENERAL_SUBTREE_num(trees) > 0)
221
47.5k
        BIO_printf(bp, "%*s%s:\n", ind, "", name);
222
309k
    for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
223
72.9k
        if (i > 0)
224
25.4k
            BIO_puts(bp, "\n");
225
72.9k
        tree = sk_GENERAL_SUBTREE_value(trees, i);
226
72.9k
        BIO_printf(bp, "%*s", ind + 2, "");
227
72.9k
        if (tree->base->type == GEN_IPADD)
228
19.2k
            print_nc_ipadd(bp, tree->base->d.ip);
229
53.7k
        else
230
53.7k
            GENERAL_NAME_print(bp, tree->base);
231
72.9k
    }
232
236k
    return 1;
233
236k
}
234
235
static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
236
19.2k
{
237
    /* ip->length should be 8 or 32 and len1 == len2 == 4 or len1 == len2 == 16 */
238
19.2k
    int len1 = ip->length >= 16 ? 16 : ip->length >= 4 ? 4
239
14.6k
                                                       : ip->length;
240
19.2k
    int len2 = ip->length - len1;
241
19.2k
    char *ip1 = ossl_ipaddr_to_asc(ip->data, len1);
242
19.2k
    char *ip2 = ossl_ipaddr_to_asc(ip->data + len1, len2);
243
19.2k
    int ret = ip1 != NULL && ip2 != NULL
244
19.2k
        && BIO_printf(bp, "IP:%s/%s", ip1, ip2) > 0;
245
246
19.2k
    OPENSSL_free(ip1);
247
19.2k
    OPENSSL_free(ip2);
248
19.2k
    return ret;
249
19.2k
}
250
251
321
#define NAME_CHECK_MAX (1 << 20)
252
253
static int add_lengths(int *out, int a, int b)
254
540
{
255
540
    int err = 0;
256
257
    /* sk_FOO_num(NULL) returns -1 but is effectively 0 when iterating. */
258
540
    if (a < 0)
259
268
        a = 0;
260
540
    if (b < 0)
261
484
        b = 0;
262
263
540
    *out = safe_add_int(a, b, &err);
264
540
    return !err;
265
540
}
266
267
/*-
268
 * Check a certificate conforms to a specified set of constraints.
269
 * Return values:
270
 *  X509_V_OK: All constraints obeyed.
271
 *  X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
272
 *  X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
273
 *  X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
274
 *  X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE:  Unsupported constraint type.
275
 *  X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
276
 *  X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
277
 */
278
279
int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
280
333
{
281
333
    int r, i, name_count, constraint_count;
282
333
    X509_NAME *nm;
283
284
333
    nm = X509_get_subject_name(x);
285
286
    /*
287
     * Guard against certificates with an excessive number of names or
288
     * constraints causing a computationally expensive name constraints check.
289
     */
290
333
    if (!add_lengths(&name_count, X509_NAME_entry_count(nm),
291
333
            sk_GENERAL_NAME_num(x->altname))
292
333
        || !add_lengths(&constraint_count,
293
333
            sk_GENERAL_SUBTREE_num(nc->permittedSubtrees),
294
333
            sk_GENERAL_SUBTREE_num(nc->excludedSubtrees))
295
333
        || (name_count > 0 && constraint_count > NAME_CHECK_MAX / name_count))
296
0
        return X509_V_ERR_UNSPECIFIED;
297
298
333
    if (X509_NAME_entry_count(nm) > 0) {
299
319
        GENERAL_NAME gntmp;
300
319
        gntmp.type = GEN_DIRNAME;
301
319
        gntmp.d.directoryName = nm;
302
303
319
        r = nc_match(&gntmp, nc);
304
305
319
        if (r != X509_V_OK)
306
0
            return r;
307
308
319
        gntmp.type = GEN_EMAIL;
309
310
        /* Process any email address attributes in subject name */
311
312
319
        for (i = -1;;) {
313
319
            const X509_NAME_ENTRY *ne;
314
315
319
            i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
316
319
            if (i == -1)
317
319
                break;
318
0
            ne = X509_NAME_get_entry(nm, i);
319
0
            gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
320
0
            if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
321
0
                return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
322
323
0
            r = nc_match(&gntmp, nc);
324
325
0
            if (r != X509_V_OK)
326
0
                return r;
327
0
        }
328
319
    }
329
330
344
    for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
331
11
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
332
11
        r = nc_match(gen, nc);
333
11
        if (r != X509_V_OK)
334
0
            return r;
335
11
    }
336
337
333
    return X509_V_OK;
338
333
}
339
340
static int cn2dnsid(ASN1_STRING *cn, unsigned char **dnsid, size_t *idlen)
341
287
{
342
287
    int utf8_length;
343
287
    unsigned char *utf8_value;
344
287
    int i;
345
287
    int isdnsname = 0;
346
347
    /* Don't leave outputs uninitialized */
348
287
    *dnsid = NULL;
349
287
    *idlen = 0;
350
351
    /*-
352
     * Per RFC 6125, DNS-IDs representing internationalized domain names appear
353
     * in certificates in A-label encoded form:
354
     *
355
     *   https://tools.ietf.org/html/rfc6125#section-6.4.2
356
     *
357
     * The same applies to CNs which are intended to represent DNS names.
358
     * However, while in the SAN DNS-IDs are IA5Strings, as CNs they may be
359
     * needlessly encoded in 16-bit Unicode.  We perform a conversion to UTF-8
360
     * to ensure that we get an ASCII representation of any CNs that are
361
     * representable as ASCII, but just not encoded as ASCII.  The UTF-8 form
362
     * may contain some non-ASCII octets, and that's fine, such CNs are not
363
     * valid legacy DNS names.
364
     *
365
     * Note, 'int' is the return type of ASN1_STRING_to_UTF8() so that's what
366
     * we must use for 'utf8_length'.
367
     */
368
287
    if ((utf8_length = ASN1_STRING_to_UTF8(&utf8_value, cn)) < 0)
369
11
        return X509_V_ERR_OUT_OF_MEM;
370
371
    /*
372
     * Some certificates have had names that include a *trailing* NUL byte.
373
     * Remove these harmless NUL characters. They would otherwise yield false
374
     * alarms with the following embedded NUL check.
375
     */
376
557
    while (utf8_length > 0 && utf8_value[utf8_length - 1] == '\0')
377
281
        --utf8_length;
378
379
    /* Reject *embedded* NULs */
380
276
    if (memchr(utf8_value, 0, utf8_length) != NULL) {
381
15
        OPENSSL_free(utf8_value);
382
15
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
383
15
    }
384
385
    /*
386
     * XXX: Deviation from strict DNS name syntax, also check names with '_'
387
     * Check DNS name syntax, any '-' or '.' must be internal,
388
     * and on either side of each '.' we can't have a '-' or '.'.
389
     *
390
     * If the name has just one label, we don't consider it a DNS name.  This
391
     * means that "CN=sometld" cannot be precluded by DNS name constraints, but
392
     * that is not a problem.
393
     */
394
1.23k
    for (i = 0; i < utf8_length; ++i) {
395
1.13k
        unsigned char c = utf8_value[i];
396
397
1.13k
        if ((c >= 'a' && c <= 'z')
398
752
            || (c >= 'A' && c <= 'Z')
399
592
            || (c >= '0' && c <= '9')
400
406
            || c == '_')
401
835
            continue;
402
403
        /* Dot and hyphen cannot be first or last. */
404
298
        if (i > 0 && i < utf8_length - 1) {
405
233
            if (c == '-')
406
73
                continue;
407
            /*
408
             * Next to a dot the preceding and following characters must not be
409
             * another dot or a hyphen.  Otherwise, record that the name is
410
             * plausible, since it has two or more labels.
411
             */
412
160
            if (c == '.'
413
84
                && utf8_value[i + 1] != '.'
414
78
                && utf8_value[i - 1] != '-'
415
71
                && utf8_value[i + 1] != '-') {
416
65
                isdnsname = 1;
417
65
                continue;
418
65
            }
419
160
        }
420
160
        isdnsname = 0;
421
160
        break;
422
298
    }
423
424
261
    if (isdnsname) {
425
19
        *dnsid = utf8_value;
426
19
        *idlen = (size_t)utf8_length;
427
19
        return X509_V_OK;
428
19
    }
429
242
    OPENSSL_free(utf8_value);
430
242
    return X509_V_OK;
431
261
}
432
433
/*
434
 * Check CN against DNS-ID name constraints.
435
 */
436
int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
437
331
{
438
331
    int r, i;
439
331
    const X509_NAME *nm = X509_get_subject_name(x);
440
331
    ASN1_STRING stmp;
441
331
    GENERAL_NAME gntmp;
442
443
331
    stmp.flags = 0;
444
331
    stmp.type = V_ASN1_IA5STRING;
445
331
    gntmp.type = GEN_DNS;
446
331
    gntmp.d.dNSName = &stmp;
447
448
    /* Process any commonName attributes in subject name */
449
450
592
    for (i = -1;;) {
451
592
        X509_NAME_ENTRY *ne;
452
592
        ASN1_STRING *cn;
453
592
        unsigned char *idval;
454
592
        size_t idlen;
455
456
592
        i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
457
592
        if (i == -1)
458
305
            break;
459
287
        ne = X509_NAME_get_entry(nm, i);
460
287
        cn = X509_NAME_ENTRY_get_data(ne);
461
462
        /* Only process attributes that look like hostnames */
463
287
        if ((r = cn2dnsid(cn, &idval, &idlen)) != X509_V_OK)
464
26
            return r;
465
261
        if (idlen == 0)
466
242
            continue;
467
468
19
        stmp.length = idlen;
469
19
        stmp.data = idval;
470
19
        r = nc_match(&gntmp, nc);
471
19
        OPENSSL_free(idval);
472
19
        if (r != X509_V_OK)
473
0
            return r;
474
19
    }
475
305
    return X509_V_OK;
476
331
}
477
478
/*
479
 * Return nonzero if the GeneralSubtree has valid 'minimum' field
480
 * (must be absent or 0) and valid 'maximum' field (must be absent).
481
 */
482
static int nc_minmax_valid(GENERAL_SUBTREE *sub)
483
0
{
484
0
    BIGNUM *bn = NULL;
485
0
    int ok = 1;
486
487
0
    if (sub->maximum)
488
0
        ok = 0;
489
490
0
    if (sub->minimum) {
491
0
        bn = ASN1_INTEGER_to_BN(sub->minimum, NULL);
492
0
        if (bn == NULL || !BN_is_zero(bn))
493
0
            ok = 0;
494
0
        BN_free(bn);
495
0
    }
496
497
0
    return ok;
498
0
}
499
500
static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
501
349
{
502
349
    GENERAL_SUBTREE *sub;
503
349
    int i, r, match = 0;
504
349
    int effective_type = gen->type;
505
506
    /*
507
     * We need to compare not gen->type field but an "effective" type because
508
     * the otherName field may contain EAI email address treated specially
509
     * according to RFC 8398, section 6
510
     */
511
349
    if (effective_type == GEN_OTHERNAME && (OBJ_obj2nid(gen->d.otherName->type_id) == NID_id_on_SmtpUTF8Mailbox)) {
512
0
        effective_type = GEN_EMAIL;
513
0
    }
514
515
    /*
516
     * Permitted subtrees: if any subtrees exist of matching the type at
517
     * least one subtree must match.
518
     */
519
520
349
    for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
521
0
        sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
522
0
        if (effective_type != sub->base->type
523
0
            || (effective_type == GEN_OTHERNAME && OBJ_cmp(gen->d.otherName->type_id, sub->base->d.otherName->type_id) != 0))
524
0
            continue;
525
0
        if (!nc_minmax_valid(sub))
526
0
            return X509_V_ERR_SUBTREE_MINMAX;
527
        /* If we already have a match don't bother trying any more */
528
0
        if (match == 2)
529
0
            continue;
530
0
        if (match == 0)
531
0
            match = 1;
532
0
        r = nc_match_single(effective_type, gen, sub->base);
533
0
        if (r == X509_V_OK)
534
0
            match = 2;
535
0
        else if (r != X509_V_ERR_PERMITTED_VIOLATION)
536
0
            return r;
537
0
    }
538
539
349
    if (match == 1)
540
0
        return X509_V_ERR_PERMITTED_VIOLATION;
541
542
    /* Excluded subtrees: must not match any of these */
543
544
349
    for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
545
0
        sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
546
0
        if (effective_type != sub->base->type
547
0
            || (effective_type == GEN_OTHERNAME && OBJ_cmp(gen->d.otherName->type_id, sub->base->d.otherName->type_id) != 0))
548
0
            continue;
549
0
        if (!nc_minmax_valid(sub))
550
0
            return X509_V_ERR_SUBTREE_MINMAX;
551
552
0
        r = nc_match_single(effective_type, gen, sub->base);
553
0
        if (r == X509_V_OK)
554
0
            return X509_V_ERR_EXCLUDED_VIOLATION;
555
0
        else if (r != X509_V_ERR_PERMITTED_VIOLATION)
556
0
            return r;
557
0
    }
558
559
349
    return X509_V_OK;
560
349
}
561
562
static int nc_match_single(int effective_type, GENERAL_NAME *gen,
563
    GENERAL_NAME *base)
564
0
{
565
0
    switch (gen->type) {
566
0
    case GEN_OTHERNAME:
567
0
        switch (effective_type) {
568
0
        case GEN_EMAIL:
569
            /*
570
             * We are here only when we have SmtpUTF8 name,
571
             * so we match the value of othername with base->d.rfc822Name
572
             */
573
0
            return nc_email_eai(gen->d.otherName->value, base->d.rfc822Name);
574
575
0
        default:
576
0
            return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
577
0
        }
578
579
0
    case GEN_DIRNAME:
580
0
        return nc_dn(gen->d.directoryName, base->d.directoryName);
581
582
0
    case GEN_DNS:
583
0
        return nc_dns(gen->d.dNSName, base->d.dNSName);
584
585
0
    case GEN_EMAIL:
586
0
        return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
587
588
0
    case GEN_URI:
589
0
        return nc_uri(gen->d.uniformResourceIdentifier,
590
0
            base->d.uniformResourceIdentifier);
591
592
0
    case GEN_IPADD:
593
0
        return nc_ip(gen->d.iPAddress, base->d.iPAddress);
594
595
0
    default:
596
0
        return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
597
0
    }
598
0
}
599
600
/*
601
 * directoryName name constraint matching. The canonical encoding of
602
 * X509_NAME makes this comparison easy. It is matched if the subtree is a
603
 * subset of the name.
604
 */
605
606
static int nc_dn(const X509_NAME *nm, const X509_NAME *base)
607
0
{
608
    /* Ensure canonical encodings are up to date.  */
609
0
    if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
610
0
        return X509_V_ERR_OUT_OF_MEM;
611
0
    if (base->modified && i2d_X509_NAME(base, NULL) < 0)
612
0
        return X509_V_ERR_OUT_OF_MEM;
613
0
    if (base->canon_enclen > nm->canon_enclen)
614
0
        return X509_V_ERR_PERMITTED_VIOLATION;
615
    /*
616
     * An empty base Name has no canonical encoding (canon_enc == NULL) and is
617
     * a prefix of every Name, so it matches unconditionally.
618
     */
619
0
    if (base->canon_enclen == 0)
620
0
        return X509_V_OK;
621
0
    if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
622
0
        return X509_V_ERR_PERMITTED_VIOLATION;
623
0
    return X509_V_OK;
624
0
}
625
626
static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
627
0
{
628
0
    char *baseptr = (char *)base->data;
629
0
    char *dnsptr = (char *)dns->data;
630
631
    /* Empty matches everything */
632
0
    if (base->length == 0)
633
0
        return X509_V_OK;
634
635
0
    if (dns->length < base->length)
636
0
        return X509_V_ERR_PERMITTED_VIOLATION;
637
638
    /*
639
     * Otherwise can add zero or more components on the left so compare RHS
640
     * and if dns is longer and expect '.' as preceding character.
641
     */
642
0
    if (dns->length > base->length) {
643
0
        dnsptr += dns->length - base->length;
644
0
        if (*baseptr != '.' && dnsptr[-1] != '.')
645
0
            return X509_V_ERR_PERMITTED_VIOLATION;
646
0
    }
647
648
0
    if (ia5ncasecmp(baseptr, dnsptr, base->length))
649
0
        return X509_V_ERR_PERMITTED_VIOLATION;
650
651
0
    return X509_V_OK;
652
0
}
653
654
/*
655
 * This function implements comparison between ASCII/U-label in emltype
656
 * and A-label in base according to RFC 8398, section 6.
657
 * Convert base to U-label and ASCII-parts of domain names, for base
658
 * Octet-to-octet comparison of `emltype` and `base` hostname parts
659
 * (ASCII-parts should be compared in case-insensitive manner)
660
 */
661
static int nc_email_eai(ASN1_TYPE *emltype, ASN1_IA5STRING *base)
662
0
{
663
0
    ASN1_UTF8STRING *eml;
664
0
    char *baseptr = NULL;
665
0
    const char *emlptr;
666
0
    const char *emlat;
667
0
    char ulabel[256];
668
0
    size_t size = sizeof(ulabel);
669
0
    int ret = X509_V_OK;
670
0
    size_t emlhostlen;
671
672
    /* We do not accept embedded NUL characters */
673
0
    if (base->length > 0 && memchr(base->data, 0, base->length) != NULL)
674
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
675
676
    /* 'base' may not be NUL terminated. Create a copy that is */
677
0
    baseptr = OPENSSL_strndup((char *)base->data, base->length);
678
0
    if (baseptr == NULL)
679
0
        return X509_V_ERR_OUT_OF_MEM;
680
681
0
    if (emltype->type != V_ASN1_UTF8STRING) {
682
0
        ret = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
683
0
        goto end;
684
0
    }
685
686
0
    eml = emltype->value.utf8string;
687
0
    emlptr = (char *)eml->data;
688
0
    emlat = ia5memrchr(eml, '@');
689
690
0
    if (emlat == NULL) {
691
0
        ret = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
692
0
        goto end;
693
0
    }
694
695
    /* Special case: initial '.' is RHS match */
696
0
    if (*baseptr == '.') {
697
0
        ulabel[0] = '.';
698
0
        if (ossl_a2ulabel(baseptr, ulabel + 1, size - 1) <= 0) {
699
0
            ret = X509_V_ERR_UNSPECIFIED;
700
0
            goto end;
701
0
        }
702
703
0
        if ((size_t)eml->length > strlen(ulabel)) {
704
0
            emlptr += eml->length - strlen(ulabel);
705
            /* X509_V_OK */
706
0
            if (ia5ncasecmp(ulabel, emlptr, strlen(ulabel)) == 0)
707
0
                goto end;
708
0
        }
709
0
        ret = X509_V_ERR_PERMITTED_VIOLATION;
710
0
        goto end;
711
0
    }
712
713
0
    if (ossl_a2ulabel(baseptr, ulabel, size) <= 0) {
714
0
        ret = X509_V_ERR_UNSPECIFIED;
715
0
        goto end;
716
0
    }
717
    /* Just have hostname left to match: case insensitive */
718
0
    emlptr = emlat + 1;
719
0
    emlhostlen = IA5_OFFSET_LEN(eml, emlptr);
720
0
    if (emlhostlen != strlen(ulabel)
721
0
        || ia5ncasecmp(ulabel, emlptr, emlhostlen) != 0) {
722
0
        ret = X509_V_ERR_PERMITTED_VIOLATION;
723
0
        goto end;
724
0
    }
725
726
0
end:
727
0
    OPENSSL_free(baseptr);
728
0
    return ret;
729
0
}
730
731
static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
732
0
{
733
0
    const char *baseptr = (char *)base->data;
734
0
    const char *emlptr = (char *)eml->data;
735
0
    const char *baseat = ia5memrchr(base, '@');
736
0
    const char *emlat = ia5memrchr(eml, '@');
737
0
    size_t basehostlen, emlhostlen;
738
739
0
    if (!emlat)
740
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
741
    /* Special case: initial '.' is RHS match */
742
0
    if (!baseat && base->length > 0 && (*baseptr == '.')) {
743
0
        if (eml->length > base->length) {
744
0
            emlptr += eml->length - base->length;
745
0
            if (ia5ncasecmp(baseptr, emlptr, base->length) == 0)
746
0
                return X509_V_OK;
747
0
        }
748
0
        return X509_V_ERR_PERMITTED_VIOLATION;
749
0
    }
750
751
    /* If we have anything before '@' match local part */
752
753
0
    if (baseat) {
754
0
        if (baseat != baseptr) {
755
0
            if ((baseat - baseptr) != (emlat - emlptr))
756
0
                return X509_V_ERR_PERMITTED_VIOLATION;
757
0
            if (memchr(baseptr, 0, baseat - baseptr) || memchr(emlptr, 0, emlat - emlptr))
758
0
                return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
759
            /* Case sensitive match of local part */
760
0
            if (strncmp(baseptr, emlptr, emlat - emlptr))
761
0
                return X509_V_ERR_PERMITTED_VIOLATION;
762
0
        }
763
        /* Position base after '@' */
764
0
        baseptr = baseat + 1;
765
0
    }
766
0
    emlptr = emlat + 1;
767
0
    basehostlen = IA5_OFFSET_LEN(base, baseptr);
768
0
    emlhostlen = IA5_OFFSET_LEN(eml, emlptr);
769
    /* Just have hostname left to match: case insensitive */
770
0
    if (basehostlen != emlhostlen || ia5ncasecmp(baseptr, emlptr, emlhostlen))
771
0
        return X509_V_ERR_PERMITTED_VIOLATION;
772
773
0
    return X509_V_OK;
774
0
}
775
776
static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
777
0
{
778
0
    const char *baseptr = (char *)base->data;
779
0
    const char *hostptr = (char *)uri->data;
780
0
    const char *p = ia5memchr(uri, (char *)uri->data, ':');
781
0
    int hostlen;
782
783
    /* Check for foo:// and skip past it */
784
0
    if (p == NULL
785
0
        || IA5_OFFSET_LEN(uri, p) < 3
786
0
        || p[1] != '/'
787
0
        || p[2] != '/')
788
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
789
0
    hostptr = p + 3;
790
791
    /* Determine length of hostname part of URI */
792
793
    /* Look for a port indicator as end of hostname first */
794
795
0
    p = ia5memchr(uri, hostptr, ':');
796
    /* Otherwise look for trailing slash */
797
0
    if (p == NULL)
798
0
        p = ia5memchr(uri, hostptr, '/');
799
800
0
    if (p == NULL)
801
0
        hostlen = IA5_OFFSET_LEN(uri, hostptr);
802
0
    else
803
0
        hostlen = p - hostptr;
804
805
0
    if (hostlen == 0)
806
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
807
808
    /* Special case: initial '.' is RHS match */
809
0
    if (base->length > 0 && *baseptr == '.') {
810
0
        if (hostlen > base->length) {
811
0
            p = hostptr + hostlen - base->length;
812
0
            if (ia5ncasecmp(p, baseptr, base->length) == 0)
813
0
                return X509_V_OK;
814
0
        }
815
0
        return X509_V_ERR_PERMITTED_VIOLATION;
816
0
    }
817
818
0
    if ((base->length != (int)hostlen)
819
0
        || ia5ncasecmp(hostptr, baseptr, hostlen))
820
0
        return X509_V_ERR_PERMITTED_VIOLATION;
821
822
0
    return X509_V_OK;
823
0
}
824
825
static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
826
0
{
827
0
    int hostlen, baselen, i;
828
0
    unsigned char *hostptr, *baseptr, *maskptr;
829
0
    hostptr = ip->data;
830
0
    hostlen = ip->length;
831
0
    baseptr = base->data;
832
0
    baselen = base->length;
833
834
    /* Invalid if not IPv4 or IPv6 */
835
0
    if (!((hostlen == 4) || (hostlen == 16)))
836
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
837
0
    if (!((baselen == 8) || (baselen == 32)))
838
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
839
840
    /* Do not match IPv4 with IPv6 */
841
0
    if (hostlen * 2 != baselen)
842
0
        return X509_V_ERR_PERMITTED_VIOLATION;
843
844
0
    maskptr = base->data + hostlen;
845
846
    /* Considering possible not aligned base ipAddress */
847
    /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
848
0
    for (i = 0; i < hostlen; i++)
849
0
        if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
850
0
            return X509_V_ERR_PERMITTED_VIOLATION;
851
852
0
    return X509_V_OK;
853
0
}