Coverage Report

Created: 2026-08-18 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/v3_asid.c
Line
Count
Source
1
/*
2
 * Copyright 2006-2026 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
0
} ASN1_SEQUENCE_END(ASRange)
38
0
39
0
ASN1_CHOICE(ASIdOrRange) = {
40
0
    ASN1_SIMPLE(ASIdOrRange, u.id, ASN1_INTEGER),
41
0
    ASN1_SIMPLE(ASIdOrRange, u.range, ASRange)
42
0
} ASN1_CHOICE_END(ASIdOrRange)
43
0
44
0
ASN1_CHOICE(ASIdentifierChoice) = {
45
0
    ASN1_SIMPLE(ASIdentifierChoice, u.inherit, ASN1_NULL),
46
0
    ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange)
47
0
} ASN1_CHOICE_END(ASIdentifierChoice)
48
0
49
0
ASN1_SEQUENCE(ASIdentifiers) = {
50
0
    ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0),
51
0
    ASN1_EXP_OPT(ASIdentifiers, rdi, ASIdentifierChoice, 1)
52
0
} ASN1_SEQUENCE_END(ASIdentifiers)
53
0
54
0
IMPLEMENT_ASN1_FUNCTIONS(ASRange)
55
0
IMPLEMENT_ASN1_FUNCTIONS(ASIdOrRange)
56
0
IMPLEMENT_ASN1_FUNCTIONS(ASIdentifierChoice)
57
0
IMPLEMENT_ASN1_FUNCTIONS(ASIdentifiers)
58
0
59
0
/*
60
0
 * i2r method for an ASIdentifierChoice.
61
0
 */
62
0
static int i2r_ASIdentifierChoice(BIO *out,
63
0
    ASIdentifierChoice *choice,
64
0
    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 = 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
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
0
{
262
0
    ASN1_INTEGER *a_max_plus_one = NULL;
263
0
    ASN1_INTEGER *orig;
264
0
    BIGNUM *bn = NULL;
265
0
    int i, ret = 0;
266
267
    /*
268
     * Empty element or inheritance is canonical.
269
     */
270
0
    if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
271
0
        return 1;
272
273
    /*
274
     * If not a list, or if empty list, it's broken.
275
     */
276
0
    if (choice->type != ASIdentifierChoice_asIdsOrRanges || sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)
277
0
        return 0;
278
279
    /*
280
     * It's a list, check it.
281
     */
282
0
    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_BN_LIB);
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_ASN1_LIB);
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
0
    i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
322
0
    {
323
0
        ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
324
0
        ASN1_INTEGER *a_min, *a_max;
325
0
        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
0
    }
331
332
0
    ret = 1;
333
334
0
done:
335
0
    ASN1_INTEGER_free(a_max_plus_one);
336
0
    BN_free(bn);
337
0
    return ret;
338
0
}
339
340
/*
341
 * Check whether an ASIdentifier extension is in canonical form.
342
 */
343
int X509v3_asid_is_canonical(ASIdentifiers *asid)
344
0
{
345
0
    return (asid == NULL || (ASIdentifierChoice_is_canonical(asid->asnum) && ASIdentifierChoice_is_canonical(asid->rdi)));
346
0
}
347
348
/*
349
 * Whack an ASIdentifierChoice into canonical form.
350
 *
351
 * After the initial sort, the merge runs as a single linear sweep
352
 * over the list using a write index.  Each entry is examined once;
353
 * adjacent / mergeable entries extend the previous output's upper
354
 * bound in O(1) and the consumed source slot is recorded as NULL.
355
 * Total cost is O(N log N) sort + O(N) merge, with no stack deletes
356
 * inside the loop.
357
 *
358
 * The NULL slots are closed up by a single compaction pass at the end,
359
 * which runs whether the sweep succeeded or failed.  This guarantees
360
 * the live stack is always left hole-free, with every occupied slot
361
 * non-NULL, so on error the caller may safely inspect, print, encode,
362
 * free, or retry canonize on the object.  The sort comparator
363
 * dereferences every slot with no NULL guard, and is_canonical's call
364
 * to extract_min_max would hit its ossl_assert(aor != NULL) on a hole.
365
 */
