blob: d89faccad7704cd07adf9139ad9a74b77713068f [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 Certidicate Revocation List (CRL) parsing
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
30 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crl.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
41#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049#else
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000051#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#endif
56
Paul Bakkerfa6a6202013-10-28 18:48:30 +010057#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#include <windows.h>
59#else
60#include <time.h>
61#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064#include <stdio.h>
65#endif
66
67/*
68 * Version ::= INTEGER { v1(0), v2(1) }
69 */
70static int x509_crl_get_version( unsigned char **p,
71 const unsigned char *end,
72 int *ver )
73{
Janos Follath865b3eb2019-12-16 11:46:15 +000074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079 {
80 *ver = 0;
81 return( 0 );
82 }
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085 }
86
87 return( 0 );
88}
89
90/*
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +010091 * X.509 CRL v2 extensions
92 *
93 * We currently don't parse any extension's content, but we do check that the
94 * list of extensions is well-formed and abort on critical extensions (that
95 * are unsupported as we don't support any extension so far)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096 */
97static int x509_get_crl_ext( unsigned char **p,
98 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100{
Janos Follath865b3eb2019-12-16 11:46:15 +0000101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102
Hanno Becker12f62fb2019-02-12 17:22:36 +0000103 if( *p == end )
104 return( 0 );
105
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100106 /*
107 * crlExtensions [0] EXPLICIT Extensions OPTIONAL
108 * -- if present, version MUST be v2
109 */
110 if( ( ret = mbedtls_x509_get_ext( p, end, ext, 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112
Hanno Becker12f62fb2019-02-12 17:22:36 +0000113 end = ext->p + ext->len;
114
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115 while( *p < end )
116 {
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100117 /*
118 * Extension ::= SEQUENCE {
119 * extnID OBJECT IDENTIFIER,
120 * critical BOOLEAN DEFAULT FALSE,
121 * extnValue OCTET STRING }
122 */
123 int is_critical = 0;
124 const unsigned char *end_ext_data;
125 size_t len;
126
127 /* Get enclosing sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
129 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
130 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200131
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100132 end_ext_data = *p + len;
133
134 /* Get OID (currently ignored) */
135 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
136 MBEDTLS_ASN1_OID ) ) != 0 )
137 {
138 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
139 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140 *p += len;
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100141
142 /* Get optional critical */
143 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data,
144 &is_critical ) ) != 0 &&
145 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
146 {
147 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
148 }
149
150 /* Data should be octet string type */
151 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
152 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
153 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
154
155 /* Ignore data so far and just check its length */
156 *p += len;
157 if( *p != end_ext_data )
158 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
159 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
160
161 /* Abort on (unsupported) critical extensions */
162 if( is_critical )
163 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
164 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165 }
166
167 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
169 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170
171 return( 0 );
172}
173
174/*
175 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
176 */
177static int x509_get_crl_entry_ext( unsigned char **p,
178 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180{
Janos Follath865b3eb2019-12-16 11:46:15 +0000181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182 size_t len = 0;
183
184 /* OPTIONAL */
Paul Bakker66d5d072014-06-17 16:39:18 +0200185 if( end <= *p )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186 return( 0 );
187
188 ext->tag = **p;
189 ext->p = *p;
190
191 /*
192 * Get CRL-entry extension sequence header
193 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
196 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199 {
200 ext->p = NULL;
201 return( 0 );
202 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204 }
205
Paul Bakker9af723c2014-05-01 13:03:14 +0200206 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207
208 if( end != *p + ext->len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
210 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211
212 while( *p < end )
213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
215 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
216 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217
218 *p += len;
219 }
220
221 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
223 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224
225 return( 0 );
226}
227
228/*
229 * X.509 CRL Entries
230 */
231static int x509_get_entries( unsigned char **p,
232 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_x509_crl_entry *entry )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200234{
Janos Follath865b3eb2019-12-16 11:46:15 +0000235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236 size_t entry_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_x509_crl_entry *cur_entry = entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238
239 if( *p == end )
240 return( 0 );
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( ( ret = mbedtls_asn1_get_tag( p, end, &entry_len,
243 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246 return( 0 );
247
248 return( ret );
249 }
250
251 end = *p + entry_len;
252
253 while( *p < end )
254 {
255 size_t len2;
256 const unsigned char *end2;
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( ( ret = mbedtls_asn1_get_tag( p, end, &len2,
259 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260 {
261 return( ret );
262 }
263
264 cur_entry->raw.tag = **p;
265 cur_entry->raw.p = *p;
266 cur_entry->raw.len = len2;
267 end2 = *p + len2;
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( ( ret = mbedtls_x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270 return( ret );
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 if( ( ret = mbedtls_x509_get_time( p, end2,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200273 &cur_entry->revocation_date ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274 return( ret );
275
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200276 if( ( ret = x509_get_crl_entry_ext( p, end2,
277 &cur_entry->entry_ext ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 return( ret );
279
Paul Bakker66d5d072014-06-17 16:39:18 +0200280 if( *p < end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200282 cur_entry->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl_entry ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283
284 if( cur_entry->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200285 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286
287 cur_entry = cur_entry->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 }
289 }
290
291 return( 0 );
292}
293
294/*
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100295 * Parse one CRLs in DER format and append it to the chained list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100298 const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299{
Janos Follath865b3eb2019-12-16 11:46:15 +0000300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301 size_t len;
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100302 unsigned char *p = NULL, *end = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
304 mbedtls_x509_crl *crl = chain;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305
306 /*
307 * Check for valid input
308 */
309 if( crl == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
313 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
314 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315
316 /*
317 * Add new CRL on the end of the chain if needed.
318 */
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100319 while( crl->version != 0 && crl->next != NULL )
320 crl = crl->next;
321
Paul Bakker66d5d072014-06-17 16:39:18 +0200322 if( crl->version != 0 && crl->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200324 crl->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325
326 if( crl->next == NULL )
327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_x509_crl_free( crl );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200329 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330 }
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_x509_crl_init( crl->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333 crl = crl->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334 }
335
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100336 /*
337 * Copy raw DER-encoded CRL
338 */
Andres Amaya Garciacb5123f2017-12-06 09:39:23 +0000339 if( buflen == 0 )
340 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Andres Amaya Garciac9d62262017-12-12 20:15:03 +0000341
342 p = mbedtls_calloc( 1, buflen );
343 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200344 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100346 memcpy( p, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347
348 crl->raw.p = p;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100349 crl->raw.len = buflen;
350
351 end = p + buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352
353 /*
354 * CertificateList ::= SEQUENCE {
355 * tbsCertList TBSCertList,
356 * signatureAlgorithm AlgorithmIdentifier,
357 * signatureValue BIT STRING }
358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
360 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_x509_crl_free( crl );
363 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 }
365
366 if( len != (size_t) ( end - p ) )
367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_x509_crl_free( crl );
369 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
370 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371 }
372
373 /*
374 * TBSCertList ::= SEQUENCE {
375 */
376 crl->tbs.p = p;
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
379 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_x509_crl_free( crl );
382 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383 }
384
385 end = p + len;
386 crl->tbs.len = end - crl->tbs.p;
387
388 /*
389 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
390 * -- if present, MUST be v2
391 *
392 * signature AlgorithmIdentifier
393 */
394 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 ( ret = mbedtls_x509_get_alg( &p, end, &crl->sig_oid, &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 return( ret );
399 }
400
Andres AG4f753c12017-02-10 14:39:58 +0000401 if( crl->version < 0 || crl->version > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_x509_crl_free( crl );
404 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 }
406
Andres AG4f753c12017-02-10 14:39:58 +0000407 crl->version++;
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 if( ( ret = mbedtls_x509_get_sig_alg( &crl->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200410 &crl->sig_md, &crl->sig_pk,
411 &crl->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_x509_crl_free( crl );
414 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 }
416
417 /*
418 * issuer Name
419 */
420 crl->issuer_raw.p = p;
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
423 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_x509_crl_free( crl );
426 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427 }
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432 return( ret );
433 }
434
435 crl->issuer_raw.len = p - crl->issuer_raw.p;
436
437 /*
438 * thisUpdate Time
439 * nextUpdate Time OPTIONAL
440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444 return( ret );
445 }
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 if( ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
450 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) &&
451 ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
452 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 return( ret );
456 }
457 }
458
459 /*
460 * revokedCertificates SEQUENCE OF SEQUENCE {
461 * userCertificate CertificateSerialNumber,
462 * revocationDate Time,
463 * crlEntryExtensions Extensions OPTIONAL
464 * -- if present, MUST be v2
465 * } OPTIONAL
466 */
467 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470 return( ret );
471 }
472
473 /*
474 * crlExtensions EXPLICIT Extensions OPTIONAL
475 * -- if present, MUST be v2
476 */
477 if( crl->version == 2 )
478 {
479 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
480
481 if( ret != 0 )
482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 return( ret );
485 }
486 }
487
488 if( p != end )
489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_x509_crl_free( crl );
491 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
492 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 }
494
495 end = crl->raw.p + crl->raw.len;
496
497 /*
498 * signatureAlgorithm AlgorithmIdentifier,
499 * signatureValue BIT STRING
500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504 return( ret );
505 }
506
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100507 if( crl->sig_oid.len != sig_oid2.len ||
508 memcmp( crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200509 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200510 ( sig_params1.len != 0 &&
511 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_x509_crl_free( crl );
514 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515 }
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 if( ( ret = mbedtls_x509_get_sig( &p, end, &crl->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520 return( ret );
521 }
522
523 if( p != end )
524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_x509_crl_free( crl );
526 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
527 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528 }
529
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100530 return( 0 );
531}
532
533/*
534 * Parse one or more CRLs and add them to the chained list
535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_PEM_PARSE_C)
Janos Follath865b3eb2019-12-16 11:46:15 +0000539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Benjamin Kier36050732019-05-30 14:49:17 -0400540 size_t use_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_pem_context pem;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100542 int is_pem = 0;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100543
544 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100546
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100547 do
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200550
Simon Butcher97e82902016-05-19 00:22:37 +0100551 // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
552 // string
553 if( buflen == 0 || buf[buflen - 1] != '\0' )
554 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
555 else
556 ret = mbedtls_pem_read_buffer( &pem,
557 "-----BEGIN X509 CRL-----",
558 "-----END X509 CRL-----",
559 buf, NULL, 0, &use_len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100561 if( ret == 0 )
562 {
563 /*
564 * Was PEM encoded
565 */
566 is_pem = 1;
567
568 buflen -= use_len;
569 buf += use_len;
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( ret = mbedtls_x509_crl_parse_der( chain,
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100572 pem.buf, pem.buflen ) ) != 0 )
573 {
Andres AG5708dcb2016-12-08 17:19:21 +0000574 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100575 return( ret );
576 }
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100577 }
Andres AG939954c2016-12-08 17:08:44 +0000578 else if( is_pem )
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100581 return( ret );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100582 }
Andres AG5708dcb2016-12-08 17:19:21 +0000583
584 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100585 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200586 /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
587 * And a valid CRL cannot be less than 1 byte anyway. */
588 while( is_pem && buflen > 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100590 if( is_pem )
591 return( 0 );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100592 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#endif /* MBEDTLS_PEM_PARSE_C */
594 return( mbedtls_x509_crl_parse_der( chain, buf, buflen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595}
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598/*
599 * Load one or more CRLs and add them to the chained list
600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602{
Janos Follath865b3eb2019-12-16 11:46:15 +0000603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604 size_t n;
605 unsigned char *buf;
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608 return( ret );
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 ret = mbedtls_x509_crl_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500612 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614
615 return( ret );
616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619/*
620 * Return an informational string about the certificate.
621 */
622#define BEFORE_COLON 14
623#define BC "14"
624/*
625 * Return an informational string about the CRL.
626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
628 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629{
Janos Follath865b3eb2019-12-16 11:46:15 +0000630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 size_t n;
632 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 const mbedtls_x509_crl_entry *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634
635 p = buf;
636 n = size;
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 prefix, crl->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200640 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200643 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200645 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 ret = mbedtls_snprintf( p, n, "\n%sthis update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
649 crl->this_update.year, crl->this_update.mon,
650 crl->this_update.day, crl->this_update.hour,
651 crl->this_update.min, crl->this_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200652 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 ret = mbedtls_snprintf( p, n, "\n%snext update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
656 crl->next_update.year, crl->next_update.mon,
657 crl->next_update.day, crl->next_update.hour,
658 crl->next_update.min, crl->next_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200659 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660
661 entry = &crl->entry;
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200665 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666
667 while( entry != NULL && entry->raw.len != 0 )
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 ret = mbedtls_snprintf( p, n, "\n%sserial number: ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200671 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 ret = mbedtls_x509_serial_gets( p, n, &entry->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200674 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 ret = mbedtls_snprintf( p, n, " revocation date: " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 "%04d-%02d-%02d %02d:%02d:%02d",
678 entry->revocation_date.year, entry->revocation_date.mon,
679 entry->revocation_date.day, entry->revocation_date.hour,
680 entry->revocation_date.min, entry->revocation_date.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200681 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
683 entry = entry->next;
684 }
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200687 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 ret = mbedtls_x509_sig_alg_gets( p, n, &crl->sig_oid, crl->sig_pk, crl->sig_md,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +0200690 crl->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200691 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200694 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695
696 return( (int) ( size - n ) );
697}
698
699/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200700 * Initialize a CRL chain
701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702void mbedtls_x509_crl_init( mbedtls_x509_crl *crl )
Paul Bakker369d2eb2013-09-18 11:58:25 +0200703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 memset( crl, 0, sizeof(mbedtls_x509_crl) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200705}
706
707/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 * Unallocate all CRL data
709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710void mbedtls_x509_crl_free( mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_x509_crl *crl_cur = crl;
713 mbedtls_x509_crl *crl_prv;
714 mbedtls_x509_name *name_cur;
715 mbedtls_x509_name *name_prv;
716 mbedtls_x509_crl_entry *entry_cur;
717 mbedtls_x509_crl_entry *entry_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718
719 if( crl == NULL )
720 return;
721
722 do
723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
725 mbedtls_free( crl_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200726#endif
727
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 name_cur = crl_cur->issuer.next;
729 while( name_cur != NULL )
730 {
731 name_prv = name_cur;
732 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500733 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 }
736
737 entry_cur = crl_cur->entry.next;
738 while( entry_cur != NULL )
739 {
740 entry_prv = entry_cur;
741 entry_cur = entry_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500742 mbedtls_platform_zeroize( entry_prv,
743 sizeof( mbedtls_x509_crl_entry ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_free( entry_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 }
746
747 if( crl_cur->raw.p != NULL )
748 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500749 mbedtls_platform_zeroize( crl_cur->raw.p, crl_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_free( crl_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751 }
752
753 crl_cur = crl_cur->next;
754 }
755 while( crl_cur != NULL );
756
757 crl_cur = crl;
758 do
759 {
760 crl_prv = crl_cur;
761 crl_cur = crl_cur->next;
762
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500763 mbedtls_platform_zeroize( crl_prv, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 if( crl_prv != crl )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_free( crl_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 }
767 while( crl_cur != NULL );
768}
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#endif /* MBEDTLS_X509_CRL_PARSE_C */