Coverage Report

Created: 2023-11-19 07:05

/src/mbedtls/library/x509_crl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  X.509 Certificate Revocation List (CRL) parsing
3
 *
4
 *  Copyright The Mbed TLS Contributors
5
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
 */
7
/*
8
 *  The ITU-T X.509 standard defines a certificate format for PKI.
9
 *
10
 *  http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
11
 *  http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
12
 *  http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
13
 *
14
 *  http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
15
 *  http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
16
 */
17
18
#include "common.h"
19
20
#if defined(MBEDTLS_X509_CRL_PARSE_C)
21
22
#include "mbedtls/x509_crl.h"
23
#include "mbedtls/error.h"
24
#include "mbedtls/oid.h"
25
#include "mbedtls/platform_util.h"
26
27
#include <string.h>
28
29
#if defined(MBEDTLS_PEM_PARSE_C)
30
#include "mbedtls/pem.h"
31
#endif
32
33
#include "mbedtls/platform.h"
34
35
#if defined(MBEDTLS_HAVE_TIME)
36
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
37
#include <windows.h>
38
#else
39
#include <time.h>
40
#endif
41
#endif
42
43
#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
44
#include <stdio.h>
45
#endif
46
47
/*
48
 *  Version  ::=  INTEGER  {  v1(0), v2(1)  }
49
 */
50
static int x509_crl_get_version(unsigned char **p,
51
                                const unsigned char *end,
52
                                int *ver)
53
4.66k
{
54
4.66k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
55
56
4.66k
    if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
57
1.39k
        if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
58
1.28k
            *ver = 0;
59
1.28k
            return 0;
60
1.28k
        }
61
62
106
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
63
1.39k
    }
64
65
3.27k
    return 0;
66
4.66k
}
67
68
/*
69
 * X.509 CRL v2 extensions
70
 *
71
 * We currently don't parse any extension's content, but we do check that the
72
 * list of extensions is well-formed and abort on critical extensions (that
73
 * are unsupported as we don't support any extension so far)
74
 */
75
static int x509_get_crl_ext(unsigned char **p,
76
                            const unsigned char *end,
77
                            mbedtls_x509_buf *ext)
78
2.99k
{
79
2.99k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
80
81
2.99k
    if (*p == end) {
82
56
        return 0;
83
56
    }
84
85
    /*
86
     * crlExtensions           [0]  EXPLICIT Extensions OPTIONAL
87
     *                              -- if present, version MUST be v2
88
     */
89
2.93k
    if ((ret = mbedtls_x509_get_ext(p, end, ext, 0)) != 0) {
90
13
        return ret;
91
13
    }
92
93
2.92k
    end = ext->p + ext->len;
94
95
5.92k
    while (*p < end) {
96
        /*
97
         * Extension  ::=  SEQUENCE  {
98
         *      extnID      OBJECT IDENTIFIER,
99
         *      critical    BOOLEAN DEFAULT FALSE,
100
         *      extnValue   OCTET STRING  }
101
         */
102
3.09k
        int is_critical = 0;
103
3.09k
        const unsigned char *end_ext_data;
104
3.09k
        size_t len;
105
106
        /* Get enclosing sequence tag */
107
3.09k
        if ((ret = mbedtls_asn1_get_tag(p, end, &len,
108
3.09k
                                        MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
109
4
            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
110
4
        }
111
112
3.09k
        end_ext_data = *p + len;
113
114
        /* Get OID (currently ignored) */
115
3.09k
        if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
116
3.09k
                                        MBEDTLS_ASN1_OID)) != 0) {
117
4
            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
118
4
        }
119
3.09k
        *p += len;
120
121
        /* Get optional critical */
122
3.09k
        if ((ret = mbedtls_asn1_get_bool(p, end_ext_data,
123
3.09k
                                         &is_critical)) != 0 &&
124
3.09k
            (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
125
67
            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
126
67
        }
127
128
        /* Data should be octet string type */
129
3.02k
        if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
130
3.02k
                                        MBEDTLS_ASN1_OCTET_STRING)) != 0) {
131
21
            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
132
21
        }
133
134
        /* Ignore data so far and just check its length */
135
3.00k
        *p += len;
136
3.00k
        if (*p != end_ext_data) {
137
4
            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
138
4
                                     MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
139
4
        }
