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