os/ossrv/ssl/libcrypto/src/crypto/x509v3/v3_asid.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
 * Contributed to the OpenSSL Project by the American Registry for
sl@0
     3
 * Internet Numbers ("ARIN").
sl@0
     4
 */
sl@0
     5
/* ====================================================================
sl@0
     6
 * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
sl@0
     7
 *
sl@0
     8
 * Redistribution and use in source and binary forms, with or without
sl@0
     9
 * modification, are permitted provided that the following conditions
sl@0
    10
 * are met:
sl@0
    11
 *
sl@0
    12
 * 1. Redistributions of source code must retain the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    14
 *
sl@0
    15
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    16
 *    notice, this list of conditions and the following disclaimer in
sl@0
    17
 *    the documentation and/or other materials provided with the
sl@0
    18
 *    distribution.
sl@0
    19
 *
sl@0
    20
 * 3. All advertising materials mentioning features or use of this
sl@0
    21
 *    software must display the following acknowledgment:
sl@0
    22
 *    "This product includes software developed by the OpenSSL Project
sl@0
    23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0
    24
 *
sl@0
    25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    26
 *    endorse or promote products derived from this software without
sl@0
    27
 *    prior written permission. For written permission, please contact
sl@0
    28
 *    licensing@OpenSSL.org.
sl@0
    29
 *
sl@0
    30
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    31
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    32
 *    permission of the OpenSSL Project.
sl@0
    33
 *
sl@0
    34
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    35
 *    acknowledgment:
sl@0
    36
 *    "This product includes software developed by the OpenSSL Project
sl@0
    37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0
    38
 *
sl@0
    39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    51
 * ====================================================================
sl@0
    52
 *
sl@0
    53
 * This product includes cryptographic software written by Eric Young
sl@0
    54
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    55
 * Hudson (tjh@cryptsoft.com).
sl@0
    56
 */
sl@0
    57
sl@0
    58
/*
sl@0
    59
 * Implementation of RFC 3779 section 3.2.
sl@0
    60
 */
sl@0
    61
sl@0
    62
#include <stdio.h>
sl@0
    63
#include <string.h>
sl@0
    64
#include <assert.h>
sl@0
    65
#include "cryptlib.h"
sl@0
    66
#include <openssl/conf.h>
sl@0
    67
#include <openssl/asn1.h>
sl@0
    68
#include <openssl/asn1t.h>
sl@0
    69
#include <openssl/x509v3.h>
sl@0
    70
#include <openssl/x509.h>
sl@0
    71
#include <openssl/bn.h>
sl@0
    72
sl@0
    73
#ifndef OPENSSL_NO_RFC3779
sl@0
    74
sl@0
    75
/*
sl@0
    76
 * OpenSSL ASN.1 template translation of RFC 3779 3.2.3.
sl@0
    77
 */
sl@0
    78
sl@0
    79
ASN1_SEQUENCE(ASRange) = {
sl@0
    80
  ASN1_SIMPLE(ASRange, min, ASN1_INTEGER),
sl@0
    81
  ASN1_SIMPLE(ASRange, max, ASN1_INTEGER)
sl@0
    82
} ASN1_SEQUENCE_END(ASRange)
sl@0
    83
sl@0
    84
ASN1_CHOICE(ASIdOrRange) = {
sl@0
    85
  ASN1_SIMPLE(ASIdOrRange, u.id,    ASN1_INTEGER),
sl@0
    86
  ASN1_SIMPLE(ASIdOrRange, u.range, ASRange)
sl@0
    87
} ASN1_CHOICE_END(ASIdOrRange)
sl@0
    88
sl@0
    89
ASN1_CHOICE(ASIdentifierChoice) = {
sl@0
    90
  ASN1_SIMPLE(ASIdentifierChoice,      u.inherit,       ASN1_NULL),
sl@0
    91
  ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange)
sl@0
    92
} ASN1_CHOICE_END(ASIdentifierChoice)
sl@0
    93
sl@0
    94
ASN1_SEQUENCE(ASIdentifiers) = {
sl@0
    95
  ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0),
sl@0
    96
  ASN1_EXP_OPT(ASIdentifiers, rdi,   ASIdentifierChoice, 1)
sl@0
    97
} ASN1_SEQUENCE_END(ASIdentifiers)
sl@0
    98
sl@0
    99
IMPLEMENT_ASN1_FUNCTIONS(ASRange)
sl@0
   100
IMPLEMENT_ASN1_FUNCTIONS(ASIdOrRange)
sl@0
   101
IMPLEMENT_ASN1_FUNCTIONS(ASIdentifierChoice)
sl@0
   102
IMPLEMENT_ASN1_FUNCTIONS(ASIdentifiers)
sl@0
   103
sl@0
   104
/*
sl@0
   105
 * i2r method for an ASIdentifierChoice.
sl@0
   106
 */
sl@0
   107
static int i2r_ASIdentifierChoice(BIO *out,
sl@0
   108
				  ASIdentifierChoice *choice,
sl@0
   109
				  int indent,
sl@0
   110
				  const char *msg)