366
static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
367
0
{
368
0
    ASN1_INTEGER *a_max_plus_one = NULL;
369
0
    ASN1_INTEGER *orig;
370
0
    BIGNUM *bn = NULL;
371
0
    int read, write = 0, n;
372
0
    int ret = 0;
373
374
    /*
375
     * Nothing to do for empty element or inheritance.
376
     */
377
0
    if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
378
0
        return 1;
379
380
    /*
381
     * If not a list, or if empty list, it's broken.
382
     */
383
0
    if (choice->type != ASIdentifierChoice_asIdsOrRanges || sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0) {
384
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
385
0
        return 0;
386
0
    }
387
388
    /*
389
     * Sort the list, then merge in a single sweep using a write index.
390
     */
391
0
    sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
392
0
    n = sk_ASIdOrRange_num(choice->u.asIdsOrRanges);
393
394
0
    for (read = 0; read < n; read++) {
395
0
        ASIdOrRange *cur = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, read);
396
0
        ASN1_INTEGER *c_min = NULL, *c_max = NULL;
397
398
0
        if (!extract_min_max(cur, &c_min, &c_max))
399
0
            goto done;
400
401
        /*
402
         * Punt inverted range.
403
         */
404
0
        if (ASN1_INTEGER_cmp(c_min, c_max) > 0)
405
0
            goto done;
406
407
0
        if (write > 0) {
408
0
            ASIdOrRange *prev = sk_ASIdOrRange_value(choice->u.asIdsOrRanges,
409
0
                write - 1);
410
0
            ASN1_INTEGER *p_min = NULL, *p_max = NULL;
411
412
0
            if (!extract_min_max(prev, &p_min, &p_max))
413
0
                goto done;
414
415
            /*
416
             * Make sure we're properly sorted (paranoia).
417
             */
418
0
            if (!ossl_assert(ASN1_INTEGER_cmp(p_min, c_min) <= 0))
419
0
                goto done;
420
421
            /*
422
             * Reject overlap with the previous accepted entry.
423
             */
424
0
            if (ASN1_INTEGER_cmp(p_max, c_min) >= 0) {
425
0
                ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
426
0
                goto done;
427
0
            }
428
429
            /*
430
             * Calculate p_max + 1 to check for adjacency.
431
             */
432
0
            if ((bn == NULL && (bn = BN_new()) == NULL)
433
0
                || ASN1_INTEGER_to_BN(p_max, bn) == NULL
434
0
                || !BN_add_word(bn, 1)) {
435
0
                ERR_raise(ERR_LIB_X509V3, ERR_R_BN_LIB);
436
0
                goto done;
437
0
            }
438
0
            if ((a_max_plus_one = BN_to_ASN1_INTEGER(bn,
439
0
                     orig = a_max_plus_one))
440
0
                == NULL) {
441
0
                a_max_plus_one = orig;
442
0
                ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
443
0
                goto done;
444
0
            }
445
446
            /*
447
             * If prev and cur are adjacent, fold cur into prev.
448
             */
449
0
            if (ASN1_INTEGER_cmp(a_max_plus_one, c_min) == 0) {
450
0
                ASRange *r;
451
452
0
                switch (prev->type) {
453
0
                case ASIdOrRange_id:
454
0
                    if ((r = OPENSSL_malloc(sizeof(*r))) == NULL)
455
0
                        goto done;
456
0
                    r->min = p_min;
457
0
                    r->max = c_max;
458
0
                    prev->type = ASIdOrRange_range;
459
0
                    prev->u.range = r;
460
0
                    break;
461
0
                case ASIdOrRange_range:
462
0
                    ASN1_INTEGER_free(prev->u.range->max);
463
0
                    prev->u.range->max = c_max;
464
0
                    break;
465
0
                }
466
                /*
467
                 * Detach c_max from cur so freeing cur does not free
468
                 * the value we just transferred to prev.
469
                 */
470
0
                switch (cur->type) {
471
0
                case ASIdOrRange_id:
472
0
                    cur->u.id = NULL;
473
0
                    break;
474
0
                case ASIdOrRange_range:
475
0
                    cur->u.range->max = NULL;
476
0
                    break;
477
0
                }
478
0
                ASIdOrRange_free(cur);
479
                /*
480
                 * Record the consumed source slot as NULL; the epilogue
481
                 * compacts NULLs out of the live stack so it is left
482
                 * hole-free whether we succeed or fail.  We do not
483
                 * advance `write`.
484
                 */
485
0
                (void)sk_ASIdOrRange_set(choice->u.asIdsOrRanges, read, NULL);
486
0
                continue;
487
0
            }
488
0
        }
489
490
        /*
491
         * Keep cur.  Slide it forward into the write slot if we have
492
         * fallen behind, and NULL the source slot to avoid duplicate
493
         * ownership.
494
         */
495
0
        if (write != read) {
496
0
            (void)sk_ASIdOrRange_set(choice->u.asIdsOrRanges, write, cur);
497
0
            (void)sk_ASIdOrRange_set(choice->u.asIdsOrRanges, read, NULL);
498
0
        }
499
0
        write++;
500
0
    }