140
141
        /* Abort on (unsupported) critical extensions */
142
2.99k
        if (is_critical) {
143
1
            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
144
1
                                     MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
145
1
        }
146
2.99k
    }
147
148
2.82k
    if (*p != end) {
149
0
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
150
0
                                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
151
0
    }
152
153
2.82k
    return 0;
154
2.82k
}
155
156
/*
157
 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
158
 */
159
static int x509_get_crl_entry_ext(unsigned char **p,
160
                                  const unsigned char *end,
161
                                  mbedtls_x509_buf *ext)
162
11.5k
{
163
11.5k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
164
11.5k
    size_t len = 0;
165
166
    /* OPTIONAL */
167
11.5k
    if (end <= *p) {
168
9.53k
        return 0;
169
9.53k
    }
170
171
2.03k
    ext->tag = **p;
172
2.03k
    ext->p = *p;
173
174
    /*
175
     * Get CRL-entry extension sequence header
176
     * crlEntryExtensions      Extensions OPTIONAL  -- if present, MUST be v2
177
     */
178
2.03k
    if ((ret = mbedtls_asn1_get_tag(p, end, &ext->len,
179
2.03k
                                    MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
180
7
        if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
181
2
            ext->p = NULL;
182
2
            return 0;
183
2
        }
184
5
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
185
7
    }
186
187
2.02k
    end = *p + ext->len;
188
189
2.02k
    if (end != *p + ext->len) {
190
0
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
191
0
                                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
192
0
    }
193
194
4.15k
    while (*p < end) {
195
2.14k
        if ((ret = mbedtls_asn1_get_tag(p, end, &len,
196
2.14k
                                        MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
197
13
            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
198
13
        }
199
200
2.13k
        *p += len;
201
2.13k
    }
202
203
2.01k
    if (*p != end) {
204
0
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
205
0
                                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
206
0
    }
207
208
2.01k
    return 0;
209
2.01k
}
210
211
/*
212
 * X.509 CRL Entries
213
 */
214
static int x509_get_entries(unsigned char **p,
215
                            const unsigned char *end,
216
                            mbedtls_x509_crl_entry *entry)
217
3.58k
{
218
3.58k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
219
3.58k
    size_t entry_len;
220
3.58k
    mbedtls_x509_crl_entry *cur_entry = entry;
221
222
3.58k
    if (*p == end) {
223
367
        return 0;
224
367
    }
225
226
3.21k
    if ((ret = mbedtls_asn1_get_tag(p, end, &entry_len,
227
3.21k
                                    MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED)) != 0) {
228
119
        if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
229
114
            return 0;
230
114
        }
231
232
5
        return ret;
233
119
    }
234
235
3.09k
    end = *p + entry_len;
236
237
14.6k
    while (*p < end) {
238
11.6k
        size_t len2;
239
11.6k
        const unsigned char *end2;
240
241
11.6k
        cur_entry->raw.tag = **p;
242
11.6k
        if ((ret = mbedtls_asn1_get_tag(p, end, &len2,
243
11.6k
                                        MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED)) != 0) {
244
46
            return ret;
245
46
        }
246
247
11.6k
        cur_entry->raw.p = *p;
248
11.6k
        cur_entry->raw.len = len2;
249
11.6k
        end2 = *p + len2;
250
251
11.6k
        if ((ret = mbedtls_x509_get_serial(p, end2, &cur_entry->serial)) != 0) {
252
26
            return ret;
253
26
        }
254
255
11.6k
        if ((ret = mbedtls_x509_get_time(p, end2,
256
11.6k
                                         &cur_entry->revocation_date)) != 0) {
257
59
            return ret;
258
59
        }
259
260
11.5k
        if ((ret = x509_get_crl_entry_ext(p, end2,
261
11.5k
                                          &cur_entry->entry_ext)) != 0) {
262
18
            return ret;
263
18
        }
264
265
11.5k
        if (*p < end) {
266
8.60k
            cur_entry->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crl_entry));
267
268
8.60k
            if (cur_entry->next == NULL) {
269
0
                return MBEDTLS_ERR_X509_ALLOC_FAILED;
270
0
            }
271
272
8.60k
            cur_entry = cur_entry->next;
273
8.60k
        }
274
11.5k
    }
275
276
2.94k
    return 0;
277
3.09k
}
278
279
/*
280
 * Parse one  CRLs in DER format and append it to the chained list
281
 */
