2 * Copyright 2003-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/cryptlib.h"
11 #include "internal/numbers.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>
19 #include "crypto/x509.h"
20 #include "crypto/punycode.h"
23 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
25 STACK_OF(CONF_VALUE) *nval);
26 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
28 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
29 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
30 int ind, const char *name);
31 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
33 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
34 static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
35 static int nc_dn(const X509_NAME *sub, const X509_NAME *nm);
36 static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
37 static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
38 static int nc_email_eai(ASN1_TYPE *emltype, ASN1_IA5STRING *base);
39 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
40 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base);
42 const X509V3_EXT_METHOD ossl_v3_name_constraints = {
43 NID_name_constraints, 0,
44 ASN1_ITEM_ref(NAME_CONSTRAINTS),
47 0, v2i_NAME_CONSTRAINTS,
48 i2r_NAME_CONSTRAINTS, 0,
52 ASN1_SEQUENCE(GENERAL_SUBTREE) = {
53 ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
54 ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
55 ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
56 } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
58 ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
59 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
61 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
63 } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
66 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
67 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
70 #define IA5_OFFSET_LEN(ia5base, offset) \
71 ((ia5base)->length - ((unsigned char *)(offset) - (ia5base)->data))
73 /* Like memchr but for ASN1_IA5STRING. Additionally you can specify the
74 * starting point to search from
76 # define ia5memchr(str, start, c) memchr(start, c, IA5_OFFSET_LEN(str, start))
78 /* Like memrrchr but for ASN1_IA5STRING */
79 static char *ia5memrchr(ASN1_IA5STRING *str, int c)
83 for (i = str->length; i > 0 && str->data[i - 1] != c; i--);
88 return (char *)&str->data[i - 1];
92 * We cannot use strncasecmp here because that applies locale specific rules. It
93 * also doesn't work with ASN1_STRINGs that may have embedded NUL characters.
94 * For example in Turkish 'I' is not the uppercase character for 'i'. We need to
95 * do a simple ASCII case comparison ignoring the locale (that is why we use
96 * numeric constants below).
98 static int ia5ncasecmp(const char *s1, const char *s2, size_t n)
100 for (; n > 0; n--, s1++, s2++) {
102 unsigned char c1 = (unsigned char)*s1, c2 = (unsigned char)*s2;
104 /* Convert to lower case */
105 if (c1 >= 0x41 /* A */ && c1 <= 0x5A /* Z */)
107 if (c2 >= 0x41 /* A */ && c2 <= 0x5A /* Z */)
124 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
125 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
128 CONF_VALUE tval, *val;
129 STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
130 NAME_CONSTRAINTS *ncons = NULL;
131 GENERAL_SUBTREE *sub = NULL;
133 ncons = NAME_CONSTRAINTS_new();
136 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
137 val = sk_CONF_VALUE_value(nval, i);
138 if (strncmp(val->name, "permitted", 9) == 0 && val->name[9]) {
139 ptree = &ncons->permittedSubtrees;
140 tval.name = val->name + 10;
141 } else if (strncmp(val->name, "excluded", 8) == 0 && val->name[8]) {
142 ptree = &ncons->excludedSubtrees;
143 tval.name = val->name + 9;
145 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SYNTAX);
148 tval.value = val->value;
149 sub = GENERAL_SUBTREE_new();
152 if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
155 *ptree = sk_GENERAL_SUBTREE_new_null();
156 if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub))
164 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
166 NAME_CONSTRAINTS_free(ncons);
167 GENERAL_SUBTREE_free(sub);
172 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
175 NAME_CONSTRAINTS *ncons = a;
176 do_i2r_name_constraints(method, ncons->permittedSubtrees,
177 bp, ind, "Permitted");
178 if (ncons->permittedSubtrees && ncons->excludedSubtrees)
180 do_i2r_name_constraints(method, ncons->excludedSubtrees,
181 bp, ind, "Excluded");
185 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
186 STACK_OF(GENERAL_SUBTREE) *trees,
187 BIO *bp, int ind, const char *name)
189 GENERAL_SUBTREE *tree;
191 if (sk_GENERAL_SUBTREE_num(trees) > 0)
192 BIO_printf(bp, "%*s%s:\n", ind, "", name);
193 for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
196 tree = sk_GENERAL_SUBTREE_value(trees, i);
197 BIO_printf(bp, "%*s", ind + 2, "");
198 if (tree->base->type == GEN_IPADD)
199 print_nc_ipadd(bp, tree->base->d.ip);
201 GENERAL_NAME_print(bp, tree->base);
206 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
208 /* ip->length should be 8 or 32 and len1 == len2 == 4 or len1 == len2 == 16 */
209 int len1 = ip->length >= 16 ? 16 : ip->length >= 4 ? 4 : ip->length;
210 int len2 = ip->length - len1;
211 char *ip1 = ossl_ipaddr_to_asc(ip->data, len1);
212 char *ip2 = ossl_ipaddr_to_asc(ip->data + len1, len2);
213 int ret = ip1 != NULL && ip2 != NULL
214 && BIO_printf(bp, "IP:%s/%s", ip1, ip2) > 0;
221 #define NAME_CHECK_MAX (1 << 20)
223 static int add_lengths(int *out, int a, int b)
225 /* sk_FOO_num(NULL) returns -1 but is effectively 0 when iterating. */
238 * Check a certificate conforms to a specified set of constraints.
240 * X509_V_OK: All constraints obeyed.
241 * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
242 * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
243 * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
244 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
245 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
246 * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
249 int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
251 int r, i, name_count, constraint_count;
254 nm = X509_get_subject_name(x);
257 * Guard against certificates with an excessive number of names or
258 * constraints causing a computationally expensive name constraints check.
260 if (!add_lengths(&name_count, X509_NAME_entry_count(nm),
261 sk_GENERAL_NAME_num(x->altname))
262 || !add_lengths(&constraint_count,
263 sk_GENERAL_SUBTREE_num(nc->permittedSubtrees),
264 sk_GENERAL_SUBTREE_num(nc->excludedSubtrees))
265 || (name_count > 0 && constraint_count > NAME_CHECK_MAX / name_count))
266 return X509_V_ERR_UNSPECIFIED;
268 if (X509_NAME_entry_count(nm) > 0) {
270 gntmp.type = GEN_DIRNAME;
271 gntmp.d.directoryName = nm;
273 r = nc_match(&gntmp, nc);
278 gntmp.type = GEN_EMAIL;
280 /* Process any email address attributes in subject name */
283 const X509_NAME_ENTRY *ne;
285 i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
288 ne = X509_NAME_get_entry(nm, i);
289 gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
290 if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
291 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
293 r = nc_match(&gntmp, nc);
301 for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
302 GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
303 r = nc_match(gen, nc);
312 static int cn2dnsid(ASN1_STRING *cn, unsigned char **dnsid, size_t *idlen)
315 unsigned char *utf8_value;
319 /* Don't leave outputs uninitialized */
324 * Per RFC 6125, DNS-IDs representing internationalized domain names appear
325 * in certificates in A-label encoded form:
327 * https://tools.ietf.org/html/rfc6125#section-6.4.2
329 * The same applies to CNs which are intended to represent DNS names.
330 * However, while in the SAN DNS-IDs are IA5Strings, as CNs they may be
331 * needlessly encoded in 16-bit Unicode. We perform a conversion to UTF-8
332 * to ensure that we get an ASCII representation of any CNs that are
333 * representable as ASCII, but just not encoded as ASCII. The UTF-8 form
334 * may contain some non-ASCII octets, and that's fine, such CNs are not
335 * valid legacy DNS names.
337 * Note, 'int' is the return type of ASN1_STRING_to_UTF8() so that's what
338 * we must use for 'utf8_length'.
340 if ((utf8_length = ASN1_STRING_to_UTF8(&utf8_value, cn)) < 0)
341 return X509_V_ERR_OUT_OF_MEM;
344 * Some certificates have had names that include a *trailing* NUL byte.
345 * Remove these harmless NUL characters. They would otherwise yield false
346 * alarms with the following embedded NUL check.
348 while (utf8_length > 0 && utf8_value[utf8_length - 1] == '\0')
351 /* Reject *embedded* NULs */
352 if (memchr(utf8_value, 0, utf8_length) != NULL) {
353 OPENSSL_free(utf8_value);
354 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
358 * XXX: Deviation from strict DNS name syntax, also check names with '_'
359 * Check DNS name syntax, any '-' or '.' must be internal,
360 * and on either side of each '.' we can't have a '-' or '.'.
362 * If the name has just one label, we don't consider it a DNS name. This
363 * means that "CN=sometld" cannot be precluded by DNS name constraints, but
364 * that is not a problem.
366 for (i = 0; i < utf8_length; ++i) {
367 unsigned char c = utf8_value[i];
369 if ((c >= 'a' && c <= 'z')
370 || (c >= 'A' && c <= 'Z')
371 || (c >= '0' && c <= '9')
375 /* Dot and hyphen cannot be first or last. */
376 if (i > 0 && i < utf8_length - 1) {
380 * Next to a dot the preceding and following characters must not be
381 * another dot or a hyphen. Otherwise, record that the name is
382 * plausible, since it has two or more labels.
385 && utf8_value[i + 1] != '.'
386 && utf8_value[i - 1] != '-'
387 && utf8_value[i + 1] != '-') {
398 *idlen = (size_t)utf8_length;
401 OPENSSL_free(utf8_value);
406 * Check CN against DNS-ID name constraints.
408 int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
411 const X509_NAME *nm = X509_get_subject_name(x);
416 stmp.type = V_ASN1_IA5STRING;
417 gntmp.type = GEN_DNS;
418 gntmp.d.dNSName = &stmp;
420 /* Process any commonName attributes in subject name */
425 unsigned char *idval;
428 i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
431 ne = X509_NAME_get_entry(nm, i);
432 cn = X509_NAME_ENTRY_get_data(ne);
434 /* Only process attributes that look like host names */
435 if ((r = cn2dnsid(cn, &idval, &idlen)) != X509_V_OK)
442 r = nc_match(&gntmp, nc);
451 * Return nonzero if the GeneralSubtree has valid 'minimum' field
452 * (must be absent or 0) and valid 'maximum' field (must be absent).
454 static int nc_minmax_valid(GENERAL_SUBTREE *sub) {
462 bn = ASN1_INTEGER_to_BN(sub->minimum, NULL);
463 if (bn == NULL || !BN_is_zero(bn))
471 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
473 GENERAL_SUBTREE *sub;
476 * We need to compare not gen->type field but an "effective" type because
477 * the otherName field may contain EAI email address treated specially
478 * according to RFC 8398, section 6
480 int effective_type = ((gen->type == GEN_OTHERNAME) &&
481 (OBJ_obj2nid(gen->d.otherName->type_id) ==
482 NID_id_on_SmtpUTF8Mailbox)) ? GEN_EMAIL : gen->type;
485 * Permitted subtrees: if any subtrees exist of matching the type at
486 * least one subtree must match.
489 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
490 sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
491 if (effective_type != sub->base->type)
493 if (!nc_minmax_valid(sub))
494 return X509_V_ERR_SUBTREE_MINMAX;
495 /* If we already have a match don't bother trying any more */
500 r = nc_match_single(gen, sub->base);
503 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
508 return X509_V_ERR_PERMITTED_VIOLATION;
510 /* Excluded subtrees: must not match any of these */
512 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
513 sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
514 if (effective_type != sub->base->type)
516 if (!nc_minmax_valid(sub))
517 return X509_V_ERR_SUBTREE_MINMAX;
519 r = nc_match_single(gen, sub->base);
521 return X509_V_ERR_EXCLUDED_VIOLATION;
522 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
531 static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
536 * We are here only when we have SmtpUTF8 name,
537 * so we match the value of othername with base->d.rfc822Name
539 return nc_email_eai(gen->d.otherName->value, base->d.rfc822Name);
542 return nc_dn(gen->d.directoryName, base->d.directoryName);
545 return nc_dns(gen->d.dNSName, base->d.dNSName);
548 return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
551 return nc_uri(gen->d.uniformResourceIdentifier,
552 base->d.uniformResourceIdentifier);
555 return nc_ip(gen->d.iPAddress, base->d.iPAddress);
558 return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
564 * directoryName name constraint matching. The canonical encoding of
565 * X509_NAME makes this comparison easy. It is matched if the subtree is a
566 * subset of the name.
569 static int nc_dn(const X509_NAME *nm, const X509_NAME *base)
571 /* Ensure canonical encodings are up to date. */
572 if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
573 return X509_V_ERR_OUT_OF_MEM;
574 if (base->modified && i2d_X509_NAME(base, NULL) < 0)
575 return X509_V_ERR_OUT_OF_MEM;
576 if (base->canon_enclen > nm->canon_enclen)
577 return X509_V_ERR_PERMITTED_VIOLATION;
578 if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
579 return X509_V_ERR_PERMITTED_VIOLATION;
583 static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
585 char *baseptr = (char *)base->data;
586 char *dnsptr = (char *)dns->data;
588 /* Empty matches everything */
589 if (base->length == 0)
592 if (dns->length < base->length)
593 return X509_V_ERR_PERMITTED_VIOLATION;
596 * Otherwise can add zero or more components on the left so compare RHS
597 * and if dns is longer and expect '.' as preceding character.
599 if (dns->length > base->length) {
600 dnsptr += dns->length - base->length;
601 if (*baseptr != '.' && dnsptr[-1] != '.')
602 return X509_V_ERR_PERMITTED_VIOLATION;
605 if (ia5ncasecmp(baseptr, dnsptr, base->length))
606 return X509_V_ERR_PERMITTED_VIOLATION;
613 * This function implements comparison between ASCII/U-label in emltype
614 * and A-label in base according to RFC 8398, section 6.
615 * Convert base to U-label and ASCII-parts of domain names, for base
616 * Octet-to-octet comparison of `emltype` and `base` hostname parts
617 * (ASCII-parts should be compared in case-insensitive manner)
619 static int nc_email_eai(ASN1_TYPE *emltype, ASN1_IA5STRING *base)
621 ASN1_UTF8STRING *eml;
622 char *baseptr = NULL;
626 size_t size = sizeof(ulabel) - 1;
630 /* We do not accept embedded NUL characters */
631 if (base->length > 0 && memchr(base->data, 0, base->length) != NULL)
632 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
634 /* 'base' may not be NUL terminated. Create a copy that is */
635 baseptr = OPENSSL_strndup((char *)base->data, base->length);
637 return X509_V_ERR_OUT_OF_MEM;
639 if (emltype->type != V_ASN1_UTF8STRING) {
640 ret = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
644 eml = emltype->value.utf8string;
645 emlptr = (char *)eml->data;
646 emlat = ia5memrchr(eml, '@');
649 ret = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
653 memset(ulabel, 0, sizeof(ulabel));
654 /* Special case: initial '.' is RHS match */
655 if (*baseptr == '.') {
658 if (ossl_a2ulabel(baseptr, ulabel + 1, &size) <= 0) {
659 ret = X509_V_ERR_UNSPECIFIED;
663 if ((size_t)eml->length > strlen(ulabel)) {
664 emlptr += eml->length - (strlen(ulabel));
666 if (ia5ncasecmp(ulabel, emlptr, strlen(ulabel)) == 0)
669 ret = X509_V_ERR_PERMITTED_VIOLATION;
673 if (ossl_a2ulabel(baseptr, ulabel, &size) <= 0) {
674 ret = X509_V_ERR_UNSPECIFIED;
677 /* Just have hostname left to match: case insensitive */
679 emlhostlen = IA5_OFFSET_LEN(eml, emlptr);
680 if (emlhostlen != strlen(ulabel)
681 || ia5ncasecmp(ulabel, emlptr, emlhostlen) != 0) {
682 ret = X509_V_ERR_PERMITTED_VIOLATION;
687 OPENSSL_free(baseptr);
691 static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
693 const char *baseptr = (char *)base->data;
694 const char *emlptr = (char *)eml->data;
695 const char *baseat = ia5memrchr(base, '@');
696 const char *emlat = ia5memrchr(eml, '@');
697 size_t basehostlen, emlhostlen;
700 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
701 /* Special case: initial '.' is RHS match */
702 if (!baseat && base->length > 0 && (*baseptr == '.')) {
703 if (eml->length > base->length) {
704 emlptr += eml->length - base->length;
705 if (ia5ncasecmp(baseptr, emlptr, base->length) == 0)
708 return X509_V_ERR_PERMITTED_VIOLATION;
711 /* If we have anything before '@' match local part */
714 if (baseat != baseptr) {
715 if ((baseat - baseptr) != (emlat - emlptr))
716 return X509_V_ERR_PERMITTED_VIOLATION;
717 if (memchr(baseptr, 0, baseat - baseptr) ||
718 memchr(emlptr, 0, emlat - emlptr))
719 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
720 /* Case sensitive match of local part */
721 if (strncmp(baseptr, emlptr, emlat - emlptr))
722 return X509_V_ERR_PERMITTED_VIOLATION;
724 /* Position base after '@' */
725 baseptr = baseat + 1;
728 basehostlen = IA5_OFFSET_LEN(base, baseptr);
729 emlhostlen = IA5_OFFSET_LEN(eml, emlptr);
730 /* Just have hostname left to match: case insensitive */
731 if (basehostlen != emlhostlen || ia5ncasecmp(baseptr, emlptr, emlhostlen))
732 return X509_V_ERR_PERMITTED_VIOLATION;
738 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
740 const char *baseptr = (char *)base->data;
741 const char *hostptr = (char *)uri->data;
742 const char *p = ia5memchr(uri, (char *)uri->data, ':');
745 /* Check for foo:// and skip past it */
747 || IA5_OFFSET_LEN(uri, p) < 3
750 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
753 /* Determine length of hostname part of URI */
755 /* Look for a port indicator as end of hostname first */
757 p = ia5memchr(uri, hostptr, ':');
758 /* Otherwise look for trailing slash */
760 p = ia5memchr(uri, hostptr, '/');
763 hostlen = IA5_OFFSET_LEN(uri, hostptr);
765 hostlen = p - hostptr;
768 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
770 /* Special case: initial '.' is RHS match */
771 if (base->length > 0 && *baseptr == '.') {
772 if (hostlen > base->length) {
773 p = hostptr + hostlen - base->length;
774 if (ia5ncasecmp(p, baseptr, base->length) == 0)
777 return X509_V_ERR_PERMITTED_VIOLATION;
780 if ((base->length != (int)hostlen)
781 || ia5ncasecmp(hostptr, baseptr, hostlen))
782 return X509_V_ERR_PERMITTED_VIOLATION;
788 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
790 int hostlen, baselen, i;
791 unsigned char *hostptr, *baseptr, *maskptr;
793 hostlen = ip->length;
794 baseptr = base->data;
795 baselen = base->length;
797 /* Invalid if not IPv4 or IPv6 */
798 if (!((hostlen == 4) || (hostlen == 16)))
799 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
800 if (!((baselen == 8) || (baselen == 32)))
801 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
803 /* Do not match IPv4 with IPv6 */
804 if (hostlen * 2 != baselen)
805 return X509_V_ERR_PERMITTED_VIOLATION;
807 maskptr = base->data + hostlen;
809 /* Considering possible not aligned base ipAddress */
810 /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
811 for (i = 0; i < hostlen; i++)
812 if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
813 return X509_V_ERR_PERMITTED_VIOLATION;