501
502
0
    ret = 1;
503
504
0
done:
505
    /*
506
     * The sweep above NULLs source slots as it folds entries.  Whether
507
     * we succeeded or bailed out on an error, the stack must be left
508
     * hole-free, with every occupied slot non-NULL, so that the caller
509
     * may inspect, print, encode, free, or even retry canonize on the
510
     * object without dereferencing NULL (the sort comparator in
511
     * particular has no NULL guard).  Slide every non-NULL slot forward
512
     * to close the holes, then pop the vacated tail.  On success this
513
     * collapses [write..n-1] (all NULL) to length `write`; on error it
514
     * removes the possibly empty contiguous run of NULL slots in
515
     * [write, read), preserving the processed output in [0, write) and
516
     * the untouched original entries in [read, n).
517
     */
518
0
    {
519
0
        int w = 0, r;
520
521
0
        for (r = 0; r < sk_ASIdOrRange_num(choice->u.asIdsOrRanges); r++) {
522
0
            ASIdOrRange *v = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, r);
523
524
0
            if (v != NULL) {
525
0
                if (w != r)
526
0
                    (void)sk_ASIdOrRange_set(choice->u.asIdsOrRanges, w, v);
527
0
                w++;
528
0
            }
529
0
        }
530
0
        while (sk_ASIdOrRange_num(choice->u.asIdsOrRanges) > w)
531
0
            (void)sk_ASIdOrRange_pop(choice->u.asIdsOrRanges);
532
0
    }
533
534
0
    if (ret) {
535
        /* Paranoia */
536
0
        if (!ossl_assert(ASIdentifierChoice_is_canonical(choice)))
537
0
            ret = 0;
538
0
    }
539
540
0
    ASN1_INTEGER_free(a_max_plus_one);
541
0
    BN_free(bn);
542
0
    return ret;
543
0
}
544
545
/*
546
 * Whack an ASIdentifier extension into canonical form.
547
 */
548
int X509v3_asid_canonize(ASIdentifiers *asid)
549
0
{
550
0
    return (asid == NULL || (ASIdentifierChoice_canonize(asid->asnum) && ASIdentifierChoice_canonize(asid->rdi)));
551
0
}
552
553
/*
554
 * v2i method for an ASIdentifier extension.
555
 */
556
static void *v2i_ASIdentifiers(const struct v3_ext_method *method,
557
    struct v3_ext_ctx *ctx,
558
    STACK_OF(CONF_VALUE) *values)
559
0
{
560
0
    ASN1_INTEGER *min = NULL, *max = NULL;
561
0
    ASIdentifiers *asid = NULL;
562
0
    int i;
563
564
0
    if ((asid = ASIdentifiers_new()) == NULL) {
565
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);
566
0
        return NULL;
567
0
    }
568
569
0
    for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
570
0
        CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
571
0
        int i1 = 0, i2 = 0, i3 = 0, is_range = 0, which = 0;
572
573
        /*
574
         * Figure out whether this is an AS or an RDI.
575
         */
576
0
        if (!ossl_v3_name_cmp(val->name, "AS")) {
577
0
            which = V3_ASID_ASNUM;
578
0
        } else if (!ossl_v3_name_cmp(val->name, "RDI")) {
579
0
            which = V3_ASID_RDI;
580
0
        } else {
581
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_NAME_ERROR);
582
0
            X509V3_conf_add_error_name_value(val);
583
0
            goto err;
584
0
        }
585
586
0
        if (val->value == NULL) {
587
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
588
0
            goto err;
589
0
        }
590
591
        /*
592
         * Handle inheritance.
593
         */
594
0
        if (strcmp(val->value, "inherit") == 0) {
595
0
            if (X509v3_asid_add_inherit(asid, which))
596
0
                continue;
597
0
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_INHERITANCE);
598
0
            X509V3_conf_add_error_name_value(val);
599
0
            goto err;
600
0
        }
601
602
        /*
603
         * Number, range, or mistake, pick it apart and figure out which.
604
         */
