Coverage Report

Created: 2018-08-29 13:53

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