sl@0
   111
{
sl@0
   112
  int i;
sl@0
   113
  char *s;
sl@0
   114
  if (choice == NULL)
sl@0
   115
    return 1;
sl@0
   116
  BIO_printf(out, "%*s%s:\n", indent, "", msg);
sl@0
   117
  switch (choice->type) {
sl@0
   118
  case ASIdentifierChoice_inherit:
sl@0
   119
    BIO_printf(out, "%*sinherit\n", indent + 2, "");
sl@0
   120
    break;
sl@0
   121
  case ASIdentifierChoice_asIdsOrRanges:
sl@0
   122
    for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges); i++) {
sl@0
   123
      ASIdOrRange *aor = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
sl@0
   124
      switch (aor->type) {
sl@0
   125
      case ASIdOrRange_id:
sl@0
   126
	if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) == NULL)
sl@0
   127
	  return 0;
sl@0
   128
	BIO_printf(out, "%*s%s\n", indent + 2, "", s);
sl@0
   129
	OPENSSL_free(s);
sl@0
   130
	break;
sl@0
   131
      case ASIdOrRange_range:
sl@0
   132
	if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->min)) == NULL)
sl@0
   133
	  return 0;
sl@0
   134
	BIO_printf(out, "%*s%s-", indent + 2, "", s);
sl@0
   135
	OPENSSL_free(s);
sl@0
   136
	if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->max)) == NULL)
sl@0
   137
	  return 0;
sl@0
   138
	BIO_printf(out, "%s\n", s);
sl@0
   139
	OPENSSL_free(s);
sl@0
   140
	break;
sl@0
   141
      default:
sl@0
   142
	return 0;
sl@0
   143
      }
sl@0
   144
    }
sl@0
   145
    break;
sl@0
   146
  default:
sl@0
   147
    return 0;
sl@0
   148
  }
sl@0
   149
  return 1;
sl@0
   150
}
sl@0
   151
sl@0
   152
/*
sl@0
   153
 * i2r method for an ASIdentifier extension.
sl@0
   154
 */
sl@0
   155
static int i2r_ASIdentifiers(X509V3_EXT_METHOD *method,
sl@0
   156
			     void *ext,
sl@0
   157
			     BIO *out,
sl@0
   158
			     int indent)
sl@0
   159
{
sl@0
   160
  ASIdentifiers *asid = ext;
sl@0
   161
  return (i2r_ASIdentifierChoice(out, asid->asnum, indent,
sl@0
   162
				 "Autonomous System Numbers") &&
sl@0
   163
	  i2r_ASIdentifierChoice(out, asid->rdi, indent,
sl@0
   164
				 "Routing Domain Identifiers"));
sl@0
   165
}
sl@0
   166
sl@0
   167
/*
sl@0
   168
 * Sort comparision function for a sequence of ASIdOrRange elements.
sl@0
   169
 */
sl@0
   170
static int ASIdOrRange_cmp(const ASIdOrRange * const *a_,
sl@0
   171
			   const ASIdOrRange * const *b_)
sl@0
   172
{
sl@0
   173
  const ASIdOrRange *a = *a_, *b = *b_;
sl@0
   174
sl@0
   175
  assert((a->type == ASIdOrRange_id && a->u.id != NULL) ||
sl@0
   176
	 (a->type == ASIdOrRange_range && a->u.range != NULL &&
sl@0
   177
	  a->u.range->min != NULL && a->u.range->max != NULL));
sl@0
   178
sl@0
   179
  assert((b->type == ASIdOrRange_id && b->u.id != NULL) ||
sl@0
   180
	 (b->type == ASIdOrRange_range && b->u.range != NULL &&
sl@0
   181
	  b->u.range->min != NULL && b->u.range->max != NULL));
sl@0
   182
sl@0
   183
  if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id)
sl@0
   184
    return ASN1_INTEGER_cmp(a->u.id, b->u.id);
sl@0
   185
sl@0
   186
  if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) {
sl@0
   187
    int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min);
sl@0
   188
    return r != 0 ? r : ASN1_INTEGER_cmp(a->u.range->max, b->u.range->max);
sl@0
   189
  }
sl@0
   190
sl@0
   191
  if (a->type == ASIdOrRange_id)
sl@0
   192
    return ASN1_INTEGER_cmp(a->u.id, b->u.range->min);
sl@0
   193
  else
sl@0
   194
    return ASN1_INTEGER_cmp(a->u.range->min, b->u.id);
sl@0
   195
}
sl@0
   196
sl@0
   197
/*
sl@0
   198
 * Add an inherit element.
sl@0
   199
 */
sl@0
   200
