blob: 1ecf7eb99bbe9e8f6b94df90b0059eaa52fe6d32 [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
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047 */
48/*
49 * The ITU-T X.509 standard defines a certificate format for PKI.
50 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020051 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
52 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
53 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054 *
55 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
56 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
57 */
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020063#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/x509_crl.h"
68#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000069
70#include <string.h>
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#else
Rich Evans00ab4702015-02-06 13:43:58 +000079#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000080#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020082#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084#endif
85
Paul Bakkerfa6a6202013-10-28 18:48:30 +010086#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087#include <windows.h>
88#else
89#include <time.h>
90#endif
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#include <stdio.h>
94#endif
95
Paul Bakker34617722014-06-13 17:20:13 +020096/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020098 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
99}
100
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101/*
102 * Version ::= INTEGER { v1(0), v2(1) }
103 */
104static int x509_crl_get_version( unsigned char **p,
105 const unsigned char *end,
106 int *ver )
107{
108 int ret;
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113 {
114 *ver = 0;
115 return( 0 );
116 }
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119 }
120
121 return( 0 );
122}
123
124/*
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100125 * X.509 CRL v2 extensions
126 *
127 * We currently don't parse any extension's content, but we do check that the
128 * list of extensions is well-formed and abort on critical extensions (that
129 * are unsupported as we don't support any extension so far)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130 */
131static int x509_get_crl_ext( unsigned char **p,
132 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134{
135 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136
Hanno Becker1de13db2019-02-12 17:22:36 +0000137 if( *p == end )
138 return( 0 );
139
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100140 /*
141 * crlExtensions [0] EXPLICIT Extensions OPTIONAL
142 * -- if present, version MUST be v2
143 */
144 if( ( ret = mbedtls_x509_get_ext( p, end, ext, 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146
Hanno Becker1de13db2019-02-12 17:22:36 +0000147 end = ext->p + ext->len;
148
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149 while( *p < end )
150 {
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100151 /*
152 * Extension ::= SEQUENCE {
153 * extnID OBJECT IDENTIFIER,
154 * critical BOOLEAN DEFAULT FALSE,
155 * extnValue OCTET STRING }
156 */
157 int is_critical = 0;
158 const unsigned char *end_ext_data;
159 size_t len;
160
161 /* Get enclosing sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
163 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
164 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100166 end_ext_data = *p + len;
167
168 /* Get OID (currently ignored) */
169 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
170 MBEDTLS_ASN1_OID ) ) != 0 )
171 {
172 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
173 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174 *p += len;
Manuel Pégourié-Gonnard5a9f46e2018-03-13 11:53:30 +0100175
176 /* Get optional critical */
177 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data,
178 &is_critical ) ) != 0 &&
179 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
180 {
181 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
182 }
183
184 /* Data should be octet string type */
185 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
186 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
187 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
188
189 /* Ignore data so far and just check its length */
190 *p += len;
191 if( *p != end_ext_data )
192 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
193 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
194
195 /* Abort on (unsupported) critical extensions */
196 if( is_critical )
197 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
198 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199 }
200
201 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
203 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
205 return( 0 );
206}
207
208/*
209 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
210 */
211static int x509_get_crl_entry_ext( unsigned char **p,
212 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214{
215 int ret;
216 size_t len = 0;
217
218 /* OPTIONAL */
Paul Bakker66d5d072014-06-17 16:39:18 +0200219 if( end <= *p )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220 return( 0 );
221
222 ext->tag = **p;
223 ext->p = *p;
224
225 /*
226 * Get CRL-entry extension sequence header
227 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
230 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233 {
234 ext->p = NULL;
235 return( 0 );
236 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238 }
239
Paul Bakker9af723c2014-05-01 13:03:14 +0200240 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241
242 if( end != *p + ext->len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
244 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200245
246 while( *p < end )
247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
249 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
250 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251
252 *p += len;
253 }
254
255 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
257 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258
259 return( 0 );
260}
261
262/*
263 * X.509 CRL Entries
264 */
265static int x509_get_entries( unsigned char **p,
266 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_x509_crl_entry *entry )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268{
269 int ret;
270 size_t entry_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_x509_crl_entry *cur_entry = entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200272
273 if( *p == end )
274 return( 0 );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( ( ret = mbedtls_asn1_get_tag( p, end, &entry_len,
277 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280 return( 0 );
281
282 return( ret );
283 }
284
285 end = *p + entry_len;
286
287 while( *p < end )
288 {
289 size_t len2;
290 const unsigned char *end2;
291
Gilles Peskine78e54b92020-07-16 18:26:29 +0200292 cur_entry->raw.tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 if( ( ret = mbedtls_asn1_get_tag( p, end, &len2,
294 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295 {
296 return( ret );
297 }
298
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299 cur_entry->raw.p = *p;
300 cur_entry->raw.len = len2;
301 end2 = *p + len2;
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 if( ( ret = mbedtls_x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304 return( ret );
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 if( ( ret = mbedtls_x509_get_time( p, end2,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200307 &cur_entry->revocation_date ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308 return( ret );
309
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200310 if( ( ret = x509_get_crl_entry_ext( p, end2,
311 &cur_entry->entry_ext ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200312 return( ret );
313
Paul Bakker66d5d072014-06-17 16:39:18 +0200314 if( *p < end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200316 cur_entry->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl_entry ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317
318 if( cur_entry->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200319 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320
321 cur_entry = cur_entry->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322 }
323 }
324
325 return( 0 );
326}
327
328/*
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100329 * Parse one CRLs in DER format and append it to the chained list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100332 const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333{
334 int ret;
335 size_t len;
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100336 unsigned char *p = NULL, *end = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
338 mbedtls_x509_crl *crl = chain;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
340 /*
341 * Check for valid input
342 */
343 if( crl == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
347 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
348 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
350 /*
351 * Add new CRL on the end of the chain if needed.
352 */
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100353 while( crl->version != 0 && crl->next != NULL )
354 crl = crl->next;
355
Paul Bakker66d5d072014-06-17 16:39:18 +0200356 if( crl->version != 0 && crl->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200358 crl->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
360 if( crl->next == NULL )
361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_x509_crl_free( crl );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200363 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 }
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_x509_crl_init( crl->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367 crl = crl->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368 }
369
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100370 /*
371 * Copy raw DER-encoded CRL
372 */
Andres Amaya Garciacb5123f2017-12-06 09:39:23 +0000373 if( buflen == 0 )
374 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Andres Amaya Garciac9d62262017-12-12 20:15:03 +0000375
376 p = mbedtls_calloc( 1, buflen );
377 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200378 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100380 memcpy( p, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
382 crl->raw.p = p;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100383 crl->raw.len = buflen;
384
385 end = p + buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
387 /*
388 * CertificateList ::= SEQUENCE {
389 * tbsCertList TBSCertList,
390 * signatureAlgorithm AlgorithmIdentifier,
391 * signatureValue BIT STRING }
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
394 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_x509_crl_free( crl );
397 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 }
399
400 if( len != (size_t) ( end - p ) )
401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_x509_crl_free( crl );
403 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
404 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 }
406
407 /*
408 * TBSCertList ::= SEQUENCE {
409 */
410 crl->tbs.p = p;
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
413 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_x509_crl_free( crl );
416 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 }
418
419 end = p + len;
420 crl->tbs.len = end - crl->tbs.p;
421
422 /*
423 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
424 * -- if present, MUST be v2
425 *
426 * signature AlgorithmIdentifier
427 */
428 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 ( ret = mbedtls_x509_get_alg( &p, end, &crl->sig_oid, &sig_params1 ) ) != 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
Andres AG4f753c12017-02-10 14:39:58 +0000435 if( crl->version < 0 || crl->version > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_x509_crl_free( crl );
438 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439 }
440
Andres AG4f753c12017-02-10 14:39:58 +0000441 crl->version++;
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 if( ( ret = mbedtls_x509_get_sig_alg( &crl->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200444 &crl->sig_md, &crl->sig_pk,
445 &crl->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_x509_crl_free( crl );
448 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449 }
450
451 /*
452 * issuer Name
453 */
454 crl->issuer_raw.p = p;
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
457 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_x509_crl_free( crl );
460 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 }
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 return( ret );
467 }
468
469 crl->issuer_raw.len = p - crl->issuer_raw.p;
470
471 /*
472 * thisUpdate Time
473 * nextUpdate Time OPTIONAL
474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 return( ret );
479 }
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
484 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) &&
485 ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
486 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489 return( ret );
490 }
491 }
492
493 /*
494 * revokedCertificates SEQUENCE OF SEQUENCE {
495 * userCertificate CertificateSerialNumber,
496 * revocationDate Time,
497 * crlEntryExtensions Extensions OPTIONAL
498 * -- if present, MUST be v2
499 * } OPTIONAL
500 */
501 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
502 {
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
507 /*
508 * crlExtensions EXPLICIT Extensions OPTIONAL
509 * -- if present, MUST be v2
510 */
511 if( crl->version == 2 )
512 {
513 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
514
515 if( ret != 0 )
516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 return( ret );
519 }
520 }
521
522 if( p != end )
523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_x509_crl_free( crl );
525 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
526 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527 }
528
529 end = crl->raw.p + crl->raw.len;
530
531 /*
532 * signatureAlgorithm AlgorithmIdentifier,
533 * signatureValue BIT STRING
534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538 return( ret );
539 }
540
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100541 if( crl->sig_oid.len != sig_oid2.len ||
542 memcmp( crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200543 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200544 ( sig_params1.len != 0 &&
545 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_x509_crl_free( crl );
548 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549 }
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 if( ( ret = mbedtls_x509_get_sig( &p, end, &crl->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554 return( ret );
555 }
556
557 if( p != end )
558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_x509_crl_free( crl );
560 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
561 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562 }
563
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100564 return( 0 );
565}
566
567/*
568 * Parse one or more CRLs and add them to the chained list
569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100571{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100573 int ret;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100574 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_pem_context pem;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100576 int is_pem = 0;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100577
578 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100580
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100581 do
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200584
Simon Butcher97e82902016-05-19 00:22:37 +0100585 // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
586 // string
587 if( buflen == 0 || buf[buflen - 1] != '\0' )
588 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
589 else
590 ret = mbedtls_pem_read_buffer( &pem,
591 "-----BEGIN X509 CRL-----",
592 "-----END X509 CRL-----",
593 buf, NULL, 0, &use_len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100595 if( ret == 0 )
596 {
597 /*
598 * Was PEM encoded
599 */
600 is_pem = 1;
601
602 buflen -= use_len;
603 buf += use_len;
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 if( ( ret = mbedtls_x509_crl_parse_der( chain,
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100606 pem.buf, pem.buflen ) ) != 0 )
607 {
Andres AG5708dcb2016-12-08 17:19:21 +0000608 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100609 return( ret );
610 }
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100611 }
Andres AG939954c2016-12-08 17:08:44 +0000612 else if( is_pem )
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100615 return( ret );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100616 }
Andres AG5708dcb2016-12-08 17:19:21 +0000617
618 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100619 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200620 /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
621 * And a valid CRL cannot be less than 1 byte anyway. */
622 while( is_pem && buflen > 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100624 if( is_pem )
625 return( 0 );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100626 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#endif /* MBEDTLS_PEM_PARSE_C */
628 return( mbedtls_x509_crl_parse_der( chain, buf, buflen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629}
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632/*
633 * Load one or more CRLs and add them to the chained list
634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636{
637 int ret;
638 size_t n;
639 unsigned char *buf;
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 return( ret );
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 ret = mbedtls_x509_crl_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200646 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648
649 return( ret );
650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653/*
654 * Return an informational string about the certificate.
655 */
656#define BEFORE_COLON 14
657#define BC "14"
658/*
659 * Return an informational string about the CRL.
660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
662 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663{
664 int ret;
665 size_t n;
666 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 const mbedtls_x509_crl_entry *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668
669 p = buf;
670 n = size;
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 prefix, crl->version );
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, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200677 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200679 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 ret = mbedtls_snprintf( p, n, "\n%sthis update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
683 crl->this_update.year, crl->this_update.mon,
684 crl->this_update.day, crl->this_update.hour,
685 crl->this_update.min, crl->this_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200686 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 ret = mbedtls_snprintf( p, n, "\n%snext update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
690 crl->next_update.year, crl->next_update.mon,
691 crl->next_update.day, crl->next_update.hour,
692 crl->next_update.min, crl->next_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200693 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694
695 entry = &crl->entry;
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
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
701 while( entry != NULL && entry->raw.len != 0 )
702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 ret = mbedtls_snprintf( p, n, "\n%sserial number: ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200705 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 ret = mbedtls_x509_serial_gets( p, n, &entry->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200708 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 ret = mbedtls_snprintf( p, n, " revocation date: " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 "%04d-%02d-%02d %02d:%02d:%02d",
712 entry->revocation_date.year, entry->revocation_date.mon,
713 entry->revocation_date.day, entry->revocation_date.hour,
714 entry->revocation_date.min, entry->revocation_date.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200715 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716
717 entry = entry->next;
718 }
719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200721 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 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 +0200724 crl->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200725 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200728 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729
730 return( (int) ( size - n ) );
731}
732
733/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200734 * Initialize a CRL chain
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736void mbedtls_x509_crl_init( mbedtls_x509_crl *crl )
Paul Bakker369d2eb2013-09-18 11:58:25 +0200737{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 memset( crl, 0, sizeof(mbedtls_x509_crl) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200739}
740
741/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742 * Unallocate all CRL data
743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744void mbedtls_x509_crl_free( mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_x509_crl *crl_cur = crl;
747 mbedtls_x509_crl *crl_prv;
748 mbedtls_x509_name *name_cur;
749 mbedtls_x509_name *name_prv;
750 mbedtls_x509_crl_entry *entry_cur;
751 mbedtls_x509_crl_entry *entry_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752
753 if( crl == NULL )
754 return;
755
756 do
757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
759 mbedtls_free( crl_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200760#endif
761
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 name_cur = crl_cur->issuer.next;
763 while( name_cur != NULL )
764 {
765 name_prv = name_cur;
766 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
768 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 }
770
771 entry_cur = crl_cur->entry.next;
772 while( entry_cur != NULL )
773 {
774 entry_prv = entry_cur;
775 entry_cur = entry_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_zeroize( entry_prv, sizeof( mbedtls_x509_crl_entry ) );
777 mbedtls_free( entry_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778 }
779
780 if( crl_cur->raw.p != NULL )
781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_zeroize( crl_cur->raw.p, crl_cur->raw.len );
783 mbedtls_free( crl_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784 }
785
786 crl_cur = crl_cur->next;
787 }
788 while( crl_cur != NULL );
789
790 crl_cur = crl;
791 do
792 {
793 crl_prv = crl_cur;
794 crl_cur = crl_cur->next;
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_zeroize( crl_prv, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797 if( crl_prv != crl )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_free( crl_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799 }
800 while( crl_cur != NULL );
801}
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_X509_CRL_PARSE_C */