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