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