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