Coverage Report

Created: 2025-06-13 06:58

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