blob: 73a300d217efd3894d6d7003b138ea58f91a2482 [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úti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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"
Rich Evans00ab4702015-02-06 13:43:58 +000067
68#include <string.h>
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#else
Rich Evans00ab4702015-02-06 13:43:58 +000077#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000078#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020080#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082#endif
83
Paul Bakkerfa6a6202013-10-28 18:48:30 +010084#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#include <windows.h>
86#else
87#include <time.h>
88#endif
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091#include <stdio.h>
92#endif
93
Paul Bakker34617722014-06-13 17:20:13 +020094/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020096 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
97}
98
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099/*
100 * Version ::= INTEGER { v1(0), v2(1) }
101 */
102static int x509_crl_get_version( unsigned char **p,
103 const unsigned char *end,
104 int *ver )
105{
106 int ret;
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111 {
112 *ver = 0;
113 return( 0 );
114 }
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117 }
118
119 return( 0 );
120}
121
122/*
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100123 * X.509 CRL v2 extensions
124 *
125 * We currently don't parse any extension's content, but we do check that the
126 * list of extensions is well-formed and abort on critical extensions (that
127 * are unsupported as we don't support any extension so far)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128 */
129static int x509_get_crl_ext( unsigned char **p,
130 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200132{
133 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134
Hanno Becker1de13db2019-02-12 17:22:36 +0000135 if( *p == end )
136 return( 0 );
137
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100138 /*
139 * crlExtensions [0] EXPLICIT Extensions OPTIONAL
140 * -- if present, version MUST be v2
141 */
142 if( ( ret = mbedtls_x509_get_ext( p, end, ext, 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200143 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144
Hanno Becker1de13db2019-02-12 17:22:36 +0000145 end = ext->p + ext->len;
146
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147 while( *p < end )
148 {
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100149 /*
150 * Extension ::= SEQUENCE {
151 * extnID OBJECT IDENTIFIER,
152 * critical BOOLEAN DEFAULT FALSE,
153 * extnValue OCTET STRING }
154 */
155 int is_critical = 0;
156 const unsigned char *end_ext_data;
157 size_t len;
158
159 /* Get enclosing sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
161 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
162 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100164 end_ext_data = *p + len;
165
166 /* Get OID (currently ignored) */
167 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
168 MBEDTLS_ASN1_OID ) ) != 0 )
169 {
170 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
171 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172 *p += len;
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100173
174 /* Get optional critical */
175 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data,
176 &is_critical ) ) != 0 &&
177 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
178 {
179 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
180 }
181
182 /* Data should be octet string type */
183 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
184 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
185 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
186
187 /* Ignore data so far and just check its length */
188 *p += len;
189 if( *p != end_ext_data )
190 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
191 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
192
193 /* Abort on (unsupported) critical extensions */
194 if( is_critical )
195 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
196 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197 }
198
199 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
201 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202
203 return( 0 );
204}
205
206/*
207 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
208 */
209static int x509_get_crl_entry_ext( unsigned char **p,
210 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212{
213 int ret;
214 size_t len = 0;
215
216 /* OPTIONAL */
Paul Bakker66d5d072014-06-17 16:39:18 +0200217 if( end <= *p )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218 return( 0 );
219
220 ext->tag = **p;
221 ext->p = *p;
222
223 /*
224 * Get CRL-entry extension sequence header
225 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
228 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231 {
232 ext->p = NULL;
233 return( 0 );
234 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236 }
237
Paul Bakker9af723c2014-05-01 13:03:14 +0200238 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239
240 if( end != *p + ext->len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
242 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243
244 while( *p < end )
245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
247 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
248 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249
250 *p += len;
251 }
252
253 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
255 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256
257 return( 0 );
258}
259
260/*
261 * X.509 CRL Entries
262 */
263static int x509_get_entries( unsigned char **p,
264 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_x509_crl_entry *entry )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200266{
267 int ret;
268 size_t entry_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_x509_crl_entry *cur_entry = entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270
271 if( *p == end )
272 return( 0 );
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 if( ( ret = mbedtls_asn1_get_tag( p, end, &entry_len,
275 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 return( 0 );
279
280 return( ret );
281 }
282
283 end = *p + entry_len;
284
285 while( *p < end )
286 {
287 size_t len2;
288 const unsigned char *end2;
289
Gilles Peskine78e54b92020-07-16 18:26:29 +0200290 cur_entry->raw.tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ( ret = mbedtls_asn1_get_tag( p, end, &len2,
292 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 {
294 return( ret );
295 }
296
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297 cur_entry->raw.p = *p;
298 cur_entry->raw.len = len2;
299 end2 = *p + len2;
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302 return( ret );
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 if( ( ret = mbedtls_x509_get_time( p, end2,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200305 &cur_entry->revocation_date ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200306 return( ret );
307
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200308 if( ( ret = x509_get_crl_entry_ext( p, end2,
309 &cur_entry->entry_ext ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200310 return( ret );
311
Paul Bakker66d5d072014-06-17 16:39:18 +0200312 if( *p < end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200314 cur_entry->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl_entry ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315
316 if( cur_entry->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200317 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318
319 cur_entry = cur_entry->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320 }
321 }
322
323 return( 0 );
324}
325
326/*
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100327 * Parse one CRLs in DER format and append it to the chained list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100330 const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331{
332 int ret;
333 size_t len;
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100334 unsigned char *p = NULL, *end = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
336 mbedtls_x509_crl *crl = chain;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337
338 /*
339 * Check for valid input
340 */
341 if( crl == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
345 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
346 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347
348 /*
349 * Add new CRL on the end of the chain if needed.
350 */
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100351 while( crl->version != 0 && crl->next != NULL )
352 crl = crl->next;
353
Paul Bakker66d5d072014-06-17 16:39:18 +0200354 if( crl->version != 0 && crl->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200356 crl->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
358 if( crl->next == NULL )
359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_x509_crl_free( crl );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200361 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 }
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_x509_crl_init( crl->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 crl = crl->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 }
367
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100368 /*
369 * Copy raw DER-encoded CRL
370 */
Andres Amaya Garciacb5123f2017-12-06 09:39:23 +0000371 if( buflen == 0 )
372 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Andres Amaya Garciac9d62262017-12-12 20:15:03 +0000373
374 p = mbedtls_calloc( 1, buflen );
375 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200376 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100378 memcpy( p, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
380 crl->raw.p = p;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100381 crl->raw.len = buflen;
382
383 end = p + buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
385 /*
386 * CertificateList ::= SEQUENCE {
387 * tbsCertList TBSCertList,
388 * signatureAlgorithm AlgorithmIdentifier,
389 * signatureValue BIT STRING }
390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
392 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_x509_crl_free( crl );
395 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396 }
397
398 if( len != (size_t) ( end - p ) )
399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_x509_crl_free( crl );
401 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
402 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 }
404
405 /*
406 * TBSCertList ::= SEQUENCE {
407 */
408 crl->tbs.p = p;
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
411 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 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_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 }
416
417 end = p + len;
418 crl->tbs.len = end - crl->tbs.p;
419
420 /*
421 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
422 * -- if present, MUST be v2
423 *
424 * signature AlgorithmIdentifier
425 */
426 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 ( ret = mbedtls_x509_get_alg( &p, end, &crl->sig_oid, &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 return( ret );
431 }
432
Andres AG4f753c12017-02-10 14:39:58 +0000433 if( crl->version < 0 || crl->version > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_x509_crl_free( crl );
436 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437 }
438
Andres AG4f753c12017-02-10 14:39:58 +0000439 crl->version++;
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 if( ( ret = mbedtls_x509_get_sig_alg( &crl->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200442 &crl->sig_md, &crl->sig_pk,
443 &crl->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_x509_crl_free( crl );
446 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447 }
448
449 /*
450 * issuer Name
451 */
452 crl->issuer_raw.p = p;
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
455 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_x509_crl_free( crl );
458 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 }
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464 return( ret );
465 }
466
467 crl->issuer_raw.len = p - crl->issuer_raw.p;
468
469 /*
470 * thisUpdate Time
471 * nextUpdate Time OPTIONAL
472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 return( ret );
477 }
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
482 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) &&
483 ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
484 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487 return( ret );
488 }
489 }
490
491 /*
492 * revokedCertificates SEQUENCE OF SEQUENCE {
493 * userCertificate CertificateSerialNumber,
494 * revocationDate Time,
495 * crlEntryExtensions Extensions OPTIONAL
496 * -- if present, MUST be v2
497 * } OPTIONAL
498 */
499 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 return( ret );
503 }
504
505 /*
506 * crlExtensions EXPLICIT Extensions OPTIONAL
507 * -- if present, MUST be v2
508 */
509 if( crl->version == 2 )
510 {
511 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
512
513 if( ret != 0 )
514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516 return( ret );
517 }
518 }
519
520 if( p != end )
521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_x509_crl_free( crl );
523 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
524 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 }
526
527 end = crl->raw.p + crl->raw.len;
528
529 /*
530 * signatureAlgorithm AlgorithmIdentifier,
531 * signatureValue BIT STRING
532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536 return( ret );
537 }
538
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100539 if( crl->sig_oid.len != sig_oid2.len ||
540 memcmp( crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200541 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200542 ( sig_params1.len != 0 &&
543 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_x509_crl_free( crl );
546 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547 }
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( ret = mbedtls_x509_get_sig( &p, end, &crl->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 return( ret );
553 }
554
555 if( p != end )
556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_x509_crl_free( crl );
558 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
559 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 }
561
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100562 return( 0 );
563}
564
565/*
566 * Parse one or more CRLs and add them to the chained list
567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100571 int ret;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100572 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_pem_context pem;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100574 int is_pem = 0;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100575
576 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100578
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100579 do
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200582
Simon Butcher97e82902016-05-19 00:22:37 +0100583 // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
584 // string
585 if( buflen == 0 || buf[buflen - 1] != '\0' )
586 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
587 else
588 ret = mbedtls_pem_read_buffer( &pem,
589 "-----BEGIN X509 CRL-----",
590 "-----END X509 CRL-----",
591 buf, NULL, 0, &use_len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100593 if( ret == 0 )
594 {
595 /*
596 * Was PEM encoded
597 */
598 is_pem = 1;
599
600 buflen -= use_len;
601 buf += use_len;
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( ( ret = mbedtls_x509_crl_parse_der( chain,
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100604 pem.buf, pem.buflen ) ) != 0 )
605 {
Andres AG5708dcb2016-12-08 17:19:21 +0000606 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100607 return( ret );
608 }
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100609 }
Andres AG939954c2016-12-08 17:08:44 +0000610 else if( is_pem )
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100613 return( ret );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100614 }
Andres AG5708dcb2016-12-08 17:19:21 +0000615
616 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100617 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200618 /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
619 * And a valid CRL cannot be less than 1 byte anyway. */
620 while( is_pem && buflen > 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100622 if( is_pem )
623 return( 0 );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100624 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#endif /* MBEDTLS_PEM_PARSE_C */
626 return( mbedtls_x509_crl_parse_der( chain, buf, buflen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627}
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630/*
631 * Load one or more CRLs and add them to the chained list
632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634{
635 int ret;
636 size_t n;
637 unsigned char *buf;
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 return( ret );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 ret = mbedtls_x509_crl_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200644 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646
647 return( ret );
648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651/*
652 * Return an informational string about the certificate.
653 */
654#define BEFORE_COLON 14
655#define BC "14"
656/*
657 * Return an informational string about the CRL.
658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
660 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661{
662 int ret;
663 size_t n;
664 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 const mbedtls_x509_crl_entry *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666
667 p = buf;
668 n = size;
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671 prefix, crl->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200672 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200675 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200677 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 ret = mbedtls_snprintf( p, n, "\n%sthis update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
681 crl->this_update.year, crl->this_update.mon,
682 crl->this_update.day, crl->this_update.hour,
683 crl->this_update.min, crl->this_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200684 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 ret = mbedtls_snprintf( p, n, "\n%snext update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
688 crl->next_update.year, crl->next_update.mon,
689 crl->next_update.day, crl->next_update.hour,
690 crl->next_update.min, crl->next_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200691 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692
693 entry = &crl->entry;
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200697 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
699 while( entry != NULL && entry->raw.len != 0 )
700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 ret = mbedtls_snprintf( p, n, "\n%sserial number: ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200703 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 ret = mbedtls_x509_serial_gets( p, n, &entry->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200706 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 ret = mbedtls_snprintf( p, n, " revocation date: " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709 "%04d-%02d-%02d %02d:%02d:%02d",
710 entry->revocation_date.year, entry->revocation_date.mon,
711 entry->revocation_date.day, entry->revocation_date.hour,
712 entry->revocation_date.min, entry->revocation_date.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200713 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714
715 entry = entry->next;
716 }
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
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_x509_sig_alg_gets( p, n, &crl->sig_oid, crl->sig_pk, crl->sig_md,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +0200722 crl->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200723 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200726 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727
728 return( (int) ( size - n ) );
729}
730
731/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200732 * Initialize a CRL chain
733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734void mbedtls_x509_crl_init( mbedtls_x509_crl *crl )
Paul Bakker369d2eb2013-09-18 11:58:25 +0200735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 memset( crl, 0, sizeof(mbedtls_x509_crl) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200737}
738
739/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 * Unallocate all CRL data
741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742void mbedtls_x509_crl_free( mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_x509_crl *crl_cur = crl;
745 mbedtls_x509_crl *crl_prv;
746 mbedtls_x509_name *name_cur;
747 mbedtls_x509_name *name_prv;
748 mbedtls_x509_crl_entry *entry_cur;
749 mbedtls_x509_crl_entry *entry_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750
751 if( crl == NULL )
752 return;
753
754 do
755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
757 mbedtls_free( crl_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200758#endif
759
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760 name_cur = crl_cur->issuer.next;
761 while( name_cur != NULL )
762 {
763 name_prv = name_cur;
764 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
766 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767 }
768
769 entry_cur = crl_cur->entry.next;
770 while( entry_cur != NULL )
771 {
772 entry_prv = entry_cur;
773 entry_cur = entry_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_zeroize( entry_prv, sizeof( mbedtls_x509_crl_entry ) );
775 mbedtls_free( entry_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776 }
777
778 if( crl_cur->raw.p != NULL )
779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_zeroize( crl_cur->raw.p, crl_cur->raw.len );
781 mbedtls_free( crl_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782 }
783
784 crl_cur = crl_cur->next;
785 }
786 while( crl_cur != NULL );
787
788 crl_cur = crl;
789 do
790 {
791 crl_prv = crl_cur;
792 crl_cur = crl_cur->next;
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_zeroize( crl_prv, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795 if( crl_prv != crl )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_free( crl_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797 }
798 while( crl_cur != NULL );
799}
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#endif /* MBEDTLS_X509_CRL_PARSE_C */