282
int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
283
                               const unsigned char *buf, size_t buflen)
284
5.26k
{
285
5.26k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
286
5.26k
    size_t len;
287
5.26k
    unsigned char *p = NULL, *end = NULL;
288
5.26k
    mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
289
5.26k
    mbedtls_x509_crl *crl = chain;
290
291
    /*
292
     * Check for valid input
293
     */
294
5.26k
    if (crl == NULL || buf == NULL) {
295
0
        return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
296
0
    }
297
298
5.26k
    memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
299
5.26k
    memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
300
5.26k
    memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
301
302
    /*
303
     * Add new CRL on the end of the chain if needed.
304
     */
305
47.3k
    while (crl->version != 0 && crl->next != NULL) {
306
42.1k
        crl = crl->next;
307
42.1k
    }
308
309
5.26k
    if (crl->version != 0 && crl->next == NULL) {
310
2.69k
        crl->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crl));
311
312
2.69k
        if (crl->next == NULL) {
313
0
            mbedtls_x509_crl_free(crl);
314
0
            return MBEDTLS_ERR_X509_ALLOC_FAILED;
315
0
        }
316
317
2.69k
        mbedtls_x509_crl_init(crl->next);
318
2.69k
        crl = crl->next;
319
2.69k
    }
320
321
    /*
322
     * Copy raw DER-encoded CRL
323
     */
324
5.26k
    if (buflen == 0) {
325
88
        return MBEDTLS_ERR_X509_INVALID_FORMAT;
326
88
    }
327
328
5.17k
    p = mbedtls_calloc(1, buflen);
329
5.17k
    if (p == NULL) {
330
0
        return MBEDTLS_ERR_X509_ALLOC_FAILED;
331
0
    }
332
333
5.17k
    memcpy(p, buf, buflen);
334
335
5.17k
    crl->raw.p = p;
336
5.17k
    crl->raw.len = buflen;
337
338
5.17k
    end = p + buflen;
339
340
    /*
341
     * CertificateList  ::=  SEQUENCE  {
342
     *      tbsCertList          TBSCertList,
343
     *      signatureAlgorithm   AlgorithmIdentifier,
344
     *      signatureValue       BIT STRING  }
345
     */
346
5.17k
    if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
347
5.17k
                                    MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
348
462
        mbedtls_x509_crl_free(crl);
349
462
        return MBEDTLS_ERR_X509_INVALID_FORMAT;
350
462
    }
351
352
4.71k
    if (len != (size_t) (end - p)) {
353
39
        mbedtls_x509_crl_free(crl);
354
39
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
355
39
                                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
356
39
    }
357
358
    /*
359
     * TBSCertList  ::=  SEQUENCE  {
360
     */
361
4.67k
    crl->tbs.p = p;
362
363
4.67k
    if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
364
4.67k
                                    MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
365
6
        mbedtls_x509_crl_free(crl);
366
6
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
367
6
    }
368
369
4.66k
    end = p + len;
370
4.66k
    crl->tbs.len = end - crl->tbs.p;
371
372
    /*
373
     * Version  ::=  INTEGER  OPTIONAL {  v1(0), v2(1)  }
374
     *               -- if present, MUST be v2
375
     *
376
     * signature            AlgorithmIdentifier
377
     */
378
4.66k
    if ((ret = x509_crl_get_version(&p, end, &crl->version)) != 0 ||
379
4.66k
        (ret = mbedtls_x509_get_alg(&p, end, &crl->sig_oid, &sig_params1)) != 0) {
380
403
        mbedtls_x509_crl_free(crl);
381
403
        return ret;
382
403
    }
383
384
4.26k
    if (crl->version < 0 || crl->version > 1) {
385
34
        mbedtls_x509_crl_free(crl);
386
34
        return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
387
34
    }
388
389
4.22k
    crl->version++;
390
391
4.22k
    if ((ret = mbedtls_x509_get_sig_alg(&crl->sig_oid, &sig_params1,
392
4.22k
                                        &crl->sig_md, &crl->sig_pk,
393
4.22k
                                        &crl->sig_opts)) != 0) {
394
261
        mbedtls_x509_crl_free(crl);
395
261
        return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
396
261
    }