605
0
        i1 = (int)strspn(val->value, "0123456789");
606
0
        if (val->value[i1] == '\0') {
607
0
            is_range = 0;
608
0
        } else {
609
0
            is_range = 1;
610
0
            i2 = i1 + (int)strspn(val->value + i1, " \t");
611
0
            if (val->value[i2] != '-') {
612
0
                ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_ASNUMBER);
613
0
                X509V3_conf_add_error_name_value(val);
614
0
                goto err;
615
0
            }
616
0
            i2++;
617
0
            i2 = i2 + (int)strspn(val->value + i2, " \t");
618
0
            i3 = i2 + (int)strspn(val->value + i2, "0123456789");
619
0
            if (val->value[i3] != '\0') {
620
0
                ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_ASRANGE);
621
0
                X509V3_conf_add_error_name_value(val);
622
0
                goto err;
623
0
            }
624
0
        }
625
626
        /*
627
         * Syntax is ok, read and add it.
628
         */
629
0
        if (!is_range) {
630
0
            if (!X509V3_get_value_int(val, &min)) {
631
0
                ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);
632
0
                goto err;
633
0
            }
634
0
        } else {
635
0
            char *s = OPENSSL_strdup(val->value);
636
0
            if (s == NULL)
637
0
                goto err;
638
0
            s[i1] = '\0';
639
0
            min = s2i_ASN1_INTEGER(NULL, s);
640
0
            max = s2i_ASN1_INTEGER(NULL, s + i2);
641
0
            OPENSSL_free(s);
642
0
            if (min == NULL || max == NULL) {
643
0
                ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);
644
0
                goto err;
645
0
            }
646
0
            if (ASN1_INTEGER_cmp(min, max) > 0) {
647
0
                ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_VALUE_ERROR);
648
0
                goto err;
649
0
            }
650
0
        }
651
0
        if (!X509v3_asid_add_id_or_range(asid, which, min, max)) {
652
0
            ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);
653
0
            goto err;
654
0
        }
655
0
        min = max = NULL;
656
0
    }
657
658
    /*
659
     * Canonize the result, then we're done.
660
     */
661
0
    if (!X509v3_asid_canonize(asid))
662
0
        goto err;
663
0
    return asid;
664
665
0
err:
666
0
    ASIdentifiers_free(asid);
667
0
    ASN1_INTEGER_free(min);
668
0
    ASN1_INTEGER_free(max);
669
0
    return NULL;
670
0
}
671
672
/*
673
 * OpenSSL dispatch.
674
 */
675
const X509V3_EXT_METHOD ossl_v3_asid = {
676
    NID_sbgp_autonomousSysNum, /* nid */
677
    0, /* flags */
678
    ASN1_ITEM_ref(ASIdentifiers), /* template */
679
    0, 0, 0, 0, /* old functions, ignored */
680
    0, /* i2s */
681
    0, /* s2i */
682
    0, /* i2v */
683
    v2i_ASIdentifiers, /* v2i */
684
    i2r_ASIdentifiers, /* i2r */
685
    0, /* r2i */
686
    NULL /* extension-specific data */
687
};
688
689
/*
690
 * Figure out whether extension uses inheritance.
691
 */
692
int X509v3_asid_inherits(ASIdentifiers *asid)
693
0
{
694
0
    return (asid != NULL && ((asid->asnum != NULL && asid->asnum->type == ASIdentifierChoice_inherit) || (asid->rdi != NULL && asid->rdi->type == ASIdentifierChoice_inherit)));
695
0
}
696
697
/*
698
 * Figure out whether parent contains child.
699
 */
700
static int asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child)
701
0
{
702
0
    ASN1_INTEGER *p_min = NULL, *p_max = NULL, *c_min = NULL, *c_max = NULL;
703
0
    int p, c;
704
705
0
    if (child == NULL || parent == child)
706
0
        return 1;
707
0
    if (parent == NULL)
708
0
        return 0;
709
710
0
    p = 0;
711
0
    for (c = 0; c < sk_ASIdOrRange_num(child); c++) {
712
0
        if (!extract_min_max(sk_ASIdOrRange_value(child, c), &c_min, &c_max))
713
0
            return 0;
714
0
        for (;; p++) {
715
0
            if (p >= sk_ASIdOrRange_num(parent))
716
0
                return 0;
717
0
            if (!extract_min_max(sk_ASIdOrRange_value(parent, p), &p_min,
718
0
                    &p_max))
719
0
                return 0;
720
0
            if (ASN1_INTEGER_cmp(p_max, c_max) < 0)
721
0
                continue;
722
0
            if (ASN1_INTEGER_cmp(p_min, c_min) > 0)
723
0
                return 0;
724
0
            break;
725
0
        }
726
0
    }
727
728
0
    return 1;
729
0
}
730
731
/*
732
 * Test whether a is a subset of b.
733
 */
