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