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