734
int X509v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b)
735
0
{
736
0
    int subset;
737
738
0
    if (a == NULL || a == b)
739
0
        return 1;
740
741
0
    if (b == NULL)
742
0
        return 0;
743
744
0
    if (X509v3_asid_inherits(a) || X509v3_asid_inherits(b))
745
0
        return 0;
746
747
0
    subset = a->asnum == NULL
748
0
        || (b->asnum != NULL
749
0
            && asid_contains(b->asnum->u.asIdsOrRanges,
750
0
                a->asnum->u.asIdsOrRanges));
751
0
    if (!subset)
752
0
        return 0;
753
754
0
    return a->rdi == NULL
755
0
        || (b->rdi != NULL
756
0
            && asid_contains(b->rdi->u.asIdsOrRanges,
757
0
                a->rdi->u.asIdsOrRanges));
758
0
}
759
760
/*
761
 * Validation error handling via callback.
762
 */
763
#define validation_err(_err_)             \
764
0
    do {                                  \
765
0
        if (ctx != NULL) {                \
766
0
            ctx->error = _err_;           \
767
0
            ctx->error_depth = i;         \
768
0
            ctx->current_cert = x;        \
769
0
            ret = ctx->verify_cb(0, ctx); \
770
0
        } else {                          \
771
0
            ret = 0;                      \
772
0
        }                                 \
773
0
        if (!ret)                         \
774
0
            goto done;                    \
775
0
    } while (0)
776
777
/*
778
 * Core code for RFC 3779 3.3 path validation.
779
 */
780
static int asid_validate_path_internal(X509_STORE_CTX *ctx,
781
    const STACK_OF(X509) *chain,
782
    ASIdentifiers *ext)
783
0
{
784
0
    ASIdOrRanges *child_as = NULL, *child_rdi = NULL;
785
0
    int i, ret = 1, inherit_as = 0, inherit_rdi = 0;
786
0
    X509 *x;
787
788
0
    if (!ossl_assert(chain != NULL && sk_X509_num(chain) > 0)
789
0
        || !ossl_assert(ctx != NULL || ext != NULL)
790
0
        || !ossl_assert(ctx == NULL || ctx->verify_cb != NULL)) {
791
0
        if (ctx != NULL)
792
0
            ctx->error = X509_V_ERR_UNSPECIFIED;
793
0
        return 0;
794
0
    }
795
796
    /*
797
     * Figure out where to start.  If we don't have an extension to
798
     * check, we're done.  Otherwise, check canonical form and
799
     * set up for walking up the chain.
800
     */
801
0
    if (ext != NULL) {
802
0
        i = -1;
803
0
        x = NULL;
804
0
    } else {
805
0
        i = 0;
806
0
        x = sk_X509_value(chain, i);
807
0
        if ((ext = x->rfc3779_asid) == NULL)
808
0
            goto done;
809
0
    }
810
0
    if (!X509v3_asid_is_canonical(ext))
811
0
        validation_err(X509_V_ERR_INVALID_EXTENSION);
812
0
    if (ext->asnum != NULL) {
813
0
        switch (ext->asnum->type) {
814
0
        case ASIdentifierChoice_inherit:
815
0
            inherit_as = 1;
816
0
            break;
817
0
        case ASIdentifierChoice_asIdsOrRanges:
818
0
            child_as = ext->asnum->u.asIdsOrRanges;
819
0
            break;
820
0
        }
821
0
    }
822
0
    if (ext->rdi != NULL) {
823
0
        switch (ext->rdi->type) {
824
0
        case ASIdentifierChoice_inherit:
825
0
            inherit_rdi = 1;
826
0
            break;
827
0
        case ASIdentifierChoice_asIdsOrRanges:
828
0
            child_rdi = ext->rdi->u.asIdsOrRanges;
829
0
            break;
830
0
        }
831
0
    }
832
833
    /*
834
     * Now walk up the chain.  Extensions must be in canonical form, no
835
     * cert may list resources that its parent doesn't list.
836
     */
837
0
    for (i++; i < sk_X509_num(chain); i++) {
838
0
        x = sk_X509_value(chain, i);
839
0
        if (!ossl_assert(x != NULL)) {
840
0
            if (ctx != NULL)
841
0
                ctx->error = X509_V_ERR_UNSPECIFIED;
842
0
            return 0;
843
0
        }
844
0
        if (x->rfc3779_asid == NULL) {
845
0
            if (child_as != NULL || child_rdi != NULL)
846
0
                validation_err(X509_V_ERR_UNNESTED_RESOURCE);
847
0
            continue;
848
0
        }
849
0
        if (!X509v3_asid_is_canonical(x->rfc3779_asid))
850
0
            validation_err(X509_V_ERR_INVALID_EXTENSION);
851
0
        if (x->rfc3779_asid->asnum == NULL && child_as != NULL) {
852
0
            validation_err(X509_V_ERR_UNNESTED_RESOURCE);
853
0
            child_as = NULL;
854
0
            inherit_as = 0;
855
0
        }
856
0
        if (x->rfc3779_asid->asnum != NULL && x->rfc3779_asid->asnum->type == ASIdentifierChoice_asIdsOrRanges) {
857
0
            if (inherit_as
858
0
                || asid_contains(x->rfc3779_asid->asnum->u.asIdsOrRanges,
859
0
                    child_as)) {
860
0
                child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges;
861
0
                inherit_as = 0;
862
0
            } else {
863
0
                validation_err(X509_V_ERR_UNNESTED_RESOURCE);
864
0
            }
865
0
        }
866
0
        if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) {
867
0
            validation_err(X509_V_ERR_UNNESTED_RESOURCE);
868
0
            child_rdi = NULL;
869
0
            inherit_rdi = 0;
870
0
        }
871
0
        if (x->rfc3779_asid->rdi != NULL && x->rfc3779_asid->rdi->type == ASIdentifierChoice_asIdsOrRanges) {
872
0
            if (inherit_rdi || asid_contains(x->rfc3779_asid->rdi->u.asIdsOrRanges, child_rdi)) {
873
0
                child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges;
874
0
                inherit_rdi = 0;
875
0
            } else {
876
0
                validation_err(X509_V_ERR_UNNESTED_RESOURCE);
877
0
            }
878
0
        }
879
0
    }
880
881
    /*
882
     * Trust anchor can't inherit.
883
     */
884
0
    if (!ossl_assert(x != NULL)) {
885
0
        if (ctx != NULL)
886
0
            ctx->error = X509_V_ERR_UNSPECIFIED;
887
0
        return 0;
888
0
    }
889
0
    if (x->rfc3779_asid != NULL) {
890
0
        if (x->rfc3779_asid->asnum != NULL && x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit)
891
0
            validation_err(X509_V_ERR_UNNESTED_RESOURCE);
892
0
        if (x->rfc3779_asid->rdi != NULL && x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit)
893
0
            validation_err(X509_V_ERR_UNNESTED_RESOURCE);
894
0
    }
895
896
0
done:
897
0
    return ret;
898
0
}
899
900
#undef validation_err
901
902
/*
903
 * RFC 3779 3.3 path validation -- called from X509_verify_cert().
904
 */
905
int X509v3_asid_validate_path(X509_STORE_CTX *ctx)
906
0
{
907
0
    if (ctx->chain == NULL
908
0
        || sk_X509_num(ctx->chain) == 0
909
0
        || ctx->verify_cb == NULL) {
910
0
        ctx->error = X509_V_ERR_UNSPECIFIED;
911
0
        return 0;
912
0
    }
913
0
    return asid_validate_path_internal(ctx, ctx->chain, NULL);
914
0
}
915
916
/*
917
 * RFC 3779 3.3 path validation of an extension.
918
 * Test whether chain covers extension.
919
 */
920
int X509v3_asid_validate_resource_set(const STACK_OF(X509) *chain,
921
    ASIdentifiers *ext, int allow_inheritance)
922
0
{
923
0
    if (ext == NULL)
924
0
        return 1;
925
0
    if (chain == NULL || sk_X509_num(chain) == 0)
926
0
        return 0;
927
0
    if (!allow_inheritance && X509v3_asid_inherits(ext))
928
0
        return 0;
929
0
    return asid_validate_path_internal(NULL, chain, ext);
930
0
}
931
932
#endif /* OPENSSL_NO_RFC3779 */