int v3_asid_add_inherit(ASIdentifiers *asid, int which)
sl@0
   201
{
sl@0
   202
  ASIdentifierChoice **choice;
sl@0
   203
  if (asid == NULL)
sl@0
   204
    return 0;
sl@0
   205
  switch (which) {
sl@0
   206
  case V3_ASID_ASNUM:
sl@0
   207
    choice = &asid->asnum;
sl@0
   208
    break;
sl@0
   209
  case V3_ASID_RDI:
sl@0
   210
    choice = &asid->rdi;
sl@0
   211
    break;
sl@0
   212
  default:
sl@0
   213
    return 0;
sl@0
   214
  }
sl@0
   215
  if (*choice == NULL) {
sl@0
   216
    if ((*choice = ASIdentifierChoice_new()) == NULL)
sl@0
   217
      return 0;
sl@0
   218
    assert((*choice)->u.inherit == NULL);
sl@0
   219
    if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL)
sl@0
   220
      return 0;
sl@0
   221
    (*choice)->type = ASIdentifierChoice_inherit;
sl@0
   222
  }
sl@0
   223
  return (*choice)->type == ASIdentifierChoice_inherit;
sl@0
   224
}
sl@0
   225
sl@0
   226
/*
sl@0
   227
 * Add an ID or range to an ASIdentifierChoice.
sl@0
   228
 */
sl@0
   229
int v3_asid_add_id_or_range(ASIdentifiers *asid,
sl@0
   230
			    int which,
sl@0
   231
			    ASN1_INTEGER *min,
sl@0
   232
			    ASN1_INTEGER *max)
sl@0
   233
{
sl@0
   234
  ASIdentifierChoice **choice;
sl@0
   235
  ASIdOrRange *aor;
sl@0
   236
  if (asid == NULL)
sl@0
   237
    return 0;
sl@0
   238
  switch (which) {
sl@0
   239
  case V3_ASID_ASNUM:
sl@0
   240
    choice = &asid->asnum;
sl@0
   241
    break;
sl@0
   242
  case V3_ASID_RDI:
sl@0
   243
    choice = &asid->rdi;
sl@0
   244
    break;
sl@0
   245
  default:
sl@0
   246
    return 0;
sl@0
   247
  }
sl@0
   248
  if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit)
sl@0
   249
    return 0;
sl@0
   250
  if (*choice == NULL) {
sl@0
   251
    if ((*choice = ASIdentifierChoice_new()) == NULL)
sl@0
   252
      return 0;
sl@0
   253
    assert((*choice)->u.asIdsOrRanges == NULL);
sl@0
   254
    (*choice)->u.asIdsOrRanges = sk_ASIdOrRange_new(ASIdOrRange_cmp);
sl@0
   255
    if ((*choice)->u.asIdsOrRanges == NULL)
sl@0
   256
      return 0;
sl@0
   257
    (*choice)->type = ASIdentifierChoice_asIdsOrRanges;
sl@0
   258
  }
sl@0
   259
  if ((aor = ASIdOrRange_new()) == NULL)
sl@0
   260
    return 0;
sl@0
   261
  if (max == NULL) {
sl@0
   262
    aor->type = ASIdOrRange_id;
sl@0
   263
    aor->u.id = min;
sl@0
   264
  } else {
sl@0
   265
    aor->type = ASIdOrRange_range;
sl@0
   266
    if ((aor->u.range = ASRange_new()) == NULL)
sl@0
   267
      goto err;
sl@0
   268
    ASN1_INTEGER_free(aor->u.range->min);
sl@0
   269
    aor->u.range->min = min;
sl@0
   270
    ASN1_INTEGER_free(aor->u.range->max);
sl@0
   271
    aor->u.range->max = max;
sl@0
   272
  }
sl@0
   273
  if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))
sl@0
   274
    goto err;
sl@0
   275
  return 1;
sl@0
   276
sl@0
   277
 err:
sl@0
   278
  ASIdOrRange_free(aor);
sl@0
   279
  return 0;
sl@0
   280
}
sl@0
   281
sl@0
   282
/*
sl@0
   283
 * Extract min and max values from an ASIdOrRange.
sl@0
   284
 */
sl@0
   285
static void extract_min_max(ASIdOrRange *aor,
sl@0
   286
			    ASN1_INTEGER **min,
sl@0
   287
			    ASN1_INTEGER **max)
sl@0
   288
{
sl@0
   289
  assert(aor != NULL && min != NULL && max != NULL);
sl@0
   290
  switch (aor->type) {
sl@0
   291
  case ASIdOrRange_id:
sl@0
   292
    *min = aor->u.id;
sl@0
   293
    *max = aor->u.id;
sl@0
   294
    return;
sl@0
   295
  case ASIdOrRange_range:
sl@0
   296
    *min = aor->u.range->min;
sl@0
   297
    *max = aor->u.range->max;
sl@0
   298
    return;
sl@0
   299
  }
sl@0
   300
}
sl@0
   301
sl@0
   302
/*
sl@0
   303
 * Check whether an ASIdentifierChoice is in canonical form.
sl@0
   304
 */
sl@0
   305
static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)
sl@0
   306
{
sl@0
   307
  ASN1_INTEGER *a_max_plus_one = NULL;
sl@0
   308
  BIGNUM *bn = NULL;
sl@0
   309
  int i, ret = 0;
sl@0
   310
sl@0
   311
  /*
sl@0
   312
   * Empty element or inheritance is canonical.
sl@0
   313
   */
sl@0
   314
  if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
sl@0
   315
    return 1;
sl@0
   316
sl@0
   317
  /*
sl@0
   318
   * If not a list, or if empty list, it's broken.
sl@0
   319
   */
sl@0
   320
  if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
sl@0
   321
      sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)
