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