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