sl@0
   322
    return 0;
sl@0
   323
sl@0
   324
  /*
sl@0
   325
   * It's a list, check it.
sl@0
   326
   */
sl@0
   327
  for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
sl@0
   328
    ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
sl@0
   329
    ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
sl@0
   330
    ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;
sl@0
   331
sl@0
   332
    extract_min_max(a, &a_min, &a_max);
sl@0
   333
    extract_min_max(b, &b_min, &b_max);
sl@0
   334
sl@0
   335
    /*
sl@0
   336
     * Punt misordered list, overlapping start, or inverted range.
sl@0
   337
     */
sl@0
   338
    if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 ||
sl@0
   339
	ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
sl@0
   340
	ASN1_INTEGER_cmp(b_min, b_max) > 0)
sl@0
   341
      goto done;
sl@0
   342
sl@0
   343
    /*
sl@0
   344
     * Calculate a_max + 1 to check for adjacency.
sl@0
   345
     */
sl@0
   346
    if ((bn == NULL && (bn = BN_new()) == NULL) ||
sl@0
   347
	ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
sl@0
   348
	!BN_add_word(bn, 1) ||
sl@0
   349
	(a_max_plus_one = BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
sl@0
   350
      X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,
sl@0
   351
		ERR_R_MALLOC_FAILURE);
sl@0
   352
      goto done;
sl@0
   353
    }
sl@0
   354
    
sl@0
   355
    /*
sl@0
   356
     * Punt if adjacent or overlapping.
sl@0
   357
     */
sl@0
   358
    if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0)
sl@0
   359
      goto done;
sl@0
   360
  }
sl@0
   361
sl@0
   362
  ret = 1;
sl@0
   363
sl@0
   364
 done:
sl@0
   365
  ASN1_INTEGER_free(a_max_plus_one);
sl@0
   366
  BN_free(bn);
sl@0
   367
  return ret;
sl@0
   368
}
sl@0
   369
sl@0
   370
/*
sl@0
   371
 * Check whether an ASIdentifier extension is in canonical form.
sl@0
   372
 */
sl@0
   373
int v3_asid_is_canonical(ASIdentifiers *asid)
sl@0
   374
{
sl@0
   375
  return (asid == NULL ||
sl@0
   376
	  (ASIdentifierChoice_is_canonical(asid->asnum) ||
sl@0
   377
	   ASIdentifierChoice_is_canonical(asid->rdi)));
sl@0
   378
}
sl@0
   379
sl@0
   380
/*
sl@0
   381
 * Whack an ASIdentifierChoice into canonical form.
sl@0
   382
 */
sl@0
   383
static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
sl@0
   384
{
sl@0
   385
  ASN1_INTEGER *a_max_plus_one = NULL;
sl@0
   386
  BIGNUM *bn = NULL;
sl@0
   387
  int i, ret = 0;
sl@0
   388
sl@0
   389
  /*
sl@0
   390
   * Nothing to do for empty element or inheritance.
sl@0
   391
   */
sl@0
   392
  if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
sl@0
   393
    return 1;
sl@0
   394
sl@0
   395
  /*
sl@0
   396
   * We have a list.  Sort it.
sl@0
   397
   */
sl@0
   398
  assert(choice->type == ASIdentifierChoice_asIdsOrRanges);
sl@0
   399
  sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
sl@0
   400
sl@0
   401
  /*
sl@0
   402
   * Now check for errors and suboptimal encoding, rejecting the
sl@0
   403
   * former and fixing the latter.
sl@0
   404
   */
sl@0
   405
  for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
sl@0
   406
    ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
sl@0
   407
    ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
sl@0
   408
    ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;
sl@0
   409
sl@0
   410
    extract_min_max(a, &a_min, &a_max);
sl@0
   411
    extract_min_max(b, &b_min, &b_max);
sl@0
   412
sl@0
   413
    /*
sl@0
   414
     * Make sure we're properly sorted (paranoia).
sl@0
   415
     */
sl@0
   416
    assert(ASN1_INTEGER_cmp(a_min, b_min) <= 0);
sl@0
   417
sl@0
   418
    /*
sl@0
   419
     * Check for overlaps.
sl@0
   420
     */
sl@0
   421
    if (ASN1_INTEGER_cmp(a_max, b_min) >= 0) {
sl@0
   422
      X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
sl@0
   423
		X509V3_R_EXTENSION_VALUE_ERROR);
sl@0
   424
      goto done;
sl@0
   425
    }
sl@0
   426
sl@0
   427
    /*
sl@0
   428
     * Calculate a_max + 1 to check for adjacency.
sl@0
   429
     */
sl@0
   430
    if ((bn == NULL && (bn = BN_new()) == NULL) ||
sl@0
   431
	ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
sl@0
   432
	!BN_add_word(bn, 1) ||
sl@0
   433
	(a_max_plus_one = BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
sl@0
   434
      X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE, ERR_R_MALLOC_FAILURE);
sl@0
   435
      goto done;
sl@0
   436
    }
