blob: 520b76ebb11d23577b20c25afb28172b3cb4947c [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 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045 */
46/*
47 * The ITU-T X.509 standard defines a certificate format for PKI.
48 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020049 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
50 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
51 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052 *
53 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
54 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
55 */
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/x509_crl.h"
66#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050067#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000068
69#include <string.h>
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#else
Rich Evans00ab4702015-02-06 13:43:58 +000078#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000079#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020081#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#endif
84
Paul Bakkerfa6a6202013-10-28 18:48:30 +010085#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086#include <windows.h>
87#else
88#include <time.h>
89#endif
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092#include <stdio.h>
93#endif
94
95/*
96 * Version ::= INTEGER { v1(0), v2(1) }
97 */
98static int x509_crl_get_version( unsigned char **p,
99 const unsigned char *end,
100 int *ver )
101{
102 int ret;
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107 {
108 *ver = 0;
109 return( 0 );
110 }
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113 }
114
115 return( 0 );
116}
117
118/*
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100119 * X.509 CRL v2 extensions
120 *
121 * We currently don't parse any extension's content, but we do check that the
122 * list of extensions is well-formed and abort on critical extensions (that
123 * are unsupported as we don't support any extension so far)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124 */
125static int x509_get_crl_ext( unsigned char **p,
126 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128{
129 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000131 if( *p == end )
132 return( 0 );
133
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100134 /*
135 * crlExtensions [0] EXPLICIT Extensions OPTIONAL
136 * -- if present, version MUST be v2
137 */
138 if( ( ret = mbedtls_x509_get_ext( p, end, ext, 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200139 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000141 end = ext->p + ext->len;
142
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200143 while( *p < end )
144 {
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100145 /*
146 * Extension ::= SEQUENCE {
147 * extnID OBJECT IDENTIFIER,
148 * critical BOOLEAN DEFAULT FALSE,
149 * extnValue OCTET STRING }
150 */
151 int is_critical = 0;
152 const unsigned char *end_ext_data;
153 size_t len;
154
155 /* Get enclosing sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
157 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
158 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100160 end_ext_data = *p + len;
161
162 /* Get OID (currently ignored) */
163 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
164 MBEDTLS_ASN1_OID ) ) != 0 )
165 {
166 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
167 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168 *p += len;
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100169
170 /* Get optional critical */
171 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data,
172 &is_critical ) ) != 0 &&
173 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
174 {
175 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
176 }
177
178 /* Data should be octet string type */
179 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
180 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
181 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
182
183 /* Ignore data so far and just check its length */
184 *p += len;
185 if( *p != end_ext_data )
186 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
187 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
188
189 /* Abort on (unsupported) critical extensions */
190 if( is_critical )
191 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
192 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200193 }
194
195 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
197 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198
199 return( 0 );
200}
201
202/*
203 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
204 */
205static int x509_get_crl_entry_ext( unsigned char **p,
206 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208{
209 int ret;
210 size_t len = 0;
211
212 /* OPTIONAL */
Paul Bakker66d5d072014-06-17 16:39:18 +0200213 if( end <= *p )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214 return( 0 );
215
216 ext->tag = **p;
217 ext->p = *p;
218
219 /*
220 * Get CRL-entry extension sequence header
221 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
224 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227 {
228 ext->p = NULL;
229 return( 0 );
230 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232 }
233
Paul Bakker9af723c2014-05-01 13:03:14 +0200234 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235
236 if( end != *p + ext->len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
238 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239
240 while( *p < end )
241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
243 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
244 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200245
246 *p += len;
247 }
248
249 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
251 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252
253 return( 0 );
254}
255
256/*
257 * X.509 CRL Entries
258 */
259static int x509_get_entries( unsigned char **p,
260 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_x509_crl_entry *entry )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262{
263 int ret;
264 size_t entry_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_x509_crl_entry *cur_entry = entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200266
267 if( *p == end )
268 return( 0 );
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( ( ret = mbedtls_asn1_get_tag( p, end, &entry_len,
271 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274 return( 0 );
275
276 return( ret );
277 }
278
279 end = *p + entry_len;
280
281 while( *p < end )
282 {
283 size_t len2;
284 const unsigned char *end2;
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( ( ret = mbedtls_asn1_get_tag( p, end, &len2,
287 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 {
289 return( ret );
290 }
291
292 cur_entry->raw.tag = **p;
293 cur_entry->raw.p = *p;
294 cur_entry->raw.len = len2;
295 end2 = *p + len2;
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( ( ret = mbedtls_x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298 return( ret );
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 if( ( ret = mbedtls_x509_get_time( p, end2,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200301 &cur_entry->revocation_date ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302 return( ret );
303
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200304 if( ( ret = x509_get_crl_entry_ext( p, end2,
305 &cur_entry->entry_ext ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200306 return( ret );
307
Paul Bakker66d5d072014-06-17 16:39:18 +0200308 if( *p < end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200310 cur_entry->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl_entry ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311
312 if( cur_entry->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200313 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314
315 cur_entry = cur_entry->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200316 }
317 }
318
319 return( 0 );
320}
321
322/*
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100323 * Parse one CRLs in DER format and append it to the chained list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100326 const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327{
328 int ret;
329 size_t len;
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100330 unsigned char *p = NULL, *end = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
332 mbedtls_x509_crl *crl = chain;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333
334 /*
335 * Check for valid input
336 */
337 if( crl == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
341 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
342 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
344 /*
345 * Add new CRL on the end of the chain if needed.
346 */
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100347 while( crl->version != 0 && crl->next != NULL )
348 crl = crl->next;
349
Paul Bakker66d5d072014-06-17 16:39:18 +0200350 if( crl->version != 0 && crl->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200352 crl->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353
354 if( crl->next == NULL )
355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_x509_crl_free( crl );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200357 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 }
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_x509_crl_init( crl->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 crl = crl->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 }
363
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100364 /*
365 * Copy raw DER-encoded CRL
366 */
Andres Amaya Garciacb5123f2017-12-06 09:39:23 +0000367 if( buflen == 0 )
368 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Andres Amaya Garciac9d62262017-12-12 20:15:03 +0000369
370 p = mbedtls_calloc( 1, buflen );
371 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200372 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100374 memcpy( p, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
376 crl->raw.p = p;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100377 crl->raw.len = buflen;
378
379 end = p + buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380
381 /*
382 * CertificateList ::= SEQUENCE {
383 * tbsCertList TBSCertList,
384 * signatureAlgorithm AlgorithmIdentifier,
385 * signatureValue BIT STRING }
386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
388 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_x509_crl_free( crl );
391 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392 }
393
394 if( len != (size_t) ( end - p ) )
395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_x509_crl_free( crl );
397 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
398 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 }
400
401 /*
402 * TBSCertList ::= SEQUENCE {
403 */
404 crl->tbs.p = p;
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
407 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_x509_crl_free( crl );
410 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411 }
412
413 end = p + len;
414 crl->tbs.len = end - crl->tbs.p;
415
416 /*
417 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
418 * -- if present, MUST be v2
419 *
420 * signature AlgorithmIdentifier
421 */
422 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 ( ret = mbedtls_x509_get_alg( &p, end, &crl->sig_oid, &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 return( ret );
427 }
428
Andres AG4f753c12017-02-10 14:39:58 +0000429 if( crl->version < 0 || crl->version > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_x509_crl_free( crl );
432 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 }
434
Andres AG4f753c12017-02-10 14:39:58 +0000435 crl->version++;
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 if( ( ret = mbedtls_x509_get_sig_alg( &crl->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200438 &crl->sig_md, &crl->sig_pk,
439 &crl->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_x509_crl_free( crl );
442 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 }
444
445 /*
446 * issuer Name
447 */
448 crl->issuer_raw.p = p;
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
451 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_x509_crl_free( crl );
454 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 }
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 return( ret );
461 }
462
463 crl->issuer_raw.len = p - crl->issuer_raw.p;
464
465 /*
466 * thisUpdate Time
467 * nextUpdate Time OPTIONAL
468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472 return( ret );
473 }
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 if( ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
478 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) &&
479 ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
480 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483 return( ret );
484 }
485 }
486
487 /*
488 * revokedCertificates SEQUENCE OF SEQUENCE {
489 * userCertificate CertificateSerialNumber,
490 * revocationDate Time,
491 * crlEntryExtensions Extensions OPTIONAL
492 * -- if present, MUST be v2
493 * } OPTIONAL
494 */
495 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 return( ret );
499 }
500
501 /*
502 * crlExtensions EXPLICIT Extensions OPTIONAL
503 * -- if present, MUST be v2
504 */
505 if( crl->version == 2 )
506 {
507 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
508
509 if( ret != 0 )
510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 return( ret );
513 }
514 }
515
516 if( p != end )
517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_x509_crl_free( crl );
519 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
520 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521 }
522
523 end = crl->raw.p + crl->raw.len;
524
525 /*
526 * signatureAlgorithm AlgorithmIdentifier,
527 * signatureValue BIT STRING
528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 return( ret );
533 }
534
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100535 if( crl->sig_oid.len != sig_oid2.len ||
536 memcmp( crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200537 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200538 ( sig_params1.len != 0 &&
539 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_x509_crl_free( crl );
542 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543 }
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( ( ret = mbedtls_x509_get_sig( &p, end, &crl->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 return( ret );
549 }
550
551 if( p != end )
552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_x509_crl_free( crl );
554 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
555 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556 }
557
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100558 return( 0 );
559}
560
561/*
562 * Parse one or more CRLs and add them to the chained list
563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100567 int ret;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100568 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_pem_context pem;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100570 int is_pem = 0;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100571
572 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100574
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100575 do
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200578
Simon Butcher97e82902016-05-19 00:22:37 +0100579 // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
580 // string
581 if( buflen == 0 || buf[buflen - 1] != '\0' )
582 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
583 else
584 ret = mbedtls_pem_read_buffer( &pem,
585 "-----BEGIN X509 CRL-----",
586 "-----END X509 CRL-----",
587 buf, NULL, 0, &use_len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100589 if( ret == 0 )
590 {
591 /*
592 * Was PEM encoded
593 */
594 is_pem = 1;
595
596 buflen -= use_len;
597 buf += use_len;
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 if( ( ret = mbedtls_x509_crl_parse_der( chain,
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100600 pem.buf, pem.buflen ) ) != 0 )
601 {
Andres AG5708dcb2016-12-08 17:19:21 +0000602 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100603 return( ret );
604 }
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100605 }
Andres AG939954c2016-12-08 17:08:44 +0000606 else if( is_pem )
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100609 return( ret );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100610 }
Andres AG5708dcb2016-12-08 17:19:21 +0000611
612 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100613 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200614 /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
615 * And a valid CRL cannot be less than 1 byte anyway. */
616 while( is_pem && buflen > 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100618 if( is_pem )
619 return( 0 );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100620 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#endif /* MBEDTLS_PEM_PARSE_C */
622 return( mbedtls_x509_crl_parse_der( chain, buf, buflen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623}
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626/*
627 * Load one or more CRLs and add them to the chained list
628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630{
631 int ret;
632 size_t n;
633 unsigned char *buf;
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 return( ret );
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 ret = mbedtls_x509_crl_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500640 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
643 return( ret );
644}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647/*
648 * Return an informational string about the certificate.
649 */
650#define BEFORE_COLON 14
651#define BC "14"
652/*
653 * Return an informational string about the CRL.
654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
656 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657{
658 int ret;
659 size_t n;
660 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 const mbedtls_x509_crl_entry *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662
663 p = buf;
664 n = size;
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667 prefix, crl->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200668 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200671 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200673 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 ret = mbedtls_snprintf( p, n, "\n%sthis update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
677 crl->this_update.year, crl->this_update.mon,
678 crl->this_update.day, crl->this_update.hour,
679 crl->this_update.min, crl->this_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200680 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 ret = mbedtls_snprintf( p, n, "\n%snext update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
684 crl->next_update.year, crl->next_update.mon,
685 crl->next_update.day, crl->next_update.hour,
686 crl->next_update.min, crl->next_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200687 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688
689 entry = &crl->entry;
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200693 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694
695 while( entry != NULL && entry->raw.len != 0 )
696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 ret = mbedtls_snprintf( p, n, "\n%sserial number: ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200699 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 ret = mbedtls_x509_serial_gets( p, n, &entry->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200702 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 ret = mbedtls_snprintf( p, n, " revocation date: " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 "%04d-%02d-%02d %02d:%02d:%02d",
706 entry->revocation_date.year, entry->revocation_date.mon,
707 entry->revocation_date.day, entry->revocation_date.hour,
708 entry->revocation_date.min, entry->revocation_date.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200709 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710
711 entry = entry->next;
712 }
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200715 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 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 +0200718 crl->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200719 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200722 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723
724 return( (int) ( size - n ) );
725}
726
727/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200728 * Initialize a CRL chain
729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730void mbedtls_x509_crl_init( mbedtls_x509_crl *crl )
Paul Bakker369d2eb2013-09-18 11:58:25 +0200731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 memset( crl, 0, sizeof(mbedtls_x509_crl) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200733}
734
735/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736 * Unallocate all CRL data
737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738void mbedtls_x509_crl_free( mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_x509_crl *crl_cur = crl;
741 mbedtls_x509_crl *crl_prv;
742 mbedtls_x509_name *name_cur;
743 mbedtls_x509_name *name_prv;
744 mbedtls_x509_crl_entry *entry_cur;
745 mbedtls_x509_crl_entry *entry_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746
747 if( crl == NULL )
748 return;
749
750 do
751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
753 mbedtls_free( crl_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200754#endif
755
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756 name_cur = crl_cur->issuer.next;
757 while( name_cur != NULL )
758 {
759 name_prv = name_cur;
760 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500761 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 }
764
765 entry_cur = crl_cur->entry.next;
766 while( entry_cur != NULL )
767 {
768 entry_prv = entry_cur;
769 entry_cur = entry_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500770 mbedtls_platform_zeroize( entry_prv,
771 sizeof( mbedtls_x509_crl_entry ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_free( entry_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 }
774
775 if( crl_cur->raw.p != NULL )
776 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500777 mbedtls_platform_zeroize( crl_cur->raw.p, crl_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_free( crl_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779 }
780
781 crl_cur = crl_cur->next;
782 }
783 while( crl_cur != NULL );
784
785 crl_cur = crl;
786 do
787 {
788 crl_prv = crl_cur;
789 crl_cur = crl_cur->next;
790
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500791 mbedtls_platform_zeroize( crl_prv, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 if( crl_prv != crl )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_free( crl_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794 }
795 while( crl_cur != NULL );
796}
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#endif /* MBEDTLS_X509_CRL_PARSE_C */