397
398
    /*
399
     * issuer               Name
400
     */
401
3.96k
    crl->issuer_raw.p = p;
402
403
3.96k
    if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
404
3.96k
                                    MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
405
19
        mbedtls_x509_crl_free(crl);
406
19
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
407
19
    }
408
409
3.94k
    if ((ret = mbedtls_x509_get_name(&p, p + len, &crl->issuer)) != 0) {
410
113
        mbedtls_x509_crl_free(crl);
411
113
        return ret;
412
113
    }
413
414
3.83k
    crl->issuer_raw.len = p - crl->issuer_raw.p;
415
416
    /*
417
     * thisUpdate          Time
418
     * nextUpdate          Time OPTIONAL
419
     */
420
3.83k
    if ((ret = mbedtls_x509_get_time(&p, end, &crl->this_update)) != 0) {
421
227
        mbedtls_x509_crl_free(crl);
422
227
        return ret;
423
227
    }
424
425
3.60k
    if ((ret = mbedtls_x509_get_time(&p, end, &crl->next_update)) != 0) {
426
563
        if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
427
563
                                      MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) &&
428
563
            ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
429
395
                                      MBEDTLS_ERR_ASN1_OUT_OF_DATA))) {
430
27
            mbedtls_x509_crl_free(crl);
431
27
            return ret;
432
27
        }
433
563
    }
434
435
    /*
436
     * revokedCertificates    SEQUENCE OF SEQUENCE   {
437
     *      userCertificate        CertificateSerialNumber,
438
     *      revocationDate         Time,
439
     *      crlEntryExtensions     Extensions OPTIONAL
440
     *                                   -- if present, MUST be v2
441
     *                        } OPTIONAL
442
     */
443
3.58k
    if ((ret = x509_get_entries(&p, end, &crl->entry)) != 0) {
444
154
        mbedtls_x509_crl_free(crl);
445
154
        return ret;
446
154
    }
447
448
    /*
449
     * crlExtensions          EXPLICIT Extensions OPTIONAL
450
     *                              -- if present, MUST be v2
451
     */
452
3.42k
    if (crl->version == 2) {
453
2.99k
        ret = x509_get_crl_ext(&p, end, &crl->crl_ext);
454
455
2.99k
        if (ret != 0) {
456
114
            mbedtls_x509_crl_free(crl);
457
114
            return ret;
458
114
        }
459
2.99k
    }
460
461
3.31k
    if (p != end) {
462
5
        mbedtls_x509_crl_free(crl);
463
5
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
464
5
                                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
465
5
    }
466
467
3.30k
    end = crl->raw.p + crl->raw.len;
468
469
    /*
470
     *  signatureAlgorithm   AlgorithmIdentifier,
471
     *  signatureValue       BIT STRING
472
     */
473
3.30k
    if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
474
22
        mbedtls_x509_crl_free(crl);
475
22
        return ret;
476
22
    }
477
478
3.28k
    if (crl->sig_oid.len != sig_oid2.len ||
479
3.28k
        memcmp(crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len) != 0 ||
480
3.28k
        sig_params1.len != sig_params2.len ||
481
3.28k
        (sig_params1.len != 0 &&
482
3.26k
         memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
483
48
        mbedtls_x509_crl_free(crl);
484
48
        return MBEDTLS_ERR_X509_SIG_MISMATCH;
485
48
    }
486
487
3.23k
    if ((ret = mbedtls_x509_get_sig(&p, end, &crl->sig)) != 0) {
488
104
        mbedtls_x509_crl_free(crl);
489
104
        return ret;
490
104
    }
491
492
3.13k
    if (p != end) {
493
6
        mbedtls_x509_crl_free(crl);
494
6
        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
495
6
                                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
496
6
    }
497
498
3.12k
    return 0;
499
3.13k
}
500
501
/*
502
 * Parse one or more CRLs and add them to the chained list
503
 */