sl@0
   437
    
sl@0
   438
    /*
sl@0
   439
     * If a and b are adjacent, merge them.
sl@0
   440
     */
sl@0
   441
    if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) == 0) {
sl@0
   442
      ASRange *r;
sl@0
   443
      switch (a->type) {
sl@0
   444
      case ASIdOrRange_id:
sl@0
   445
	if ((r = OPENSSL_malloc(sizeof(ASRange))) == NULL) {
sl@0
   446
	  X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
sl@0
   447
		    ERR_R_MALLOC_FAILURE);
sl@0
   448
	  goto done;
sl@0
   449
	}
sl@0
   450
	r->min = a_min;
sl@0
   451
	r->max = b_max;
sl@0
   452
	a->type = ASIdOrRange_range;
sl@0
   453
	a->u.range = r;
sl@0
   454
	break;
sl@0
   455
      case ASIdOrRange_range:
sl@0
   456
	ASN1_INTEGER_free(a->u.range->max);
sl@0
   457
	a->u.range->max = b_max;
sl@0
   458
	break;
sl@0
   459
      }
sl@0
   460
      switch (b->type) {
sl@0
   461
      case ASIdOrRange_id:
sl@0
   462
	b->u.id = NULL;
sl@0
   463
	break;
sl@0
   464
      case ASIdOrRange_range:
sl@0
   465
	b->u.range->max = NULL;
sl@0
   466
	break;
sl@0
   467
      }
sl@0
   468
      ASIdOrRange_free(b);
sl@0
   469
      sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i + 1);
sl@0
   470
      i--;
sl@0
   471
      continue;
sl@0
   472
    }
sl@0
   473
  }
sl@0
   474
sl@0
   475
  assert(ASIdentifierChoice_is_canonical(choice)); /* Paranoia */
sl@0
   476
sl@0
   477
  ret = 1;
sl@0
   478
sl@0
   479
 done:
sl@0
   480
  ASN1_INTEGER_free(a_max_plus_one);
sl@0
   481
  BN_free(bn);
sl@0
   482
  return ret;
sl@0
   483
}
sl@0
   484
sl@0
   485
/*
sl@0
   486
 * Whack an ASIdentifier extension into canonical form.
sl@0
   487
 */
sl@0
   488
int v3_asid_canonize(ASIdentifiers *asid)
sl@0
   489
{
sl@0
   490
  return (asid == NULL ||
sl@0
   491
	  (ASIdentifierChoice_canonize(asid->asnum) &&
sl@0
   492
	   ASIdentifierChoice_canonize(asid->rdi)));
sl@0
   493
}
sl@0
   494
sl@0
   495
/*
sl@0
   496
 * v2i method for an ASIdentifier extension.
sl@0
   497
 */
sl@0
   498
static void *v2i_ASIdentifiers(struct v3_ext_method *method,
sl@0
   499
			       struct v3_ext_ctx *ctx,
sl@0
   500
			       STACK_OF(CONF_VALUE) *values)
sl@0
   501
{
sl@0
   502
  ASIdentifiers *asid = NULL;
sl@0
   503
  int i;
sl@0
   504
sl@0
   505
  if ((asid = ASIdentifiers_new()) == NULL) {
sl@0
   506
    X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
sl@0
   507
    return NULL;
sl@0
   508
  }
sl@0
   509
sl@0
   510
  for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
sl@0
   511
    CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
sl@0
   512
    ASN1_INTEGER *min = NULL, *max = NULL;
sl@0
   513
    int i1, i2, i3, is_range, which;
sl@0
   514
sl@0
   515
    /*
sl@0
   516
     * Figure out whether this is an AS or an RDI.
sl@0
   517
     */
sl@0
   518
    if (       !name_cmp(val->name, "AS")) {
sl@0
   519
      which = V3_ASID_ASNUM;
sl@0
   520
    } else if (!name_cmp(val->name, "RDI")) {
sl@0
   521
      which = V3_ASID_RDI;
sl@0
   522
    } else {
sl@0
   523
      X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_EXTENSION_NAME_ERROR);
sl@0
   524
      X509V3_conf_err(val);
sl@0
   525
      goto err;
sl@0
   526
    }
sl@0
   527
sl@0
   528
    /*
sl@0
   529
     * Handle inheritance.
sl@0
   530
     */
sl@0
   531
    if (!strcmp(val->value, "inherit")) {
sl@0
   532
      if (v3_asid_add_inherit(asid, which))
sl@0
   533
	continue;
sl@0
   534
      X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_INHERITANCE);
sl@0
   535
      X509V3_conf_err(val);
sl@0
   536
      goto err;
sl@0
   537
    }
sl@0
   538
sl@0
   539
    /*
sl@0
   540
     * Number, range, or mistake, pick it apart and figure out which.
sl@0
   541
     */
sl@0
   542
    i1 = strspn(val->value, "0123456789");
