blob: e20a258be170ba7df14ac8f9f8c86adff89bd2c3 [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ú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 * **********
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"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050069#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000070
71#include <string.h>
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#endif
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#else
Rich Evans00ab4702015-02-06 13:43:58 +000080#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000081#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020083#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#endif
86
Paul Bakkerfa6a6202013-10-28 18:48:30 +010087#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088#include <windows.h>
89#else
90#include <time.h>
91#endif
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094#include <stdio.h>
95#endif
96
97/*
98 * Version ::= INTEGER { v1(0), v2(1) }
99 */
100static int x509_crl_get_version( unsigned char **p,
101 const unsigned char *end,
102 int *ver )
103{
104 int ret;
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109 {
110 *ver = 0;
111 return( 0 );
112 }
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115 }
116
117 return( 0 );
118}
119
120/*
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100121 * X.509 CRL v2 extensions
122 *
123 * We currently don't parse any extension's content, but we do check that the
124 * list of extensions is well-formed and abort on critical extensions (that
125 * are unsupported as we don't support any extension so far)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200126 */
127static int x509_get_crl_ext( unsigned char **p,
128 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130{
131 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200132
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000133 if( *p == end )
134 return( 0 );
135
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100136 /*
137 * crlExtensions [0] EXPLICIT Extensions OPTIONAL
138 * -- if present, version MUST be v2
139 */
140 if( ( ret = mbedtls_x509_get_ext( p, end, ext, 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000143 end = ext->p + ext->len;
144
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 while( *p < end )
146 {
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100147 /*
148 * Extension ::= SEQUENCE {
149 * extnID OBJECT IDENTIFIER,
150 * critical BOOLEAN DEFAULT FALSE,
151 * extnValue OCTET STRING }
152 */
153 int is_critical = 0;
154 const unsigned char *end_ext_data;
155 size_t len;
156
157 /* Get enclosing sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
159 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
160 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100162 end_ext_data = *p + len;
163
164 /* Get OID (currently ignored) */
165 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
166 MBEDTLS_ASN1_OID ) ) != 0 )
167 {
168 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
169 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170 *p += len;
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100171
172 /* Get optional critical */
173 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data,
174 &is_critical ) ) != 0 &&
175 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
176 {
177 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
178 }
179
180 /* Data should be octet string type */
181 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
182 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
183 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
184
185 /* Ignore data so far and just check its length */
186 *p += len;
187 if( *p != end_ext_data )
188 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
189 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
190
191 /* Abort on (unsupported) critical extensions */
192 if( is_critical )
193 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
194 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195 }
196
197 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
199 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200200
201 return( 0 );
202}
203
204/*
205 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
206 */
207static int x509_get_crl_entry_ext( unsigned char **p,
208 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210{
211 int ret;
212 size_t len = 0;
213
214 /* OPTIONAL */
Paul Bakker66d5d072014-06-17 16:39:18 +0200215 if( end <= *p )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216 return( 0 );
217
218 ext->tag = **p;
219 ext->p = *p;
220
221 /*
222 * Get CRL-entry extension sequence header
223 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
226 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 {
230 ext->p = NULL;
231 return( 0 );
232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200234 }
235
Paul Bakker9af723c2014-05-01 13:03:14 +0200236 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237
238 if( end != *p + ext->len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
240 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241
242 while( *p < end )
243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
245 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
246 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247
248 *p += len;
249 }
250
251 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
253 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254
255 return( 0 );
256}
257
258/*
259 * X.509 CRL Entries
260 */
261static int x509_get_entries( unsigned char **p,
262 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_x509_crl_entry *entry )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264{
265 int ret;
266 size_t entry_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_x509_crl_entry *cur_entry = entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268
269 if( *p == end )
270 return( 0 );
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 if( ( ret = mbedtls_asn1_get_tag( p, end, &entry_len,
273 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276 return( 0 );
277
278 return( ret );
279 }
280
281 end = *p + entry_len;
282
283 while( *p < end )
284 {
285 size_t len2;
286 const unsigned char *end2;
287
Gilles Peskine65792352020-07-16 18:26:29 +0200288 cur_entry->raw.tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ( ret = mbedtls_asn1_get_tag( p, end, &len2,
290 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291 {
292 return( ret );
293 }
294
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295 cur_entry->raw.p = *p;
296 cur_entry->raw.len = len2;
297 end2 = *p + len2;
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 return( ret );
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 if( ( ret = mbedtls_x509_get_time( p, end2,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200303 &cur_entry->revocation_date ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304 return( ret );
305
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200306 if( ( ret = x509_get_crl_entry_ext( p, end2,
307 &cur_entry->entry_ext ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308 return( ret );
309
Paul Bakker66d5d072014-06-17 16:39:18 +0200310 if( *p < end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200312 cur_entry->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl_entry ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313
314 if( cur_entry->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200315 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200316
317 cur_entry = cur_entry->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318 }
319 }
320
321 return( 0 );
322}
323
324/*
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100325 * Parse one CRLs in DER format and append it to the chained list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100328 const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329{
330 int ret;
331 size_t len;
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100332 unsigned char *p = NULL, *end = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
334 mbedtls_x509_crl *crl = chain;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335
336 /*
337 * Check for valid input
338 */
339 if( crl == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
343 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
344 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
346 /*
347 * Add new CRL on the end of the chain if needed.
348 */
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100349 while( crl->version != 0 && crl->next != NULL )
350 crl = crl->next;
351
Paul Bakker66d5d072014-06-17 16:39:18 +0200352 if( crl->version != 0 && crl->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200354 crl->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355
356 if( crl->next == NULL )
357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_x509_crl_free( crl );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200359 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 }
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_x509_crl_init( crl->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 crl = crl->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 }
365
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100366 /*
367 * Copy raw DER-encoded CRL
368 */
Andres Amaya Garciacb5123f2017-12-06 09:39:23 +0000369 if( buflen == 0 )
370 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Andres Amaya Garciac9d62262017-12-12 20:15:03 +0000371
372 p = mbedtls_calloc( 1, buflen );
373 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200374 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100376 memcpy( p, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
378 crl->raw.p = p;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100379 crl->raw.len = buflen;
380
381 end = p + buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382
383 /*
384 * CertificateList ::= SEQUENCE {
385 * tbsCertList TBSCertList,
386 * signatureAlgorithm AlgorithmIdentifier,
387 * signatureValue BIT STRING }
388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
390 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_x509_crl_free( crl );
393 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394 }
395
396 if( len != (size_t) ( end - p ) )
397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_x509_crl_free( crl );
399 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
400 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 }
402
403 /*
404 * TBSCertList ::= SEQUENCE {
405 */
406 crl->tbs.p = p;
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
409 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_x509_crl_free( crl );
412 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 }
414
415 end = p + len;
416 crl->tbs.len = end - crl->tbs.p;
417
418 /*
419 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
420 * -- if present, MUST be v2
421 *
422 * signature AlgorithmIdentifier
423 */
424 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 ( ret = mbedtls_x509_get_alg( &p, end, &crl->sig_oid, &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428 return( ret );
429 }
430
Andres AG4f753c12017-02-10 14:39:58 +0000431 if( crl->version < 0 || crl->version > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_x509_crl_free( crl );
434 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435 }
436
Andres AG4f753c12017-02-10 14:39:58 +0000437 crl->version++;
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 if( ( ret = mbedtls_x509_get_sig_alg( &crl->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200440 &crl->sig_md, &crl->sig_pk,
441 &crl->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_x509_crl_free( crl );
444 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 }
446
447 /*
448 * issuer Name
449 */
450 crl->issuer_raw.p = p;
451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
453 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_x509_crl_free( crl );
456 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457 }
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 return( ret );
463 }
464
465 crl->issuer_raw.len = p - crl->issuer_raw.p;
466
467 /*
468 * thisUpdate Time
469 * nextUpdate Time OPTIONAL
470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 return( ret );
475 }
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 if( ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
480 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) &&
481 ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
482 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 return( ret );
486 }
487 }
488
489 /*
490 * revokedCertificates SEQUENCE OF SEQUENCE {
491 * userCertificate CertificateSerialNumber,
492 * revocationDate Time,
493 * crlEntryExtensions Extensions OPTIONAL
494 * -- if present, MUST be v2
495 * } OPTIONAL
496 */
497 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 return( ret );
501 }
502
503 /*
504 * crlExtensions EXPLICIT Extensions OPTIONAL
505 * -- if present, MUST be v2
506 */
507 if( crl->version == 2 )
508 {
509 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
510
511 if( ret != 0 )
512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514 return( ret );
515 }
516 }
517
518 if( p != end )
519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_x509_crl_free( crl );
521 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
522 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 }
524
525 end = crl->raw.p + crl->raw.len;
526
527 /*
528 * signatureAlgorithm AlgorithmIdentifier,
529 * signatureValue BIT STRING
530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 return( ret );
535 }
536
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100537 if( crl->sig_oid.len != sig_oid2.len ||
538 memcmp( crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200539 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200540 ( sig_params1.len != 0 &&
541 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_x509_crl_free( crl );
544 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545 }
546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 if( ( ret = mbedtls_x509_get_sig( &p, end, &crl->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550 return( ret );
551 }
552
553 if( p != end )
554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_x509_crl_free( crl );
556 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
557 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558 }
559
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100560 return( 0 );
561}
562
563/*
564 * Parse one or more CRLs and add them to the chained list
565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100569 int ret;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100570 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_pem_context pem;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100572 int is_pem = 0;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100573
574 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100576
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100577 do
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200580
Simon Butcher97e82902016-05-19 00:22:37 +0100581 // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
582 // string
583 if( buflen == 0 || buf[buflen - 1] != '\0' )
584 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
585 else
586 ret = mbedtls_pem_read_buffer( &pem,
587 "-----BEGIN X509 CRL-----",
588 "-----END X509 CRL-----",
589 buf, NULL, 0, &use_len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100591 if( ret == 0 )
592 {
593 /*
594 * Was PEM encoded
595 */
596 is_pem = 1;
597
598 buflen -= use_len;
599 buf += use_len;
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 if( ( ret = mbedtls_x509_crl_parse_der( chain,
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100602 pem.buf, pem.buflen ) ) != 0 )
603 {
Andres AG5708dcb2016-12-08 17:19:21 +0000604 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100605 return( ret );
606 }
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100607 }
Andres AG939954c2016-12-08 17:08:44 +0000608 else if( is_pem )
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100611 return( ret );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100612 }
Andres AG5708dcb2016-12-08 17:19:21 +0000613
614 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100615 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200616 /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
617 * And a valid CRL cannot be less than 1 byte anyway. */
618 while( is_pem && buflen > 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100620 if( is_pem )
621 return( 0 );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100622 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#endif /* MBEDTLS_PEM_PARSE_C */
624 return( mbedtls_x509_crl_parse_der( chain, buf, buflen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625}
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628/*
629 * Load one or more CRLs and add them to the chained list
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632{
633 int ret;
634 size_t n;
635 unsigned char *buf;
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638 return( ret );
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 ret = mbedtls_x509_crl_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500642 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644
645 return( ret );
646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649/*
650 * Return an informational string about the certificate.
651 */
652#define BEFORE_COLON 14
653#define BC "14"
654/*
655 * Return an informational string about the CRL.
656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
658 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659{
660 int ret;
661 size_t n;
662 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 const mbedtls_x509_crl_entry *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
665 p = buf;
666 n = size;
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200669 prefix, crl->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200670 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200673 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200675 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 ret = mbedtls_snprintf( p, n, "\n%sthis update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
679 crl->this_update.year, crl->this_update.mon,
680 crl->this_update.day, crl->this_update.hour,
681 crl->this_update.min, crl->this_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200682 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 ret = mbedtls_snprintf( p, n, "\n%snext update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
686 crl->next_update.year, crl->next_update.mon,
687 crl->next_update.day, crl->next_update.hour,
688 crl->next_update.min, crl->next_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200689 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
691 entry = &crl->entry;
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200695 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
697 while( entry != NULL && entry->raw.len != 0 )
698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 ret = mbedtls_snprintf( p, n, "\n%sserial number: ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200701 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 ret = mbedtls_x509_serial_gets( p, n, &entry->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200704 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 ret = mbedtls_snprintf( p, n, " revocation date: " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707 "%04d-%02d-%02d %02d:%02d:%02d",
708 entry->revocation_date.year, entry->revocation_date.mon,
709 entry->revocation_date.day, entry->revocation_date.hour,
710 entry->revocation_date.min, entry->revocation_date.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200711 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712
713 entry = entry->next;
714 }
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200717 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 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 +0200720 crl->sig_opts );
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_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200724 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725
726 return( (int) ( size - n ) );
727}
728
729/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200730 * Initialize a CRL chain
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732void mbedtls_x509_crl_init( mbedtls_x509_crl *crl )
Paul Bakker369d2eb2013-09-18 11:58:25 +0200733{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 memset( crl, 0, sizeof(mbedtls_x509_crl) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200735}
736
737/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 * Unallocate all CRL data
739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740void mbedtls_x509_crl_free( mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_x509_crl *crl_cur = crl;
743 mbedtls_x509_crl *crl_prv;
744 mbedtls_x509_name *name_cur;
745 mbedtls_x509_name *name_prv;
746 mbedtls_x509_crl_entry *entry_cur;
747 mbedtls_x509_crl_entry *entry_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748
749 if( crl == NULL )
750 return;
751
752 do
753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
755 mbedtls_free( crl_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200756#endif
757
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758 name_cur = crl_cur->issuer.next;
759 while( name_cur != NULL )
760 {
761 name_prv = name_cur;
762 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500763 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 }
766
767 entry_cur = crl_cur->entry.next;
768 while( entry_cur != NULL )
769 {
770 entry_prv = entry_cur;
771 entry_cur = entry_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500772 mbedtls_platform_zeroize( entry_prv,
773 sizeof( mbedtls_x509_crl_entry ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_free( entry_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775 }
776
777 if( crl_cur->raw.p != NULL )
778 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500779 mbedtls_platform_zeroize( crl_cur->raw.p, crl_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_free( crl_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781 }
782
783 crl_cur = crl_cur->next;
784 }
785 while( crl_cur != NULL );
786
787 crl_cur = crl;
788 do
789 {
790 crl_prv = crl_cur;
791 crl_cur = crl_cur->next;
792
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500793 mbedtls_platform_zeroize( crl_prv, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794 if( crl_prv != crl )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_free( crl_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796 }
797 while( crl_cur != NULL );
798}
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#endif /* MBEDTLS_X509_CRL_PARSE_C */