Coverage Report

Created: 2026-03-09 06:55

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