sl@0: /* sl@0: * Contributed to the OpenSSL Project by the American Registry for sl@0: * Internet Numbers ("ARIN"). sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 2006 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: */ sl@0: sl@0: /* sl@0: * Implementation of RFC 3779 section 3.2. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifndef OPENSSL_NO_RFC3779 sl@0: sl@0: /* sl@0: * OpenSSL ASN.1 template translation of RFC 3779 3.2.3. sl@0: */ sl@0: sl@0: ASN1_SEQUENCE(ASRange) = { sl@0: ASN1_SIMPLE(ASRange, min, ASN1_INTEGER), sl@0: ASN1_SIMPLE(ASRange, max, ASN1_INTEGER) sl@0: } ASN1_SEQUENCE_END(ASRange) sl@0: sl@0: ASN1_CHOICE(ASIdOrRange) = { sl@0: ASN1_SIMPLE(ASIdOrRange, u.id, ASN1_INTEGER), sl@0: ASN1_SIMPLE(ASIdOrRange, u.range, ASRange) sl@0: } ASN1_CHOICE_END(ASIdOrRange) sl@0: sl@0: ASN1_CHOICE(ASIdentifierChoice) = { sl@0: ASN1_SIMPLE(ASIdentifierChoice, u.inherit, ASN1_NULL), sl@0: ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange) sl@0: } ASN1_CHOICE_END(ASIdentifierChoice) sl@0: sl@0: ASN1_SEQUENCE(ASIdentifiers) = { sl@0: ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0), sl@0: ASN1_EXP_OPT(ASIdentifiers, rdi, ASIdentifierChoice, 1) sl@0: } ASN1_SEQUENCE_END(ASIdentifiers) sl@0: sl@0: IMPLEMENT_ASN1_FUNCTIONS(ASRange) sl@0: IMPLEMENT_ASN1_FUNCTIONS(ASIdOrRange) sl@0: IMPLEMENT_ASN1_FUNCTIONS(ASIdentifierChoice) sl@0: IMPLEMENT_ASN1_FUNCTIONS(ASIdentifiers) sl@0: sl@0: /* sl@0: * i2r method for an ASIdentifierChoice. sl@0: */ sl@0: static int i2r_ASIdentifierChoice(BIO *out, sl@0: ASIdentifierChoice *choice, sl@0: int indent, sl@0: const char *msg) sl@0: { sl@0: int i; sl@0: char *s; sl@0: if (choice == NULL) sl@0: return 1; sl@0: BIO_printf(out, "%*s%s:\n", indent, "", msg); sl@0: switch (choice->type) { sl@0: case ASIdentifierChoice_inherit: sl@0: BIO_printf(out, "%*sinherit\n", indent + 2, ""); sl@0: break; sl@0: case ASIdentifierChoice_asIdsOrRanges: sl@0: for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges); i++) { sl@0: ASIdOrRange *aor = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i); sl@0: switch (aor->type) { sl@0: case ASIdOrRange_id: sl@0: if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) == NULL) sl@0: return 0; sl@0: BIO_printf(out, "%*s%s\n", indent + 2, "", s); sl@0: OPENSSL_free(s); sl@0: break; sl@0: case ASIdOrRange_range: sl@0: if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->min)) == NULL) sl@0: return 0; sl@0: BIO_printf(out, "%*s%s-", indent + 2, "", s); sl@0: OPENSSL_free(s); sl@0: if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->max)) == NULL) sl@0: return 0; sl@0: BIO_printf(out, "%s\n", s); sl@0: OPENSSL_free(s); sl@0: break; sl@0: default: sl@0: return 0; sl@0: } sl@0: } sl@0: break; sl@0: default: sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: /* sl@0: * i2r method for an ASIdentifier extension. sl@0: */ sl@0: static int i2r_ASIdentifiers(X509V3_EXT_METHOD *method, sl@0: void *ext, sl@0: BIO *out, sl@0: int indent) sl@0: { sl@0: ASIdentifiers *asid = ext; sl@0: return (i2r_ASIdentifierChoice(out, asid->asnum, indent, sl@0: "Autonomous System Numbers") && sl@0: i2r_ASIdentifierChoice(out, asid->rdi, indent, sl@0: "Routing Domain Identifiers")); sl@0: } sl@0: sl@0: /* sl@0: * Sort comparision function for a sequence of ASIdOrRange elements. sl@0: */ sl@0: static int ASIdOrRange_cmp(const ASIdOrRange * const *a_, sl@0: const ASIdOrRange * const *b_) sl@0: { sl@0: const ASIdOrRange *a = *a_, *b = *b_; sl@0: sl@0: assert((a->type == ASIdOrRange_id && a->u.id != NULL) || sl@0: (a->type == ASIdOrRange_range && a->u.range != NULL && sl@0: a->u.range->min != NULL && a->u.range->max != NULL)); sl@0: sl@0: assert((b->type == ASIdOrRange_id && b->u.id != NULL) || sl@0: (b->type == ASIdOrRange_range && b->u.range != NULL && sl@0: b->u.range->min != NULL && b->u.range->max != NULL)); sl@0: sl@0: if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id) sl@0: return ASN1_INTEGER_cmp(a->u.id, b->u.id); sl@0: sl@0: if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) { sl@0: int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min); sl@0: return r != 0 ? r : ASN1_INTEGER_cmp(a->u.range->max, b->u.range->max); sl@0: } sl@0: sl@0: if (a->type == ASIdOrRange_id) sl@0: return ASN1_INTEGER_cmp(a->u.id, b->u.range->min); sl@0: else sl@0: return ASN1_INTEGER_cmp(a->u.range->min, b->u.id); sl@0: } sl@0: sl@0: /* sl@0: * Add an inherit element. sl@0: */ sl@0: int v3_asid_add_inherit(ASIdentifiers *asid, int which) sl@0: { sl@0: ASIdentifierChoice **choice; sl@0: if (asid == NULL) sl@0: return 0; sl@0: switch (which) { sl@0: case V3_ASID_ASNUM: sl@0: choice = &asid->asnum; sl@0: break; sl@0: case V3_ASID_RDI: sl@0: choice = &asid->rdi; sl@0: break; sl@0: default: sl@0: return 0; sl@0: } sl@0: if (*choice == NULL) { sl@0: if ((*choice = ASIdentifierChoice_new()) == NULL) sl@0: return 0; sl@0: assert((*choice)->u.inherit == NULL); sl@0: if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL) sl@0: return 0; sl@0: (*choice)->type = ASIdentifierChoice_inherit; sl@0: } sl@0: return (*choice)->type == ASIdentifierChoice_inherit; sl@0: } sl@0: sl@0: /* sl@0: * Add an ID or range to an ASIdentifierChoice. sl@0: */ sl@0: int v3_asid_add_id_or_range(ASIdentifiers *asid, sl@0: int which, sl@0: ASN1_INTEGER *min, sl@0: ASN1_INTEGER *max) sl@0: { sl@0: ASIdentifierChoice **choice; sl@0: ASIdOrRange *aor; sl@0: if (asid == NULL) sl@0: return 0; sl@0: switch (which) { sl@0: case V3_ASID_ASNUM: sl@0: choice = &asid->asnum; sl@0: break; sl@0: case V3_ASID_RDI: sl@0: choice = &asid->rdi; sl@0: break; sl@0: default: sl@0: return 0; sl@0: } sl@0: if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit) sl@0: return 0; sl@0: if (*choice == NULL) { sl@0: if ((*choice = ASIdentifierChoice_new()) == NULL) sl@0: return 0; sl@0: assert((*choice)->u.asIdsOrRanges == NULL); sl@0: (*choice)->u.asIdsOrRanges = sk_ASIdOrRange_new(ASIdOrRange_cmp); sl@0: if ((*choice)->u.asIdsOrRanges == NULL) sl@0: return 0; sl@0: (*choice)->type = ASIdentifierChoice_asIdsOrRanges; sl@0: } sl@0: if ((aor = ASIdOrRange_new()) == NULL) sl@0: return 0; sl@0: if (max == NULL) { sl@0: aor->type = ASIdOrRange_id; sl@0: aor->u.id = min; sl@0: } else { sl@0: aor->type = ASIdOrRange_range; sl@0: if ((aor->u.range = ASRange_new()) == NULL) sl@0: goto err; sl@0: ASN1_INTEGER_free(aor->u.range->min); sl@0: aor->u.range->min = min; sl@0: ASN1_INTEGER_free(aor->u.range->max); sl@0: aor->u.range->max = max; sl@0: } sl@0: if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor))) sl@0: goto err; sl@0: return 1; sl@0: sl@0: err: sl@0: ASIdOrRange_free(aor); sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: * Extract min and max values from an ASIdOrRange. sl@0: */ sl@0: static void extract_min_max(ASIdOrRange *aor, sl@0: ASN1_INTEGER **min, sl@0: ASN1_INTEGER **max) sl@0: { sl@0: assert(aor != NULL && min != NULL && max != NULL); sl@0: switch (aor->type) { sl@0: case ASIdOrRange_id: sl@0: *min = aor->u.id; sl@0: *max = aor->u.id; sl@0: return; sl@0: case ASIdOrRange_range: sl@0: *min = aor->u.range->min; sl@0: *max = aor->u.range->max; sl@0: return; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Check whether an ASIdentifierChoice is in canonical form. sl@0: */ sl@0: static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice) sl@0: { sl@0: ASN1_INTEGER *a_max_plus_one = NULL; sl@0: BIGNUM *bn = NULL; sl@0: int i, ret = 0; sl@0: sl@0: /* sl@0: * Empty element or inheritance is canonical. sl@0: */ sl@0: if (choice == NULL || choice->type == ASIdentifierChoice_inherit) sl@0: return 1; sl@0: sl@0: /* sl@0: * If not a list, or if empty list, it's broken. sl@0: */ sl@0: if (choice->type != ASIdentifierChoice_asIdsOrRanges || sl@0: sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0) sl@0: return 0; sl@0: sl@0: /* sl@0: * It's a list, check it. sl@0: */ sl@0: for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) { sl@0: ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i); sl@0: ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1); sl@0: ASN1_INTEGER *a_min, *a_max, *b_min, *b_max; sl@0: sl@0: extract_min_max(a, &a_min, &a_max); sl@0: extract_min_max(b, &b_min, &b_max); sl@0: sl@0: /* sl@0: * Punt misordered list, overlapping start, or inverted range. sl@0: */ sl@0: if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 || sl@0: ASN1_INTEGER_cmp(a_min, a_max) > 0 || sl@0: ASN1_INTEGER_cmp(b_min, b_max) > 0) sl@0: goto done; sl@0: sl@0: /* sl@0: * Calculate a_max + 1 to check for adjacency. sl@0: */ sl@0: if ((bn == NULL && (bn = BN_new()) == NULL) || sl@0: ASN1_INTEGER_to_BN(a_max, bn) == NULL || sl@0: !BN_add_word(bn, 1) || sl@0: (a_max_plus_one = BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) { sl@0: X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL, sl@0: ERR_R_MALLOC_FAILURE); sl@0: goto done; sl@0: } sl@0: sl@0: /* sl@0: * Punt if adjacent or overlapping. sl@0: */ sl@0: if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0) sl@0: goto done; sl@0: } sl@0: sl@0: ret = 1; sl@0: sl@0: done: sl@0: ASN1_INTEGER_free(a_max_plus_one); sl@0: BN_free(bn); sl@0: return ret; sl@0: } sl@0: sl@0: /* sl@0: * Check whether an ASIdentifier extension is in canonical form. sl@0: */ sl@0: int v3_asid_is_canonical(ASIdentifiers *asid) sl@0: { sl@0: return (asid == NULL || sl@0: (ASIdentifierChoice_is_canonical(asid->asnum) || sl@0: ASIdentifierChoice_is_canonical(asid->rdi))); sl@0: } sl@0: sl@0: /* sl@0: * Whack an ASIdentifierChoice into canonical form. sl@0: */ sl@0: static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice) sl@0: { sl@0: ASN1_INTEGER *a_max_plus_one = NULL; sl@0: BIGNUM *bn = NULL; sl@0: int i, ret = 0; sl@0: sl@0: /* sl@0: * Nothing to do for empty element or inheritance. sl@0: */ sl@0: if (choice == NULL || choice->type == ASIdentifierChoice_inherit) sl@0: return 1; sl@0: sl@0: /* sl@0: * We have a list. Sort it. sl@0: */ sl@0: assert(choice->type == ASIdentifierChoice_asIdsOrRanges); sl@0: sk_ASIdOrRange_sort(choice->u.asIdsOrRanges); sl@0: sl@0: /* sl@0: * Now check for errors and suboptimal encoding, rejecting the sl@0: * former and fixing the latter. sl@0: */ sl@0: for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) { sl@0: ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i); sl@0: ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1); sl@0: ASN1_INTEGER *a_min, *a_max, *b_min, *b_max; sl@0: sl@0: extract_min_max(a, &a_min, &a_max); sl@0: extract_min_max(b, &b_min, &b_max); sl@0: sl@0: /* sl@0: * Make sure we're properly sorted (paranoia). sl@0: */ sl@0: assert(ASN1_INTEGER_cmp(a_min, b_min) <= 0); sl@0: sl@0: /* sl@0: * Check for overlaps. sl@0: */ sl@0: if (ASN1_INTEGER_cmp(a_max, b_min) >= 0) { sl@0: X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE, sl@0: X509V3_R_EXTENSION_VALUE_ERROR); sl@0: goto done; sl@0: } sl@0: sl@0: /* sl@0: * Calculate a_max + 1 to check for adjacency. sl@0: */ sl@0: if ((bn == NULL && (bn = BN_new()) == NULL) || sl@0: ASN1_INTEGER_to_BN(a_max, bn) == NULL || sl@0: !BN_add_word(bn, 1) || sl@0: (a_max_plus_one = BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) { sl@0: X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE, ERR_R_MALLOC_FAILURE); sl@0: goto done; sl@0: } sl@0: sl@0: /* sl@0: * If a and b are adjacent, merge them. sl@0: */ sl@0: if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) == 0) { sl@0: ASRange *r; sl@0: switch (a->type) { sl@0: case ASIdOrRange_id: sl@0: if ((r = OPENSSL_malloc(sizeof(ASRange))) == NULL) { sl@0: X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE, sl@0: ERR_R_MALLOC_FAILURE); sl@0: goto done; sl@0: } sl@0: r->min = a_min; sl@0: r->max = b_max; sl@0: a->type = ASIdOrRange_range; sl@0: a->u.range = r; sl@0: break; sl@0: case ASIdOrRange_range: sl@0: ASN1_INTEGER_free(a->u.range->max); sl@0: a->u.range->max = b_max; sl@0: break; sl@0: } sl@0: switch (b->type) { sl@0: case ASIdOrRange_id: sl@0: b->u.id = NULL; sl@0: break; sl@0: case ASIdOrRange_range: sl@0: b->u.range->max = NULL; sl@0: break; sl@0: } sl@0: ASIdOrRange_free(b); sl@0: sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i + 1); sl@0: i--; sl@0: continue; sl@0: } sl@0: } sl@0: sl@0: assert(ASIdentifierChoice_is_canonical(choice)); /* Paranoia */ sl@0: sl@0: ret = 1; sl@0: sl@0: done: sl@0: ASN1_INTEGER_free(a_max_plus_one); sl@0: BN_free(bn); sl@0: return ret; sl@0: } sl@0: sl@0: /* sl@0: * Whack an ASIdentifier extension into canonical form. sl@0: */ sl@0: int v3_asid_canonize(ASIdentifiers *asid) sl@0: { sl@0: return (asid == NULL || sl@0: (ASIdentifierChoice_canonize(asid->asnum) && sl@0: ASIdentifierChoice_canonize(asid->rdi))); sl@0: } sl@0: sl@0: /* sl@0: * v2i method for an ASIdentifier extension. sl@0: */ sl@0: static void *v2i_ASIdentifiers(struct v3_ext_method *method, sl@0: struct v3_ext_ctx *ctx, sl@0: STACK_OF(CONF_VALUE) *values) sl@0: { sl@0: ASIdentifiers *asid = NULL; sl@0: int i; sl@0: sl@0: if ((asid = ASIdentifiers_new()) == NULL) { sl@0: X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); sl@0: return NULL; sl@0: } sl@0: sl@0: for (i = 0; i < sk_CONF_VALUE_num(values); i++) { sl@0: CONF_VALUE *val = sk_CONF_VALUE_value(values, i); sl@0: ASN1_INTEGER *min = NULL, *max = NULL; sl@0: int i1, i2, i3, is_range, which; sl@0: sl@0: /* sl@0: * Figure out whether this is an AS or an RDI. sl@0: */ sl@0: if ( !name_cmp(val->name, "AS")) { sl@0: which = V3_ASID_ASNUM; sl@0: } else if (!name_cmp(val->name, "RDI")) { sl@0: which = V3_ASID_RDI; sl@0: } else { sl@0: X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_EXTENSION_NAME_ERROR); sl@0: X509V3_conf_err(val); sl@0: goto err; sl@0: } sl@0: sl@0: /* sl@0: * Handle inheritance. sl@0: */ sl@0: if (!strcmp(val->value, "inherit")) { sl@0: if (v3_asid_add_inherit(asid, which)) sl@0: continue; sl@0: X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_INHERITANCE); sl@0: X509V3_conf_err(val); sl@0: goto err; sl@0: } sl@0: sl@0: /* sl@0: * Number, range, or mistake, pick it apart and figure out which. sl@0: */ sl@0: i1 = strspn(val->value, "0123456789"); sl@0: if (val->value[i1] == '\0') { sl@0: is_range = 0; sl@0: } else { sl@0: is_range = 1; sl@0: i2 = i1 + strspn(val->value + i1, " \t"); sl@0: if (val->value[i2] != '-') { sl@0: X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_ASNUMBER); sl@0: X509V3_conf_err(val); sl@0: goto err; sl@0: } sl@0: i2++; sl@0: i2 = i2 + strspn(val->value + i2, " \t"); sl@0: i3 = i2 + strspn(val->value + i2, "0123456789"); sl@0: if (val->value[i3] != '\0') { sl@0: X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_ASRANGE); sl@0: X509V3_conf_err(val); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Syntax is ok, read and add it. sl@0: */ sl@0: if (!is_range) { sl@0: if (!X509V3_get_value_int(val, &min)) { sl@0: X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: } else { sl@0: char *s = BUF_strdup(val->value); sl@0: if (s == NULL) { sl@0: X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: s[i1] = '\0'; sl@0: min = s2i_ASN1_INTEGER(NULL, s); sl@0: max = s2i_ASN1_INTEGER(NULL, s + i2); sl@0: OPENSSL_free(s); sl@0: if (min == NULL || max == NULL) { sl@0: ASN1_INTEGER_free(min); sl@0: ASN1_INTEGER_free(max); sl@0: X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: if (!v3_asid_add_id_or_range(asid, which, min, max)) { sl@0: ASN1_INTEGER_free(min); sl@0: ASN1_INTEGER_free(max); sl@0: X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Canonize the result, then we're done. sl@0: */ sl@0: if (!v3_asid_canonize(asid)) sl@0: goto err; sl@0: return asid; sl@0: sl@0: err: sl@0: ASIdentifiers_free(asid); sl@0: return NULL; sl@0: } sl@0: sl@0: /* sl@0: * OpenSSL dispatch. sl@0: */ sl@0: const X509V3_EXT_METHOD v3_asid = { sl@0: NID_sbgp_autonomousSysNum, /* nid */ sl@0: 0, /* flags */ sl@0: ASN1_ITEM_ref(ASIdentifiers), /* template */ sl@0: 0, 0, 0, 0, /* old functions, ignored */ sl@0: 0, /* i2s */ sl@0: 0, /* s2i */ sl@0: 0, /* i2v */ sl@0: v2i_ASIdentifiers, /* v2i */ sl@0: i2r_ASIdentifiers, /* i2r */ sl@0: 0, /* r2i */ sl@0: NULL /* extension-specific data */ sl@0: }; sl@0: sl@0: /* sl@0: * Figure out whether extension uses inheritance. sl@0: */ sl@0: int v3_asid_inherits(ASIdentifiers *asid) sl@0: { sl@0: return (asid != NULL && sl@0: ((asid->asnum != NULL && sl@0: asid->asnum->type == ASIdentifierChoice_inherit) || sl@0: (asid->rdi != NULL && sl@0: asid->rdi->type == ASIdentifierChoice_inherit))); sl@0: } sl@0: sl@0: /* sl@0: * Figure out whether parent contains child. sl@0: */ sl@0: static int asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child) sl@0: { sl@0: ASN1_INTEGER *p_min, *p_max, *c_min, *c_max; sl@0: int p, c; sl@0: sl@0: if (child == NULL || parent == child) sl@0: return 1; sl@0: if (parent == NULL) sl@0: return 0; sl@0: sl@0: p = 0; sl@0: for (c = 0; c < sk_ASIdOrRange_num(child); c++) { sl@0: extract_min_max(sk_ASIdOrRange_value(child, c), &c_min, &c_max); sl@0: for (;; p++) { sl@0: if (p >= sk_ASIdOrRange_num(parent)) sl@0: return 0; sl@0: extract_min_max(sk_ASIdOrRange_value(parent, p), &p_min, &p_max); sl@0: if (ASN1_INTEGER_cmp(p_max, c_max) < 0) sl@0: continue; sl@0: if (ASN1_INTEGER_cmp(p_min, c_min) > 0) sl@0: return 0; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: /* sl@0: * Test whether a is a subet of b. sl@0: */ sl@0: int v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b) sl@0: { sl@0: return (a == NULL || sl@0: a == b || sl@0: (b != NULL && sl@0: !v3_asid_inherits(a) && sl@0: !v3_asid_inherits(b) && sl@0: asid_contains(b->asnum->u.asIdsOrRanges, sl@0: a->asnum->u.asIdsOrRanges) && sl@0: asid_contains(b->rdi->u.asIdsOrRanges, sl@0: a->rdi->u.asIdsOrRanges))); sl@0: } sl@0: sl@0: /* sl@0: * Validation error handling via callback. sl@0: */ sl@0: #define validation_err(_err_) \ sl@0: do { \ sl@0: if (ctx != NULL) { \ sl@0: ctx->error = _err_; \ sl@0: ctx->error_depth = i; \ sl@0: ctx->current_cert = x; \ sl@0: ret = ctx->verify_cb(0, ctx); \ sl@0: } else { \ sl@0: ret = 0; \ sl@0: } \ sl@0: if (!ret) \ sl@0: goto done; \ sl@0: } while (0) sl@0: sl@0: /* sl@0: * Core code for RFC 3779 3.3 path validation. sl@0: */ sl@0: static int v3_asid_validate_path_internal(X509_STORE_CTX *ctx, sl@0: STACK_OF(X509) *chain, sl@0: ASIdentifiers *ext) sl@0: { sl@0: ASIdOrRanges *child_as = NULL, *child_rdi = NULL; sl@0: int i, ret = 1, inherit_as = 0, inherit_rdi = 0; sl@0: X509 *x = NULL; sl@0: sl@0: assert(chain != NULL && sk_X509_num(chain) > 0); sl@0: assert(ctx != NULL || ext != NULL); sl@0: assert(ctx == NULL || ctx->verify_cb != NULL); sl@0: sl@0: /* sl@0: * Figure out where to start. If we don't have an extension to sl@0: * check, we're done. Otherwise, check canonical form and sl@0: * set up for walking up the chain. sl@0: */ sl@0: if (ext != NULL) { sl@0: i = -1; sl@0: } else { sl@0: i = 0; sl@0: x = sk_X509_value(chain, i); sl@0: assert(x != NULL); sl@0: if ((ext = x->rfc3779_asid) == NULL) sl@0: goto done; sl@0: } sl@0: if (!v3_asid_is_canonical(ext)) sl@0: validation_err(X509_V_ERR_INVALID_EXTENSION); sl@0: if (ext->asnum != NULL) { sl@0: switch (ext->asnum->type) { sl@0: case ASIdentifierChoice_inherit: sl@0: inherit_as = 1; sl@0: break; sl@0: case ASIdentifierChoice_asIdsOrRanges: sl@0: child_as = ext->asnum->u.asIdsOrRanges; sl@0: break; sl@0: } sl@0: } sl@0: if (ext->rdi != NULL) { sl@0: switch (ext->rdi->type) { sl@0: case ASIdentifierChoice_inherit: sl@0: inherit_rdi = 1; sl@0: break; sl@0: case ASIdentifierChoice_asIdsOrRanges: sl@0: child_rdi = ext->rdi->u.asIdsOrRanges; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Now walk up the chain. Extensions must be in canonical form, no sl@0: * cert may list resources that its parent doesn't list. sl@0: */ sl@0: for (i++; i < sk_X509_num(chain); i++) { sl@0: x = sk_X509_value(chain, i); sl@0: assert(x != NULL); sl@0: if (x->rfc3779_asid == NULL) { sl@0: if (child_as != NULL || child_rdi != NULL) sl@0: validation_err(X509_V_ERR_UNNESTED_RESOURCE); sl@0: continue; sl@0: } sl@0: if (!v3_asid_is_canonical(x->rfc3779_asid)) sl@0: validation_err(X509_V_ERR_INVALID_EXTENSION); sl@0: if (x->rfc3779_asid->asnum == NULL && child_as != NULL) { sl@0: validation_err(X509_V_ERR_UNNESTED_RESOURCE); sl@0: child_as = NULL; sl@0: inherit_as = 0; sl@0: } sl@0: if (x->rfc3779_asid->asnum != NULL && sl@0: x->rfc3779_asid->asnum->type == ASIdentifierChoice_asIdsOrRanges) { sl@0: if (inherit_as || sl@0: asid_contains(x->rfc3779_asid->asnum->u.asIdsOrRanges, child_as)) { sl@0: child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges; sl@0: inherit_as = 0; sl@0: } else { sl@0: validation_err(X509_V_ERR_UNNESTED_RESOURCE); sl@0: } sl@0: } sl@0: if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) { sl@0: validation_err(X509_V_ERR_UNNESTED_RESOURCE); sl@0: child_rdi = NULL; sl@0: inherit_rdi = 0; sl@0: } sl@0: if (x->rfc3779_asid->rdi != NULL && sl@0: x->rfc3779_asid->rdi->type == ASIdentifierChoice_asIdsOrRanges) { sl@0: if (inherit_rdi || sl@0: asid_contains(x->rfc3779_asid->rdi->u.asIdsOrRanges, child_rdi)) { sl@0: child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges; sl@0: inherit_rdi = 0; sl@0: } else { sl@0: validation_err(X509_V_ERR_UNNESTED_RESOURCE); sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Trust anchor can't inherit. sl@0: */ sl@0: if (x->rfc3779_asid != NULL) { sl@0: if (x->rfc3779_asid->asnum != NULL && sl@0: x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit) sl@0: validation_err(X509_V_ERR_UNNESTED_RESOURCE); sl@0: if (x->rfc3779_asid->rdi != NULL && sl@0: x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit) sl@0: validation_err(X509_V_ERR_UNNESTED_RESOURCE); sl@0: } sl@0: sl@0: done: sl@0: return ret; sl@0: } sl@0: sl@0: #undef validation_err sl@0: sl@0: /* sl@0: * RFC 3779 3.3 path validation -- called from X509_verify_cert(). sl@0: */ sl@0: int v3_asid_validate_path(X509_STORE_CTX *ctx) sl@0: { sl@0: return v3_asid_validate_path_internal(ctx, ctx->chain, NULL); sl@0: } sl@0: sl@0: /* sl@0: * RFC 3779 3.3 path validation of an extension. sl@0: * Test whether chain covers extension. sl@0: */ sl@0: int v3_asid_validate_resource_set(STACK_OF(X509) *chain, sl@0: ASIdentifiers *ext, sl@0: int allow_inheritance) sl@0: { sl@0: if (ext == NULL) sl@0: return 1; sl@0: if (chain == NULL || sk_X509_num(chain) == 0) sl@0: return 0; sl@0: if (!allow_inheritance && v3_asid_inherits(ext)) sl@0: return 0; sl@0: return v3_asid_validate_path_internal(NULL, chain, ext); sl@0: } sl@0: sl@0: #endif /* OPENSSL_NO_RFC3779 */