⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 v3_asid.c

📁 OpenSSL 0.9.8k 最新版OpenSSL
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Contributed to the OpenSSL Project by the American Registry for * Internet Numbers ("ARIN"). *//* ==================================================================== * Copyright (c) 2006 The OpenSSL Project.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. All advertising materials mentioning features or use of this *    software must display the following acknowledgment: *    "This product includes software developed by the OpenSSL Project *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to *    endorse or promote products derived from this software without *    prior written permission. For written permission, please contact *    licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" *    nor may "OpenSSL" appear in their names without prior written *    permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following *    acknowledgment: *    "This product includes software developed by the OpenSSL Project *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com).  This product includes software written by Tim * Hudson (tjh@cryptsoft.com). *//* * Implementation of RFC 3779 section 3.2. */#include <stdio.h>#include <string.h>#include <assert.h>#include "cryptlib.h"#include <openssl/conf.h>#include <openssl/asn1.h>#include <openssl/asn1t.h>#include <openssl/x509v3.h>#include <openssl/x509.h>#include <openssl/bn.h>#ifndef OPENSSL_NO_RFC3779/* * OpenSSL ASN.1 template translation of RFC 3779 3.2.3. */ASN1_SEQUENCE(ASRange) = {  ASN1_SIMPLE(ASRange, min, ASN1_INTEGER),  ASN1_SIMPLE(ASRange, max, ASN1_INTEGER)} ASN1_SEQUENCE_END(ASRange)ASN1_CHOICE(ASIdOrRange) = {  ASN1_SIMPLE(ASIdOrRange, u.id,    ASN1_INTEGER),  ASN1_SIMPLE(ASIdOrRange, u.range, ASRange)} ASN1_CHOICE_END(ASIdOrRange)ASN1_CHOICE(ASIdentifierChoice) = {  ASN1_SIMPLE(ASIdentifierChoice,      u.inherit,       ASN1_NULL),  ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange)} ASN1_CHOICE_END(ASIdentifierChoice)ASN1_SEQUENCE(ASIdentifiers) = {  ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0),  ASN1_EXP_OPT(ASIdentifiers, rdi,   ASIdentifierChoice, 1)} ASN1_SEQUENCE_END(ASIdentifiers)IMPLEMENT_ASN1_FUNCTIONS(ASRange)IMPLEMENT_ASN1_FUNCTIONS(ASIdOrRange)IMPLEMENT_ASN1_FUNCTIONS(ASIdentifierChoice)IMPLEMENT_ASN1_FUNCTIONS(ASIdentifiers)/* * i2r method for an ASIdentifierChoice. */static int i2r_ASIdentifierChoice(BIO *out,				  ASIdentifierChoice *choice,				  int indent,				  const char *msg){  int i;  char *s;  if (choice == NULL)    return 1;  BIO_printf(out, "%*s%s:\n", indent, "", msg);  switch (choice->type) {  case ASIdentifierChoice_inherit:    BIO_printf(out, "%*sinherit\n", indent + 2, "");    break;  case ASIdentifierChoice_asIdsOrRanges:    for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges); i++) {      ASIdOrRange *aor = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);      switch (aor->type) {      case ASIdOrRange_id:	if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) == NULL)	  return 0;	BIO_printf(out, "%*s%s\n", indent + 2, "", s);	OPENSSL_free(s);	break;      case ASIdOrRange_range:	if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->min)) == NULL)	  return 0;	BIO_printf(out, "%*s%s-", indent + 2, "", s);	OPENSSL_free(s);	if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->max)) == NULL)	  return 0;	BIO_printf(out, "%s\n", s);	OPENSSL_free(s);	break;      default:	return 0;      }    }    break;  default:    return 0;  }  return 1;}/* * i2r method for an ASIdentifier extension. */static int i2r_ASIdentifiers(X509V3_EXT_METHOD *method,			     void *ext,			     BIO *out,			     int indent){  ASIdentifiers *asid = ext;  return (i2r_ASIdentifierChoice(out, asid->asnum, indent,				 "Autonomous System Numbers") &&	  i2r_ASIdentifierChoice(out, asid->rdi, indent,				 "Routing Domain Identifiers"));}/* * Sort comparision function for a sequence of ASIdOrRange elements. */static int ASIdOrRange_cmp(const ASIdOrRange * const *a_,			   const ASIdOrRange * const *b_){  const ASIdOrRange *a = *a_, *b = *b_;  assert((a->type == ASIdOrRange_id && a->u.id != NULL) ||	 (a->type == ASIdOrRange_range && a->u.range != NULL &&	  a->u.range->min != NULL && a->u.range->max != NULL));  assert((b->type == ASIdOrRange_id && b->u.id != NULL) ||	 (b->type == ASIdOrRange_range && b->u.range != NULL &&	  b->u.range->min != NULL && b->u.range->max != NULL));  if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id)    return ASN1_INTEGER_cmp(a->u.id, b->u.id);  if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) {    int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min);    return r != 0 ? r : ASN1_INTEGER_cmp(a->u.range->max, b->u.range->max);  }  if (a->type == ASIdOrRange_id)    return ASN1_INTEGER_cmp(a->u.id, b->u.range->min);  else    return ASN1_INTEGER_cmp(a->u.range->min, b->u.id);}/* * Add an inherit element. */int v3_asid_add_inherit(ASIdentifiers *asid, int which){  ASIdentifierChoice **choice;  if (asid == NULL)    return 0;  switch (which) {  case V3_ASID_ASNUM:    choice = &asid->asnum;    break;  case V3_ASID_RDI:    choice = &asid->rdi;    break;  default:    return 0;  }  if (*choice == NULL) {    if ((*choice = ASIdentifierChoice_new()) == NULL)      return 0;    assert((*choice)->u.inherit == NULL);    if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL)      return 0;    (*choice)->type = ASIdentifierChoice_inherit;  }  return (*choice)->type == ASIdentifierChoice_inherit;}/* * Add an ID or range to an ASIdentifierChoice. */int v3_asid_add_id_or_range(ASIdentifiers *asid,			    int which,			    ASN1_INTEGER *min,			    ASN1_INTEGER *max){  ASIdentifierChoice **choice;  ASIdOrRange *aor;  if (asid == NULL)    return 0;  switch (which) {  case V3_ASID_ASNUM:    choice = &asid->asnum;    break;  case V3_ASID_RDI:    choice = &asid->rdi;    break;  default:    return 0;  }  if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit)    return 0;  if (*choice == NULL) {    if ((*choice = ASIdentifierChoice_new()) == NULL)      return 0;    assert((*choice)->u.asIdsOrRanges == NULL);    (*choice)->u.asIdsOrRanges = sk_ASIdOrRange_new(ASIdOrRange_cmp);    if ((*choice)->u.asIdsOrRanges == NULL)      return 0;    (*choice)->type = ASIdentifierChoice_asIdsOrRanges;  }  if ((aor = ASIdOrRange_new()) == NULL)    return 0;  if (max == NULL) {    aor->type = ASIdOrRange_id;    aor->u.id = min;  } else {    aor->type = ASIdOrRange_range;    if ((aor->u.range = ASRange_new()) == NULL)      goto err;    ASN1_INTEGER_free(aor->u.range->min);    aor->u.range->min = min;    ASN1_INTEGER_free(aor->u.range->max);    aor->u.range->max = max;  }  if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))    goto err;  return 1; err:  ASIdOrRange_free(aor);  return 0;}/* * Extract min and max values from an ASIdOrRange. */static void extract_min_max(ASIdOrRange *aor,			    ASN1_INTEGER **min,			    ASN1_INTEGER **max){  assert(aor != NULL && min != NULL && max != NULL);  switch (aor->type) {  case ASIdOrRange_id:    *min = aor->u.id;    *max = aor->u.id;    return;  case ASIdOrRange_range:    *min = aor->u.range->min;    *max = aor->u.range->max;    return;  }}/* * Check whether an ASIdentifierChoice is in canonical form. */static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice){  ASN1_INTEGER *a_max_plus_one = NULL;  BIGNUM *bn = NULL;  int i, ret = 0;  /*   * Empty element or inheritance is canonical.   */  if (choice == NULL || choice->type == ASIdentifierChoice_inherit)    return 1;  /*   * If not a list, or if empty list, it's broken.   */  if (choice->type != ASIdentifierChoice_asIdsOrRanges ||      sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)    return 0;  /*   * It's a list, check it.   */  for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {    ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);    ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);    ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;    extract_min_max(a, &a_min, &a_max);    extract_min_max(b, &b_min, &b_max);    /*     * Punt misordered list, overlapping start, or inverted range.     */    if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 ||	ASN1_INTEGER_cmp(a_min, a_max) > 0 ||	ASN1_INTEGER_cmp(b_min, b_max) > 0)      goto done;    /*     * Calculate a_max + 1 to check for adjacency.     */    if ((bn == NULL && (bn = BN_new()) == NULL) ||	ASN1_INTEGER_to_BN(a_max, bn) == NULL ||	!BN_add_word(bn, 1) ||	(a_max_plus_one = BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {      X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,		ERR_R_MALLOC_FAILURE);      goto done;    }        /*     * Punt if adjacent or overlapping.     */    if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0)      goto done;  }  ret = 1; done:  ASN1_INTEGER_free(a_max_plus_one);  BN_free(bn);  return ret;}/* * Check whether an ASIdentifier extension is in canonical form. */int v3_asid_is_canonical(ASIdentifiers *asid){  return (asid == NULL ||	  (ASIdentifierChoice_is_canonical(asid->asnum) ||	   ASIdentifierChoice_is_canonical(asid->rdi)));}/* * Whack an ASIdentifierChoice into canonical form. */static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice){  ASN1_INTEGER *a_max_plus_one = NULL;  BIGNUM *bn = NULL;  int i, ret = 0;  /*   * Nothing to do for empty element or inheritance.   */  if (choice == NULL || choice->type == ASIdentifierChoice_inherit)    return 1;  /*   * We have a list.  Sort it.   */  assert(choice->type == ASIdentifierChoice_asIdsOrRanges);  sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);  /*   * Now check for errors and suboptimal encoding, rejecting the   * former and fixing the latter.   */  for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {    ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);    ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);    ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;    extract_min_max(a, &a_min, &a_max);    extract_min_max(b, &b_min, &b_max);    /*     * Make sure we're properly sorted (paranoia).     */    assert(ASN1_INTEGER_cmp(a_min, b_min) <= 0);    /*     * Check for overlaps.     */    if (ASN1_INTEGER_cmp(a_max, b_min) >= 0) {      X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -