Coverage Report

Created: 2026-10-17 06:10

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