sl@0
   543
    if (val->value[i1] == '\0') {
sl@0
   544
      is_range = 0;
sl@0
   545
    } else {
sl@0
   546
      is_range = 1;
sl@0
   547
      i2 = i1 + strspn(val->value + i1, " \t");
sl@0
   548
      if (val->value[i2] != '-') {
sl@0
   549
	X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_ASNUMBER);
sl@0
   550
	X509V3_conf_err(val);
sl@0
   551
	goto err;
sl@0
   552
      }
sl@0
   553
      i2++;
sl@0
   554
      i2 = i2 + strspn(val->value + i2, " \t");
sl@0
   555
      i3 = i2 + strspn(val->value + i2, "0123456789");
sl@0
   556
      if (val->value[i3] != '\0') {
sl@0
   557
	X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_ASRANGE);
sl@0
   558
	X509V3_conf_err(val);
sl@0
   559
	goto err;
sl@0
   560
      }
sl@0
   561
    }
sl@0
   562
sl@0
   563
    /*
sl@0
   564
     * Syntax is ok, read and add it.
sl@0
   565
     */
sl@0
   566
    if (!is_range) {
sl@0
   567
      if (!X509V3_get_value_int(val, &min)) {
sl@0
   568
	X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
sl@0
   569
	goto err;
sl@0
   570
      }
sl@0
   571
    } else {
sl@0
   572
      char *s = BUF_strdup(val->value);
sl@0
   573
      if (s == NULL) {
sl@0
   574
	X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
sl@0
   575
	goto err;
sl@0
   576
      }
sl@0
   577
      s[i1] = '\0';
sl@0
   578
      min = s2i_ASN1_INTEGER(NULL, s);
sl@0
   579
      max = s2i_ASN1_INTEGER(NULL, s + i2);
sl@0
   580
      OPENSSL_free(s);
sl@0
   581
      if (min == NULL || max == NULL) {
sl@0
   582
	ASN1_INTEGER_free(min);
sl@0
   583
	ASN1_INTEGER_free(max);
sl@0
   584
	X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
sl@0
   585
	goto err;
sl@0
   586
      }
sl@0
   587
    }
sl@0
   588
    if (!v3_asid_add_id_or_range(asid, which, min, max)) {
sl@0
   589
      ASN1_INTEGER_free(min);
sl@0
   590
      ASN1_INTEGER_free(max);
sl@0
   591
      X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
sl@0
   592
      goto err;
sl@0
   593
    }
sl@0
   594
  }
sl@0
   595
sl@0
   596
  /*
sl@0
   597
   * Canonize the result, then we're done.
sl@0
   598
   */
sl@0
   599
  if (!v3_asid_canonize(asid))
sl@0
   600
    goto err;
sl@0
   601
  return asid;
sl@0
   602
sl@0
   603
 err:
sl@0
   604
  ASIdentifiers_free(asid);
sl@0
   605
  return NULL;
sl@0
   606
}
sl@0
   607
sl@0
   608
/*
sl@0
   609
 * OpenSSL dispatch.
sl@0
   610
 */
sl@0
   611
const X509V3_EXT_METHOD v3_asid = {
sl@0
   612
  NID_sbgp_autonomousSysNum,	/* nid */
sl@0
   613
  0,				/* flags */
sl@0
   614
  ASN1_ITEM_ref(ASIdentifiers),	/* template */
sl@0
   615
  0, 0, 0, 0,			/* old functions, ignored */
sl@0
   616
  0,				/* i2s */
sl@0
   617
  0,				/* s2i */
sl@0
   618
  0,				/* i2v */
sl@0
   619
  v2i_ASIdentifiers,		/* v2i */
sl@0
   620
  i2r_ASIdentifiers,		/* i2r */
sl@0
   621
  0,				/* r2i */
sl@0
   622
  NULL				/* extension-specific data */
sl@0
   623
};
sl@0
   624
sl@0
   625
/*
sl@0
   626
 * Figure out whether extension uses inheritance.
sl@0
   627
 */
sl@0
   628
int v3_asid_inherits(ASIdentifiers *asid)
sl@0
   629
{
sl@0
   630
  return (asid != NULL &&
sl@0
   631
	  ((asid->asnum != NULL &&
sl@0
   632
	    asid->asnum->type == ASIdentifierChoice_inherit) ||
sl@0
   633
	   (asid->rdi != NULL &&
sl@0
   634
	    asid->rdi->type == ASIdentifierChoice_inherit)));
sl@0
   635
}
sl@0
   636
sl@0
   637
/*
sl@0
   638
 * Figure out whether parent contains child.
sl@0
   639
 */
sl@0
   640
