blob: eb3ee990b1460566739404b745baa7967fa32e49 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
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
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020030 *
31 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/x509_crt.h"
Hanno Beckerf6bc8882019-05-02 13:05:58 +010043#include "mbedtls/x509_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000046
Rich Evans00ab4702015-02-06 13:43:58 +000047#include <string.h>
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#else
Simon Butcherd2642582018-10-03 15:11:19 +010056#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010065#endif
66
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <windows.h>
69#else
70#include <time.h>
71#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000074#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020075#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#include <sys/types.h>
77#include <sys/stat.h>
78#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000079#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#endif
81
Hanno Becker38f0cb42019-03-04 15:13:45 +000082#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
83static void x509_buf_to_buf_raw( mbedtls_x509_buf_raw *dst,
84 mbedtls_x509_buf const *src )
85{
86 dst->p = src->p;
87 dst->len = src->len;
88}
89
90static void x509_buf_raw_to_buf( mbedtls_x509_buf *dst,
91 mbedtls_x509_buf_raw const *src )
92{
93 dst->p = src->p;
94 dst->len = src->len;
95}
96#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
97
Hanno Becker21f55672019-02-15 15:27:59 +000098static int x509_crt_parse_frame( unsigned char *start,
99 unsigned char *end,
100 mbedtls_x509_crt_frame *frame );
Hanno Becker12506232019-05-13 13:53:21 +0100101static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000102 mbedtls_x509_name *subject );
Hanno Becker12506232019-05-13 13:53:21 +0100103static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000104 mbedtls_x509_name *issuer );
Hanno Becker12506232019-05-13 13:53:21 +0100105static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000106 mbedtls_x509_sequence *subject_alt );
Hanno Becker12506232019-05-13 13:53:21 +0100107static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000108 mbedtls_x509_sequence *ext_key_usage );
109
Hanno Beckerbc685192019-03-05 15:35:31 +0000110int mbedtls_x509_crt_flush_cache_pk( mbedtls_x509_crt const *crt )
111{
112#if defined(MBEDTLS_THREADING_C)
113 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
114 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100115#endif
Hanno Beckerfc99a092019-06-28 14:45:26 +0100116
117#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
118 defined(MBEDTLS_THREADING_C)
119 /* Can only free the PK context if nobody is using it.
120 * If MBEDTLS_X509_ALWAYS_FLUSH is set, nested uses
121 * of xxx_acquire() are prohibited, and no reference
122 * counting is needed. Also, notice that the code-path
123 * below is safe if the cache isn't filled. */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100124 if( crt->cache->pk_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100125#endif /* !MBEDTLS_X509_ALWAYS_FLUSH ||
126 MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100127 {
Hanno Beckerbc685192019-03-05 15:35:31 +0000128#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100129 /* The cache holds a shallow copy of the PK context
130 * in the legacy struct, so don't free PK context. */
131 mbedtls_free( crt->cache->pk );
Hanno Beckerbc685192019-03-05 15:35:31 +0000132#else
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100133 mbedtls_pk_free( crt->cache->pk );
134 mbedtls_free( crt->cache->pk );
Hanno Beckerbc685192019-03-05 15:35:31 +0000135#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100136 crt->cache->pk = NULL;
137 }
Hanno Beckerbc685192019-03-05 15:35:31 +0000138
139#if defined(MBEDTLS_THREADING_C)
140 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
141 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
142#endif
143 return( 0 );
144}
145
146int mbedtls_x509_crt_flush_cache_frame( mbedtls_x509_crt const *crt )
147{
148#if defined(MBEDTLS_THREADING_C)
149 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
150 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100151#endif
Hanno Beckerbc685192019-03-05 15:35:31 +0000152
Hanno Beckerfc99a092019-06-28 14:45:26 +0100153#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
154 defined(MBEDTLS_THREADING_C)
155 /* Can only free the PK context if nobody is using it.
156 * If MBEDTLS_X509_ALWAYS_FLUSH is set, nested uses
157 * of xxx_acquire() are prohibited, and no reference
158 * counting is needed. Also, notice that the code-path
159 * below is safe if the cache isn't filled. */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100160 if( crt->cache->frame_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100161#endif /* !MBEDTLS_X509_ALWAYS_FLUSH ||
162 MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100163 {
164 mbedtls_free( crt->cache->frame );
165 crt->cache->frame = NULL;
166 }
Hanno Beckerbc685192019-03-05 15:35:31 +0000167
168#if defined(MBEDTLS_THREADING_C)
169 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
170 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
171#endif
172 return( 0 );
173}
174
175int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt )
176{
177 int ret;
178 ret = mbedtls_x509_crt_flush_cache_frame( crt );
179 if( ret != 0 )
180 return( ret );
181 ret = mbedtls_x509_crt_flush_cache_pk( crt );
182 if( ret != 0 )
183 return( ret );
184 return( 0 );
185}
186
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000187static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame );
Hanno Beckered058882019-06-28 10:46:43 +0100188
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000189int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt )
Hanno Becker337088a2019-02-25 14:53:14 +0000190{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000191 mbedtls_x509_crt_cache *cache = crt->cache;
Hanno Becker337088a2019-02-25 14:53:14 +0000192 mbedtls_x509_crt_frame *frame;
193
Hanno Becker76428352019-03-05 15:29:23 +0000194 if( cache->frame != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100195 {
Hanno Becker410322f2019-07-02 13:37:12 +0100196#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
197 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000198 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100199#else
200 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
201 * allow nested uses of acquire. */
202 return( MBEDTLS_ERR_X509_FATAL_ERROR );
203#endif
204 }
Hanno Becker76428352019-03-05 15:29:23 +0000205
Hanno Becker337088a2019-02-25 14:53:14 +0000206 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
207 if( frame == NULL )
208 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000209 cache->frame = frame;
Hanno Becker337088a2019-02-25 14:53:14 +0000210
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000211#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
212 /* This would work with !MBEDTLS_X509_ON_DEMAND_PARSING, too,
213 * but is inefficient compared to copying the respective fields
214 * from the legacy mbedtls_x509_crt. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000215 return( x509_crt_parse_frame( crt->raw.p,
216 crt->raw.p + crt->raw.len,
217 frame ) );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000218#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
219 /* Make sure all extension related fields are properly initialized. */
220 frame->ca_istrue = 0;
221 frame->max_pathlen = 0;
222 frame->ext_types = 0;
223 frame->version = crt->version;
224 frame->sig_md = crt->sig_md;
225 frame->sig_pk = crt->sig_pk;
226 frame->valid_from = crt->valid_from;
227 frame->valid_to = crt->valid_to;
Hanno Becker38f0cb42019-03-04 15:13:45 +0000228 x509_buf_to_buf_raw( &frame->raw, &crt->raw );
229 x509_buf_to_buf_raw( &frame->tbs, &crt->tbs );
230 x509_buf_to_buf_raw( &frame->serial, &crt->serial );
231 x509_buf_to_buf_raw( &frame->pubkey_raw, &crt->pk_raw );
232 x509_buf_to_buf_raw( &frame->issuer_raw, &crt->issuer_raw );
233 x509_buf_to_buf_raw( &frame->subject_raw, &crt->subject_raw );
234 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
235 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
236 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
237 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000238
239 /* The legacy CRT structure doesn't explicitly contain
240 * the `AlgorithmIdentifier` bounds; however, those can
241 * be inferred from the surrounding (mandatory) `SerialNumber`
242 * and `Issuer` fields. */
243 frame->sig_alg.p = crt->serial.p + crt->serial.len;
244 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
245
246 return( x509_crt_frame_parse_ext( frame ) );
247#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000248}
Hanno Becker337088a2019-02-25 14:53:14 +0000249
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000250int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
251{
252 mbedtls_x509_crt_cache *cache = crt->cache;
253 mbedtls_pk_context *pk;
254
Hanno Becker76428352019-03-05 15:29:23 +0000255 if( cache->pk != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100256 {
Hanno Becker410322f2019-07-02 13:37:12 +0100257#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
258 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000259 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100260#else
261 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
262 * allow nested uses of acquire. */
263 return( MBEDTLS_ERR_X509_FATAL_ERROR );
264#endif
265 }
Hanno Becker76428352019-03-05 15:29:23 +0000266
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000267 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
268 if( pk == NULL )
269 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000270 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000271
272#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
273 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000274 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000275#else
276 {
277 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
278 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
279 pk_raw.p + pk_raw.len,
280 pk ) );
281 }
282#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000283}
284
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000285static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000286{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000287 memset( cache, 0, sizeof( *cache ) );
288#if defined(MBEDTLS_THREADING_C)
289 mbedtls_mutex_init( &cache->frame_mutex );
290 mbedtls_mutex_init( &cache->pk_mutex );
291#endif
292}
293
294static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
295{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000296#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000297 /* The cache holds a shallow copy of the PK context
298 * in the legacy struct, so don't free PK context. */
299 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000300#else
301 mbedtls_pk_free( cache->pk );
302 mbedtls_free( cache->pk );
303#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
304
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000305 cache->pk = NULL;
306}
307
308static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
309{
310 mbedtls_free( cache->frame );
311 cache->frame = NULL;
312}
313
314static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
315{
316 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000317 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000318
319#if defined(MBEDTLS_THREADING_C)
320 mbedtls_mutex_free( &cache->frame_mutex );
321 mbedtls_mutex_free( &cache->pk_mutex );
322#endif
323
324 x509_crt_cache_clear_frame( cache );
325 x509_crt_cache_clear_pk( cache );
326
327 memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000328}
329
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000330int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
331 mbedtls_x509_sequence **subj_alt )
332{
333 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100334 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000335 mbedtls_x509_sequence *seq;
336
337 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
338 if( ret != 0 )
339 return( ret );
340
341 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
342 if( seq == NULL )
343 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
344 else
345 ret = x509_crt_subject_alt_from_frame( frame, seq );
346
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000347 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000348
349 *subj_alt = seq;
350 return( ret );
351}
352
353int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
354 mbedtls_x509_sequence **ext_key_usage )
355{
356 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100357 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000358 mbedtls_x509_sequence *seq;
359
360 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
361 if( ret != 0 )
362 return( ret );
363
364 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
365 if( seq == NULL )
366 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
367 else
368 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
369
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000370 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000371
372 *ext_key_usage = seq;
373 return( ret );
374}
375
Hanno Becker63e69982019-02-26 18:50:49 +0000376int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
377 mbedtls_x509_name **subject )
378{
379 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100380 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000381 mbedtls_x509_name *name;
382
383 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
384 if( ret != 0 )
385 return( ret );
386
387 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
388 if( name == NULL )
389 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
390 else
391 ret = x509_crt_subject_from_frame( frame, name );
392
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000393 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000394
395 *subject = name;
396 return( ret );
397}
398
399int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
400 mbedtls_x509_name **issuer )
401{
402 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100403 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000404 mbedtls_x509_name *name;
405
406 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
407 if( ret != 0 )
408 return( ret );
409
410 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
411 if( name == NULL )
412 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
413 else
414 ret = x509_crt_issuer_from_frame( frame, name );
415
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000416 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000417
418 *issuer = name;
419 return( ret );
420}
421
Hanno Becker823efad2019-02-28 13:23:58 +0000422int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
423 mbedtls_x509_crt_frame *dst )
424{
425 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100426 mbedtls_x509_crt_frame const *frame;
Hanno Becker823efad2019-02-28 13:23:58 +0000427 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
428 if( ret != 0 )
429 return( ret );
430 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000431 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000432 return( 0 );
433}
434
435int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
436 mbedtls_pk_context *dst )
437{
438#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
439 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
440 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
441 pk_raw.p + pk_raw.len,
442 dst ) );
443#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
444 int ret;
445 mbedtls_pk_context *pk;
446 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
447 if( ret != 0 )
448 return( ret );
449
450 /* Move PK from CRT cache to destination pointer
451 * to avoid a copy. */
452 *dst = *pk;
453 mbedtls_free( crt->cache->pk );
454 crt->cache->pk = NULL;
455
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000456 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000457 return( 0 );
458#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
459}
460
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200461/*
462 * Item in a verification chain: cert and flags for it
463 */
464typedef struct {
465 mbedtls_x509_crt *crt;
466 uint32_t flags;
467} x509_crt_verify_chain_item;
468
469/*
470 * Max size of verification chain: end-entity + intermediates + trusted root
471 */
472#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200473
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200475 * Default profile
476 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200477const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
478{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200479#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200480 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200481 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200482#endif
483 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200484 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
485 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
486 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
487 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
488 0xFFFFFFF, /* Any PK alg */
489 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200490 2048,
491};
492
493/*
494 * Next-default profile
495 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200496const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
497{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200498 /* Hashes from SHA-256 and above */
499 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
500 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
501 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
502 0xFFFFFFF, /* Any PK alg */
503#if defined(MBEDTLS_ECP_C)
504 /* Curves at or above 128-bit security level */
505 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
506 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
507 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
508 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
509 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
510 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
511 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
512#else
513 0,
514#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200515 2048,
516};
517
518/*
519 * NSA Suite B Profile
520 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200521const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
522{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200523 /* Only SHA-256 and 384 */
524 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
525 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
526 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200527 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
528 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200529#if defined(MBEDTLS_ECP_C)
530 /* Only NIST P-256 and P-384 */
531 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
532 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
533#else
534 0,
535#endif
536 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200537};
538
539/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200540 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200541 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200542 */
543static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
544 mbedtls_md_type_t md_alg )
545{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200546 if( md_alg == MBEDTLS_MD_NONE )
547 return( -1 );
548
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200549 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
550 return( 0 );
551
552 return( -1 );
553}
554
555/*
556 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200557 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200558 */
559static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
560 mbedtls_pk_type_t pk_alg )
561{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200562 if( pk_alg == MBEDTLS_PK_NONE )
563 return( -1 );
564
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200565 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
566 return( 0 );
567
568 return( -1 );
569}
570
571/*
572 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200573 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200574 */
575static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200576 const mbedtls_pk_context *pk )
577{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200578 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200579
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200580#if defined(MBEDTLS_RSA_C)
581 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
582 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200583 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200584 return( 0 );
585
586 return( -1 );
587 }
588#endif
589
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200590#if defined(MBEDTLS_ECP_C)
591 if( pk_alg == MBEDTLS_PK_ECDSA ||
592 pk_alg == MBEDTLS_PK_ECKEY ||
593 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200594 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200595 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200596
Philippe Antoineb5b25432018-05-11 11:06:29 +0200597 if( gid == MBEDTLS_ECP_DP_NONE )
598 return( -1 );
599
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200600 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
601 return( 0 );
602
603 return( -1 );
604 }
605#endif
606
607 return( -1 );
608}
609
610/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000611 * Return 0 if name matches wildcard, -1 otherwise
612 */
Hanno Becker24926222019-02-21 13:10:55 +0000613static int x509_check_wildcard( char const *cn,
614 size_t cn_len,
615 unsigned char const *buf,
616 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000617{
618 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000619 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000620
621 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000622 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000623 return( -1 );
624
625 for( i = 0; i < cn_len; ++i )
626 {
627 if( cn[i] == '.' )
628 {
629 cn_idx = i;
630 break;
631 }
632 }
633
634 if( cn_idx == 0 )
635 return( -1 );
636
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000637 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
638 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000639 {
640 return( 0 );
641 }
642
643 return( -1 );
644}
645
646/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200647 * Reset (init or clear) a verify_chain
648 */
649static void x509_crt_verify_chain_reset(
650 mbedtls_x509_crt_verify_chain *ver_chain )
651{
652 size_t i;
653
654 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
655 {
656 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000657 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200658 }
659
660 ver_chain->len = 0;
661}
662
663/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
665 */
666static int x509_get_version( unsigned char **p,
667 const unsigned char *end,
668 int *ver )
669{
670 int ret;
671 size_t len;
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
674 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 {
678 *ver = 0;
679 return( 0 );
680 }
681
Hanno Becker2f472142019-02-12 11:52:10 +0000682 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 }
684
685 end = *p + len;
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
688 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689
690 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_X509_INVALID_VERSION +
692 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
694 return( 0 );
695}
696
697/*
698 * Validity ::= SEQUENCE {
699 * notBefore Time,
700 * notAfter Time }
701 */
702static int x509_get_dates( unsigned char **p,
703 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_x509_time *from,
705 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706{
707 int ret;
708 size_t len;
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
711 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
712 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713
714 end = *p + len;
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717 return( ret );
718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720 return( ret );
721
722 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 return( MBEDTLS_ERR_X509_INVALID_DATE +
724 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725
726 return( 0 );
727}
728
729/*
730 * X.509 v2/v3 unique identifier (not parsed)
731 */
732static int x509_get_uid( unsigned char **p,
733 const unsigned char *end,
Hanno Beckere9084122019-06-07 12:04:39 +0100734 mbedtls_x509_buf_raw *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735{
736 int ret;
737
738 if( *p == end )
739 return( 0 );
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
742 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 return( 0 );
746
Hanno Becker2f472142019-02-12 11:52:10 +0000747 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 }
749
750 uid->p = *p;
751 *p += uid->len;
752
753 return( 0 );
754}
755
756static int x509_get_basic_constraints( unsigned char **p,
757 const unsigned char *end,
758 int *ca_istrue,
759 int *max_pathlen )
760{
761 int ret;
762 size_t len;
763
764 /*
765 * BasicConstraints ::= SEQUENCE {
766 * cA BOOLEAN DEFAULT FALSE,
767 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
768 */
769 *ca_istrue = 0; /* DEFAULT FALSE */
770 *max_pathlen = 0; /* endless */
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
773 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000774 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775
776 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200777 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
782 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783
784 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000785 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786
787 if( *ca_istrue != 0 )
788 *ca_istrue = 1;
789 }
790
791 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200792 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000795 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796
797 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000798 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799
800 (*max_pathlen)++;
801
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200802 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803}
804
805static int x509_get_ns_cert_type( unsigned char **p,
806 const unsigned char *end,
807 unsigned char *ns_cert_type)
808{
809 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000813 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814
815 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000816 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817
818 /* Get actual bitstring */
819 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200820 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821}
822
823static int x509_get_key_usage( unsigned char **p,
824 const unsigned char *end,
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100825 uint16_t *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826{
827 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200828 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000832 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833
834 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000835 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
837 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200838 *key_usage = 0;
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100839 for( i = 0; i < bs.len && i < sizeof( *key_usage ); i++ )
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200840 {
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100841 *key_usage |= (uint16_t) bs.p[i] << ( 8*i );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200842 }
843
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200844 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845}
846
Hanno Becker529f25d2019-05-02 14:48:25 +0100847static int asn1_build_sequence_cb( void *ctx,
848 int tag,
849 unsigned char *data,
850 size_t data_len )
Hanno Becker15b73b42019-05-02 13:21:27 +0100851{
852 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
853 mbedtls_asn1_sequence *cur = *cur_ptr;
854
855 /* Allocate and assign next pointer */
856 if( cur->buf.p != NULL )
857 {
858 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
859 if( cur->next == NULL )
860 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
861 cur = cur->next;
862 }
863
864 cur->buf.tag = tag;
865 cur->buf.p = data;
866 cur->buf.len = data_len;
867
868 *cur_ptr = cur;
869 return( 0 );
870}
871
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872/*
Hanno Becker529f25d2019-05-02 14:48:25 +0100873 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
874 *
875 * KeyPurposeId ::= OBJECT IDENTIFIER
876 */
877static int x509_get_ext_key_usage( unsigned char **p,
878 const unsigned char *end,
879 mbedtls_x509_sequence *ext_key_usage)
880{
881 return( mbedtls_asn1_traverse_sequence_of( p, end,
882 0xFF, MBEDTLS_ASN1_OID,
883 0, 0,
884 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100885 (void *) &ext_key_usage ) );
Hanno Becker529f25d2019-05-02 14:48:25 +0100886}
887
888/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 * SubjectAltName ::= GeneralNames
890 *
891 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
892 *
893 * GeneralName ::= CHOICE {
894 * otherName [0] OtherName,
895 * rfc822Name [1] IA5String,
896 * dNSName [2] IA5String,
897 * x400Address [3] ORAddress,
898 * directoryName [4] Name,
899 * ediPartyName [5] EDIPartyName,
900 * uniformResourceIdentifier [6] IA5String,
901 * iPAddress [7] OCTET STRING,
902 * registeredID [8] OBJECT IDENTIFIER }
903 *
904 * OtherName ::= SEQUENCE {
905 * type-id OBJECT IDENTIFIER,
906 * value [0] EXPLICIT ANY DEFINED BY type-id }
907 *
908 * EDIPartyName ::= SEQUENCE {
909 * nameAssigner [0] DirectoryString OPTIONAL,
910 * partyName [1] DirectoryString }
911 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000912 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 */
Hanno Becker5984d302019-02-21 14:46:54 +0000914static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000918 return( mbedtls_asn1_traverse_sequence_of( &p, end,
919 MBEDTLS_ASN1_TAG_CLASS_MASK,
920 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
921 MBEDTLS_ASN1_TAG_VALUE_MASK,
922 2 /* SubjectAlt DNS */,
Hanno Becker529f25d2019-05-02 14:48:25 +0100923 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100924 (void *) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925}
926
927/*
928 * X.509 v3 extensions
929 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000931static int x509_crt_get_ext_cb( void *ctx,
932 int tag,
933 unsigned char *p,
934 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935{
936 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000937 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000939 unsigned char *end, *end_ext_octet;
940 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
941 int is_critical = 0; /* DEFAULT FALSE */
942 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000944 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000945
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000946 /*
947 * Extension ::= SEQUENCE {
948 * extnID OBJECT IDENTIFIER,
949 * critical BOOLEAN DEFAULT FALSE,
950 * extnValue OCTET STRING }
951 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000953 end = p + ext_len;
954
955 /* Get extension ID */
956 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
957 MBEDTLS_ASN1_OID ) ) != 0 )
958 goto err;
959
960 extn_oid.tag = MBEDTLS_ASN1_OID;
961 extn_oid.p = p;
962 p += extn_oid.len;
963
964 /* Get optional critical */
965 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
966 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
967 goto err;
968
969 /* Data should be octet string type */
970 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
971 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
972 goto err;
973
974 end_ext_octet = p + len;
975 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000977 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
978 goto err;
979 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000981 /*
982 * Detect supported extensions
983 */
984 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
985 if( ret != 0 )
986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000988 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000990 /* Data is marked as critical: fail */
991 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
992 goto err;
993 }
Hanno Beckerb36a2452019-05-29 14:43:17 +0100994#endif /* MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000995 return( 0 );
996 }
997
998 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +0000999 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001000 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
1001
Hanno Becker21f55672019-02-15 15:27:59 +00001002 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001003 switch( ext_type )
1004 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001005 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001006 {
1007 int ca_istrue;
1008 int max_pathlen;
1009
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001011 ret = x509_get_basic_constraints( &p, end_ext_octet,
1012 &ca_istrue,
1013 &max_pathlen );
1014 if( ret != 0 )
1015 goto err;
1016
Hanno Becker21f55672019-02-15 15:27:59 +00001017 frame->ca_istrue = ca_istrue;
1018 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001020 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001024 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001025 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001026 if( ret != 0 )
1027 goto err;
1028 break;
1029
1030 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1031 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +00001032 frame->subject_alt_raw.p = p;
1033 frame->subject_alt_raw.len = end_ext_octet - p;
1034
1035 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1036 MBEDTLS_ASN1_TAG_CLASS_MASK,
1037 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
1038 MBEDTLS_ASN1_TAG_VALUE_MASK,
1039 2 /* SubjectAlt DNS */,
1040 NULL, NULL );
1041 if( ret != 0 )
1042 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 break;
1044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001047 frame->ext_key_usage_raw.p = p;
1048 frame->ext_key_usage_raw.len = end_ext_octet - p;
1049 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001050 {
Hanno Becker21f55672019-02-15 15:27:59 +00001051 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1052 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001053 }
Hanno Becker21f55672019-02-15 15:27:59 +00001054
1055 /* Check structural sanity of extension. */
1056 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1057 0xFF, MBEDTLS_ASN1_OID,
1058 0, 0, NULL, NULL );
1059 if( ret != 0 )
1060 goto err;
1061
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 break;
1063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001066 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001067 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001068 if( ret != 0 )
1069 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070 break;
1071
1072 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001073 /*
1074 * If this is a non-critical extension, which the oid layer
1075 * supports, but there isn't an X.509 parser for it,
1076 * skip the extension.
1077 */
1078#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1079 if( is_critical )
1080 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1081#endif
1082 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083 }
1084
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001086
1087err:
1088 return( ret );
1089}
1090
Hanno Becker21f55672019-02-15 15:27:59 +00001091static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001092{
1093 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001094 unsigned char *p = frame->v3_ext.p;
1095 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001096
Hanno Becker21f55672019-02-15 15:27:59 +00001097 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001098 return( 0 );
1099
Hanno Becker21f55672019-02-15 15:27:59 +00001100 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001101 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001102 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001103
1104 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1105 return( ret );
1106 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1107 return( ret );
1108
1109 if( ret != 0 )
1110 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1111
1112 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113}
1114
Hanno Becker21f55672019-02-15 15:27:59 +00001115static int x509_crt_parse_frame( unsigned char *start,
1116 unsigned char *end,
1117 mbedtls_x509_crt_frame *frame )
1118{
1119 int ret;
1120 unsigned char *p;
1121 size_t len;
1122
1123 mbedtls_x509_buf tmp;
1124 unsigned char *tbs_start;
1125
1126 mbedtls_x509_buf outer_sig_alg;
1127 size_t inner_sig_alg_len;
1128 unsigned char *inner_sig_alg_start;
1129
1130 memset( frame, 0, sizeof( *frame ) );
1131
1132 /*
1133 * Certificate ::= SEQUENCE {
1134 * tbsCertificate TBSCertificate,
1135 * signatureAlgorithm AlgorithmIdentifier,
1136 * signatureValue BIT STRING
1137 * }
1138 *
1139 */
1140 p = start;
1141
1142 frame->raw.p = p;
1143 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1144 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1145 {
1146 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1147 }
1148
1149 /* NOTE: We are currently not checking that the `Certificate`
1150 * structure spans the entire buffer. */
1151 end = p + len;
1152 frame->raw.len = end - frame->raw.p;
1153
1154 /*
1155 * TBSCertificate ::= SEQUENCE { ...
1156 */
1157 frame->tbs.p = p;
1158 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1159 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1160 {
1161 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1162 }
1163 tbs_start = p;
1164
1165 /* Breadth-first parsing: Jump over TBS for now. */
1166 p += len;
1167 frame->tbs.len = p - frame->tbs.p;
1168
1169 /*
1170 * AlgorithmIdentifier ::= SEQUENCE { ...
1171 */
1172 outer_sig_alg.p = p;
1173 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1174 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1175 {
1176 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1177 }
1178 p += len;
1179 outer_sig_alg.len = p - outer_sig_alg.p;
1180
1181 /*
1182 * signatureValue BIT STRING
1183 */
1184 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1185 if( ret != 0 )
1186 return( ret );
1187 frame->sig.p = tmp.p;
1188 frame->sig.len = tmp.len;
1189
1190 /* Check that we consumed the entire `Certificate` structure. */
1191 if( p != end )
1192 {
1193 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1194 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1195 }
1196
1197 /* Parse TBSCertificate structure
1198 *
1199 * TBSCertificate ::= SEQUENCE {
1200 * version [0] EXPLICIT Version DEFAULT v1,
1201 * serialNumber CertificateSerialNumber,
1202 * signature AlgorithmIdentifier,
1203 * issuer Name,
1204 * validity Validity,
1205 * subject Name,
1206 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1207 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1208 * -- If present, version MUST be v2 or v3
1209 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1210 * -- If present, version MUST be v2 or v3
1211 * extensions [3] EXPLICIT Extensions OPTIONAL
1212 * -- If present, version MUST be v3
1213 * }
1214 */
1215 end = frame->tbs.p + frame->tbs.len;
1216 p = tbs_start;
1217
1218 /*
1219 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1220 */
1221 {
1222 int version;
1223 ret = x509_get_version( &p, end, &version );
1224 if( ret != 0 )
1225 return( ret );
1226
1227 if( version < 0 || version > 2 )
1228 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1229
1230 frame->version = version + 1;
1231 }
1232
1233 /*
1234 * CertificateSerialNumber ::= INTEGER
1235 */
1236 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1237 if( ret != 0 )
1238 return( ret );
1239
1240 frame->serial.p = tmp.p;
1241 frame->serial.len = tmp.len;
1242
1243 /*
1244 * signature AlgorithmIdentifier
1245 */
1246 inner_sig_alg_start = p;
1247 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1248 &frame->sig_pk, NULL );
1249 if( ret != 0 )
1250 return( ret );
1251 inner_sig_alg_len = p - inner_sig_alg_start;
1252
1253 frame->sig_alg.p = inner_sig_alg_start;
1254 frame->sig_alg.len = inner_sig_alg_len;
1255
1256 /* Consistency check:
1257 * Inner and outer AlgorithmIdentifier structures must coincide:
1258 *
1259 * Quoting RFC 5280, Section 4.1.1.2:
1260 * This field MUST contain the same algorithm identifier as the
1261 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1262 */
1263 if( outer_sig_alg.len != inner_sig_alg_len ||
1264 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1265 {
1266 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1267 }
1268
1269 /*
1270 * issuer Name
1271 *
1272 * Name ::= CHOICE { -- only one possibility for now --
1273 * rdnSequence RDNSequence }
1274 *
1275 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1276 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001277 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001278
1279 ret = mbedtls_asn1_get_tag( &p, end, &len,
1280 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1281 if( ret != 0 )
1282 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001283 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001284 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001285
Hanno Becker3aa12162019-07-02 16:47:40 +01001286 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001287 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1288 &frame->issuer_raw,
1289 NULL, NULL );
1290 if( ret != 0 )
1291 return( ret );
1292
Hanno Becker21f55672019-02-15 15:27:59 +00001293 /*
1294 * Validity ::= SEQUENCE { ...
1295 */
1296 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1297 if( ret != 0 )
1298 return( ret );
1299
1300 /*
1301 * subject Name
1302 *
1303 * Name ::= CHOICE { -- only one possibility for now --
1304 * rdnSequence RDNSequence }
1305 *
1306 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1307 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001308 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001309
1310 ret = mbedtls_asn1_get_tag( &p, end, &len,
1311 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1312 if( ret != 0 )
1313 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001314 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001315 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001316
Hanno Becker3aa12162019-07-02 16:47:40 +01001317 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001318 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1319 &frame->subject_raw,
1320 NULL, NULL );
1321 if( ret != 0 )
1322 return( ret );
1323
Hanno Becker21f55672019-02-15 15:27:59 +00001324 /*
1325 * SubjectPublicKeyInfo
1326 */
1327 frame->pubkey_raw.p = p;
1328 ret = mbedtls_asn1_get_tag( &p, end, &len,
1329 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1330 if( ret != 0 )
1331 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1332 p += len;
1333 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1334
Hanno Becker97aa4362019-06-08 07:38:20 +01001335 if( frame->version != 1 )
Hanno Becker21f55672019-02-15 15:27:59 +00001336 {
Hanno Beckerfd64f142019-06-07 11:47:12 +01001337 /*
1338 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1339 * -- If present, version shall be v2 or v3
1340 */
Hanno Beckere9084122019-06-07 12:04:39 +01001341 ret = x509_get_uid( &p, end, &frame->issuer_id, 1 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001342 if( ret != 0 )
1343 return( ret );
1344
Hanno Beckerfd64f142019-06-07 11:47:12 +01001345 /*
1346 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1347 * -- If present, version shall be v2 or v3
1348 */
Hanno Beckere9084122019-06-07 12:04:39 +01001349 ret = x509_get_uid( &p, end, &frame->subject_id, 2 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001350 if( ret != 0 )
1351 return( ret );
Hanno Becker21f55672019-02-15 15:27:59 +00001352 }
1353
1354 /*
1355 * extensions [3] EXPLICIT Extensions OPTIONAL
1356 * -- If present, version shall be v3
1357 */
1358#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1359 if( frame->version == 3 )
1360#endif
1361 {
1362 if( p != end )
1363 {
1364 ret = mbedtls_asn1_get_tag( &p, end, &len,
1365 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1366 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1367 if( len == 0 )
1368 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1369 if( ret != 0 )
1370 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1371
1372 frame->v3_ext.p = p;
1373 frame->v3_ext.len = len;
1374
1375 p += len;
1376 }
1377
1378 ret = x509_crt_frame_parse_ext( frame );
1379 if( ret != 0 )
1380 return( ret );
1381 }
1382
1383 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1384 if( p != end )
1385 {
1386 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1387 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1388 }
1389
1390 return( 0 );
1391}
1392
Hanno Becker12506232019-05-13 13:53:21 +01001393static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001394 mbedtls_x509_name *subject )
1395{
Hanno Becker1e11f212019-03-04 14:43:43 +00001396 return( mbedtls_x509_get_name( frame->subject_raw.p,
1397 frame->subject_raw.len,
1398 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001399}
1400
Hanno Becker12506232019-05-13 13:53:21 +01001401static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001402 mbedtls_x509_name *issuer )
1403{
Hanno Becker1e11f212019-03-04 14:43:43 +00001404 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1405 frame->issuer_raw.len,
1406 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001407}
1408
Hanno Becker12506232019-05-13 13:53:21 +01001409static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001410 mbedtls_x509_sequence *subject_alt )
1411{
1412 int ret;
1413 unsigned char *p = frame->subject_alt_raw.p;
1414 unsigned char *end = p + frame->subject_alt_raw.len;
1415
1416 memset( subject_alt, 0, sizeof( *subject_alt ) );
1417
1418 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1419 return( 0 );
1420
1421 ret = x509_get_subject_alt_name( p, end, subject_alt );
1422 if( ret != 0 )
1423 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1424 return( ret );
1425}
1426
Hanno Becker12506232019-05-13 13:53:21 +01001427static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001428 mbedtls_x509_sequence *ext_key_usage )
1429{
1430 int ret;
1431 unsigned char *p = frame->ext_key_usage_raw.p;
1432 unsigned char *end = p + frame->ext_key_usage_raw.len;
1433
1434 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1435
1436 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1437 return( 0 );
1438
1439 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1440 if( ret != 0 )
1441 {
1442 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1443 return( ret );
1444 }
1445
1446 return( 0 );
1447}
1448
Hanno Becker180f7bf2019-02-28 13:23:38 +00001449#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001450static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1451 mbedtls_pk_context *pk )
1452{
1453 unsigned char *p = frame->pubkey_raw.p;
1454 unsigned char *end = p + frame->pubkey_raw.len;
1455 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1456}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001457#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001458
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459/*
1460 * Parse and fill a single X.509 certificate in DER format
1461 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001462static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1463 const unsigned char *buf,
1464 size_t buflen,
1465 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466{
1467 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001468 mbedtls_x509_crt_frame *frame;
1469 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001470
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001473
Hanno Becker21f55672019-02-15 15:27:59 +00001474 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 {
Hanno Becker21f55672019-02-15 15:27:59 +00001476 crt->raw.p = (unsigned char*) buf;
1477 crt->raw.len = buflen;
1478 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001479 }
1480 else
1481 {
Hanno Becker7b8e11e2019-05-03 12:37:12 +01001482 /* Call mbedtls_calloc with buflen + 1 in order to avoid potential
1483 * return of NULL in case of length 0 certificates, which we want
1484 * to cleanly fail with MBEDTLS_ERR_X509_INVALID_FORMAT in the
1485 * core parsing routine, but not here. */
1486 crt->raw.p = mbedtls_calloc( 1, buflen + 1 );
Hanno Becker21f55672019-02-15 15:27:59 +00001487 if( crt->raw.p == NULL )
1488 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1489 crt->raw.len = buflen;
1490 memcpy( crt->raw.p, buf, buflen );
1491
1492 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001493 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001494
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001495 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1496 if( cache == NULL )
1497 {
1498 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1499 goto exit;
1500 }
1501 crt->cache = cache;
1502 x509_crt_cache_init( cache );
1503
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001504#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1505
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001506 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001507 if( ret != 0 )
1508 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509
Hanno Becker76428352019-03-05 15:29:23 +00001510 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001511
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001512#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001513
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001514 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1515 if( frame == NULL )
1516 {
1517 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1518 goto exit;
1519 }
1520 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001521
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001522 ret = x509_crt_parse_frame( crt->raw.p,
1523 crt->raw.p + crt->raw.len,
1524 frame );
1525 if( ret != 0 )
1526 goto exit;
1527
Hanno Becker21f55672019-02-15 15:27:59 +00001528 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1529 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001530 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1531 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1532 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1533 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
1534 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1535 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
1536 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1537 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1538 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001539 crt->valid_from = frame->valid_from;
1540 crt->valid_to = frame->valid_to;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001541 crt->version = frame->version;
1542 crt->ca_istrue = frame->ca_istrue;
1543 crt->max_pathlen = frame->max_pathlen;
1544 crt->ext_types = frame->ext_types;
1545 crt->key_usage = frame->key_usage;
1546 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
1548 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001549 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 {
Hanno Becker21f55672019-02-15 15:27:59 +00001553 /* sig_oid: Previously, needed for convenience in
1554 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001555 unsigned char *tmp = frame->sig_alg.p;
1556 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001557 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Hanno Becker21f55672019-02-15 15:27:59 +00001559 ret = mbedtls_x509_get_alg( &tmp, end,
1560 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561 if( ret != 0 )
1562 {
Hanno Becker21f55672019-02-15 15:27:59 +00001563 /* This should never happen, because we check
1564 * the sanity of the AlgorithmIdentifier structure
1565 * during frame parsing. */
1566 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1567 goto exit;
1568 }
1569 crt->sig_oid = sig_oid;
1570
1571 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001572 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001573 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1574 &crt->sig_md, &crt->sig_pk,
1575 &crt->sig_opts );
1576 if( ret != 0 )
1577 {
1578 /* Again, this should never happen. */
1579 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1580 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581 }
1582 }
1583
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001584 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001585 if( ret != 0 )
1586 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001588 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001589 if( ret != 0 )
1590 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001592 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001593 if( ret != 0 )
1594 goto exit;
1595
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001596 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001597 if( ret != 0 )
1598 goto exit;
1599
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001600 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1601 if( ret != 0 )
1602 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001603#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001604
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001605 /* Currently, we accept DER encoded CRTs with trailing garbage
1606 * and promise to not account for the garbage in the `raw` field.
1607 *
1608 * Note that this means that `crt->raw.len` is not necessarily the
1609 * full size of the heap buffer allocated at `crt->raw.p` in case
1610 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1611 * need the size, and the garbage data doesn't need zeroization. */
1612 crt->raw.len = frame->raw.len;
1613
1614 cache->pk_raw = frame->pubkey_raw;
1615
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001616 /* Free the frame before parsing the public key to
1617 * keep peak RAM usage low. This is slightly inefficient
1618 * because the frame will need to be parsed again on the
1619 * first usage of the CRT, but that seems acceptable.
1620 * As soon as the frame gets used multiple times, it
1621 * will be cached by default. */
1622 x509_crt_cache_clear_frame( crt->cache );
1623
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001624 /* The cache just references the PK structure from the legacy
1625 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001626 * the cache.
1627 *
1628 * We're not actually using the parsed PK context here;
1629 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001630 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001631 if( ret != 0 )
1632 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001633 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001634
1635exit:
1636 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638
Hanno Becker21f55672019-02-15 15:27:59 +00001639 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640}
1641
1642/*
1643 * Parse one X.509 certificate in DER format from a buffer and add them to a
1644 * chained list
1645 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001646static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1647 const unsigned char *buf,
1648 size_t buflen,
1649 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650{
1651 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653
1654 /*
1655 * Check for valid input
1656 */
1657 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659
Hanno Becker371e0e42019-02-25 18:08:59 +00001660 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 {
1662 prev = crt;
1663 crt = crt->next;
1664 }
1665
1666 /*
1667 * Add new certificate on the end of the chain if needed.
1668 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001669 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001671 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001672
1673 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001674 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675
1676 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679 }
1680
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001681 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682 {
1683 if( prev )
1684 prev->next = NULL;
1685
1686 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688
1689 return( ret );
1690 }
1691
1692 return( 0 );
1693}
1694
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001695int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1696 const unsigned char *buf,
1697 size_t buflen )
1698{
1699 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1700}
1701
1702int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1703 const unsigned char *buf,
1704 size_t buflen )
1705{
1706 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1707}
1708
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001709/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001710 * Parse one or more PEM certificates from a buffer and add them to the chained
1711 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001713int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1714 const unsigned char *buf,
1715 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001716{
Janos Follath98e28a72016-05-31 14:03:54 +01001717#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001718 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001720#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721
1722 /*
1723 * Check for valid input
1724 */
1725 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001727
1728 /*
1729 * Determine buffer content. Buffer contains either one DER certificate or
1730 * one or more PEM certificates.
1731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001733 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001734 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001737 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1740 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001741#else
1742 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1743#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745#if defined(MBEDTLS_PEM_PARSE_C)
1746 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747 {
1748 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001751 /* 1 rather than 0 since the terminating NULL byte is counted in */
1752 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753 {
1754 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001757 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759 "-----BEGIN CERTIFICATE-----",
1760 "-----END CERTIFICATE-----",
1761 buf, NULL, 0, &use_len );
1762
1763 if( ret == 0 )
1764 {
1765 /*
1766 * Was PEM encoded
1767 */
1768 buflen -= use_len;
1769 buf += use_len;
1770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772 {
1773 return( ret );
1774 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001778
1779 /*
1780 * PEM header and footer were found
1781 */
1782 buflen -= use_len;
1783 buf += use_len;
1784
1785 if( first_error == 0 )
1786 first_error = ret;
1787
Paul Bakker5a5fa922014-09-26 14:53:04 +02001788 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789 continue;
1790 }
1791 else
1792 break;
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797
1798 if( ret != 0 )
1799 {
1800 /*
1801 * Quit parsing on a memory error
1802 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001803 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001804 return( ret );
1805
1806 if( first_error == 0 )
1807 first_error = ret;
1808
1809 total_failed++;
1810 continue;
1811 }
1812
1813 success = 1;
1814 }
1815 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816
1817 if( success )
1818 return( total_failed );
1819 else if( first_error )
1820 return( first_error );
1821 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001823#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001824}
1825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827/*
1828 * Load one or more certificates and add them to the chained list
1829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001831{
1832 int ret;
1833 size_t n;
1834 unsigned char *buf;
1835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001837 return( ret );
1838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001840
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001841 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001843
1844 return( ret );
1845}
1846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001848{
1849 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001850#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001851 int w_ret;
1852 WCHAR szDir[MAX_PATH];
1853 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001854 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001855 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001856
Paul Bakker9af723c2014-05-01 13:03:14 +02001857 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001858 HANDLE hFind;
1859
1860 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001862
Paul Bakker9af723c2014-05-01 13:03:14 +02001863 memset( szDir, 0, sizeof(szDir) );
1864 memset( filename, 0, MAX_PATH );
1865 memcpy( filename, path, len );
1866 filename[len++] = '\\';
1867 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001868 filename[len++] = '*';
1869
Simon B3c6b18d2016-11-03 01:11:37 +00001870 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001871 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001872 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001874
1875 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001876 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001878
1879 len = MAX_PATH - len;
1880 do
1881 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001882 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001883
1884 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1885 continue;
1886
Paul Bakker9af723c2014-05-01 13:03:14 +02001887 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001888 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001889 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001890 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001891 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001892 {
1893 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1894 goto cleanup;
1895 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001898 if( w_ret < 0 )
1899 ret++;
1900 else
1901 ret += w_ret;
1902 }
1903 while( FindNextFileW( hFind, &file_data ) != 0 );
1904
Paul Bakker66d5d072014-06-17 16:39:18 +02001905 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001907
Ron Eldor36d90422017-01-09 15:09:16 +02001908cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001910#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001911 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001912 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001913 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001914 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001915 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001916 DIR *dir = opendir( path );
1917
Paul Bakker66d5d072014-06-17 16:39:18 +02001918 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001920
Ron Eldor63140682017-01-09 19:27:59 +02001921#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001922 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001923 {
1924 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001925 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001926 }
Ron Eldor63140682017-01-09 19:27:59 +02001927#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001928
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001929 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001930 {
Andres AGf9113192016-09-02 14:06:04 +01001931 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1932 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001933
Andres AGf9113192016-09-02 14:06:04 +01001934 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001935 {
Andres AGf9113192016-09-02 14:06:04 +01001936 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1937 goto cleanup;
1938 }
1939 else if( stat( entry_name, &sb ) == -1 )
1940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001942 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001943 }
1944
1945 if( !S_ISREG( sb.st_mode ) )
1946 continue;
1947
1948 // Ignore parse errors
1949 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951 if( t_ret < 0 )
1952 ret++;
1953 else
1954 ret += t_ret;
1955 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001956
1957cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001958 closedir( dir );
1959
Ron Eldor63140682017-01-09 19:27:59 +02001960#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001961 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001963#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001964
Paul Bakkerbe089b02013-10-14 15:51:50 +02001965#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001966
1967 return( ret );
1968}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001970
Hanno Becker08d34122019-06-25 09:42:57 +01001971typedef struct mbedtls_x509_crt_sig_info
1972{
1973 mbedtls_md_type_t sig_md;
1974 mbedtls_pk_type_t sig_pk;
1975 void *sig_opts;
1976 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
1977 size_t crt_hash_len;
1978 mbedtls_x509_buf_raw sig;
1979 mbedtls_x509_buf_raw issuer_raw;
1980} mbedtls_x509_crt_sig_info;
1981
1982static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
1983{
1984#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
1985 mbedtls_free( info->sig_opts );
1986#else
1987 ((void) info);
1988#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
1989}
1990
1991static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
1992 mbedtls_x509_crt_sig_info *info )
1993{
1994 const mbedtls_md_info_t *md_info;
1995
1996 md_info = mbedtls_md_info_from_type( frame->sig_md );
1997 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
1998 info->crt_hash ) != 0 )
1999 {
2000 /* Note: this can't happen except after an internal error */
2001 return( -1 );
2002 }
2003
2004 info->crt_hash_len = mbedtls_md_get_size( md_info );
2005
2006 /* Make sure that this function leaves the target structure
2007 * ready to be freed, regardless of success of failure. */
2008 info->sig_opts = NULL;
2009
2010#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2011 {
2012 int ret;
2013 unsigned char *alg_start = frame->sig_alg.p;
2014 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2015
2016 /* Get signature options -- currently only
2017 * necessary for RSASSA-PSS. */
2018 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2019 &info->sig_pk, &info->sig_opts );
2020 if( ret != 0 )
2021 {
2022 /* Note: this can't happen except after an internal error */
2023 return( -1 );
2024 }
2025 }
2026#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2027 info->sig_md = frame->sig_md;
2028 info->sig_pk = frame->sig_pk;
2029#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2030
2031 info->issuer_raw = frame->issuer_raw;
2032 info->sig = frame->sig;
2033 return( 0 );
2034}
2035
Hanno Becker02a21932019-06-10 15:08:43 +01002036#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002037static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002039{
2040 size_t i;
2041 size_t n = *size;
2042 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002044 const char *sep = "";
2045 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002046
2047 while( cur != NULL )
2048 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002049 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002050 {
2051 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002052 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002053 }
2054
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002055 n -= cur->buf.len + sep_len;
2056 for( i = 0; i < sep_len; i++ )
2057 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002058 for( i = 0; i < cur->buf.len; i++ )
2059 *p++ = cur->buf.p[i];
2060
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002061 sep = ", ";
2062 sep_len = 2;
2063
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002064 cur = cur->next;
2065 }
2066
2067 *p = '\0';
2068
2069 *size = n;
2070 *buf = p;
2071
2072 return( 0 );
2073}
2074
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002075#define PRINT_ITEM(i) \
2076 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002078 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002079 sep = ", "; \
2080 }
2081
2082#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002083 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002084 PRINT_ITEM( name );
2085
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002086static int x509_info_cert_type( char **buf, size_t *size,
2087 unsigned char ns_cert_type )
2088{
2089 int ret;
2090 size_t n = *size;
2091 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002092 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002095 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2097 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2098 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002099 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2100 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2101 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002102
2103 *size = n;
2104 *buf = p;
2105
2106 return( 0 );
2107}
2108
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002109#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002110 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002111 PRINT_ITEM( name );
2112
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002113static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002114 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002115{
2116 int ret;
2117 size_t n = *size;
2118 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002119 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2122 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002123 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2124 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2125 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2127 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002128 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2129 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002130
2131 *size = n;
2132 *buf = p;
2133
2134 return( 0 );
2135}
2136
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002137static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002139{
2140 int ret;
2141 const char *desc;
2142 size_t n = *size;
2143 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002145 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002146
2147 while( cur != NULL )
2148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002150 desc = "???";
2151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002153 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002154
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002155 sep = ", ";
2156
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002157 cur = cur->next;
2158 }
2159
2160 *size = n;
2161 *buf = p;
2162
2163 return( 0 );
2164}
2165
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002166/*
2167 * Return an informational string about the certificate.
2168 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002169#define BEFORE_COLON 18
2170#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002172 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002173{
2174 int ret;
2175 size_t n;
2176 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002177 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002178 mbedtls_x509_crt_frame frame;
2179 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002180
Hanno Becker5226c532019-02-27 17:38:40 +00002181 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2182 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002183 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002184
2185 p = buf;
2186 n = size;
2187
Hanno Becker4f869ed2019-02-24 16:47:57 +00002188 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002189 mbedtls_pk_init( &pk );
2190
2191 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002192 {
2193 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002194 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Janos Follath98e28a72016-05-31 14:03:54 +01002195
2196 return( (int) ( size - n ) );
2197 }
2198
Hanno Becker5226c532019-02-27 17:38:40 +00002199 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002200 if( ret != 0 )
2201 {
2202 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2203 goto cleanup;
2204 }
2205
Hanno Becker5226c532019-02-27 17:38:40 +00002206 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002207 if( ret != 0 )
2208 {
2209 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2210 goto cleanup;
2211 }
2212
Hanno Becker5226c532019-02-27 17:38:40 +00002213 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002214 if( ret != 0 )
2215 {
2216 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2217 goto cleanup;
2218 }
2219
Hanno Becker5226c532019-02-27 17:38:40 +00002220 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002221 if( ret != 0 )
2222 {
2223 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2224 goto cleanup;
2225 }
2226
Hanno Becker5226c532019-02-27 17:38:40 +00002227 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002228 if( ret != 0 )
2229 {
2230 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2231 goto cleanup;
2232 }
2233
Hanno Becker5226c532019-02-27 17:38:40 +00002234 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2235 if( ret != 0 )
2236 {
2237 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2238 goto cleanup;
2239 }
2240
2241 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002242 if( ret != 0 )
2243 {
2244 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2245 goto cleanup;
2246 }
2247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002249 prefix, frame.version );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002250 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002251
Hanno Becker4f869ed2019-02-24 16:47:57 +00002252 {
2253 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002254 serial.p = frame.serial.p;
2255 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002256 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2257 prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002258 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002259 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002260 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002261 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002264 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002265 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002266 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002269 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002270 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002271 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002274 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002275 frame.valid_from.year, frame.valid_from.mon,
2276 frame.valid_from.day, frame.valid_from.hour,
2277 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002278 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002281 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002282 frame.valid_to.year, frame.valid_to.mon,
2283 frame.valid_to.day, frame.valid_to.hour,
2284 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002285 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002288 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002289
Hanno Becker83cd8672019-02-21 17:13:46 +00002290 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2291 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002292 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002293
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002294 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002296 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002297 {
2298 return( ret );
2299 }
2300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002302 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002303 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002304
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002305 /*
2306 * Optional extensions
2307 */
2308
Hanno Becker5226c532019-02-27 17:38:40 +00002309 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002312 frame.ca_istrue ? "true" : "false" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002313 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002314
Hanno Becker5226c532019-02-27 17:38:40 +00002315 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002316 {
Hanno Becker5226c532019-02-27 17:38:40 +00002317 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002318 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002319 }
2320 }
2321
Hanno Becker5226c532019-02-27 17:38:40 +00002322 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002325 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002326
2327 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002328 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002329 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002330 }
2331
Hanno Becker5226c532019-02-27 17:38:40 +00002332 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002335 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002336
Hanno Becker5226c532019-02-27 17:38:40 +00002337 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002338 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002339 }
2340
Hanno Becker5226c532019-02-27 17:38:40 +00002341 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002344 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002345
Hanno Becker5226c532019-02-27 17:38:40 +00002346 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002347 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002348 }
2349
Hanno Becker5226c532019-02-27 17:38:40 +00002350 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002353 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002354
2355 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002356 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002357 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002358 }
2359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002361 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002362
Hanno Becker4f869ed2019-02-24 16:47:57 +00002363 ret = (int) ( size - n );
2364
2365cleanup:
2366
Hanno Becker4f869ed2019-02-24 16:47:57 +00002367 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002368 mbedtls_pk_free( &pk );
2369 mbedtls_x509_name_free( issuer );
2370 mbedtls_x509_name_free( subject );
2371 mbedtls_x509_sequence_free( ext_key_usage );
2372 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002373
2374 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002375}
2376
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002377struct x509_crt_verify_string {
2378 int code;
2379 const char *string;
2380};
2381
2382static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002383 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002384 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2385 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2386 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2387 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2388 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002389 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2390 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2391 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002392 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002393 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2394 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2395 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2396 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002397 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2398 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2399 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2400 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2401 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2402 { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002403 { 0, NULL }
2404};
2405
2406int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002407 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002408{
2409 int ret;
2410 const struct x509_crt_verify_string *cur;
2411 char *p = buf;
2412 size_t n = size;
2413
2414 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2415 {
2416 if( ( flags & cur->code ) == 0 )
2417 continue;
2418
2419 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002420 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002421 flags ^= cur->code;
2422 }
2423
2424 if( flags != 0 )
2425 {
2426 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2427 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002428 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002429 }
2430
2431 return( (int) ( size - n ) );
2432}
Hanno Becker02a21932019-06-10 15:08:43 +01002433#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002436static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2437 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002438{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002439 unsigned int usage_must, usage_may;
2440 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2441 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2442
2443 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2444 return( 0 );
2445
2446 usage_must = usage & ~may_mask;
2447
2448 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2449 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2450
2451 usage_may = usage & may_mask;
2452
2453 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002455
2456 return( 0 );
2457}
Hanno Becker45eedf12019-02-25 13:55:33 +00002458
2459int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2460 unsigned int usage )
2461{
2462 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002463 mbedtls_x509_crt_frame const *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002464 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002465 if( ret != 0 )
2466 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2467
2468 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002469 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002470
2471 return( ret );
2472}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002473#endif
2474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002476typedef struct
2477{
2478 const char *oid;
2479 size_t oid_len;
2480} x509_crt_check_ext_key_usage_cb_ctx_t;
2481
2482static int x509_crt_check_ext_key_usage_cb( void *ctx,
2483 int tag,
2484 unsigned char *data,
2485 size_t data_len )
2486{
2487 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2488 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2489 ((void) tag);
2490
2491 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2492 data, data_len ) == 0 )
2493 {
2494 return( 1 );
2495 }
2496
2497 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2498 data_len ) == 0 )
2499 {
2500 return( 1 );
2501 }
2502
2503 return( 0 );
2504}
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002507 const char *usage_oid,
2508 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002509{
Hanno Beckere1956af2019-02-21 14:28:12 +00002510 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002511 mbedtls_x509_crt_frame const *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002512 unsigned ext_types;
2513 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002514 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002515
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002516 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002517 if( ret != 0 )
2518 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2519
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002520 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002521 ext_types = frame->ext_types;
2522 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2523 {
2524 p = frame->ext_key_usage_raw.p;
2525 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002526
Hanno Beckere9718b42019-02-25 18:11:42 +00002527 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2528 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2529 x509_crt_check_ext_key_usage_cb,
2530 &cb_ctx );
2531 if( ret == 1 )
2532 ret = 0;
2533 else
2534 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2535 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002536
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002537 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002538 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002543/*
2544 * Return 1 if the certificate is revoked, or 0 otherwise.
2545 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002546static int x509_serial_is_revoked( unsigned char const *serial,
2547 size_t serial_len,
2548 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002551
2552 while( cur != NULL && cur->serial.len != 0 )
2553 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002554 if( serial_len == cur->serial.len &&
2555 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002556 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002557 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002558 return( 1 );
2559 }
2560
2561 cur = cur->next;
2562 }
2563
2564 return( 0 );
2565}
2566
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002567int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2568 const mbedtls_x509_crl *crl )
2569{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002570 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002571 mbedtls_x509_crt_frame const *frame;
Hanno Becker79ae5b62019-02-25 18:12:00 +00002572
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002573 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002574 if( ret != 0 )
2575 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2576
2577 ret = x509_serial_is_revoked( frame->serial.p,
2578 frame->serial.len,
2579 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002580 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002581 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002582}
2583
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002584/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002585 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002586 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002587 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002588static int x509_crt_verifycrl( unsigned char *crt_serial,
2589 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002590 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002591 mbedtls_x509_crl *crl_list,
2592 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002593{
Hanno Beckerbb266132019-02-25 18:12:46 +00002594 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002595 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2597 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002598 mbedtls_x509_buf_raw ca_subject;
2599 mbedtls_pk_context *pk;
2600 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002601
Hanno Beckerbb266132019-02-25 18:12:46 +00002602 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002603 return( flags );
2604
Hanno Beckerbb266132019-02-25 18:12:46 +00002605 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002606 mbedtls_x509_crt_frame const *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002607 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002608 if( ret != 0 )
2609 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2610
2611 ca_subject = ca->subject_raw;
2612
2613 can_sign = 0;
2614 if( x509_crt_check_key_usage_frame( ca,
2615 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2616 {
2617 can_sign = 1;
2618 }
2619
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002620 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002621 }
2622
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002623 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002624 if( ret != 0 )
2625 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2626
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002627 while( crl_list != NULL )
2628 {
2629 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002630 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002631 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002632 {
2633 crl_list = crl_list->next;
2634 continue;
2635 }
2636
2637 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002638 * Check if the CA is configured to sign CRLs
2639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002641 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002644 break;
2645 }
2646#endif
2647
2648 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002649 * Check if CRL is correctly signed by the trusted CA
2650 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002651 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2652 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2653
2654 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2655 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002658 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002659 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002660 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002662 break;
2663 }
2664
Hanno Beckerbb266132019-02-25 18:12:46 +00002665 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002666 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002667
Hanno Beckerbb266132019-02-25 18:12:46 +00002668 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002670 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002673 break;
2674 }
2675
2676 /*
2677 * Check for validity of CRL (Do not drop out)
2678 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002679 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002681
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002682 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002683 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002684
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002685 /*
2686 * Check if certificate is revoked
2687 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002688 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2689 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002692 break;
2693 }
2694
2695 crl_list = crl_list->next;
2696 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002697
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002698 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002699 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002700}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002702
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002703/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002704 * Check the signature of a certificate by its parent
2705 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002706static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002707 mbedtls_x509_crt *parent,
2708 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002709{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002710 int ret;
2711 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002712
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002713 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002714 if( ret != 0 )
2715 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2716
2717 /* Skip expensive computation on obvious mismatch */
2718 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002719 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002720 ret = -1;
2721 goto exit;
2722 }
2723
2724#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2725 ((void) rs_ctx);
2726#else
2727 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2728 {
2729 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002730 sig_info->sig_md,
2731 sig_info->crt_hash, sig_info->crt_hash_len,
2732 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002733 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002734 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002735 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002736#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002737 {
2738 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2739 sig_info->sig_opts,
2740 pk,
2741 sig_info->sig_md,
2742 sig_info->crt_hash, sig_info->crt_hash_len,
2743 sig_info->sig.p, sig_info->sig.len );
2744 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002745
Hanno Beckere449e2d2019-02-25 14:45:31 +00002746exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002747 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002748 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002749}
2750
2751/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002752 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2753 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002754 *
2755 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002756 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002757static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2758 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002759 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002760{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002761 int need_ca_bit;
2762
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002763 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002764 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2765 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002766 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002767 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002768 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002769 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002770
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002771 /* Parent must have the basicConstraints CA bit set as a general rule */
2772 need_ca_bit = 1;
2773
2774 /* Exception: v1/v2 certificates that are locally trusted. */
2775 if( top && parent->version < 3 )
2776 need_ca_bit = 0;
2777
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002778 if( need_ca_bit && ! parent->ca_istrue )
2779 return( -1 );
2780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002782 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002783 x509_crt_check_key_usage_frame( parent,
2784 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002785 {
2786 return( -1 );
2787 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002788#endif
2789
2790 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002791}
2792
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002793/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002794 * Find a suitable parent for child in candidates, or return NULL.
2795 *
2796 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002797 * 1. subject name matches child's issuer
2798 * 2. if necessary, the CA bit is set and key usage allows signing certs
2799 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002800 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002801 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002802 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002803 * If there's a suitable candidate which is also time-valid, return the first
2804 * such. Otherwise, return the first suitable candidate (or NULL if there is
2805 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002806 *
2807 * The rationale for this rule is that someone could have a list of trusted
2808 * roots with two versions on the same root with different validity periods.
2809 * (At least one user reported having such a list and wanted it to just work.)
2810 * The reason we don't just require time-validity is that generally there is
2811 * only one version, and if it's expired we want the flags to state that
2812 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002813 *
2814 * The rationale for rule 3 (signature for trusted roots) is that users might
2815 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002816 * way we select the correct one is by checking the signature (as we don't
2817 * rely on key identifier extensions). (This is one way users might choose to
2818 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002819 *
2820 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002821 * - [in] child: certificate for which we're looking for a parent
2822 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002823 * - [out] r_parent: parent found (or NULL)
2824 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002825 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2826 * of the chain, 0 otherwise
2827 * - [in] path_cnt: number of intermediates seen so far
2828 * - [in] self_cnt: number of self-signed intermediates seen so far
2829 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002830 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002831 *
2832 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002833 * - 0 on success
2834 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002835 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002836static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002837 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002838 mbedtls_x509_crt *candidates,
2839 mbedtls_x509_crt **r_parent,
2840 int *r_signature_is_good,
2841 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002842 unsigned path_cnt,
2843 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002844 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002845{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002846 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002847 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002848 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002849
2850#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002851 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002852 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2853 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002854 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002855 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002856 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002857 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002858
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002859 /* clear saved state */
2860 rs_ctx->parent = NULL;
2861 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002862 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002863
2864 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002865 goto check_signature;
2866 }
2867#endif
2868
2869 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002870 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002871
Hanno Becker43bf9002019-02-25 14:46:49 +00002872 for( parent_crt = candidates; parent_crt != NULL;
2873 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002874 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002875 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002876
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002877#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2878check_signature:
2879#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002880
2881 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002882 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002883 mbedtls_x509_crt_frame const *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002884
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002885 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002886 if( ret != 0 )
2887 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002888
Hanno Becker040c5642019-06-10 11:14:24 +01002889 if( !mbedtls_x509_time_is_past( &parent->valid_to ) &&
2890 !mbedtls_x509_time_is_future( &parent->valid_from ) )
Hanno Becker43bf9002019-02-25 14:46:49 +00002891 {
2892 parent_valid = 1;
2893 }
2894
2895 /* basic parenting skills (name, CA bit, key usage) */
2896 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2897 parent_match = 1;
2898
2899 /* +1 because the stored max_pathlen is 1 higher
2900 * than the actual value */
2901 if( !( parent->max_pathlen > 0 &&
2902 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2903 {
2904 path_len_ok = 1;
2905 }
2906
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002907 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002908 }
2909
2910 if( parent_match == 0 || path_len_ok == 0 )
2911 continue;
2912
2913 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002914 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002915
2916#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002917 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2918 {
2919 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002920 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002921 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002922 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002923
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002924 return( ret );
2925 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002926#else
2927 (void) ret;
2928#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002929
2930 signature_is_good = ret == 0;
2931 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002932 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002933
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002934 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002935 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002936 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002937 if( fallback_parent == NULL )
2938 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002939 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002940 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002941 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002942
2943 continue;
2944 }
2945
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002946 break;
2947 }
2948
Hanno Becker43bf9002019-02-25 14:46:49 +00002949 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002950 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002951 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002952 *r_signature_is_good = signature_is_good;
2953 }
2954 else
2955 {
2956 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002957 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002958 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002959
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002960 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002961}
2962
2963/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002964 * Find a parent in trusted CAs or the provided chain, or return NULL.
2965 *
2966 * Searches in trusted CAs first, and return the first suitable parent found
2967 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002968 *
2969 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002970 * - [in] child: certificate for which we're looking for a parent, followed
2971 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002972 * - [in] trust_ca: list of locally trusted certificates
2973 * - [out] parent: parent found (or NULL)
2974 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2975 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2976 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2977 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002978 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002979 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002980 *
2981 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002982 * - 0 on success
2983 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002984 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002985static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002986 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002987 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002988 mbedtls_x509_crt *trust_ca,
2989 mbedtls_x509_crt **parent,
2990 int *parent_is_trusted,
2991 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002992 unsigned path_cnt,
2993 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002994 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002995{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002996 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002997 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002998
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002999 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003000
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003001#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003002 /* restore then clear saved state if we have some stored */
3003 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
3004 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003005 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003006 rs_ctx->parent_is_trusted = -1;
3007 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003008#endif
3009
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003010 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00003011 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003012
Hanno Becker5299cf82019-02-25 13:50:41 +00003013 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003014 parent, signature_is_good,
3015 *parent_is_trusted,
3016 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003017
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003018#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003019 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3020 {
3021 /* save state */
3022 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003023 return( ret );
3024 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003025#else
3026 (void) ret;
3027#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003028
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003029 /* stop here if found or already in second iteration */
3030 if( *parent != NULL || *parent_is_trusted == 0 )
3031 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003032
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003033 /* prepare second iteration */
3034 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003035 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003036
3037 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01003038 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003039 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02003040 *parent_is_trusted = 0;
3041 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003042 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003043
3044 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003045}
3046
3047/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003048 * Check if an end-entity certificate is locally trusted
3049 *
3050 * Currently we require such certificates to be self-signed (actually only
3051 * check for self-issued as self-signatures are not checked)
3052 */
3053static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003054 mbedtls_x509_crt_frame const *crt,
3055 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003056{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003057 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003058
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003059 /* look for an exact match with trusted cert */
3060 for( cur = trust_ca; cur != NULL; cur = cur->next )
3061 {
3062 if( crt->raw.len == cur->raw.len &&
3063 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3064 {
3065 return( 0 );
3066 }
3067 }
3068
3069 /* too bad */
3070 return( -1 );
3071}
3072
3073/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003074 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003075 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003076 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3077 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003078 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003079 * such that every cert in the chain is a child of the next one,
3080 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003081 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003082 * Verify that chain and return it with flags for all issues found.
3083 *
3084 * Special cases:
3085 * - EE == Rj -> return a one-element list containing it
3086 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3087 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003088 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003089 * Tests for (aspects of) this function should include at least:
3090 * - trusted EE
3091 * - EE -> trusted root
Antonin Décimod5f47592019-01-23 15:24:37 +01003092 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003093 * - if relevant: EE untrusted
3094 * - if relevant: EE -> intermediate, untrusted
3095 * with the aspect under test checked at each relevant level (EE, int, root).
3096 * For some aspects longer chains are required, but usually length 2 is
3097 * enough (but length 1 is not in general).
3098 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003099 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003100 * - [in] crt: the cert list EE, C1, ..., Cn
3101 * - [in] trust_ca: the trusted list R1, ..., Rp
3102 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003103 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003104 * Only valid when return value is 0, may contain garbage otherwise!
3105 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003106 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003107 *
3108 * Return value:
3109 * - non-zero if the chain could not be fully built and examined
3110 * - 0 is the chain was successfully built and examined,
3111 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003112 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003113static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003114 mbedtls_x509_crt *crt,
3115 mbedtls_x509_crt *trust_ca,
3116 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003117 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003118 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003119 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003120{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003121 /* Don't initialize any of those variables here, so that the compiler can
3122 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003123 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003124 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003125 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003126 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003127 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003128 int parent_is_trusted;
3129 int child_is_trusted;
3130 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003131 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003132
3133#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3134 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003135 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003136 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003137 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003138 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003139 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003140
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003141 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003142 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003143 child_crt = cur->crt;
3144
3145 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003146 goto find_parent;
3147 }
3148#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003149
Hanno Becker1e0677a2019-02-25 14:58:22 +00003150 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003151 self_cnt = 0;
3152 parent_is_trusted = 0;
3153 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003154
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003155 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003156#if defined(MBEDTLS_X509_CRL_PARSE_C)
3157 mbedtls_x509_buf_raw child_serial;
3158#endif /* MBEDTLS_X509_CRL_PARSE_C */
3159 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003160
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003161 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003162 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003163 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003164 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003165 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003166
3167#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3168find_parent:
3169#endif
3170
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003171 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003172
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003173 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003174 mbedtls_x509_crt_sig_info child_sig;
3175 {
Hanno Becker5f268b32019-05-20 16:26:34 +01003176 mbedtls_x509_crt_frame const *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003177
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003178 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003179 if( ret != 0 )
3180 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3181
3182 /* Check time-validity (all certificates) */
3183 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3184 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3185 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3186 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
3187
3188 /* Stop here for trusted roots (but not for trusted EE certs) */
3189 if( child_is_trusted )
3190 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003191 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003192 return( 0 );
3193 }
3194
3195 self_issued = 0;
3196 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3197 &child->subject_raw,
3198 NULL, NULL ) == 0 )
3199 {
3200 self_issued = 1;
3201 }
3202
3203 /* Check signature algorithm: MD & PK algs */
3204 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3205 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3206
3207 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3208 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3209
3210 /* Special case: EE certs that are locally trusted */
3211 if( ver_chain->len == 1 && self_issued &&
3212 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3213 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003214 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003215 return( 0 );
3216 }
3217
3218#if defined(MBEDTLS_X509_CRL_PARSE_C)
3219 child_serial = child->serial;
3220#endif /* MBEDTLS_X509_CRL_PARSE_C */
3221
3222 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003223 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003224
Hanno Becker5299cf82019-02-25 13:50:41 +00003225 if( ret != 0 )
3226 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3227 }
3228
3229 /* Look for a parent in trusted CAs or up the chain */
3230 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003231 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003232 &parent_is_trusted, &signature_is_good,
3233 ver_chain->len - 1, self_cnt, rs_ctx );
3234
3235 x509_crt_free_sig_info( &child_sig );
3236 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003237
3238#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003239 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3240 {
3241 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003242 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003243 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003244 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003245 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003246 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003247#else
3248 (void) ret;
3249#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003250
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003251 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003252 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003253 {
3254 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003255 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003256 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003257
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003258 /* Count intermediate self-issued (not necessarily self-signed) certs.
3259 * These can occur with some strategies for key rollover, see [SIRO],
3260 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003261 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003262 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003263
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003264 /* path_cnt is 0 for the first intermediate CA,
3265 * and if parent is trusted it's not an intermediate CA */
3266 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003267 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003268 {
3269 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003270 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003271 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003272
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003273 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003274 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003275 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3276
Hanno Becker58c35642019-02-25 18:13:46 +00003277 {
3278 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003279 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003280 if( ret != 0 )
3281 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3282
3283 /* check size of signing key */
3284 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3285 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3286
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003287 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003288 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003291 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003292 *flags |= x509_crt_verifycrl( child_serial.p,
3293 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003294 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003295#else
3296 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003297#endif
3298
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003299 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003300 child_crt = parent_crt;
3301 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003302 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003303 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003304 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003305}
3306
3307/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003308 * Check for CN match
3309 */
Hanno Becker24926222019-02-21 13:10:55 +00003310static int x509_crt_check_cn( unsigned char const *buf,
3311 size_t buflen,
3312 const char *cn,
3313 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003314{
Hanno Becker24926222019-02-21 13:10:55 +00003315 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003316 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003317 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003318
3319 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003320 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003321 {
3322 return( 0 );
3323 }
3324
3325 return( -1 );
3326}
3327
Hanno Becker8b543b32019-02-21 11:50:44 +00003328/* Returns 1 on a match and 0 on a mismatch.
3329 * This is because this function is used as a callback for
3330 * mbedtls_x509_name_cmp_raw(), which continues the name
3331 * traversal as long as the callback returns 0. */
3332static int x509_crt_check_name( void *ctx,
3333 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003334 mbedtls_x509_buf *val,
3335 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003336{
3337 char const *cn = (char const*) ctx;
3338 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003339 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003340
3341 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003342 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003343 {
3344 return( 1 );
3345 }
3346
3347 return( 0 );
3348}
3349
Hanno Beckerda410822019-02-21 13:36:59 +00003350/* Returns 1 on a match and 0 on a mismatch.
3351 * This is because this function is used as a callback for
3352 * mbedtls_asn1_traverse_sequence_of(), which continues the
3353 * traversal as long as the callback returns 0. */
3354static int x509_crt_subject_alt_check_name( void *ctx,
3355 int tag,
3356 unsigned char *data,
3357 size_t data_len )
3358{
3359 char const *cn = (char const*) ctx;
3360 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003361 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003362
3363 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3364 return( 1 );
3365
3366 return( 0 );
3367}
3368
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003369/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003370 * Verify the requested CN - only call this if cn is not NULL!
3371 */
Hanno Becker082435c2019-02-25 18:14:40 +00003372static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3373 const char *cn,
3374 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003375{
Hanno Beckerda410822019-02-21 13:36:59 +00003376 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01003377 mbedtls_x509_crt_frame const *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003378
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003379 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003380 if( ret != 0 )
3381 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3382
3383 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003384 {
Hanno Becker90b94082019-02-21 21:13:21 +00003385 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003386 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003387 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003388 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003389
Hanno Becker90b94082019-02-21 21:13:21 +00003390 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3391 MBEDTLS_ASN1_TAG_CLASS_MASK,
3392 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3393 MBEDTLS_ASN1_TAG_VALUE_MASK,
3394 2 /* SubjectAlt DNS */,
3395 x509_crt_subject_alt_check_name,
Hanno Becker484caf02019-05-29 14:41:44 +01003396 (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003397 }
3398 else
3399 {
Hanno Becker082435c2019-02-25 18:14:40 +00003400 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3401 &frame->subject_raw,
Hanno Becker484caf02019-05-29 14:41:44 +01003402 x509_crt_check_name, (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003403 }
Hanno Beckerda410822019-02-21 13:36:59 +00003404
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003405 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003406
3407 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3408 * return 1 when finding a name component matching `cn`. */
3409 if( ret == 1 )
3410 return( 0 );
3411
3412 if( ret != 0 )
3413 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3414
3415 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3416 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003417}
3418
3419/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003420 * Merge the flags for all certs in the chain, after calling callback
3421 */
3422static int x509_crt_merge_flags_with_cb(
3423 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003424 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003425 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3426 void *p_vrfy )
3427{
3428 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003429 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003430 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003431 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003432
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003433 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003434 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003435 cur = &ver_chain->items[i-1];
3436 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003437
3438 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003439 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003440 return( ret );
3441
3442 *flags |= cur_flags;
3443 }
3444
3445 return( 0 );
3446}
3447
3448/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003449 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3452 mbedtls_x509_crt *trust_ca,
3453 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003454 const char *cn, uint32_t *flags,
3455 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003456 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003457{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003458 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3459 &mbedtls_x509_crt_profile_default, cn, flags,
3460 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003461}
3462
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003463/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003464 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003465 */
3466int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3467 mbedtls_x509_crt *trust_ca,
3468 mbedtls_x509_crl *ca_crl,
3469 const mbedtls_x509_crt_profile *profile,
3470 const char *cn, uint32_t *flags,
3471 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3472 void *p_vrfy )
3473{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003474 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3475 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3476}
3477
3478/*
3479 * Verify the certificate validity, with profile, restartable version
3480 *
3481 * This function:
3482 * - checks the requested CN (if any)
3483 * - checks the type and size of the EE cert's key,
3484 * as that isn't done as part of chain building/verification currently
3485 * - builds and verifies the chain
3486 * - then calls the callback and merges the flags
3487 */
3488int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3489 mbedtls_x509_crt *trust_ca,
3490 mbedtls_x509_crl *ca_crl,
3491 const mbedtls_x509_crt_profile *profile,
3492 const char *cn, uint32_t *flags,
3493 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3494 void *p_vrfy,
3495 mbedtls_x509_crt_restart_ctx *rs_ctx )
3496{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003497 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003498 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003499 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003500
3501 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003502 ee_flags = 0;
3503 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003504
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003505 if( profile == NULL )
3506 {
3507 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3508 goto exit;
3509 }
3510
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003511 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003512 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003513 {
3514 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3515 if( ret != 0 )
3516 return( ret );
3517 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003518
Hanno Becker87233362019-02-25 18:15:33 +00003519 {
3520 mbedtls_pk_context *pk;
3521 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003522
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003523 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003524 if( ret != 0 )
3525 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003526
Hanno Becker87233362019-02-25 18:15:33 +00003527 /* Check the type and size of the key */
3528 pk_type = mbedtls_pk_get_type( pk );
3529
3530 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3531 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3532
3533 if( x509_profile_check_key( profile, pk ) != 0 )
3534 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3535
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003536 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003537 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003538
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003539 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003540 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003541 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003542
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003543 if( ret != 0 )
3544 goto exit;
3545
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003546 /* Merge end-entity flags */
3547 ver_chain.items[0].flags |= ee_flags;
3548
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003549 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003550 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003551
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003552exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003553#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3554 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3555 mbedtls_x509_crt_restart_free( rs_ctx );
3556#endif
3557
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003558 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3559 * the SSL module for authmode optional, but non-zero return from the
3560 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003561 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3562 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3563
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003564 if( ret != 0 )
3565 {
3566 *flags = (uint32_t) -1;
3567 return( ret );
3568 }
3569
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003570 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003572
3573 return( 0 );
3574}
3575
3576/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003577 * Initialize a certificate chain
3578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003582}
3583
3584/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003585 * Unallocate all certificate data
3586 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 mbedtls_x509_crt *cert_cur = crt;
3591 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003592
3593 if( crt == NULL )
3594 return;
3595
3596 do
3597 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003598 x509_crt_cache_free( cert_cur->cache );
3599 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003600
3601#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3605 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003606#endif
3607
Hanno Becker2bcc7642019-02-26 19:01:00 +00003608 mbedtls_x509_name_free( cert_cur->issuer.next );
3609 mbedtls_x509_name_free( cert_cur->subject.next );
3610 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3611 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003612#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003613
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003614 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003615 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003616 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003618 }
3619
3620 cert_cur = cert_cur->next;
3621 }
3622 while( cert_cur != NULL );
3623
3624 cert_cur = crt;
3625 do
3626 {
3627 cert_prv = cert_cur;
3628 cert_cur = cert_cur->next;
3629
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003630 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003631 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003633 }
3634 while( cert_cur != NULL );
3635}
3636
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003637#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3638/*
3639 * Initialize a restart context
3640 */
3641void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3642{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003643 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003644
3645 ctx->parent = NULL;
3646 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003647 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003648
3649 ctx->parent_is_trusted = -1;
3650
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003651 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003652 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003653 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003654}
3655
3656/*
3657 * Free the components of a restart context
3658 */
3659void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3660{
3661 if( ctx == NULL )
3662 return;
3663
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003664 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003665 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003666}
3667#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669#endif /* MBEDTLS_X509_CRT_PARSE_C */