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