static int asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child)
sl@0
   641
{
sl@0
   642
  ASN1_INTEGER *p_min, *p_max, *c_min, *c_max;
sl@0
   643
  int p, c;
sl@0
   644
sl@0
   645
  if (child == NULL || parent == child)
sl@0
   646
    return 1;
sl@0
   647
  if (parent == NULL)
sl@0
   648
    return 0;
sl@0
   649
sl@0
   650
  p = 0;
sl@0
   651
  for (c = 0; c < sk_ASIdOrRange_num(child); c++) {
sl@0
   652
    extract_min_max(sk_ASIdOrRange_value(child, c), &c_min, &c_max);
sl@0
   653
    for (;; p++) {
sl@0
   654
      if (p >= sk_ASIdOrRange_num(parent))
sl@0
   655
	return 0;
sl@0
   656
      extract_min_max(sk_ASIdOrRange_value(parent, p), &p_min, &p_max);
sl@0
   657
      if (ASN1_INTEGER_cmp(p_max, c_max) < 0)
sl@0
   658
	continue;
sl@0
   659
      if (ASN1_INTEGER_cmp(p_min, c_min) > 0)
sl@0
   660
	return 0;
sl@0
   661
      break;
sl@0
   662
    }
sl@0
   663
  }
sl@0
   664
sl@0
   665
  return 1;
sl@0
   666
}
sl@0
   667
sl@0
   668
/*
sl@0
   669
 * Test whether a is a subet of b.
sl@0
   670
 */
sl@0
   671
int v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b)
sl@0
   672
{
sl@0
   673
  return (a == NULL ||
sl@0
   674
	  a == b ||
sl@0
   675
	  (b != NULL &&
sl@0
   676
	   !v3_asid_inherits(a) &&
sl@0
   677
	   !v3_asid_inherits(b) &&
sl@0
   678
	   asid_contains(b->asnum->u.asIdsOrRanges,
sl@0
   679
			 a->asnum->u.asIdsOrRanges) &&
sl@0
   680
	   asid_contains(b->rdi->u.asIdsOrRanges,
sl@0
   681
			 a->rdi->u.asIdsOrRanges)));
sl@0
   682
}
sl@0
   683
sl@0
   684
/*
sl@0
   685
 * Validation error handling via callback.
sl@0
   686
 */
sl@0
   687
#define validation_err(_err_)		\
sl@0
   688
  do {					\
sl@0
   689
    if (ctx != NULL) {			\
sl@0
   690
      ctx->error = _err_;		\
sl@0
   691
      ctx->error_depth = i;		\
sl@0
   692
      ctx->current_cert = x;		\
sl@0
   693
      ret = ctx->verify_cb(0, ctx);	\
sl@0
   694
    } else {				\
sl@0
   695
      ret = 0;				\
sl@0
   696
    }					\
sl@0
   697
    if (!ret)				\
sl@0
   698
      goto done;			\
sl@0
   699
  } while (0)
sl@0
   700
sl@0
   701
/*
sl@0
   702
 * Core code for RFC 3779 3.3 path validation.
sl@0
   703
 */
sl@0
   704
static int v3_asid_validate_path_internal(X509_STORE_CTX *ctx,
sl@0
   705
					  STACK_OF(X509) *chain,
sl@0
   706
					  ASIdentifiers *ext)