504
int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen)
505
2.56k
{
506
2.56k
#if defined(MBEDTLS_PEM_PARSE_C)
507
2.56k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
508
2.56k
    size_t use_len = 0;
509
2.56k
    mbedtls_pem_context pem;
510
2.56k
    int is_pem = 0;
511
512
2.56k
    if (chain == NULL || buf == NULL) {
513
0
        return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
514
0
    }
515
516
5.31k
    do {
517
5.31k
        mbedtls_pem_init(&pem);
518
519
        // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
520
        // string
521
5.31k
        if (buflen == 0 || buf[buflen - 1] != '\0') {
522
1.53k
            ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
523
3.78k
        } else {
524
3.78k
            ret = mbedtls_pem_read_buffer(&pem,
525
3.78k
                                          "-----BEGIN X509 CRL-----",
526
3.78k
                                          "-----END X509 CRL-----",
527
3.78k
                                          buf, NULL, 0, &use_len);
528
3.78k
        }
529
530
5.31k
        if (ret == 0) {
531
            /*
532
             * Was PEM encoded
533
             */
534
3.03k
            is_pem = 1;
535
536
3.03k
            buflen -= use_len;
537
3.03k
            buf += use_len;
538
539
3.03k
            if ((ret = mbedtls_x509_crl_parse_der(chain,
540
3.03k
                                                  pem.buf, pem.buflen)) != 0) {
541
251
                mbedtls_pem_free(&pem);
542
251
                return ret;
543
251
            }
544
3.03k
        } else if (is_pem) {
545
58
            mbedtls_pem_free(&pem);
546
58
            return ret;
547
58
        }
548
549
5.01k
        mbedtls_pem_free(&pem);
550
5.01k
    }
551
    /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
552
     * And a valid CRL cannot be less than 1 byte anyway. */
553
5.01k
    while (is_pem && buflen > 1);
554
555
2.25k
    if (is_pem) {
556
27
        return 0;
557
27
    } else
558
2.23k
#endif /* MBEDTLS_PEM_PARSE_C */
559
2.23k
    return mbedtls_x509_crl_parse_der(chain, buf, buflen);
560
2.25k
}
561
562
#if defined(MBEDTLS_FS_IO)
563
/*
564
 * Load one or more CRLs and add them to the chained list
565
 */
566
int mbedtls_x509_crl_parse_file(mbedtls_x509_crl *chain, const char *path)
567
0
{
568
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
569
0
    size_t n;
570
0
    unsigned char *buf;
571
572
0
    if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
573
0
        return ret;
574
0
    }
575
576
0
    ret = mbedtls_x509_crl_parse(chain, buf, n);
577
578
0
    mbedtls_zeroize_and_free(buf, n);
579
580
0
    return ret;
581
0
}
582
#endif /* MBEDTLS_FS_IO */
583
584
#if !defined(MBEDTLS_X509_REMOVE_INFO)
585
/*
586
 * Return an informational string about the certificate.
587
 */
588
#define BEFORE_COLON    14
589
#define BC              "14"
590
/*
591
 * Return an informational string about the CRL.
592
 */
593
int mbedtls_x509_crl_info(char *buf, size_t size, const char *prefix,
594
                          const mbedtls_x509_crl *crl)
