Coverage Report

Created: 2025-12-31 06:58

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
911k
} ASN1_SEQUENCE_END(GENERAL_SUBTREE)
81
911k
82
911k
ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
83
911k
    ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
84
911k
        GENERAL_SUBTREE, 0),
85
911k
    ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
86
911k
        GENERAL_SUBTREE, 1),
87
2.20M
} ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
88
2.20M
89
2.20M
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
90
2.20M
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
91
2.20M
92
2.20M
#define IA5_OFFSET_LEN(ia5base, offset) \
93
2.20M
    ((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
133k
{
204
133k
    NAME_CONSTRAINTS *ncons = a;
205
133k
    do_i2r_name_constraints(method, ncons->permittedSubtrees,
206
133k
        bp, ind, "Permitted");
207
133k
    if (ncons->permittedSubtrees && ncons->excludedSubtrees)
208
3.54k
        BIO_puts(bp, "\n");
209
133k
    do_i2r_name_constraints(method, ncons->excludedSubtrees,
210
133k
        bp, ind, "Excluded");
211
133k
    return 1;
212
133k
}
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
266k
{
218
266k
    GENERAL_SUBTREE *tree;
219
266k
    int i;
220
266k
    if (sk_GENERAL_SUBTREE_num(trees) > 0)
221
52.1k
        BIO_printf(bp, "%*s%s:\n", ind, "", name);
222
337k
    for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
223
70.7k
        if (i > 0)
224
18.5k
            BIO_puts(bp, "\n");
225
70.7k
        tree = sk_GENERAL_SUBTREE_value(trees, i);
226
70.7k
        BIO_printf(bp, "%*s", ind + 2, "");
227
70.7k
        if (tree->base->type == GEN_IPADD)
228
13.3k
            print_nc_ipadd(bp, tree->base->d.ip);
229
57.4k
        else
230
57.4k
            GENERAL_NAME_print(bp, tree->base);
231
70.7k
    }
232
266k
    return 1;
233
266k
}
234
235
static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
236
13.3k
{
237
    /* ip->length should be 8 or 32 and len1 == len2 == 4 or len1 == len2 == 16 */
238
13.3k
    int len1 = ip->length >= 16 ? 16 : ip->length >= 4 ? 4
239
9.18k
                                                       : ip->length;
240
13.3k
    int len2 = ip->length - len1;
241
13.3k
    char *ip1 = ossl_ipaddr_to_asc(ip->data, len1);
242
13.3k
    char *ip2 = ossl_ipaddr_to_asc(ip->data + len1, len2);
243
13.3k
    int ret = ip1 != NULL && ip2 != NULL
244
13.3k
        && BIO_printf(bp, "IP:%s/%s", ip1, ip2) > 0;
245
246
13.3k
    OPENSSL_free(ip1);
247
13.3k
    OPENSSL_free(ip2);
248
13.3k
    return ret;
249
13.3k
}
250
251
299
#define NAME_CHECK_MAX (1 << 20)
252
253
static int add_lengths(int *out, int a, int b)
254
430
{
255
430
    int err = 0;
256
257
    /* sk_FOO_num(NULL) returns -1 but is effectively 0 when iterating. */
258
430
    if (a < 0)
259
215
        a = 0;
260
430
    if (b < 0)
261
401
        b = 0;
262
263
430
    *out = safe_add_int(a, b, &err);
264
430
    return !err;
265
430
}
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
305
{
281
305
    int r, i, name_count, constraint_count;
282
305
    X509_NAME *nm;
283
284
305
    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
305
    if (!add_lengths(&name_count, X509_NAME_entry_count(nm),
291
305
            sk_GENERAL_NAME_num(x->altname))
292
305
        || !add_lengths(&constraint_count,
293
305
            sk_GENERAL_SUBTREE_num(nc->permittedSubtrees),
294
305
            sk_GENERAL_SUBTREE_num(nc->excludedSubtrees))
295
305
        || (name_count > 0 && constraint_count > NAME_CHECK_MAX / name_count))
296
0
        return X509_V_ERR_UNSPECIFIED;
297
298
305
    if (X509_NAME_entry_count(nm) > 0) {
299
295
        GENERAL_NAME gntmp;
300
295
        gntmp.type = GEN_DIRNAME;
301
295
        gntmp.d.directoryName = nm;
302
303
295
        r = nc_match(&gntmp, nc);
304
305
295
        if (r != X509_V_OK)
306
0
            return r;
307
308
295
        gntmp.type = GEN_EMAIL;
309
310
        /* Process any email address attributes in subject name */
311
312
295
        for (i = -1;;) {
313
295
            const X509_NAME_ENTRY *ne;
314
315
295
            i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
316
295
            if (i == -1)
317
295
                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
295
    }
329
330
316
    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
305
    return X509_V_OK;
338
305
}
339
340
static int cn2dnsid(ASN1_STRING *cn, unsigned char **dnsid, size_t *idlen)
341
253
{
342
253
    int utf8_length;
343
253
    unsigned char *utf8_value;
344
253
    int i;
345
253
    int isdnsname = 0;
346
347
    /* Don't leave outputs uninitialized */
348
253
    *dnsid = NULL;
349
253
    *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
253
    if ((utf8_length = ASN1_STRING_to_UTF8(&utf8_value, cn)) < 0)
369
9
        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
549
    while (utf8_length > 0 && utf8_value[utf8_length - 1] == '\0')
377
305
        --utf8_length;
378
379
    /* Reject *embedded* NULs */
380
244
    if (memchr(utf8_value, 0, utf8_length) != NULL) {
381
10
        OPENSSL_free(utf8_value);
382
10
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
383
10
    }
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.14k
    for (i = 0; i < utf8_length; ++i) {
395
1.06k
        unsigned char c = utf8_value[i];
396
397
1.06k
        if ((c >= 'a' && c <= 'z')
398
733
            || (c >= 'A' && c <= 'Z')
399
565
            || (c >= '0' && c <= '9')
400
359
            || c == '_')
401
781
            continue;
402
403
        /* Dot and hyphen cannot be first or last. */
404
283
        if (i > 0 && i < utf8_length - 1) {
405
221
            if (c == '-')
406
66
                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
155
            if (c == '.'
413
80
                && utf8_value[i + 1] != '.'
414
74
                && utf8_value[i - 1] != '-'
415
67
                && utf8_value[i + 1] != '-') {
416
61
                isdnsname = 1;
417
61
                continue;
418
61
            }
419
155
        }
420
156
        isdnsname = 0;
421
156
        break;
422
283
    }
423
424
234
    if (isdnsname) {
425
12
        *dnsid = utf8_value;
426
12
        *idlen = (size_t)utf8_length;
427
12
        return X509_V_OK;
428
12
    }
429
222
    OPENSSL_free(utf8_value);
430
222
    return X509_V_OK;
431
234
}
432
433
/*
434
 * Check CN against DNS-ID name constraints.
435
 */
436
int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
437
302
{
438
302
    int r, i;
439
302
    const X509_NAME *nm = X509_get_subject_name(x);
440
302
    ASN1_STRING stmp;
441
302
    GENERAL_NAME gntmp;
442
443
302
    stmp.flags = 0;
444
302
    stmp.type = V_ASN1_IA5STRING;
445
302
    gntmp.type = GEN_DNS;
446
302
    gntmp.d.dNSName = &stmp;
447
448
    /* Process any commonName attributes in subject name */
449
450
536
    for (i = -1;;) {
451
536
        X509_NAME_ENTRY *ne;
452
536
        ASN1_STRING *cn;
453
536
        unsigned char *idval;
454
536
        size_t idlen;
455
456
536
        i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
457
536
        if (i == -1)
458
283
            break;
459
253
        ne = X509_NAME_get_entry(nm, i);
460
253
        cn = X509_NAME_ENTRY_get_data(ne);
461
462
        /* Only process attributes that look like hostnames */
463
253
        if ((r = cn2dnsid(cn, &idval, &idlen)) != X509_V_OK)
464
19
            return r;
465
234
        if (idlen == 0)
466
222
            continue;
467
468
12
        stmp.length = idlen;
469
12
        stmp.data = idval;
470
12
        r = nc_match(&gntmp, nc);
471
12
        OPENSSL_free(idval);
472
12
        if (r != X509_V_OK)
473
0
            return r;
474
12
    }
475
283
    return X509_V_OK;
476
302
}
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
318
{
502
318
    GENERAL_SUBTREE *sub;
503
318
    int i, r, match = 0;
504
318
    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
318
    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
318
    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
318
    if (match == 1)
540
0
        return X509_V_ERR_PERMITTED_VIOLATION;
541
542
    /* Excluded subtrees: must not match any of these */
543
544
318
    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
318
    return X509_V_OK;
560
318
}
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
0
    if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
616
0
        return X509_V_ERR_PERMITTED_VIOLATION;
617
0
    return X509_V_OK;
618
0
}
619
620
static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
621
0
{
622
0
    char *baseptr = (char *)base->data;
623
0
    char *dnsptr = (char *)dns->data;
624
625
    /* Empty matches everything */
626
0
    if (base->length == 0)
627
0
        return X509_V_OK;
628
629
0
    if (dns->length < base->length)
630
0
        return X509_V_ERR_PERMITTED_VIOLATION;
631
632
    /*
633
     * Otherwise can add zero or more components on the left so compare RHS
634
     * and if dns is longer and expect '.' as preceding character.
635
     */
636
0
    if (dns->length > base->length) {
637
0
        dnsptr += dns->length - base->length;
638
0
        if (*baseptr != '.' && dnsptr[-1] != '.')
639
0
            return X509_V_ERR_PERMITTED_VIOLATION;
640
0
    }
641
642
0
    if (ia5ncasecmp(baseptr, dnsptr, base->length))
643
0
        return X509_V_ERR_PERMITTED_VIOLATION;
644
645
0
    return X509_V_OK;
646
0
}
647
648
/*
649
 * This function implements comparison between ASCII/U-label in emltype
650
 * and A-label in base according to RFC 8398, section 6.
651
 * Convert base to U-label and ASCII-parts of domain names, for base
652
 * Octet-to-octet comparison of `emltype` and `base` hostname parts
653
 * (ASCII-parts should be compared in case-insensitive manner)
654
 */
655
static int nc_email_eai(ASN1_TYPE *emltype, ASN1_IA5STRING *base)
656
0
{
657
0
    ASN1_UTF8STRING *eml;
658
0
    char *baseptr = NULL;
659
0
    const char *emlptr;
660
0
    const char *emlat;
661
0
    char ulabel[256];
662
0
    size_t size = sizeof(ulabel);
663
0
    int ret = X509_V_OK;
664
0
    size_t emlhostlen;
665
666
    /* We do not accept embedded NUL characters */
667
0
    if (base->length > 0 && memchr(base->data, 0, base->length) != NULL)
668
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
669
670
    /* 'base' may not be NUL terminated. Create a copy that is */
671
0
    baseptr = OPENSSL_strndup((char *)base->data, base->length);
672
0
    if (baseptr == NULL)
673
0
        return X509_V_ERR_OUT_OF_MEM;
674
675
0
    if (emltype->type != V_ASN1_UTF8STRING) {
676
0
        ret = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
677
0
        goto end;
678
0
    }
679
680
0
    eml = emltype->value.utf8string;
681
0
    emlptr = (char *)eml->data;
682
0
    emlat = ia5memrchr(eml, '@');
683
684
0
    if (emlat == NULL) {
685
0
        ret = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
686
0
        goto end;
687
0
    }
688
689
    /* Special case: initial '.' is RHS match */
690
0
    if (*baseptr == '.') {
691
0
        ulabel[0] = '.';
692
0
        if (ossl_a2ulabel(baseptr, ulabel + 1, size - 1) <= 0) {
693
0
            ret = X509_V_ERR_UNSPECIFIED;
694
0
            goto end;
695
0
        }
696
697
0
        if ((size_t)eml->length > strlen(ulabel)) {
698
0
            emlptr += eml->length - strlen(ulabel);
699
            /* X509_V_OK */
700
0
            if (ia5ncasecmp(ulabel, emlptr, strlen(ulabel)) == 0)
701
0
                goto end;
702
0
        }
703
0
        ret = X509_V_ERR_PERMITTED_VIOLATION;
704
0
        goto end;
705
0
    }
706
707
0
    if (ossl_a2ulabel(baseptr, ulabel, size) <= 0) {
708
0
        ret = X509_V_ERR_UNSPECIFIED;
709
0
        goto end;
710
0
    }
711
    /* Just have hostname left to match: case insensitive */
712
0
    emlptr = emlat + 1;
713
0
    emlhostlen = IA5_OFFSET_LEN(eml, emlptr);
714
0
    if (emlhostlen != strlen(ulabel)
715
0
        || ia5ncasecmp(ulabel, emlptr, emlhostlen) != 0) {
716
0
        ret = X509_V_ERR_PERMITTED_VIOLATION;
717
0
        goto end;
718
0
    }
719
720
0
end:
721
0
    OPENSSL_free(baseptr);
722
0
    return ret;
723
0
}
724
725
static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
726
0
{
727
0
    const char *baseptr = (char *)base->data;
728
0
    const char *emlptr = (char *)eml->data;
729
0
    const char *baseat = ia5memrchr(base, '@');
730
0
    const char *emlat = ia5memrchr(eml, '@');
731
0
    size_t basehostlen, emlhostlen;
732
733
0
    if (!emlat)
734
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
735
    /* Special case: initial '.' is RHS match */
736
0
    if (!baseat && base->length > 0 && (*baseptr == '.')) {
737
0
        if (eml->length > base->length) {
738
0
            emlptr += eml->length - base->length;
739
0
            if (ia5ncasecmp(baseptr, emlptr, base->length) == 0)
740
0
                return X509_V_OK;
741
0
        }
742
0
        return X509_V_ERR_PERMITTED_VIOLATION;
743
0
    }
744
745
    /* If we have anything before '@' match local part */
746
747
0
    if (baseat) {
748
0
        if (baseat != baseptr) {
749
0
            if ((baseat - baseptr) != (emlat - emlptr))
750
0
                return X509_V_ERR_PERMITTED_VIOLATION;
751
0
            if (memchr(baseptr, 0, baseat - baseptr) || memchr(emlptr, 0, emlat - emlptr))
752
0
                return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
753
            /* Case sensitive match of local part */
754
0
            if (strncmp(baseptr, emlptr, emlat - emlptr))
755
0
                return X509_V_ERR_PERMITTED_VIOLATION;
756
0
        }
757
        /* Position base after '@' */
758
0
        baseptr = baseat + 1;
759
0
    }
760
0
    emlptr = emlat + 1;
761
0
    basehostlen = IA5_OFFSET_LEN(base, baseptr);
762
0
    emlhostlen = IA5_OFFSET_LEN(eml, emlptr);
763
    /* Just have hostname left to match: case insensitive */
764
0
    if (basehostlen != emlhostlen || ia5ncasecmp(baseptr, emlptr, emlhostlen))
765
0
        return X509_V_ERR_PERMITTED_VIOLATION;
766
767
0
    return X509_V_OK;
768
0
}
769
770
static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
771
0
{
772
0
    const char *baseptr = (char *)base->data;
773
0
    const char *hostptr = (char *)uri->data;
774
0
    const char *p = ia5memchr(uri, (char *)uri->data, ':');
775
0
    int hostlen;
776
777
    /* Check for foo:// and skip past it */
778
0
    if (p == NULL
779
0
        || IA5_OFFSET_LEN(uri, p) < 3
780
0
        || p[1] != '/'
781
0
        || p[2] != '/')
782
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
783
0
    hostptr = p + 3;
784
785
    /* Determine length of hostname part of URI */
786
787
    /* Look for a port indicator as end of hostname first */
788
789
0
    p = ia5memchr(uri, hostptr, ':');
790
    /* Otherwise look for trailing slash */
791
0
    if (p == NULL)
792
0
        p = ia5memchr(uri, hostptr, '/');
793
794
0
    if (p == NULL)
795
0
        hostlen = IA5_OFFSET_LEN(uri, hostptr);
796
0
    else
797
0
        hostlen = p - hostptr;
798
799
0
    if (hostlen == 0)
800
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
801
802
    /* Special case: initial '.' is RHS match */
803
0
    if (base->length > 0 && *baseptr == '.') {
804
0
        if (hostlen > base->length) {
805
0
            p = hostptr + hostlen - base->length;
806
0
            if (ia5ncasecmp(p, baseptr, base->length) == 0)
807
0
                return X509_V_OK;
808
0
        }
809
0
        return X509_V_ERR_PERMITTED_VIOLATION;
810
0
    }
811
812
0
    if ((base->length != (int)hostlen)
813
0
        || ia5ncasecmp(hostptr, baseptr, hostlen))
814
0
        return X509_V_ERR_PERMITTED_VIOLATION;
815
816
0
    return X509_V_OK;
817
0
}
818
819
static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
820
0
{
821
0
    int hostlen, baselen, i;
822
0
    unsigned char *hostptr, *baseptr, *maskptr;
823
0
    hostptr = ip->data;
824
0
    hostlen = ip->length;
825
0
    baseptr = base->data;
826
0
    baselen = base->length;
827
828
    /* Invalid if not IPv4 or IPv6 */
829
0
    if (!((hostlen == 4) || (hostlen == 16)))
830
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
831
0
    if (!((baselen == 8) || (baselen == 32)))
832
0
        return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
833
834
    /* Do not match IPv4 with IPv6 */
835
0
    if (hostlen * 2 != baselen)
836
0
        return X509_V_ERR_PERMITTED_VIOLATION;
837
838
0
    maskptr = base->data + hostlen;
839
840
    /* Considering possible not aligned base ipAddress */
841
    /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
842
0
    for (i = 0; i < hostlen; i++)
843
0
        if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
844
0
            return X509_V_ERR_PERMITTED_VIOLATION;
845
846
0
    return X509_V_OK;
847
0
}