Coverage Report

Created: 2025-12-31 06:58

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