Coverage Report

Created: 2023-06-08 06:40

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