/src/openssl/crypto/x509v3/v3_ncons.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2003-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 "internal/asn1_int.h" |
14 | | #include <openssl/asn1t.h> |
15 | | #include <openssl/conf.h> |
16 | | #include <openssl/x509v3.h> |
17 | | |
18 | | #include "internal/x509_int.h" |
19 | | #include "ext_dat.h" |
20 | | |
21 | | static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, |
22 | | X509V3_CTX *ctx, |
23 | | STACK_OF(CONF_VALUE) *nval); |
24 | | static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a, |
25 | | BIO *bp, int ind); |
26 | | static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method, |
27 | | STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp, |
28 | | int ind, const char *name); |
29 | | static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip); |
30 | | |
31 | | static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc); |
32 | | static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen); |
33 | | static int nc_dn(X509_NAME *sub, X509_NAME *nm); |
34 | | static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns); |
35 | | static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml); |
36 | | static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base); |
37 | | static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base); |
38 | | |
39 | | const X509V3_EXT_METHOD v3_name_constraints = { |
40 | | NID_name_constraints, 0, |
41 | | ASN1_ITEM_ref(NAME_CONSTRAINTS), |
42 | | 0, 0, 0, 0, |
43 | | 0, 0, |
44 | | 0, v2i_NAME_CONSTRAINTS, |
45 | | i2r_NAME_CONSTRAINTS, 0, |
46 | | NULL |
47 | | }; |
48 | | |
49 | | ASN1_SEQUENCE(GENERAL_SUBTREE) = { |
50 | | ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME), |
51 | | ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0), |
52 | | ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1) |
53 | | } ASN1_SEQUENCE_END(GENERAL_SUBTREE) |
54 | | |
55 | | ASN1_SEQUENCE(NAME_CONSTRAINTS) = { |
56 | | ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees, |
57 | | GENERAL_SUBTREE, 0), |
58 | | ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees, |
59 | | GENERAL_SUBTREE, 1), |
60 | | } ASN1_SEQUENCE_END(NAME_CONSTRAINTS) |
61 | | |
62 | | |
63 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE) |
64 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS) |
65 | | |
66 | | /* |
67 | | * We cannot use strncasecmp here because that applies locale specific rules. |
68 | | * For example in Turkish 'I' is not the uppercase character for 'i'. We need to |
69 | | * do a simple ASCII case comparison ignoring the locale (that is why we use |
70 | | * numeric constants below). |
71 | | */ |
72 | | static int ia5ncasecmp(const char *s1, const char *s2, size_t n) |
73 | 0 | { |
74 | 0 | for (; n > 0; n--, s1++, s2++) { |
75 | 0 | if (*s1 != *s2) { |
76 | 0 | unsigned char c1 = (unsigned char)*s1, c2 = (unsigned char)*s2; |
77 | 0 |
|
78 | 0 | /* Convert to lower case */ |
79 | 0 | if (c1 >= 0x41 /* A */ && c1 <= 0x5A /* Z */) |
80 | 0 | c1 += 0x20; |
81 | 0 | if (c2 >= 0x41 /* A */ && c2 <= 0x5A /* Z */) |
82 | 0 | c2 += 0x20; |
83 | 0 |
|
84 | 0 | if (c1 == c2) |
85 | 0 | continue; |
86 | 0 | |
87 | 0 | if (c1 < c2) |
88 | 0 | return -1; |
89 | 0 | |
90 | 0 | /* c1 > c2 */ |
91 | 0 | return 1; |
92 | 0 | } else if (*s1 == 0) { |
93 | 0 | /* If we get here we know that *s2 == 0 too */ |
94 | 0 | return 0; |
95 | 0 | } |
96 | 0 | } |
97 | 0 |
|
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | | static int ia5casecmp(const char *s1, const char *s2) |
102 | 0 | { |
103 | 0 | return ia5ncasecmp(s1, s2, SIZE_MAX); |
104 | 0 | } |
105 | | |
106 | | static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, |
107 | | X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) |
108 | | { |
109 | | int i; |
110 | | CONF_VALUE tval, *val; |
111 | | STACK_OF(GENERAL_SUBTREE) **ptree = NULL; |
112 | | NAME_CONSTRAINTS *ncons = NULL; |
113 | | GENERAL_SUBTREE *sub = NULL; |
114 | | |
115 | | ncons = NAME_CONSTRAINTS_new(); |
116 | | if (ncons == NULL) |
117 | | goto memerr; |
118 | | for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
119 | | val = sk_CONF_VALUE_value(nval, i); |
120 | | if (strncmp(val->name, "permitted", 9) == 0 && val->name[9]) { |
121 | | ptree = &ncons->permittedSubtrees; |
122 | | tval.name = val->name + 10; |
123 | | } else if (strncmp(val->name, "excluded", 8) == 0 && val->name[8]) { |
124 | | ptree = &ncons->excludedSubtrees; |
125 | | tval.name = val->name + 9; |
126 | | } else { |
127 | | X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, X509V3_R_INVALID_SYNTAX); |
128 | | goto err; |
129 | | } |
130 | | tval.value = val->value; |
131 | | sub = GENERAL_SUBTREE_new(); |
132 | | if (sub == NULL) |
133 | | goto memerr; |
134 | | if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1)) |
135 | | goto err; |
136 | | if (*ptree == NULL) |
137 | | *ptree = sk_GENERAL_SUBTREE_new_null(); |
138 | | if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub)) |
139 | | goto memerr; |
140 | | sub = NULL; |
141 | | } |
142 | | |
143 | | return ncons; |
144 | | |
145 | | memerr: |
146 | | X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE); |
147 | | err: |
148 | | NAME_CONSTRAINTS_free(ncons); |
149 | | GENERAL_SUBTREE_free(sub); |
150 | | |
151 | | return NULL; |
152 | | } |
153 | | |
154 | | static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a, |
155 | | BIO *bp, int ind) |
156 | 0 | { |
157 | 0 | NAME_CONSTRAINTS *ncons = a; |
158 | 0 | do_i2r_name_constraints(method, ncons->permittedSubtrees, |
159 | 0 | bp, ind, "Permitted"); |
160 | 0 | do_i2r_name_constraints(method, ncons->excludedSubtrees, |
161 | 0 | bp, ind, "Excluded"); |
162 | 0 | return 1; |
163 | 0 | } |
164 | | |
165 | | static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method, |
166 | | STACK_OF(GENERAL_SUBTREE) *trees, |
167 | | BIO *bp, int ind, const char *name) |
168 | 0 | { |
169 | 0 | GENERAL_SUBTREE *tree; |
170 | 0 | int i; |
171 | 0 | if (sk_GENERAL_SUBTREE_num(trees) > 0) |
172 | 0 | BIO_printf(bp, "%*s%s:\n", ind, "", name); |
173 | 0 | for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) { |
174 | 0 | tree = sk_GENERAL_SUBTREE_value(trees, i); |
175 | 0 | BIO_printf(bp, "%*s", ind + 2, ""); |
176 | 0 | if (tree->base->type == GEN_IPADD) |
177 | 0 | print_nc_ipadd(bp, tree->base->d.ip); |
178 | 0 | else |
179 | 0 | GENERAL_NAME_print(bp, tree->base); |
180 | 0 | BIO_puts(bp, "\n"); |
181 | 0 | } |
182 | 0 | return 1; |
183 | 0 | } |
184 | | |
185 | | static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip) |
186 | 0 | { |
187 | 0 | int i, len; |
188 | 0 | unsigned char *p; |
189 | 0 | p = ip->data; |
190 | 0 | len = ip->length; |
191 | 0 | BIO_puts(bp, "IP:"); |
192 | 0 | if (len == 8) { |
193 | 0 | BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d", |
194 | 0 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); |
195 | 0 | } else if (len == 32) { |
196 | 0 | for (i = 0; i < 16; i++) { |
197 | 0 | BIO_printf(bp, "%X", p[0] << 8 | p[1]); |
198 | 0 | p += 2; |
199 | 0 | if (i == 7) |
200 | 0 | BIO_puts(bp, "/"); |
201 | 0 | else if (i != 15) |
202 | 0 | BIO_puts(bp, ":"); |
203 | 0 | } |
204 | 0 | } else |
205 | 0 | BIO_printf(bp, "IP Address:<invalid>"); |
206 | 0 | return 1; |
207 | 0 | } |
208 | | |
209 | 0 | #define NAME_CHECK_MAX (1 << 20) |
210 | | |
211 | | static int add_lengths(int *out, int a, int b) |
212 | 0 | { |
213 | 0 | /* sk_FOO_num(NULL) returns -1 but is effectively 0 when iterating. */ |
214 | 0 | if (a < 0) |
215 | 0 | a = 0; |
216 | 0 | if (b < 0) |
217 | 0 | b = 0; |
218 | 0 |
|
219 | 0 | if (a > INT_MAX - b) |
220 | 0 | return 0; |
221 | 0 | *out = a + b; |
222 | 0 | return 1; |
223 | 0 | } |
224 | | |
225 | | /*- |
226 | | * Check a certificate conforms to a specified set of constraints. |
227 | | * Return values: |
228 | | * X509_V_OK: All constraints obeyed. |
229 | | * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation. |
230 | | * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation. |
231 | | * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type. |
232 | | * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type. |
233 | | * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax. |
234 | | * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name |
235 | | */ |
236 | | |
237 | | int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc) |
238 | 0 | { |
239 | 0 | int r, i, name_count, constraint_count; |
240 | 0 | X509_NAME *nm; |
241 | 0 |
|
242 | 0 | nm = X509_get_subject_name(x); |
243 | 0 |
|
244 | 0 | /* |
245 | 0 | * Guard against certificates with an excessive number of names or |
246 | 0 | * constraints causing a computationally expensive name constraints check. |
247 | 0 | */ |
248 | 0 | if (!add_lengths(&name_count, X509_NAME_entry_count(nm), |
249 | 0 | sk_GENERAL_NAME_num(x->altname)) |
250 | 0 | || !add_lengths(&constraint_count, |
251 | 0 | sk_GENERAL_SUBTREE_num(nc->permittedSubtrees), |
252 | 0 | sk_GENERAL_SUBTREE_num(nc->excludedSubtrees)) |
253 | 0 | || (name_count > 0 && constraint_count > NAME_CHECK_MAX / name_count)) |
254 | 0 | return X509_V_ERR_UNSPECIFIED; |
255 | 0 | |
256 | 0 | if (X509_NAME_entry_count(nm) > 0) { |
257 | 0 | GENERAL_NAME gntmp; |
258 | 0 | gntmp.type = GEN_DIRNAME; |
259 | 0 | gntmp.d.directoryName = nm; |
260 | 0 |
|
261 | 0 | r = nc_match(&gntmp, nc); |
262 | 0 |
|
263 | 0 | if (r != X509_V_OK) |
264 | 0 | return r; |
265 | 0 | |
266 | 0 | gntmp.type = GEN_EMAIL; |
267 | 0 |
|
268 | 0 | /* Process any email address attributes in subject name */ |
269 | 0 |
|
270 | 0 | for (i = -1;;) { |
271 | 0 | const X509_NAME_ENTRY *ne; |
272 | 0 |
|
273 | 0 | i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i); |
274 | 0 | if (i == -1) |
275 | 0 | break; |
276 | 0 | ne = X509_NAME_get_entry(nm, i); |
277 | 0 | gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne); |
278 | 0 | if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING) |
279 | 0 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
280 | 0 | |
281 | 0 | r = nc_match(&gntmp, nc); |
282 | 0 |
|
283 | 0 | if (r != X509_V_OK) |
284 | 0 | return r; |
285 | 0 | } |
286 | 0 |
|
287 | 0 | } |
288 | 0 |
|
289 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) { |
290 | 0 | GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i); |
291 | 0 | r = nc_match(gen, nc); |
292 | 0 | if (r != X509_V_OK) |
293 | 0 | return r; |
294 | 0 | } |
295 | 0 |
|
296 | 0 | return X509_V_OK; |
297 | 0 |
|
298 | 0 | } |
299 | | |
300 | | static int cn2dnsid(ASN1_STRING *cn, unsigned char **dnsid, size_t *idlen) |
301 | 0 | { |
302 | 0 | int utf8_length; |
303 | 0 | unsigned char *utf8_value; |
304 | 0 | int i; |
305 | 0 | int isdnsname = 0; |
306 | 0 |
|
307 | 0 | /* Don't leave outputs uninitialized */ |
308 | 0 | *dnsid = NULL; |
309 | 0 | *idlen = 0; |
310 | 0 |
|
311 | 0 | /*- |
312 | 0 | * Per RFC 6125, DNS-IDs representing internationalized domain names appear |
313 | 0 | * in certificates in A-label encoded form: |
314 | 0 | * |
315 | 0 | * https://tools.ietf.org/html/rfc6125#section-6.4.2 |
316 | 0 | * |
317 | 0 | * The same applies to CNs which are intended to represent DNS names. |
318 | 0 | * However, while in the SAN DNS-IDs are IA5Strings, as CNs they may be |
319 | 0 | * needlessly encoded in 16-bit Unicode. We perform a conversion to UTF-8 |
320 | 0 | * to ensure that we get an ASCII representation of any CNs that are |
321 | 0 | * representable as ASCII, but just not encoded as ASCII. The UTF-8 form |
322 | 0 | * may contain some non-ASCII octets, and that's fine, such CNs are not |
323 | 0 | * valid legacy DNS names. |
324 | 0 | * |
325 | 0 | * Note, 'int' is the return type of ASN1_STRING_to_UTF8() so that's what |
326 | 0 | * we must use for 'utf8_length'. |
327 | 0 | */ |
328 | 0 | if ((utf8_length = ASN1_STRING_to_UTF8(&utf8_value, cn)) < 0) |
329 | 0 | return X509_V_ERR_OUT_OF_MEM; |
330 | 0 | |
331 | 0 | /* |
332 | 0 | * Some certificates have had names that include a *trailing* NUL byte. |
333 | 0 | * Remove these harmless NUL characters. They would otherwise yield false |
334 | 0 | * alarms with the following embedded NUL check. |
335 | 0 | */ |
336 | 0 | while (utf8_length > 0 && utf8_value[utf8_length - 1] == '\0') |
337 | 0 | --utf8_length; |
338 | 0 |
|
339 | 0 | /* Reject *embedded* NULs */ |
340 | 0 | if ((size_t)utf8_length != strlen((char *)utf8_value)) { |
341 | 0 | OPENSSL_free(utf8_value); |
342 | 0 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
343 | 0 | } |
344 | 0 |
|
345 | 0 | /* |
346 | 0 | * XXX: Deviation from strict DNS name syntax, also check names with '_' |
347 | 0 | * Check DNS name syntax, any '-' or '.' must be internal, |
348 | 0 | * and on either side of each '.' we can't have a '-' or '.'. |
349 | 0 | * |
350 | 0 | * If the name has just one label, we don't consider it a DNS name. This |
351 | 0 | * means that "CN=sometld" cannot be precluded by DNS name constraints, but |
352 | 0 | * that is not a problem. |
353 | 0 | */ |
354 | 0 | for (i = 0; i < utf8_length; ++i) { |
355 | 0 | unsigned char c = utf8_value[i]; |
356 | 0 |
|
357 | 0 | if ((c >= 'a' && c <= 'z') |
358 | 0 | || (c >= 'A' && c <= 'Z') |
359 | 0 | || (c >= '0' && c <= '9') |
360 | 0 | || c == '_') |
361 | 0 | continue; |
362 | 0 | |
363 | 0 | /* Dot and hyphen cannot be first or last. */ |
364 | 0 | if (i > 0 && i < utf8_length - 1) { |
365 | 0 | if (c == '-') |
366 | 0 | continue; |
367 | 0 | /* |
368 | 0 | * Next to a dot the preceding and following characters must not be |
369 | 0 | * another dot or a hyphen. Otherwise, record that the name is |
370 | 0 | * plausible, since it has two or more labels. |
371 | 0 | */ |
372 | 0 | if (c == '.' |
373 | 0 | && utf8_value[i + 1] != '.' |
374 | 0 | && utf8_value[i - 1] != '-' |
375 | 0 | && utf8_value[i + 1] != '-') { |
376 | 0 | isdnsname = 1; |
377 | 0 | continue; |
378 | 0 | } |
379 | 0 | } |
380 | 0 | isdnsname = 0; |
381 | 0 | break; |
382 | 0 | } |
383 | 0 |
|
384 | 0 | if (isdnsname) { |
385 | 0 | *dnsid = utf8_value; |
386 | 0 | *idlen = (size_t)utf8_length; |
387 | 0 | return X509_V_OK; |
388 | 0 | } |
389 | 0 | OPENSSL_free(utf8_value); |
390 | 0 | return X509_V_OK; |
391 | 0 | } |
392 | | |
393 | | /* |
394 | | * Check CN against DNS-ID name constraints. |
395 | | */ |
396 | | int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc) |
397 | 0 | { |
398 | 0 | int r, i; |
399 | 0 | X509_NAME *nm = X509_get_subject_name(x); |
400 | 0 | ASN1_STRING stmp; |
401 | 0 | GENERAL_NAME gntmp; |
402 | 0 |
|
403 | 0 | stmp.flags = 0; |
404 | 0 | stmp.type = V_ASN1_IA5STRING; |
405 | 0 | gntmp.type = GEN_DNS; |
406 | 0 | gntmp.d.dNSName = &stmp; |
407 | 0 |
|
408 | 0 | /* Process any commonName attributes in subject name */ |
409 | 0 |
|
410 | 0 | for (i = -1;;) { |
411 | 0 | X509_NAME_ENTRY *ne; |
412 | 0 | ASN1_STRING *cn; |
413 | 0 | unsigned char *idval; |
414 | 0 | size_t idlen; |
415 | 0 |
|
416 | 0 | i = X509_NAME_get_index_by_NID(nm, NID_commonName, i); |
417 | 0 | if (i == -1) |
418 | 0 | break; |
419 | 0 | ne = X509_NAME_get_entry(nm, i); |
420 | 0 | cn = X509_NAME_ENTRY_get_data(ne); |
421 | 0 |
|
422 | 0 | /* Only process attributes that look like host names */ |
423 | 0 | if ((r = cn2dnsid(cn, &idval, &idlen)) != X509_V_OK) |
424 | 0 | return r; |
425 | 0 | if (idlen == 0) |
426 | 0 | continue; |
427 | 0 | |
428 | 0 | stmp.length = idlen; |
429 | 0 | stmp.data = idval; |
430 | 0 | r = nc_match(&gntmp, nc); |
431 | 0 | OPENSSL_free(idval); |
432 | 0 | if (r != X509_V_OK) |
433 | 0 | return r; |
434 | 0 | } |
435 | 0 | return X509_V_OK; |
436 | 0 | } |
437 | | |
438 | | static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc) |
439 | 0 | { |
440 | 0 | GENERAL_SUBTREE *sub; |
441 | 0 | int i, r, match = 0; |
442 | 0 |
|
443 | 0 | /* |
444 | 0 | * Permitted subtrees: if any subtrees exist of matching the type at |
445 | 0 | * least one subtree must match. |
446 | 0 | */ |
447 | 0 |
|
448 | 0 | for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) { |
449 | 0 | sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i); |
450 | 0 | if (gen->type != sub->base->type) |
451 | 0 | continue; |
452 | 0 | if (sub->minimum || sub->maximum) |
453 | 0 | return X509_V_ERR_SUBTREE_MINMAX; |
454 | 0 | /* If we already have a match don't bother trying any more */ |
455 | 0 | if (match == 2) |
456 | 0 | continue; |
457 | 0 | if (match == 0) |
458 | 0 | match = 1; |
459 | 0 | r = nc_match_single(gen, sub->base); |
460 | 0 | if (r == X509_V_OK) |
461 | 0 | match = 2; |
462 | 0 | else if (r != X509_V_ERR_PERMITTED_VIOLATION) |
463 | 0 | return r; |
464 | 0 | } |
465 | 0 |
|
466 | 0 | if (match == 1) |
467 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
468 | 0 | |
469 | 0 | /* Excluded subtrees: must not match any of these */ |
470 | 0 | |
471 | 0 | for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) { |
472 | 0 | sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i); |
473 | 0 | if (gen->type != sub->base->type) |
474 | 0 | continue; |
475 | 0 | if (sub->minimum || sub->maximum) |
476 | 0 | return X509_V_ERR_SUBTREE_MINMAX; |
477 | 0 | |
478 | 0 | r = nc_match_single(gen, sub->base); |
479 | 0 | if (r == X509_V_OK) |
480 | 0 | return X509_V_ERR_EXCLUDED_VIOLATION; |
481 | 0 | else if (r != X509_V_ERR_PERMITTED_VIOLATION) |
482 | 0 | return r; |
483 | 0 |
|
484 | 0 | } |
485 | 0 |
|
486 | 0 | return X509_V_OK; |
487 | 0 |
|
488 | 0 | } |
489 | | |
490 | | static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base) |
491 | 0 | { |
492 | 0 | switch (base->type) { |
493 | 0 | case GEN_DIRNAME: |
494 | 0 | return nc_dn(gen->d.directoryName, base->d.directoryName); |
495 | 0 |
|
496 | 0 | case GEN_DNS: |
497 | 0 | return nc_dns(gen->d.dNSName, base->d.dNSName); |
498 | 0 |
|
499 | 0 | case GEN_EMAIL: |
500 | 0 | return nc_email(gen->d.rfc822Name, base->d.rfc822Name); |
501 | 0 |
|
502 | 0 | case GEN_URI: |
503 | 0 | return nc_uri(gen->d.uniformResourceIdentifier, |
504 | 0 | base->d.uniformResourceIdentifier); |
505 | 0 |
|
506 | 0 | case GEN_IPADD: |
507 | 0 | return nc_ip(gen->d.iPAddress, base->d.iPAddress); |
508 | 0 |
|
509 | 0 | default: |
510 | 0 | return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE; |
511 | 0 | } |
512 | 0 |
|
513 | 0 | } |
514 | | |
515 | | /* |
516 | | * directoryName name constraint matching. The canonical encoding of |
517 | | * X509_NAME makes this comparison easy. It is matched if the subtree is a |
518 | | * subset of the name. |
519 | | */ |
520 | | |
521 | | static int nc_dn(X509_NAME *nm, X509_NAME *base) |
522 | 0 | { |
523 | 0 | /* Ensure canonical encodings are up to date. */ |
524 | 0 | if (nm->modified && i2d_X509_NAME(nm, NULL) < 0) |
525 | 0 | return X509_V_ERR_OUT_OF_MEM; |
526 | 0 | if (base->modified && i2d_X509_NAME(base, NULL) < 0) |
527 | 0 | return X509_V_ERR_OUT_OF_MEM; |
528 | 0 | if (base->canon_enclen > nm->canon_enclen) |
529 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
530 | 0 | if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen)) |
531 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
532 | 0 | return X509_V_OK; |
533 | 0 | } |
534 | | |
535 | | static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base) |
536 | 0 | { |
537 | 0 | char *baseptr = (char *)base->data; |
538 | 0 | char *dnsptr = (char *)dns->data; |
539 | 0 | /* Empty matches everything */ |
540 | 0 | if (!*baseptr) |
541 | 0 | return X509_V_OK; |
542 | 0 | /* |
543 | 0 | * Otherwise can add zero or more components on the left so compare RHS |
544 | 0 | * and if dns is longer and expect '.' as preceding character. |
545 | 0 | */ |
546 | 0 | if (dns->length > base->length) { |
547 | 0 | dnsptr += dns->length - base->length; |
548 | 0 | if (*baseptr != '.' && dnsptr[-1] != '.') |
549 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
550 | 0 | } |
551 | 0 | |
552 | 0 | if (ia5casecmp(baseptr, dnsptr)) |
553 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
554 | 0 | |
555 | 0 | return X509_V_OK; |
556 | 0 |
|
557 | 0 | } |
558 | | |
559 | | static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base) |
560 | 0 | { |
561 | 0 | const char *baseptr = (char *)base->data; |
562 | 0 | const char *emlptr = (char *)eml->data; |
563 | 0 |
|
564 | 0 | const char *baseat = strchr(baseptr, '@'); |
565 | 0 | const char *emlat = strchr(emlptr, '@'); |
566 | 0 | if (!emlat) |
567 | 0 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
568 | 0 | /* Special case: initial '.' is RHS match */ |
569 | 0 | if (!baseat && (*baseptr == '.')) { |
570 | 0 | if (eml->length > base->length) { |
571 | 0 | emlptr += eml->length - base->length; |
572 | 0 | if (ia5casecmp(baseptr, emlptr) == 0) |
573 | 0 | return X509_V_OK; |
574 | 0 | } |
575 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
576 | 0 | } |
577 | 0 |
|
578 | 0 | /* If we have anything before '@' match local part */ |
579 | 0 |
|
580 | 0 | if (baseat) { |
581 | 0 | if (baseat != baseptr) { |
582 | 0 | if ((baseat - baseptr) != (emlat - emlptr)) |
583 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
584 | 0 | /* Case sensitive match of local part */ |
585 | 0 | if (strncmp(baseptr, emlptr, emlat - emlptr)) |
586 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
587 | 0 | } |
588 | 0 | /* Position base after '@' */ |
589 | 0 | baseptr = baseat + 1; |
590 | 0 | } |
591 | 0 | emlptr = emlat + 1; |
592 | 0 | /* Just have hostname left to match: case insensitive */ |
593 | 0 | if (ia5casecmp(baseptr, emlptr)) |
594 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
595 | 0 | |
596 | 0 | return X509_V_OK; |
597 | 0 |
|
598 | 0 | } |
599 | | |
600 | | static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base) |
601 | 0 | { |
602 | 0 | const char *baseptr = (char *)base->data; |
603 | 0 | const char *hostptr = (char *)uri->data; |
604 | 0 | const char *p = strchr(hostptr, ':'); |
605 | 0 | int hostlen; |
606 | 0 | /* Check for foo:// and skip past it */ |
607 | 0 | if (!p || (p[1] != '/') || (p[2] != '/')) |
608 | 0 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
609 | 0 | hostptr = p + 3; |
610 | 0 |
|
611 | 0 | /* Determine length of hostname part of URI */ |
612 | 0 |
|
613 | 0 | /* Look for a port indicator as end of hostname first */ |
614 | 0 |
|
615 | 0 | p = strchr(hostptr, ':'); |
616 | 0 | /* Otherwise look for trailing slash */ |
617 | 0 | if (!p) |
618 | 0 | p = strchr(hostptr, '/'); |
619 | 0 |
|
620 | 0 | if (!p) |
621 | 0 | hostlen = strlen(hostptr); |
622 | 0 | else |
623 | 0 | hostlen = p - hostptr; |
624 | 0 |
|
625 | 0 | if (hostlen == 0) |
626 | 0 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
627 | 0 | |
628 | 0 | /* Special case: initial '.' is RHS match */ |
629 | 0 | if (*baseptr == '.') { |
630 | 0 | if (hostlen > base->length) { |
631 | 0 | p = hostptr + hostlen - base->length; |
632 | 0 | if (ia5ncasecmp(p, baseptr, base->length) == 0) |
633 | 0 | return X509_V_OK; |
634 | 0 | } |
635 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
636 | 0 | } |
637 | 0 |
|
638 | 0 | if ((base->length != (int)hostlen) |
639 | 0 | || ia5ncasecmp(hostptr, baseptr, hostlen)) |
640 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
641 | 0 | |
642 | 0 | return X509_V_OK; |
643 | 0 |
|
644 | 0 | } |
645 | | |
646 | | static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base) |
647 | 0 | { |
648 | 0 | int hostlen, baselen, i; |
649 | 0 | unsigned char *hostptr, *baseptr, *maskptr; |
650 | 0 | hostptr = ip->data; |
651 | 0 | hostlen = ip->length; |
652 | 0 | baseptr = base->data; |
653 | 0 | baselen = base->length; |
654 | 0 |
|
655 | 0 | /* Invalid if not IPv4 or IPv6 */ |
656 | 0 | if (!((hostlen == 4) || (hostlen == 16))) |
657 | 0 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
658 | 0 | if (!((baselen == 8) || (baselen == 32))) |
659 | 0 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
660 | 0 | |
661 | 0 | /* Do not match IPv4 with IPv6 */ |
662 | 0 | if (hostlen * 2 != baselen) |
663 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
664 | 0 | |
665 | 0 | maskptr = base->data + hostlen; |
666 | 0 |
|
667 | 0 | /* Considering possible not aligned base ipAddress */ |
668 | 0 | /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */ |
669 | 0 | for (i = 0; i < hostlen; i++) |
670 | 0 | if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i])) |
671 | 0 | return X509_V_ERR_PERMITTED_VIOLATION; |
672 | 0 |
|
673 | 0 | return X509_V_OK; |
674 | 0 |
|
675 | 0 | } |