Coverage Report

Created: 2025-12-31 06:58

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