blob: 67eb5c1a17031d67f3e989a026b6ed7dbc40f784 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 Certidicate Revocation List (CRL) parsing
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
30 */
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/x509_crl.h"
Janos Follath73c616b2019-12-18 15:07:04 +000041#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050043#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000044
45#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#else
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000055#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020057#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059#endif
60
Paul Bakkerfa6a6202013-10-28 18:48:30 +010061#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <windows.h>
63#else
64#include <time.h>
65#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <stdio.h>
69#endif
70
71/*
72 * Version ::= INTEGER { v1(0), v2(1) }
73 */
74static int x509_crl_get_version( unsigned char **p,
75 const unsigned char *end,
76 int *ver )
77{
Janos Follath865b3eb2019-12-16 11:46:15 +000078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083 {
84 *ver = 0;
85 return( 0 );
86 }
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089 }
90
91 return( 0 );
92}
93
94/*
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +010095 * X.509 CRL v2 extensions
96 *
97 * We currently don't parse any extension's content, but we do check that the
98 * list of extensions is well-formed and abort on critical extensions (that
99 * are unsupported as we don't support any extension so far)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100 */
101static int x509_get_crl_ext( unsigned char **p,
102 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104{
Janos Follath865b3eb2019-12-16 11:46:15 +0000105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106
Hanno Becker12f62fb2019-02-12 17:22:36 +0000107 if( *p == end )
108 return( 0 );
109
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100110 /*
111 * crlExtensions [0] EXPLICIT Extensions OPTIONAL
112 * -- if present, version MUST be v2
113 */
114 if( ( ret = mbedtls_x509_get_ext( p, end, ext, 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116
Hanno Becker12f62fb2019-02-12 17:22:36 +0000117 end = ext->p + ext->len;
118
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119 while( *p < end )
120 {
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100121 /*
122 * Extension ::= SEQUENCE {
123 * extnID OBJECT IDENTIFIER,
124 * critical BOOLEAN DEFAULT FALSE,
125 * extnValue OCTET STRING }
126 */
127 int is_critical = 0;
128 const unsigned char *end_ext_data;
129 size_t len;
130
131 /* Get enclosing sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
133 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
134 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100136 end_ext_data = *p + len;
137
138 /* Get OID (currently ignored) */
139 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
140 MBEDTLS_ASN1_OID ) ) != 0 )
141 {
142 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
143 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144 *p += len;
Manuel Pégourié-Gonnardfd3e4fb2018-03-13 11:53:30 +0100145
146 /* Get optional critical */
147 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data,
148 &is_critical ) ) != 0 &&
149 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
150 {
151 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
152 }
153
154 /* Data should be octet string type */
155 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
156 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
157 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
158
159 /* Ignore data so far and just check its length */
160 *p += len;
161 if( *p != end_ext_data )
162 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
163 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
164
165 /* Abort on (unsupported) critical extensions */
166 if( is_critical )
167 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
168 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169 }
170
171 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
173 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174
175 return( 0 );
176}
177
178/*
179 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
180 */
181static int x509_get_crl_entry_ext( unsigned char **p,
182 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_x509_buf *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200184{
Janos Follath865b3eb2019-12-16 11:46:15 +0000185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186 size_t len = 0;
187
188 /* OPTIONAL */
Paul Bakker66d5d072014-06-17 16:39:18 +0200189 if( end <= *p )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190 return( 0 );
191
192 ext->tag = **p;
193 ext->p = *p;
194
195 /*
196 * Get CRL-entry extension sequence header
197 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203 {
204 ext->p = NULL;
205 return( 0 );
206 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208 }
209
Paul Bakker9af723c2014-05-01 13:03:14 +0200210 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211
212 if( end != *p + ext->len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
214 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200215
216 while( *p < end )
217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
219 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
220 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221
222 *p += len;
223 }
224
225 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
227 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228
229 return( 0 );
230}
231
232/*
233 * X.509 CRL Entries
234 */
235static int x509_get_entries( unsigned char **p,
236 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_x509_crl_entry *entry )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238{
Janos Follath865b3eb2019-12-16 11:46:15 +0000239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240 size_t entry_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509_crl_entry *cur_entry = entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242
243 if( *p == end )
244 return( 0 );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( ( ret = mbedtls_asn1_get_tag( p, end, &entry_len,
247 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200250 return( 0 );
251
252 return( ret );
253 }
254
255 end = *p + entry_len;
256
257 while( *p < end )
258 {
259 size_t len2;
260 const unsigned char *end2;
261
Gilles Peskine5dd5a492020-07-16 18:26:29 +0200262 cur_entry->raw.tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ( ret = mbedtls_asn1_get_tag( p, end, &len2,
264 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265 {
266 return( ret );
267 }
268
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269 cur_entry->raw.p = *p;
270 cur_entry->raw.len = len2;
271 end2 = *p + len2;
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( ( ret = mbedtls_x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274 return( ret );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( ( ret = mbedtls_x509_get_time( p, end2,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200277 &cur_entry->revocation_date ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 return( ret );
279
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200280 if( ( ret = x509_get_crl_entry_ext( p, end2,
281 &cur_entry->entry_ext ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282 return( ret );
283
Paul Bakker66d5d072014-06-17 16:39:18 +0200284 if( *p < end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200286 cur_entry->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl_entry ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287
288 if( cur_entry->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290
291 cur_entry = cur_entry->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292 }
293 }
294
295 return( 0 );
296}
297
298/*
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100299 * Parse one CRLs in DER format and append it to the chained list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100302 const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200303{
Janos Follath865b3eb2019-12-16 11:46:15 +0000304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305 size_t len;
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100306 unsigned char *p = NULL, *end = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
308 mbedtls_x509_crl *crl = chain;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309
310 /*
311 * Check for valid input
312 */
313 if( crl == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
317 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
318 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319
320 /*
321 * Add new CRL on the end of the chain if needed.
322 */
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100323 while( crl->version != 0 && crl->next != NULL )
324 crl = crl->next;
325
Paul Bakker66d5d072014-06-17 16:39:18 +0200326 if( crl->version != 0 && crl->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200328 crl->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329
330 if( crl->next == NULL )
331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_x509_crl_free( crl );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200333 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334 }
335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_x509_crl_init( crl->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337 crl = crl->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338 }
339
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100340 /*
341 * Copy raw DER-encoded CRL
342 */
Andres Amaya Garciacb5123f2017-12-06 09:39:23 +0000343 if( buflen == 0 )
344 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Andres Amaya Garciac9d62262017-12-12 20:15:03 +0000345
346 p = mbedtls_calloc( 1, buflen );
347 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200348 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100350 memcpy( p, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351
352 crl->raw.p = p;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100353 crl->raw.len = buflen;
354
355 end = p + buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356
357 /*
358 * CertificateList ::= SEQUENCE {
359 * tbsCertList TBSCertList,
360 * signatureAlgorithm AlgorithmIdentifier,
361 * signatureValue BIT STRING }
362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
364 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_x509_crl_free( crl );
367 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368 }
369
370 if( len != (size_t) ( end - p ) )
371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_x509_crl_free( crl );
373 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
374 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375 }
376
377 /*
378 * TBSCertList ::= SEQUENCE {
379 */
380 crl->tbs.p = p;
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
383 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_x509_crl_free( crl );
386 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387 }
388
389 end = p + len;
390 crl->tbs.len = end - crl->tbs.p;
391
392 /*
393 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
394 * -- if present, MUST be v2
395 *
396 * signature AlgorithmIdentifier
397 */
398 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 ( ret = mbedtls_x509_get_alg( &p, end, &crl->sig_oid, &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 return( ret );
403 }
404
Andres AG4f753c12017-02-10 14:39:58 +0000405 if( crl->version < 0 || crl->version > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_x509_crl_free( crl );
408 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409 }
410
Andres AG4f753c12017-02-10 14:39:58 +0000411 crl->version++;
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 if( ( ret = mbedtls_x509_get_sig_alg( &crl->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200414 &crl->sig_md, &crl->sig_pk,
415 &crl->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_x509_crl_free( crl );
418 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 }
420
421 /*
422 * issuer Name
423 */
424 crl->issuer_raw.p = p;
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
427 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_x509_crl_free( crl );
430 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 }
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 return( ret );
437 }
438
439 crl->issuer_raw.len = p - crl->issuer_raw.p;
440
441 /*
442 * thisUpdate Time
443 * nextUpdate Time OPTIONAL
444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448 return( ret );
449 }
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
454 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) &&
455 ret != ( MBEDTLS_ERR_X509_INVALID_DATE +
456 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 return( ret );
460 }
461 }
462
463 /*
464 * revokedCertificates SEQUENCE OF SEQUENCE {
465 * userCertificate CertificateSerialNumber,
466 * revocationDate Time,
467 * crlEntryExtensions Extensions OPTIONAL
468 * -- if present, MUST be v2
469 * } OPTIONAL
470 */
471 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
472 {
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
477 /*
478 * crlExtensions EXPLICIT Extensions OPTIONAL
479 * -- if present, MUST be v2
480 */
481 if( crl->version == 2 )
482 {
483 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
484
485 if( ret != 0 )
486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 return( ret );
489 }
490 }
491
492 if( p != end )
493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_x509_crl_free( crl );
495 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
496 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 }
498
499 end = crl->raw.p + crl->raw.len;
500
501 /*
502 * signatureAlgorithm AlgorithmIdentifier,
503 * signatureValue BIT STRING
504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508 return( ret );
509 }
510
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100511 if( crl->sig_oid.len != sig_oid2.len ||
512 memcmp( crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200513 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200514 ( sig_params1.len != 0 &&
515 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_x509_crl_free( crl );
518 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519 }
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ( ret = mbedtls_x509_get_sig( &p, end, &crl->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_x509_crl_free( crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524 return( ret );
525 }
526
527 if( p != end )
528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_x509_crl_free( crl );
530 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
531 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 }
533
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100534 return( 0 );
535}
536
537/*
538 * Parse one or more CRLs and add them to the chained list
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#if defined(MBEDTLS_PEM_PARSE_C)
Janos Follath865b3eb2019-12-16 11:46:15 +0000543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Benjamin Kier36050732019-05-30 14:49:17 -0400544 size_t use_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_pem_context pem;
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100546 int is_pem = 0;
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100547
548 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100550
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100551 do
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200554
Simon Butcher97e82902016-05-19 00:22:37 +0100555 // Avoid calling mbedtls_pem_read_buffer() on non-null-terminated
556 // string
557 if( buflen == 0 || buf[buflen - 1] != '\0' )
558 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
559 else
560 ret = mbedtls_pem_read_buffer( &pem,
561 "-----BEGIN X509 CRL-----",
562 "-----END X509 CRL-----",
563 buf, NULL, 0, &use_len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100565 if( ret == 0 )
566 {
567 /*
568 * Was PEM encoded
569 */
570 is_pem = 1;
571
572 buflen -= use_len;
573 buf += use_len;
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 if( ( ret = mbedtls_x509_crl_parse_der( chain,
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100576 pem.buf, pem.buflen ) ) != 0 )
577 {
Andres AG5708dcb2016-12-08 17:19:21 +0000578 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100579 return( ret );
580 }
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100581 }
Andres AG939954c2016-12-08 17:08:44 +0000582 else if( is_pem )
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100585 return( ret );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100586 }
Andres AG5708dcb2016-12-08 17:19:21 +0000587
588 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100589 }
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200590 /* In the PEM case, buflen is 1 at the end, for the terminated NULL byte.
591 * And a valid CRL cannot be less than 1 byte anyway. */
592 while( is_pem && buflen > 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
Manuel Pégourié-Gonnard6ed2d922014-11-19 19:05:03 +0100594 if( is_pem )
595 return( 0 );
Manuel Pégourié-Gonnard426d4ae2014-11-19 16:58:28 +0100596 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#endif /* MBEDTLS_PEM_PARSE_C */
598 return( mbedtls_x509_crl_parse_der( chain, buf, buflen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599}
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602/*
603 * Load one or more CRLs and add them to the chained list
604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606{
Janos Follath865b3eb2019-12-16 11:46:15 +0000607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608 size_t n;
609 unsigned char *buf;
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612 return( ret );
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 ret = mbedtls_x509_crl_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500616 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618
619 return( ret );
620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623/*
624 * Return an informational string about the certificate.
625 */
626#define BEFORE_COLON 14
627#define BC "14"
628/*
629 * Return an informational string about the CRL.
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
632 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633{
Janos Follath865b3eb2019-12-16 11:46:15 +0000634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 size_t n;
636 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 const mbedtls_x509_crl_entry *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638
639 p = buf;
640 n = size;
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643 prefix, crl->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200644 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200647 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200649 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 ret = mbedtls_snprintf( p, n, "\n%sthis update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
653 crl->this_update.year, crl->this_update.mon,
654 crl->this_update.day, crl->this_update.hour,
655 crl->this_update.min, crl->this_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200656 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 ret = mbedtls_snprintf( p, n, "\n%snext update : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
660 crl->next_update.year, crl->next_update.mon,
661 crl->next_update.day, crl->next_update.hour,
662 crl->next_update.min, crl->next_update.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200663 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
665 entry = &crl->entry;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200669 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
671 while( entry != NULL && entry->raw.len != 0 )
672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 ret = mbedtls_snprintf( p, n, "\n%sserial number: ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 prefix );
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_x509_serial_gets( p, n, &entry->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200678 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 ret = mbedtls_snprintf( p, n, " revocation date: " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681 "%04d-%02d-%02d %02d:%02d:%02d",
682 entry->revocation_date.year, entry->revocation_date.mon,
683 entry->revocation_date.day, entry->revocation_date.hour,
684 entry->revocation_date.min, entry->revocation_date.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200685 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686
687 entry = entry->next;
688 }
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200691 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 ret = mbedtls_x509_sig_alg_gets( p, n, &crl->sig_oid, crl->sig_pk, crl->sig_md,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +0200694 crl->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200695 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200698 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
700 return( (int) ( size - n ) );
701}
702
703/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200704 * Initialize a CRL chain
705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706void mbedtls_x509_crl_init( mbedtls_x509_crl *crl )
Paul Bakker369d2eb2013-09-18 11:58:25 +0200707{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 memset( crl, 0, sizeof(mbedtls_x509_crl) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200709}
710
711/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712 * Unallocate all CRL data
713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714void mbedtls_x509_crl_free( mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_x509_crl *crl_cur = crl;
717 mbedtls_x509_crl *crl_prv;
718 mbedtls_x509_name *name_cur;
719 mbedtls_x509_name *name_prv;
720 mbedtls_x509_crl_entry *entry_cur;
721 mbedtls_x509_crl_entry *entry_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722
723 if( crl == NULL )
724 return;
725
726 do
727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
729 mbedtls_free( crl_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200730#endif
731
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732 name_cur = crl_cur->issuer.next;
733 while( name_cur != NULL )
734 {
735 name_prv = name_cur;
736 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500737 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 }
740
741 entry_cur = crl_cur->entry.next;
742 while( entry_cur != NULL )
743 {
744 entry_prv = entry_cur;
745 entry_cur = entry_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500746 mbedtls_platform_zeroize( entry_prv,
747 sizeof( mbedtls_x509_crl_entry ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_free( entry_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749 }
750
751 if( crl_cur->raw.p != NULL )
752 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500753 mbedtls_platform_zeroize( crl_cur->raw.p, crl_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_free( crl_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 }
756
757 crl_cur = crl_cur->next;
758 }
759 while( crl_cur != NULL );
760
761 crl_cur = crl;
762 do
763 {
764 crl_prv = crl_cur;
765 crl_cur = crl_cur->next;
766
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500767 mbedtls_platform_zeroize( crl_prv, sizeof( mbedtls_x509_crl ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 if( crl_prv != crl )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_free( crl_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 }
771 while( crl_cur != NULL );
772}
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#endif /* MBEDTLS_X509_CRL_PARSE_C */