sl@0
   707
{
sl@0
   708
  ASIdOrRanges *child_as = NULL, *child_rdi = NULL;
sl@0
   709
  int i, ret = 1, inherit_as = 0, inherit_rdi = 0;
sl@0
   710
  X509 *x = NULL;
sl@0
   711
sl@0
   712
  assert(chain != NULL && sk_X509_num(chain) > 0);
sl@0
   713
  assert(ctx != NULL || ext != NULL);
sl@0
   714
  assert(ctx == NULL || ctx->verify_cb != NULL);
sl@0
   715
sl@0
   716
  /*
sl@0
   717
   * Figure out where to start.  If we don't have an extension to
sl@0
   718
   * check, we're done.  Otherwise, check canonical form and
sl@0
   719
   * set up for walking up the chain.
sl@0
   720
   */
sl@0
   721
  if (ext != NULL) {
sl@0
   722
    i = -1;
sl@0
   723
  } else {
sl@0
   724
    i = 0;
sl@0
   725
    x = sk_X509_value(chain, i);
sl@0
   726
    assert(x != NULL);
sl@0
   727
    if ((ext = x->rfc3779_asid) == NULL)
sl@0
   728
      goto done;
sl@0
   729
  }
sl@0
   730
  if (!v3_asid_is_canonical(ext))
sl@0
   731
    validation_err(X509_V_ERR_INVALID_EXTENSION);
sl@0
   732
  if (ext->asnum != NULL)  {
sl@0
   733
    switch (ext->asnum->type) {
sl@0
   734
    case ASIdentifierChoice_inherit:
sl@0
   735
      inherit_as = 1;
sl@0
   736
      break;
sl@0
   737
    case ASIdentifierChoice_asIdsOrRanges:
sl@0
   738
      child_as = ext->asnum->u.asIdsOrRanges;
sl@0
   739
      break;
sl@0
   740
    }
sl@0
   741
  }
sl@0
   742
  if (ext->rdi != NULL) {
sl@0
   743
    switch (ext->rdi->type) {
sl@0
   744
    case ASIdentifierChoice_inherit:
sl@0
   745
      inherit_rdi = 1;
sl@0
   746
      break;
sl@0
   747
    case ASIdentifierChoice_asIdsOrRanges:
sl@0
   748
      child_rdi = ext->rdi->u.asIdsOrRanges;
sl@0
   749
      break;
sl@0
   750
    }
sl@0
   751
  }
sl@0
   752
sl@0
   753
  /*
sl@0
   754
   * Now walk up the chain.  Extensions must be in canonical form, no
sl@0
   755
   * cert may list resources that its parent doesn't list.
sl@0
   756
   */
sl@0
   757
  for (i++; i < sk_X509_num(chain); i++) {
sl@0
   758
    x = sk_X509_value(chain, i);
sl@0
   759
    assert(x != NULL);
sl@0
   760
    if (x->rfc3779_asid == NULL) {
sl@0
   761
      if (child_as != NULL || child_rdi != NULL)
sl@0
   762
	validation_err(X509_V_ERR_UNNESTED_RESOURCE);
sl@0
   763
      continue;
sl@0
   764
    }
sl@0
   765
    if (!v3_asid_is_canonical(x->rfc3779_asid))
sl@0
   766
      validation_err(X509_V_ERR_INVALID_EXTENSION);
sl@0
   767
    if (x->rfc3779_asid->asnum == NULL && child_as != NULL) {
sl@0
   768
      validation_err(X509_V_ERR_UNNESTED_RESOURCE);
sl@0
   769
      child_as = NULL;
sl@0
   770
      inherit_as = 0;
sl@0
   771
    }
sl@0
   772
    if (x->rfc3779_asid->asnum != NULL &&
sl@0
   773
	x->rfc3779_asid->asnum->type == ASIdentifierChoice_asIdsOrRanges) {
sl@0
   774
      if (inherit_as ||
sl@0
   775
	  asid_contains(x->rfc3779_asid->asnum->u.asIdsOrRanges, child_as)) {
sl@0
   776
	child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges;
sl@0
   777
	inherit_as = 0;
sl@0
   778
      } else {
sl@0
   779
	validation_err(X509_V_ERR_UNNESTED_RESOURCE);
sl@0
   780
      }
sl@0
   781
    }
sl@0
   782
    if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) {
sl@0
   783
      validation_err(X509_V_ERR_UNNESTED_RESOURCE);
sl@0
   784
      child_rdi = NULL;
sl@0
   785
      inherit_rdi = 0;
sl@0
   786
    }
sl@0
   787
    if (x->rfc3779_asid->rdi != NULL &&
sl@0
   788
	x->rfc3779_asid->rdi->type == ASIdentifierChoice_asIdsOrRanges) {
sl@0
   789
      if (inherit_rdi ||
sl@0
   790
	  asid_contains(x->rfc3779_asid->rdi->u.asIdsOrRanges, child_rdi)) {
sl@0
   791
	child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges;
sl@0
   792
	inherit_rdi = 0;
sl@0
   793
      } else {
sl@0
   794
	validation_err(X509_V_ERR_UNNESTED_RESOURCE);
sl@0
   795
      }
sl@0
   796
    }
sl@0
   797
  }
sl@0
   798
sl@0
   799
  /*
sl@0
   800
   * Trust anchor can't inherit.
sl@0
   801
   */
sl@0
   802
  if (x->rfc3779_asid != NULL) {
sl@0
   803
    if (x->rfc3779_asid->asnum != NULL &&
sl@0
   804
	x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit)
sl@0
   805
      validation_err(X509_V_ERR_UNNESTED_RESOURCE);
sl@0
   806
    if (x->rfc3779_asid->rdi != NULL &&
sl@0
   807
	x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit)
sl@0
   808
      validation_err(X509_V_ERR_UNNESTED_RESOURCE);
sl@0
   809
  }
sl@0
   810
sl@0
   811
 done:
sl@0
   812
  return ret;
sl@0
   813
}
sl@0
   814
sl@0
   815
#undef validation_err
sl@0
   816
sl@0
   817
/*
sl@0
   818
 * RFC 3779 3.3 path validation -- called from X509_verify_cert().
sl@0
   819
 */
sl@0
   820
int v3_asid_validate_path(X509_STORE_CTX *ctx)
sl@0
   821
{
sl@0
   822
  return v3_asid_validate_path_internal(ctx, ctx->chain, NULL);
sl@0
   823
}
sl@0
   824
sl@0
   825
/*
sl@0
   826
 * RFC 3779 3.3 path validation of an extension.
sl@0
   827
 * Test whether chain covers extension.
sl@0
   828
 */
sl@0
   829
int v3_asid_validate_resource_set(STACK_OF(X509) *chain,
sl@0
   830
				  ASIdentifiers *ext,
sl@0
   831
				  int allow_inheritance)
sl@0
   832
{
sl@0
   833
  if (ext == NULL)
sl@0
   834
    return 1;
sl@0
   835
  if (chain == NULL || sk_X509_num(chain) == 0)
sl@0
   836
    return 0;
sl@0
   837
  if (!allow_inheritance && v3_asid_inherits(ext))
sl@0
   838
    return 0;
sl@0
   839
  return v3_asid_validate_path_internal(NULL, chain, ext);
sl@0
   840
}
sl@0
   841
sl@0
   842
#endif /* OPENSSL_NO_RFC3779 */