595
376
{
596
376
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
597
376
    size_t n;
598
376
    char *p;
599
376
    const mbedtls_x509_crl_entry *entry;
600
601
376
    p = buf;
602
376
    n = size;
603
604
376
    ret = mbedtls_snprintf(p, n, "%sCRL version   : %d",
605
376
                           prefix, crl->version);
606
376
    MBEDTLS_X509_SAFE_SNPRINTF;
607
608
376
    ret = mbedtls_snprintf(p, n, "\n%sissuer name   : ", prefix);
609
376
    MBEDTLS_X509_SAFE_SNPRINTF;
610
376
    ret = mbedtls_x509_dn_gets(p, n, &crl->issuer);
611
376
    MBEDTLS_X509_SAFE_SNPRINTF;
612
613
372
    ret = mbedtls_snprintf(p, n, "\n%sthis update   : " \
614
372
                                 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
615
372
                           crl->this_update.year, crl->this_update.mon,
616
372
                           crl->this_update.day,  crl->this_update.hour,
617
372
                           crl->this_update.min,  crl->this_update.sec);
618
372
    MBEDTLS_X509_SAFE_SNPRINTF;
619
620
372
    ret = mbedtls_snprintf(p, n, "\n%snext update   : " \
621
372
                                 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
622
372
                           crl->next_update.year, crl->next_update.mon,
623
372
                           crl->next_update.day,  crl->next_update.hour,
624
372
                           crl->next_update.min,  crl->next_update.sec);
625
372
    MBEDTLS_X509_SAFE_SNPRINTF;
626
627
372
    entry = &crl->entry;
628
629
372
    ret = mbedtls_snprintf(p, n, "\n%sRevoked certificates:",
630
372
                           prefix);
631
372
    MBEDTLS_X509_SAFE_SNPRINTF;
632
633
2.34k
    while (entry != NULL && entry->raw.len != 0) {
634
1.98k
        ret = mbedtls_snprintf(p, n, "\n%sserial number: ",
635
1.98k
                               prefix);
636
1.98k
        MBEDTLS_X509_SAFE_SNPRINTF;
637
638
1.98k
        ret = mbedtls_x509_serial_gets(p, n, &entry->serial);
639
1.98k
        MBEDTLS_X509_SAFE_SNPRINTF;
640
641
1.98k
        ret = mbedtls_snprintf(p, n, " revocation date: " \
642
1.98k
                                     "%04d-%02d-%02d %02d:%02d:%02d",
643
1.98k
                               entry->revocation_date.year, entry->revocation_date.mon,
644
1.98k
                               entry->revocation_date.day,  entry->revocation_date.hour,
645
1.98k
                               entry->revocation_date.min,  entry->revocation_date.sec);
646
1.98k
        MBEDTLS_X509_SAFE_SNPRINTF;
647
648
1.97k
        entry = entry->next;
649
1.97k
    }
650
651
360
    ret = mbedtls_snprintf(p, n, "\n%ssigned using  : ", prefix);
652
360
    MBEDTLS_X509_SAFE_SNPRINTF;
653
654
358
    ret = mbedtls_x509_sig_alg_gets(p, n, &crl->sig_oid, crl->sig_pk, crl->sig_md,
655
358
                                    crl->sig_opts);
656
358
    MBEDTLS_X509_SAFE_SNPRINTF;
657
658
352
    ret = mbedtls_snprintf(p, n, "\n");
659
352
    MBEDTLS_X509_SAFE_SNPRINTF;
660
661
351
    return (int) (size - n);
662
352
}
663
#endif /* MBEDTLS_X509_REMOVE_INFO */
664
665
/*
666
 * Initialize a CRL chain
667
 */
668
void mbedtls_x509_crl_init(mbedtls_x509_crl *crl)
669
5.26k
{
670
5.26k
    memset(crl, 0, sizeof(mbedtls_x509_crl));
671
5.26k
}
672
673
/*
674
 * Unallocate all CRL data
675
 */
676
void mbedtls_x509_crl_free(mbedtls_x509_crl *crl)
677
7.07k
{
678
7.07k
    mbedtls_x509_crl *crl_cur = crl;
679
7.07k
    mbedtls_x509_crl *crl_prv;
680
7.07k
    mbedtls_x509_crl_entry *entry_cur;
681
7.07k
    mbedtls_x509_crl_entry *entry_prv;
682
683
16.8k
    while (crl_cur != NULL) {
684
9.77k
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
685
9.77k
        mbedtls_free(crl_cur->sig_opts);
686
9.77k
#endif
687
688
9.77k
        mbedtls_asn1_free_named_data_list_shallow(crl_cur->issuer.next);
689
690
9.77k
        entry_cur = crl_cur->entry.next;
691
18.3k
        while (entry_cur != NULL) {
692
8.60k
            entry_prv = entry_cur;
693
8.60k
            entry_cur = entry_cur->next;
694
8.60k
            mbedtls_zeroize_and_free(entry_prv,
695
8.60k
                                     sizeof(mbedtls_x509_crl_entry));
696
8.60k
        }
697
698
9.77k
        if (crl_cur->raw.p != NULL) {
699
5.17k
            mbedtls_zeroize_and_free(crl_cur->raw.p, crl_cur->raw.len);
700
5.17k
        }
701
702
9.77k
        crl_prv = crl_cur;
703
9.77k
        crl_cur = crl_cur->next;
704
705
9.77k
        mbedtls_platform_zeroize(crl_prv, sizeof(mbedtls_x509_crl));
706
9.77k
        if (crl_prv != crl) {
707
2.69k
            mbedtls_free(crl_prv);
708
2.69k
        }
709
9.77k
    }
710
7.07k
}
711
712
#endif /* MBEDTLS_X509_CRL_PARSE_C */