blob: eb746deb004ed0048519ac26669f13f630826cef [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;
Hanno Becker843b71a2019-06-25 09:39:21 +0100226
227#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000228 frame->valid_from = crt->valid_from;
229 frame->valid_to = crt->valid_to;
Hanno Becker843b71a2019-06-25 09:39:21 +0100230#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
231
Hanno Becker38f0cb42019-03-04 15:13:45 +0000232 x509_buf_to_buf_raw( &frame->raw, &crt->raw );
233 x509_buf_to_buf_raw( &frame->tbs, &crt->tbs );
234 x509_buf_to_buf_raw( &frame->serial, &crt->serial );
235 x509_buf_to_buf_raw( &frame->pubkey_raw, &crt->pk_raw );
236 x509_buf_to_buf_raw( &frame->issuer_raw, &crt->issuer_raw );
237 x509_buf_to_buf_raw( &frame->subject_raw, &crt->subject_raw );
238 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
239 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
240 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
241 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000242
243 /* The legacy CRT structure doesn't explicitly contain
244 * the `AlgorithmIdentifier` bounds; however, those can
245 * be inferred from the surrounding (mandatory) `SerialNumber`
246 * and `Issuer` fields. */
247 frame->sig_alg.p = crt->serial.p + crt->serial.len;
248 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
249
250 return( x509_crt_frame_parse_ext( frame ) );
251#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000252}
Hanno Becker337088a2019-02-25 14:53:14 +0000253
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000254int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
255{
256 mbedtls_x509_crt_cache *cache = crt->cache;
257 mbedtls_pk_context *pk;
258
Hanno Becker76428352019-03-05 15:29:23 +0000259 if( cache->pk != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100260 {
Hanno Becker410322f2019-07-02 13:37:12 +0100261#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
262 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000263 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100264#else
265 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
266 * allow nested uses of acquire. */
267 return( MBEDTLS_ERR_X509_FATAL_ERROR );
268#endif
269 }
Hanno Becker76428352019-03-05 15:29:23 +0000270
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000271 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
272 if( pk == NULL )
273 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000274 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000275
276#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
277 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000278 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000279#else
280 {
281 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
282 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
283 pk_raw.p + pk_raw.len,
284 pk ) );
285 }
286#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000287}
288
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000289static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000290{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000291 memset( cache, 0, sizeof( *cache ) );
292#if defined(MBEDTLS_THREADING_C)
293 mbedtls_mutex_init( &cache->frame_mutex );
294 mbedtls_mutex_init( &cache->pk_mutex );
295#endif
296}
297
298static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
299{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000300#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000301 /* The cache holds a shallow copy of the PK context
302 * in the legacy struct, so don't free PK context. */
303 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000304#else
305 mbedtls_pk_free( cache->pk );
306 mbedtls_free( cache->pk );
307#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
308
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000309 cache->pk = NULL;
310}
311
312static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
313{
314 mbedtls_free( cache->frame );
315 cache->frame = NULL;
316}
317
318static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
319{
320 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000321 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000322
323#if defined(MBEDTLS_THREADING_C)
324 mbedtls_mutex_free( &cache->frame_mutex );
325 mbedtls_mutex_free( &cache->pk_mutex );
326#endif
327
328 x509_crt_cache_clear_frame( cache );
329 x509_crt_cache_clear_pk( cache );
330
331 memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000332}
333
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000334int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
335 mbedtls_x509_sequence **subj_alt )
336{
337 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100338 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000339 mbedtls_x509_sequence *seq;
340
341 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
342 if( ret != 0 )
343 return( ret );
344
345 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
346 if( seq == NULL )
347 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
348 else
349 ret = x509_crt_subject_alt_from_frame( frame, seq );
350
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000351 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000352
353 *subj_alt = seq;
354 return( ret );
355}
356
357int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
358 mbedtls_x509_sequence **ext_key_usage )
359{
360 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100361 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000362 mbedtls_x509_sequence *seq;
363
364 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
365 if( ret != 0 )
366 return( ret );
367
368 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
369 if( seq == NULL )
370 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
371 else
372 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
373
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000374 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000375
376 *ext_key_usage = seq;
377 return( ret );
378}
379
Hanno Becker63e69982019-02-26 18:50:49 +0000380int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
381 mbedtls_x509_name **subject )
382{
383 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100384 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000385 mbedtls_x509_name *name;
386
387 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
388 if( ret != 0 )
389 return( ret );
390
391 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
392 if( name == NULL )
393 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
394 else
395 ret = x509_crt_subject_from_frame( frame, name );
396
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000397 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000398
399 *subject = name;
400 return( ret );
401}
402
403int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
404 mbedtls_x509_name **issuer )
405{
406 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100407 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000408 mbedtls_x509_name *name;
409
410 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
411 if( ret != 0 )
412 return( ret );
413
414 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
415 if( name == NULL )
416 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
417 else
418 ret = x509_crt_issuer_from_frame( frame, name );
419
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000420 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000421
422 *issuer = name;
423 return( ret );
424}
425
Hanno Becker823efad2019-02-28 13:23:58 +0000426int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
427 mbedtls_x509_crt_frame *dst )
428{
429 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100430 mbedtls_x509_crt_frame const *frame;
Hanno Becker823efad2019-02-28 13:23:58 +0000431 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
432 if( ret != 0 )
433 return( ret );
434 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000435 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000436 return( 0 );
437}
438
439int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
440 mbedtls_pk_context *dst )
441{
442#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
443 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
444 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
445 pk_raw.p + pk_raw.len,
446 dst ) );
447#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
448 int ret;
449 mbedtls_pk_context *pk;
450 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
451 if( ret != 0 )
452 return( ret );
453
454 /* Move PK from CRT cache to destination pointer
455 * to avoid a copy. */
456 *dst = *pk;
457 mbedtls_free( crt->cache->pk );
458 crt->cache->pk = NULL;
459
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000460 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000461 return( 0 );
462#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
463}
464
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200465/*
466 * Item in a verification chain: cert and flags for it
467 */
468typedef struct {
469 mbedtls_x509_crt *crt;
470 uint32_t flags;
471} x509_crt_verify_chain_item;
472
473/*
474 * Max size of verification chain: end-entity + intermediates + trusted root
475 */
476#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200477
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200479 * Default profile
480 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200481const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
482{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200483#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200484 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200485 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200486#endif
487 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200488 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
489 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
490 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
491 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
492 0xFFFFFFF, /* Any PK alg */
493 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200494 2048,
495};
496
497/*
498 * Next-default profile
499 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200500const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
501{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200502 /* Hashes from SHA-256 and above */
503 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
504 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
505 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
506 0xFFFFFFF, /* Any PK alg */
507#if defined(MBEDTLS_ECP_C)
508 /* Curves at or above 128-bit security level */
509 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
510 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
511 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
512 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
513 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
514 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
515 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
516#else
517 0,
518#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200519 2048,
520};
521
522/*
523 * NSA Suite B Profile
524 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200525const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
526{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200527 /* Only SHA-256 and 384 */
528 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
529 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
530 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200531 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
532 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200533#if defined(MBEDTLS_ECP_C)
534 /* Only NIST P-256 and P-384 */
535 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
536 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
537#else
538 0,
539#endif
540 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200541};
542
543/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200544 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200545 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200546 */
547static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
548 mbedtls_md_type_t md_alg )
549{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200550 if( md_alg == MBEDTLS_MD_NONE )
551 return( -1 );
552
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200553 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
554 return( 0 );
555
556 return( -1 );
557}
558
559/*
560 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200561 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200562 */
563static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
564 mbedtls_pk_type_t pk_alg )
565{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200566 if( pk_alg == MBEDTLS_PK_NONE )
567 return( -1 );
568
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200569 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
570 return( 0 );
571
572 return( -1 );
573}
574
575/*
576 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200577 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200578 */
579static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200580 const mbedtls_pk_context *pk )
581{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200582 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200583
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200584#if defined(MBEDTLS_RSA_C)
585 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
586 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200587 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200588 return( 0 );
589
590 return( -1 );
591 }
592#endif
593
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200594#if defined(MBEDTLS_ECP_C)
595 if( pk_alg == MBEDTLS_PK_ECDSA ||
596 pk_alg == MBEDTLS_PK_ECKEY ||
597 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200598 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200599 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200600
Philippe Antoineb5b25432018-05-11 11:06:29 +0200601 if( gid == MBEDTLS_ECP_DP_NONE )
602 return( -1 );
603
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200604 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
605 return( 0 );
606
607 return( -1 );
608 }
609#endif
610
611 return( -1 );
612}
613
614/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000615 * Return 0 if name matches wildcard, -1 otherwise
616 */
Hanno Becker24926222019-02-21 13:10:55 +0000617static int x509_check_wildcard( char const *cn,
618 size_t cn_len,
619 unsigned char const *buf,
620 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000621{
622 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000623 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000624
625 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000626 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000627 return( -1 );
628
629 for( i = 0; i < cn_len; ++i )
630 {
631 if( cn[i] == '.' )
632 {
633 cn_idx = i;
634 break;
635 }
636 }
637
638 if( cn_idx == 0 )
639 return( -1 );
640
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000641 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
642 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000643 {
644 return( 0 );
645 }
646
647 return( -1 );
648}
649
650/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200651 * Reset (init or clear) a verify_chain
652 */
653static void x509_crt_verify_chain_reset(
654 mbedtls_x509_crt_verify_chain *ver_chain )
655{
656 size_t i;
657
658 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
659 {
660 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000661 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200662 }
663
664 ver_chain->len = 0;
665}
666
667/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
669 */
670static int x509_get_version( unsigned char **p,
671 const unsigned char *end,
672 int *ver )
673{
674 int ret;
675 size_t len;
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
678 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681 {
682 *ver = 0;
683 return( 0 );
684 }
685
Hanno Becker2f472142019-02-12 11:52:10 +0000686 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 }
688
689 end = *p + len;
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
692 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
694 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( MBEDTLS_ERR_X509_INVALID_VERSION +
696 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697
698 return( 0 );
699}
700
Hanno Becker843b71a2019-06-25 09:39:21 +0100701#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702/*
703 * Validity ::= SEQUENCE {
704 * notBefore Time,
705 * notAfter Time }
706 */
707static int x509_get_dates( unsigned char **p,
708 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_x509_time *from,
710 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711{
712 int ret;
713 size_t len;
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
716 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
717 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718
719 end = *p + len;
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 return( ret );
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 return( ret );
726
727 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_X509_INVALID_DATE +
729 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
731 return( 0 );
732}
Hanno Becker843b71a2019-06-25 09:39:21 +0100733#else /* !MBEDTLS_X509_CRT_REMOVE_TIME */
734static int x509_skip_dates( unsigned char **p,
735 const unsigned char *end )
736{
737 int ret;
738 size_t len;
739
740 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
741 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
742 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
743
744 end = *p + len;
745
746 if( *p != end )
747 return( MBEDTLS_ERR_X509_INVALID_DATE +
748 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
749
750 return( 0 );
751}
752#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753
754/*
755 * X.509 v2/v3 unique identifier (not parsed)
756 */
757static int x509_get_uid( unsigned char **p,
758 const unsigned char *end,
Hanno Beckere9084122019-06-07 12:04:39 +0100759 mbedtls_x509_buf_raw *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760{
761 int ret;
762
763 if( *p == end )
764 return( 0 );
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
767 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 return( 0 );
771
Hanno Becker2f472142019-02-12 11:52:10 +0000772 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 }
774
775 uid->p = *p;
776 *p += uid->len;
777
778 return( 0 );
779}
780
781static int x509_get_basic_constraints( unsigned char **p,
782 const unsigned char *end,
783 int *ca_istrue,
784 int *max_pathlen )
785{
786 int ret;
787 size_t len;
788
789 /*
790 * BasicConstraints ::= SEQUENCE {
791 * cA BOOLEAN DEFAULT FALSE,
792 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
793 */
794 *ca_istrue = 0; /* DEFAULT FALSE */
795 *max_pathlen = 0; /* endless */
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
798 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000799 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800
801 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200802 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
807 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808
809 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000810 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811
812 if( *ca_istrue != 0 )
813 *ca_istrue = 1;
814 }
815
816 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200817 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000820 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821
822 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000823 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824
825 (*max_pathlen)++;
826
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200827 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828}
829
830static int x509_get_ns_cert_type( unsigned char **p,
831 const unsigned char *end,
832 unsigned char *ns_cert_type)
833{
834 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000838 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839
840 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000841 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200842
843 /* Get actual bitstring */
844 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200845 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846}
847
848static int x509_get_key_usage( unsigned char **p,
849 const unsigned char *end,
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100850 uint16_t *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851{
852 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200853 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000857 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200858
859 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000860 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861
862 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200863 *key_usage = 0;
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100864 for( i = 0; i < bs.len && i < sizeof( *key_usage ); i++ )
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200865 {
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100866 *key_usage |= (uint16_t) bs.p[i] << ( 8*i );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200867 }
868
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200869 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870}
871
Hanno Becker529f25d2019-05-02 14:48:25 +0100872static int asn1_build_sequence_cb( void *ctx,
873 int tag,
874 unsigned char *data,
875 size_t data_len )
Hanno Becker15b73b42019-05-02 13:21:27 +0100876{
877 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
878 mbedtls_asn1_sequence *cur = *cur_ptr;
879
880 /* Allocate and assign next pointer */
881 if( cur->buf.p != NULL )
882 {
883 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
884 if( cur->next == NULL )
885 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
886 cur = cur->next;
887 }
888
889 cur->buf.tag = tag;
890 cur->buf.p = data;
891 cur->buf.len = data_len;
892
893 *cur_ptr = cur;
894 return( 0 );
895}
896
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897/*
Hanno Becker529f25d2019-05-02 14:48:25 +0100898 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
899 *
900 * KeyPurposeId ::= OBJECT IDENTIFIER
901 */
902static int x509_get_ext_key_usage( unsigned char **p,
903 const unsigned char *end,
904 mbedtls_x509_sequence *ext_key_usage)
905{
906 return( mbedtls_asn1_traverse_sequence_of( p, end,
907 0xFF, MBEDTLS_ASN1_OID,
908 0, 0,
909 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100910 (void *) &ext_key_usage ) );
Hanno Becker529f25d2019-05-02 14:48:25 +0100911}
912
913/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 * SubjectAltName ::= GeneralNames
915 *
916 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
917 *
918 * GeneralName ::= CHOICE {
919 * otherName [0] OtherName,
920 * rfc822Name [1] IA5String,
921 * dNSName [2] IA5String,
922 * x400Address [3] ORAddress,
923 * directoryName [4] Name,
924 * ediPartyName [5] EDIPartyName,
925 * uniformResourceIdentifier [6] IA5String,
926 * iPAddress [7] OCTET STRING,
927 * registeredID [8] OBJECT IDENTIFIER }
928 *
929 * OtherName ::= SEQUENCE {
930 * type-id OBJECT IDENTIFIER,
931 * value [0] EXPLICIT ANY DEFINED BY type-id }
932 *
933 * EDIPartyName ::= SEQUENCE {
934 * nameAssigner [0] DirectoryString OPTIONAL,
935 * partyName [1] DirectoryString }
936 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000937 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 */
Hanno Becker5984d302019-02-21 14:46:54 +0000939static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000943 return( mbedtls_asn1_traverse_sequence_of( &p, end,
944 MBEDTLS_ASN1_TAG_CLASS_MASK,
945 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
946 MBEDTLS_ASN1_TAG_VALUE_MASK,
947 2 /* SubjectAlt DNS */,
Hanno Becker529f25d2019-05-02 14:48:25 +0100948 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100949 (void *) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950}
951
952/*
953 * X.509 v3 extensions
954 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000956static int x509_crt_get_ext_cb( void *ctx,
957 int tag,
958 unsigned char *p,
959 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960{
961 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000962 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000964 unsigned char *end, *end_ext_octet;
965 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
966 int is_critical = 0; /* DEFAULT FALSE */
967 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000969 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000970
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000971 /*
972 * Extension ::= SEQUENCE {
973 * extnID OBJECT IDENTIFIER,
974 * critical BOOLEAN DEFAULT FALSE,
975 * extnValue OCTET STRING }
976 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000978 end = p + ext_len;
979
980 /* Get extension ID */
981 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
982 MBEDTLS_ASN1_OID ) ) != 0 )
983 goto err;
984
985 extn_oid.tag = MBEDTLS_ASN1_OID;
986 extn_oid.p = p;
987 p += extn_oid.len;
988
989 /* Get optional critical */
990 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
991 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
992 goto err;
993
994 /* Data should be octet string type */
995 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
996 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
997 goto err;
998
999 end_ext_octet = p + len;
1000 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001002 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
1003 goto err;
1004 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001006 /*
1007 * Detect supported extensions
1008 */
1009 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
1010 if( ret != 0 )
1011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001013 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001015 /* Data is marked as critical: fail */
1016 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
1017 goto err;
1018 }
Hanno Beckerb36a2452019-05-29 14:43:17 +01001019#endif /* MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001020 return( 0 );
1021 }
1022
1023 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +00001024 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001025 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
1026
Hanno Becker21f55672019-02-15 15:27:59 +00001027 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001028 switch( ext_type )
1029 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001030 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001031 {
1032 int ca_istrue;
1033 int max_pathlen;
1034
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001036 ret = x509_get_basic_constraints( &p, end_ext_octet,
1037 &ca_istrue,
1038 &max_pathlen );
1039 if( ret != 0 )
1040 goto err;
1041
Hanno Becker21f55672019-02-15 15:27:59 +00001042 frame->ca_istrue = ca_istrue;
1043 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001045 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001049 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001050 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001051 if( ret != 0 )
1052 goto err;
1053 break;
1054
1055 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1056 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +00001057 frame->subject_alt_raw.p = p;
1058 frame->subject_alt_raw.len = end_ext_octet - p;
1059
1060 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1061 MBEDTLS_ASN1_TAG_CLASS_MASK,
1062 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
1063 MBEDTLS_ASN1_TAG_VALUE_MASK,
1064 2 /* SubjectAlt DNS */,
1065 NULL, NULL );
1066 if( ret != 0 )
1067 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 break;
1069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001072 frame->ext_key_usage_raw.p = p;
1073 frame->ext_key_usage_raw.len = end_ext_octet - p;
1074 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001075 {
Hanno Becker21f55672019-02-15 15:27:59 +00001076 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1077 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001078 }
Hanno Becker21f55672019-02-15 15:27:59 +00001079
1080 /* Check structural sanity of extension. */
1081 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1082 0xFF, MBEDTLS_ASN1_OID,
1083 0, 0, NULL, NULL );
1084 if( ret != 0 )
1085 goto err;
1086
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087 break;
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001091 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001092 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001093 if( ret != 0 )
1094 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095 break;
1096
1097 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001098 /*
1099 * If this is a non-critical extension, which the oid layer
1100 * supports, but there isn't an X.509 parser for it,
1101 * skip the extension.
1102 */
1103#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1104 if( is_critical )
1105 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1106#endif
1107 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108 }
1109
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001111
1112err:
1113 return( ret );
1114}
1115
Hanno Becker21f55672019-02-15 15:27:59 +00001116static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001117{
1118 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001119 unsigned char *p = frame->v3_ext.p;
1120 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001121
Hanno Becker21f55672019-02-15 15:27:59 +00001122 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001123 return( 0 );
1124
Hanno Becker21f55672019-02-15 15:27:59 +00001125 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001126 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001127 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001128
1129 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1130 return( ret );
1131 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1132 return( ret );
1133
1134 if( ret != 0 )
1135 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1136
1137 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138}
1139
Hanno Becker21f55672019-02-15 15:27:59 +00001140static int x509_crt_parse_frame( unsigned char *start,
1141 unsigned char *end,
1142 mbedtls_x509_crt_frame *frame )
1143{
1144 int ret;
1145 unsigned char *p;
1146 size_t len;
1147
1148 mbedtls_x509_buf tmp;
1149 unsigned char *tbs_start;
1150
1151 mbedtls_x509_buf outer_sig_alg;
1152 size_t inner_sig_alg_len;
1153 unsigned char *inner_sig_alg_start;
1154
1155 memset( frame, 0, sizeof( *frame ) );
1156
1157 /*
1158 * Certificate ::= SEQUENCE {
1159 * tbsCertificate TBSCertificate,
1160 * signatureAlgorithm AlgorithmIdentifier,
1161 * signatureValue BIT STRING
1162 * }
1163 *
1164 */
1165 p = start;
1166
1167 frame->raw.p = p;
1168 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1169 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1170 {
1171 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1172 }
1173
1174 /* NOTE: We are currently not checking that the `Certificate`
1175 * structure spans the entire buffer. */
1176 end = p + len;
1177 frame->raw.len = end - frame->raw.p;
1178
1179 /*
1180 * TBSCertificate ::= SEQUENCE { ...
1181 */
1182 frame->tbs.p = p;
1183 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1184 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1185 {
1186 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1187 }
1188 tbs_start = p;
1189
1190 /* Breadth-first parsing: Jump over TBS for now. */
1191 p += len;
1192 frame->tbs.len = p - frame->tbs.p;
1193
1194 /*
1195 * AlgorithmIdentifier ::= SEQUENCE { ...
1196 */
1197 outer_sig_alg.p = p;
1198 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1199 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1200 {
1201 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1202 }
1203 p += len;
1204 outer_sig_alg.len = p - outer_sig_alg.p;
1205
1206 /*
1207 * signatureValue BIT STRING
1208 */
1209 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1210 if( ret != 0 )
1211 return( ret );
1212 frame->sig.p = tmp.p;
1213 frame->sig.len = tmp.len;
1214
1215 /* Check that we consumed the entire `Certificate` structure. */
1216 if( p != end )
1217 {
1218 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1219 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1220 }
1221
1222 /* Parse TBSCertificate structure
1223 *
1224 * TBSCertificate ::= SEQUENCE {
1225 * version [0] EXPLICIT Version DEFAULT v1,
1226 * serialNumber CertificateSerialNumber,
1227 * signature AlgorithmIdentifier,
1228 * issuer Name,
1229 * validity Validity,
1230 * subject Name,
1231 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1232 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1233 * -- If present, version MUST be v2 or v3
1234 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1235 * -- If present, version MUST be v2 or v3
1236 * extensions [3] EXPLICIT Extensions OPTIONAL
1237 * -- If present, version MUST be v3
1238 * }
1239 */
1240 end = frame->tbs.p + frame->tbs.len;
1241 p = tbs_start;
1242
1243 /*
1244 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1245 */
1246 {
1247 int version;
1248 ret = x509_get_version( &p, end, &version );
1249 if( ret != 0 )
1250 return( ret );
1251
1252 if( version < 0 || version > 2 )
1253 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1254
1255 frame->version = version + 1;
1256 }
1257
1258 /*
1259 * CertificateSerialNumber ::= INTEGER
1260 */
1261 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1262 if( ret != 0 )
1263 return( ret );
1264
1265 frame->serial.p = tmp.p;
1266 frame->serial.len = tmp.len;
1267
1268 /*
1269 * signature AlgorithmIdentifier
1270 */
1271 inner_sig_alg_start = p;
1272 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1273 &frame->sig_pk, NULL );
1274 if( ret != 0 )
1275 return( ret );
1276 inner_sig_alg_len = p - inner_sig_alg_start;
1277
1278 frame->sig_alg.p = inner_sig_alg_start;
1279 frame->sig_alg.len = inner_sig_alg_len;
1280
1281 /* Consistency check:
1282 * Inner and outer AlgorithmIdentifier structures must coincide:
1283 *
1284 * Quoting RFC 5280, Section 4.1.1.2:
1285 * This field MUST contain the same algorithm identifier as the
1286 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1287 */
1288 if( outer_sig_alg.len != inner_sig_alg_len ||
1289 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1290 {
1291 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1292 }
1293
1294 /*
1295 * issuer Name
1296 *
1297 * Name ::= CHOICE { -- only one possibility for now --
1298 * rdnSequence RDNSequence }
1299 *
1300 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1301 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001302 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001303
1304 ret = mbedtls_asn1_get_tag( &p, end, &len,
1305 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1306 if( ret != 0 )
1307 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001308 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001309 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001310
Hanno Becker3aa12162019-07-02 16:47:40 +01001311 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001312 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1313 &frame->issuer_raw,
1314 NULL, NULL );
1315 if( ret != 0 )
1316 return( ret );
1317
Hanno Becker21f55672019-02-15 15:27:59 +00001318 /*
1319 * Validity ::= SEQUENCE { ...
1320 */
Hanno Becker843b71a2019-06-25 09:39:21 +01001321#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker21f55672019-02-15 15:27:59 +00001322 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1323 if( ret != 0 )
1324 return( ret );
Hanno Becker843b71a2019-06-25 09:39:21 +01001325#else /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1326 ret = x509_skip_dates( &p, end );
1327 if( ret != 0 )
1328 return( ret );
1329#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker21f55672019-02-15 15:27:59 +00001330
1331 /*
1332 * subject Name
1333 *
1334 * Name ::= CHOICE { -- only one possibility for now --
1335 * rdnSequence RDNSequence }
1336 *
1337 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1338 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001339 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001340
1341 ret = mbedtls_asn1_get_tag( &p, end, &len,
1342 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1343 if( ret != 0 )
1344 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001345 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001346 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001347
Hanno Becker3aa12162019-07-02 16:47:40 +01001348 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001349 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1350 &frame->subject_raw,
1351 NULL, NULL );
1352 if( ret != 0 )
1353 return( ret );
1354
Hanno Becker21f55672019-02-15 15:27:59 +00001355 /*
1356 * SubjectPublicKeyInfo
1357 */
1358 frame->pubkey_raw.p = p;
1359 ret = mbedtls_asn1_get_tag( &p, end, &len,
1360 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1361 if( ret != 0 )
1362 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1363 p += len;
1364 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1365
Hanno Becker97aa4362019-06-08 07:38:20 +01001366 if( frame->version != 1 )
Hanno Becker21f55672019-02-15 15:27:59 +00001367 {
Hanno Beckerfd64f142019-06-07 11:47:12 +01001368 /*
1369 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1370 * -- If present, version shall be v2 or v3
1371 */
Hanno Beckere9084122019-06-07 12:04:39 +01001372 ret = x509_get_uid( &p, end, &frame->issuer_id, 1 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001373 if( ret != 0 )
1374 return( ret );
1375
Hanno Beckerfd64f142019-06-07 11:47:12 +01001376 /*
1377 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1378 * -- If present, version shall be v2 or v3
1379 */
Hanno Beckere9084122019-06-07 12:04:39 +01001380 ret = x509_get_uid( &p, end, &frame->subject_id, 2 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001381 if( ret != 0 )
1382 return( ret );
Hanno Becker21f55672019-02-15 15:27:59 +00001383 }
1384
1385 /*
1386 * extensions [3] EXPLICIT Extensions OPTIONAL
1387 * -- If present, version shall be v3
1388 */
1389#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1390 if( frame->version == 3 )
1391#endif
1392 {
1393 if( p != end )
1394 {
1395 ret = mbedtls_asn1_get_tag( &p, end, &len,
1396 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1397 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1398 if( len == 0 )
1399 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1400 if( ret != 0 )
1401 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1402
1403 frame->v3_ext.p = p;
1404 frame->v3_ext.len = len;
1405
1406 p += len;
1407 }
1408
1409 ret = x509_crt_frame_parse_ext( frame );
1410 if( ret != 0 )
1411 return( ret );
1412 }
1413
1414 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1415 if( p != end )
1416 {
1417 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1418 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1419 }
1420
1421 return( 0 );
1422}
1423
Hanno Becker12506232019-05-13 13:53:21 +01001424static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001425 mbedtls_x509_name *subject )
1426{
Hanno Becker1e11f212019-03-04 14:43:43 +00001427 return( mbedtls_x509_get_name( frame->subject_raw.p,
1428 frame->subject_raw.len,
1429 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001430}
1431
Hanno Becker12506232019-05-13 13:53:21 +01001432static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001433 mbedtls_x509_name *issuer )
1434{
Hanno Becker1e11f212019-03-04 14:43:43 +00001435 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1436 frame->issuer_raw.len,
1437 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001438}
1439
Hanno Becker12506232019-05-13 13:53:21 +01001440static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001441 mbedtls_x509_sequence *subject_alt )
1442{
1443 int ret;
1444 unsigned char *p = frame->subject_alt_raw.p;
1445 unsigned char *end = p + frame->subject_alt_raw.len;
1446
1447 memset( subject_alt, 0, sizeof( *subject_alt ) );
1448
1449 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1450 return( 0 );
1451
1452 ret = x509_get_subject_alt_name( p, end, subject_alt );
1453 if( ret != 0 )
1454 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1455 return( ret );
1456}
1457
Hanno Becker12506232019-05-13 13:53:21 +01001458static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001459 mbedtls_x509_sequence *ext_key_usage )
1460{
1461 int ret;
1462 unsigned char *p = frame->ext_key_usage_raw.p;
1463 unsigned char *end = p + frame->ext_key_usage_raw.len;
1464
1465 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1466
1467 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1468 return( 0 );
1469
1470 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1471 if( ret != 0 )
1472 {
1473 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1474 return( ret );
1475 }
1476
1477 return( 0 );
1478}
1479
Hanno Becker180f7bf2019-02-28 13:23:38 +00001480#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001481static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1482 mbedtls_pk_context *pk )
1483{
1484 unsigned char *p = frame->pubkey_raw.p;
1485 unsigned char *end = p + frame->pubkey_raw.len;
1486 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1487}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001488#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001489
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490/*
1491 * Parse and fill a single X.509 certificate in DER format
1492 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001493static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1494 const unsigned char *buf,
1495 size_t buflen,
1496 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497{
1498 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001499 mbedtls_x509_crt_frame *frame;
1500 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001501
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504
Hanno Becker21f55672019-02-15 15:27:59 +00001505 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506 {
Hanno Becker21f55672019-02-15 15:27:59 +00001507 crt->raw.p = (unsigned char*) buf;
1508 crt->raw.len = buflen;
1509 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001510 }
1511 else
1512 {
Hanno Becker7b8e11e2019-05-03 12:37:12 +01001513 /* Call mbedtls_calloc with buflen + 1 in order to avoid potential
1514 * return of NULL in case of length 0 certificates, which we want
1515 * to cleanly fail with MBEDTLS_ERR_X509_INVALID_FORMAT in the
1516 * core parsing routine, but not here. */
1517 crt->raw.p = mbedtls_calloc( 1, buflen + 1 );
Hanno Becker21f55672019-02-15 15:27:59 +00001518 if( crt->raw.p == NULL )
1519 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1520 crt->raw.len = buflen;
1521 memcpy( crt->raw.p, buf, buflen );
1522
1523 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001524 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001525
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001526 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1527 if( cache == NULL )
1528 {
1529 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1530 goto exit;
1531 }
1532 crt->cache = cache;
1533 x509_crt_cache_init( cache );
1534
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001535#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1536
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001537 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001538 if( ret != 0 )
1539 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540
Hanno Becker76428352019-03-05 15:29:23 +00001541 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001542
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001543#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001545 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1546 if( frame == NULL )
1547 {
1548 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1549 goto exit;
1550 }
1551 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001552
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001553 ret = x509_crt_parse_frame( crt->raw.p,
1554 crt->raw.p + crt->raw.len,
1555 frame );
1556 if( ret != 0 )
1557 goto exit;
1558
Hanno Becker21f55672019-02-15 15:27:59 +00001559 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1560 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001561 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1562 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1563 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1564 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
1565 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1566 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
1567 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1568 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1569 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Becker843b71a2019-06-25 09:39:21 +01001570
1571#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001572 crt->valid_from = frame->valid_from;
1573 crt->valid_to = frame->valid_to;
Hanno Becker843b71a2019-06-25 09:39:21 +01001574#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1575
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001576 crt->version = frame->version;
1577 crt->ca_istrue = frame->ca_istrue;
1578 crt->max_pathlen = frame->max_pathlen;
1579 crt->ext_types = frame->ext_types;
1580 crt->key_usage = frame->key_usage;
1581 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
1583 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001584 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587 {
Hanno Becker21f55672019-02-15 15:27:59 +00001588 /* sig_oid: Previously, needed for convenience in
1589 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001590 unsigned char *tmp = frame->sig_alg.p;
1591 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001592 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
Hanno Becker21f55672019-02-15 15:27:59 +00001594 ret = mbedtls_x509_get_alg( &tmp, end,
1595 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596 if( ret != 0 )
1597 {
Hanno Becker21f55672019-02-15 15:27:59 +00001598 /* This should never happen, because we check
1599 * the sanity of the AlgorithmIdentifier structure
1600 * during frame parsing. */
1601 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1602 goto exit;
1603 }
1604 crt->sig_oid = sig_oid;
1605
1606 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001607 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001608 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1609 &crt->sig_md, &crt->sig_pk,
1610 &crt->sig_opts );
1611 if( ret != 0 )
1612 {
1613 /* Again, this should never happen. */
1614 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1615 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616 }
1617 }
1618
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001619 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001620 if( ret != 0 )
1621 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001623 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001624 if( ret != 0 )
1625 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001627 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001628 if( ret != 0 )
1629 goto exit;
1630
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001631 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001632 if( ret != 0 )
1633 goto exit;
1634
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001635 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1636 if( ret != 0 )
1637 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001638#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001639
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001640 /* Currently, we accept DER encoded CRTs with trailing garbage
1641 * and promise to not account for the garbage in the `raw` field.
1642 *
1643 * Note that this means that `crt->raw.len` is not necessarily the
1644 * full size of the heap buffer allocated at `crt->raw.p` in case
1645 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1646 * need the size, and the garbage data doesn't need zeroization. */
1647 crt->raw.len = frame->raw.len;
1648
1649 cache->pk_raw = frame->pubkey_raw;
1650
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001651 /* Free the frame before parsing the public key to
1652 * keep peak RAM usage low. This is slightly inefficient
1653 * because the frame will need to be parsed again on the
1654 * first usage of the CRT, but that seems acceptable.
1655 * As soon as the frame gets used multiple times, it
1656 * will be cached by default. */
1657 x509_crt_cache_clear_frame( crt->cache );
1658
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001659 /* The cache just references the PK structure from the legacy
1660 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001661 * the cache.
1662 *
1663 * We're not actually using the parsed PK context here;
1664 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001665 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001666 if( ret != 0 )
1667 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001668 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001669
1670exit:
1671 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673
Hanno Becker21f55672019-02-15 15:27:59 +00001674 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675}
1676
1677/*
1678 * Parse one X.509 certificate in DER format from a buffer and add them to a
1679 * chained list
1680 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001681static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1682 const unsigned char *buf,
1683 size_t buflen,
1684 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685{
1686 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688
1689 /*
1690 * Check for valid input
1691 */
1692 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694
Hanno Becker371e0e42019-02-25 18:08:59 +00001695 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696 {
1697 prev = crt;
1698 crt = crt->next;
1699 }
1700
1701 /*
1702 * Add new certificate on the end of the chain if needed.
1703 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001704 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001706 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707
1708 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001709 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001710
1711 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714 }
1715
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001716 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717 {
1718 if( prev )
1719 prev->next = NULL;
1720
1721 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723
1724 return( ret );
1725 }
1726
1727 return( 0 );
1728}
1729
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001730int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1731 const unsigned char *buf,
1732 size_t buflen )
1733{
1734 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1735}
1736
1737int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1738 const unsigned char *buf,
1739 size_t buflen )
1740{
1741 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1742}
1743
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001744/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001745 * Parse one or more PEM certificates from a buffer and add them to the chained
1746 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001748int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1749 const unsigned char *buf,
1750 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751{
Janos Follath98e28a72016-05-31 14:03:54 +01001752#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001753 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001755#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756
1757 /*
1758 * Check for valid input
1759 */
1760 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001762
1763 /*
1764 * Determine buffer content. Buffer contains either one DER certificate or
1765 * one or more PEM certificates.
1766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001768 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001769 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001772 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1775 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001776#else
1777 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1778#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780#if defined(MBEDTLS_PEM_PARSE_C)
1781 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782 {
1783 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001786 /* 1 rather than 0 since the terminating NULL byte is counted in */
1787 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001788 {
1789 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001791
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001792 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794 "-----BEGIN CERTIFICATE-----",
1795 "-----END CERTIFICATE-----",
1796 buf, NULL, 0, &use_len );
1797
1798 if( ret == 0 )
1799 {
1800 /*
1801 * Was PEM encoded
1802 */
1803 buflen -= use_len;
1804 buf += use_len;
1805 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001807 {
1808 return( ret );
1809 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001813
1814 /*
1815 * PEM header and footer were found
1816 */
1817 buflen -= use_len;
1818 buf += use_len;
1819
1820 if( first_error == 0 )
1821 first_error = ret;
1822
Paul Bakker5a5fa922014-09-26 14:53:04 +02001823 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001824 continue;
1825 }
1826 else
1827 break;
1828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001832
1833 if( ret != 0 )
1834 {
1835 /*
1836 * Quit parsing on a memory error
1837 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001838 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001839 return( ret );
1840
1841 if( first_error == 0 )
1842 first_error = ret;
1843
1844 total_failed++;
1845 continue;
1846 }
1847
1848 success = 1;
1849 }
1850 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001851
1852 if( success )
1853 return( total_failed );
1854 else if( first_error )
1855 return( first_error );
1856 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001858#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001859}
1860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001862/*
1863 * Load one or more certificates and add them to the chained list
1864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001866{
1867 int ret;
1868 size_t n;
1869 unsigned char *buf;
1870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001872 return( ret );
1873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001875
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001876 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001878
1879 return( ret );
1880}
1881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001883{
1884 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001885#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001886 int w_ret;
1887 WCHAR szDir[MAX_PATH];
1888 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001889 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001890 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001891
Paul Bakker9af723c2014-05-01 13:03:14 +02001892 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001893 HANDLE hFind;
1894
1895 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001897
Paul Bakker9af723c2014-05-01 13:03:14 +02001898 memset( szDir, 0, sizeof(szDir) );
1899 memset( filename, 0, MAX_PATH );
1900 memcpy( filename, path, len );
1901 filename[len++] = '\\';
1902 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001903 filename[len++] = '*';
1904
Simon B3c6b18d2016-11-03 01:11:37 +00001905 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001906 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001907 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909
1910 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001911 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001913
1914 len = MAX_PATH - len;
1915 do
1916 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001917 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918
1919 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1920 continue;
1921
Paul Bakker9af723c2014-05-01 13:03:14 +02001922 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001923 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001924 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001925 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001926 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001927 {
1928 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1929 goto cleanup;
1930 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001933 if( w_ret < 0 )
1934 ret++;
1935 else
1936 ret += w_ret;
1937 }
1938 while( FindNextFileW( hFind, &file_data ) != 0 );
1939
Paul Bakker66d5d072014-06-17 16:39:18 +02001940 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001942
Ron Eldor36d90422017-01-09 15:09:16 +02001943cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001944 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001945#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001946 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001947 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001949 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001950 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951 DIR *dir = opendir( path );
1952
Paul Bakker66d5d072014-06-17 16:39:18 +02001953 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001955
Ron Eldor63140682017-01-09 19:27:59 +02001956#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001957 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001958 {
1959 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001960 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001961 }
Ron Eldor63140682017-01-09 19:27:59 +02001962#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001963
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001964 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001965 {
Andres AGf9113192016-09-02 14:06:04 +01001966 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1967 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001968
Andres AGf9113192016-09-02 14:06:04 +01001969 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001970 {
Andres AGf9113192016-09-02 14:06:04 +01001971 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1972 goto cleanup;
1973 }
1974 else if( stat( entry_name, &sb ) == -1 )
1975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001977 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001978 }
1979
1980 if( !S_ISREG( sb.st_mode ) )
1981 continue;
1982
1983 // Ignore parse errors
1984 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986 if( t_ret < 0 )
1987 ret++;
1988 else
1989 ret += t_ret;
1990 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001991
1992cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001993 closedir( dir );
1994
Ron Eldor63140682017-01-09 19:27:59 +02001995#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001996 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001998#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001999
Paul Bakkerbe089b02013-10-14 15:51:50 +02002000#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002001
2002 return( ret );
2003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002005
Hanno Becker08d34122019-06-25 09:42:57 +01002006typedef struct mbedtls_x509_crt_sig_info
2007{
2008 mbedtls_md_type_t sig_md;
2009 mbedtls_pk_type_t sig_pk;
2010 void *sig_opts;
2011 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
2012 size_t crt_hash_len;
2013 mbedtls_x509_buf_raw sig;
2014 mbedtls_x509_buf_raw issuer_raw;
2015} mbedtls_x509_crt_sig_info;
2016
2017static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2018{
2019#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2020 mbedtls_free( info->sig_opts );
2021#else
2022 ((void) info);
2023#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2024}
2025
2026static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2027 mbedtls_x509_crt_sig_info *info )
2028{
2029 const mbedtls_md_info_t *md_info;
2030
2031 md_info = mbedtls_md_info_from_type( frame->sig_md );
2032 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2033 info->crt_hash ) != 0 )
2034 {
2035 /* Note: this can't happen except after an internal error */
2036 return( -1 );
2037 }
2038
2039 info->crt_hash_len = mbedtls_md_get_size( md_info );
2040
2041 /* Make sure that this function leaves the target structure
2042 * ready to be freed, regardless of success of failure. */
2043 info->sig_opts = NULL;
2044
2045#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2046 {
2047 int ret;
2048 unsigned char *alg_start = frame->sig_alg.p;
2049 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2050
2051 /* Get signature options -- currently only
2052 * necessary for RSASSA-PSS. */
2053 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2054 &info->sig_pk, &info->sig_opts );
2055 if( ret != 0 )
2056 {
2057 /* Note: this can't happen except after an internal error */
2058 return( -1 );
2059 }
2060 }
2061#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2062 info->sig_md = frame->sig_md;
2063 info->sig_pk = frame->sig_pk;
2064#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2065
2066 info->issuer_raw = frame->issuer_raw;
2067 info->sig = frame->sig;
2068 return( 0 );
2069}
2070
Hanno Becker02a21932019-06-10 15:08:43 +01002071#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002072static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002074{
2075 size_t i;
2076 size_t n = *size;
2077 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002079 const char *sep = "";
2080 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002081
2082 while( cur != NULL )
2083 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002084 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002085 {
2086 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002087 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002088 }
2089
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002090 n -= cur->buf.len + sep_len;
2091 for( i = 0; i < sep_len; i++ )
2092 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002093 for( i = 0; i < cur->buf.len; i++ )
2094 *p++ = cur->buf.p[i];
2095
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002096 sep = ", ";
2097 sep_len = 2;
2098
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002099 cur = cur->next;
2100 }
2101
2102 *p = '\0';
2103
2104 *size = n;
2105 *buf = p;
2106
2107 return( 0 );
2108}
2109
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002110#define PRINT_ITEM(i) \
2111 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002113 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002114 sep = ", "; \
2115 }
2116
2117#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002118 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002119 PRINT_ITEM( name );
2120
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002121static int x509_info_cert_type( char **buf, size_t *size,
2122 unsigned char ns_cert_type )
2123{
2124 int ret;
2125 size_t n = *size;
2126 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002127 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002130 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2132 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2133 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002134 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2135 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2136 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002137
2138 *size = n;
2139 *buf = p;
2140
2141 return( 0 );
2142}
2143
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002144#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002145 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002146 PRINT_ITEM( name );
2147
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002148static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002149 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002150{
2151 int ret;
2152 size_t n = *size;
2153 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002154 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2157 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002158 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2159 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2160 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2162 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002163 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2164 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002165
2166 *size = n;
2167 *buf = p;
2168
2169 return( 0 );
2170}
2171
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002172static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002174{
2175 int ret;
2176 const char *desc;
2177 size_t n = *size;
2178 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002180 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002181
2182 while( cur != NULL )
2183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002185 desc = "???";
2186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002188 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002189
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002190 sep = ", ";
2191
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002192 cur = cur->next;
2193 }
2194
2195 *size = n;
2196 *buf = p;
2197
2198 return( 0 );
2199}
2200
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002201/*
2202 * Return an informational string about the certificate.
2203 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002204#define BEFORE_COLON 18
2205#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002207 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002208{
2209 int ret;
2210 size_t n;
2211 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002212 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002213 mbedtls_x509_crt_frame frame;
2214 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002215
Hanno Becker5226c532019-02-27 17:38:40 +00002216 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2217 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002218 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002219
2220 p = buf;
2221 n = size;
2222
Hanno Becker4f869ed2019-02-24 16:47:57 +00002223 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002224 mbedtls_pk_init( &pk );
2225
2226 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002227 {
2228 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002229 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Janos Follath98e28a72016-05-31 14:03:54 +01002230
2231 return( (int) ( size - n ) );
2232 }
2233
Hanno Becker5226c532019-02-27 17:38:40 +00002234 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002235 if( ret != 0 )
2236 {
2237 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2238 goto cleanup;
2239 }
2240
Hanno Becker5226c532019-02-27 17:38:40 +00002241 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002242 if( ret != 0 )
2243 {
2244 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2245 goto cleanup;
2246 }
2247
Hanno Becker5226c532019-02-27 17:38:40 +00002248 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002249 if( ret != 0 )
2250 {
2251 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2252 goto cleanup;
2253 }
2254
Hanno Becker5226c532019-02-27 17:38:40 +00002255 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002256 if( ret != 0 )
2257 {
2258 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2259 goto cleanup;
2260 }
2261
Hanno Becker5226c532019-02-27 17:38:40 +00002262 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002263 if( ret != 0 )
2264 {
2265 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2266 goto cleanup;
2267 }
2268
Hanno Becker5226c532019-02-27 17:38:40 +00002269 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2270 if( ret != 0 )
2271 {
2272 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2273 goto cleanup;
2274 }
2275
2276 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002277 if( ret != 0 )
2278 {
2279 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2280 goto cleanup;
2281 }
2282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002284 prefix, frame.version );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002285 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002286
Hanno Becker4f869ed2019-02-24 16:47:57 +00002287 {
2288 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002289 serial.p = frame.serial.p;
2290 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002291 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2292 prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002293 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002294 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002295 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002296 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002299 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002300 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002301 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002304 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002305 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002306 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002307
Hanno Becker843b71a2019-06-25 09:39:21 +01002308#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002310 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002311 frame.valid_from.year, frame.valid_from.mon,
2312 frame.valid_from.day, frame.valid_from.hour,
2313 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002314 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002318 frame.valid_to.year, frame.valid_to.mon,
2319 frame.valid_to.day, frame.valid_to.hour,
2320 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002321 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker843b71a2019-06-25 09:39:21 +01002322#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002325 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002326
Hanno Becker83cd8672019-02-21 17:13:46 +00002327 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2328 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002329 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002330
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002331 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002333 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334 {
2335 return( ret );
2336 }
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002339 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002340 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002342 /*
2343 * Optional extensions
2344 */
2345
Hanno Becker5226c532019-02-27 17:38:40 +00002346 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002349 frame.ca_istrue ? "true" : "false" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002350 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002351
Hanno Becker5226c532019-02-27 17:38:40 +00002352 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002353 {
Hanno Becker5226c532019-02-27 17:38:40 +00002354 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002355 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002356 }
2357 }
2358
Hanno Becker5226c532019-02-27 17:38:40 +00002359 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002362 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002363
2364 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002365 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002366 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002367 }
2368
Hanno Becker5226c532019-02-27 17:38:40 +00002369 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002372 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002373
Hanno Becker5226c532019-02-27 17:38:40 +00002374 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002375 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002376 }
2377
Hanno Becker5226c532019-02-27 17:38:40 +00002378 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002381 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002382
Hanno Becker5226c532019-02-27 17:38:40 +00002383 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002384 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002385 }
2386
Hanno Becker5226c532019-02-27 17:38:40 +00002387 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002390 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002391
2392 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002393 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002394 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002395 }
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002398 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002399
Hanno Becker4f869ed2019-02-24 16:47:57 +00002400 ret = (int) ( size - n );
2401
2402cleanup:
2403
Hanno Becker4f869ed2019-02-24 16:47:57 +00002404 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002405 mbedtls_pk_free( &pk );
2406 mbedtls_x509_name_free( issuer );
2407 mbedtls_x509_name_free( subject );
2408 mbedtls_x509_sequence_free( ext_key_usage );
2409 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002410
2411 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002412}
2413
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002414struct x509_crt_verify_string {
2415 int code;
2416 const char *string;
2417};
2418
2419static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002420 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002421 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2422 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2423 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2424 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2425 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002426 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2427 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2428 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002429 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002430 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2431 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2432 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2433 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002434 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2435 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2436 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2437 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2438 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2439 { 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 +01002440 { 0, NULL }
2441};
2442
2443int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002444 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002445{
2446 int ret;
2447 const struct x509_crt_verify_string *cur;
2448 char *p = buf;
2449 size_t n = size;
2450
2451 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2452 {
2453 if( ( flags & cur->code ) == 0 )
2454 continue;
2455
2456 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002457 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002458 flags ^= cur->code;
2459 }
2460
2461 if( flags != 0 )
2462 {
2463 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2464 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002465 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002466 }
2467
2468 return( (int) ( size - n ) );
2469}
Hanno Becker02a21932019-06-10 15:08:43 +01002470#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002473static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2474 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002475{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002476 unsigned int usage_must, usage_may;
2477 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2478 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2479
2480 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2481 return( 0 );
2482
2483 usage_must = usage & ~may_mask;
2484
2485 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2486 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2487
2488 usage_may = usage & may_mask;
2489
2490 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002492
2493 return( 0 );
2494}
Hanno Becker45eedf12019-02-25 13:55:33 +00002495
2496int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2497 unsigned int usage )
2498{
2499 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002500 mbedtls_x509_crt_frame const *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002501 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002502 if( ret != 0 )
2503 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2504
2505 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002506 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002507
2508 return( ret );
2509}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002510#endif
2511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002513typedef struct
2514{
2515 const char *oid;
2516 size_t oid_len;
2517} x509_crt_check_ext_key_usage_cb_ctx_t;
2518
2519static int x509_crt_check_ext_key_usage_cb( void *ctx,
2520 int tag,
2521 unsigned char *data,
2522 size_t data_len )
2523{
2524 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2525 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2526 ((void) tag);
2527
2528 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2529 data, data_len ) == 0 )
2530 {
2531 return( 1 );
2532 }
2533
2534 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2535 data_len ) == 0 )
2536 {
2537 return( 1 );
2538 }
2539
2540 return( 0 );
2541}
2542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002544 const char *usage_oid,
2545 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002546{
Hanno Beckere1956af2019-02-21 14:28:12 +00002547 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002548 mbedtls_x509_crt_frame const *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002549 unsigned ext_types;
2550 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002551 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002552
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002553 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002554 if( ret != 0 )
2555 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2556
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002557 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002558 ext_types = frame->ext_types;
2559 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2560 {
2561 p = frame->ext_key_usage_raw.p;
2562 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002563
Hanno Beckere9718b42019-02-25 18:11:42 +00002564 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2565 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2566 x509_crt_check_ext_key_usage_cb,
2567 &cb_ctx );
2568 if( ret == 1 )
2569 ret = 0;
2570 else
2571 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2572 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002573
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002574 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002575 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002580/*
2581 * Return 1 if the certificate is revoked, or 0 otherwise.
2582 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002583static int x509_serial_is_revoked( unsigned char const *serial,
2584 size_t serial_len,
2585 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002588
2589 while( cur != NULL && cur->serial.len != 0 )
2590 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002591 if( serial_len == cur->serial.len &&
2592 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002593 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002594 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002595 return( 1 );
2596 }
2597
2598 cur = cur->next;
2599 }
2600
2601 return( 0 );
2602}
2603
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002604int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2605 const mbedtls_x509_crl *crl )
2606{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002607 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002608 mbedtls_x509_crt_frame const *frame;
Hanno Becker79ae5b62019-02-25 18:12:00 +00002609
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002610 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002611 if( ret != 0 )
2612 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2613
2614 ret = x509_serial_is_revoked( frame->serial.p,
2615 frame->serial.len,
2616 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002617 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002618 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002619}
2620
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002621/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002622 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002623 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002624 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002625static int x509_crt_verifycrl( unsigned char *crt_serial,
2626 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002627 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002628 mbedtls_x509_crl *crl_list,
2629 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002630{
Hanno Beckerbb266132019-02-25 18:12:46 +00002631 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002632 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2634 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002635 mbedtls_x509_buf_raw ca_subject;
2636 mbedtls_pk_context *pk;
2637 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002638
Hanno Beckerbb266132019-02-25 18:12:46 +00002639 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002640 return( flags );
2641
Hanno Beckerbb266132019-02-25 18:12:46 +00002642 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002643 mbedtls_x509_crt_frame const *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002644 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002645 if( ret != 0 )
2646 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2647
2648 ca_subject = ca->subject_raw;
2649
2650 can_sign = 0;
2651 if( x509_crt_check_key_usage_frame( ca,
2652 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2653 {
2654 can_sign = 1;
2655 }
2656
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002657 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002658 }
2659
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002660 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002661 if( ret != 0 )
2662 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2663
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002664 while( crl_list != NULL )
2665 {
2666 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002667 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002668 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002669 {
2670 crl_list = crl_list->next;
2671 continue;
2672 }
2673
2674 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002675 * Check if the CA is configured to sign CRLs
2676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002678 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002681 break;
2682 }
2683#endif
2684
2685 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002686 * Check if CRL is correctly signed by the trusted CA
2687 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002688 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2689 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2690
2691 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2692 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002695 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002696 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002697 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002699 break;
2700 }
2701
Hanno Beckerbb266132019-02-25 18:12:46 +00002702 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002703 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002704
Hanno Beckerbb266132019-02-25 18:12:46 +00002705 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002707 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002710 break;
2711 }
2712
2713 /*
2714 * Check for validity of CRL (Do not drop out)
2715 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002716 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002718
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002719 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002720 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002721
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002722 /*
2723 * Check if certificate is revoked
2724 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002725 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2726 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002729 break;
2730 }
2731
2732 crl_list = crl_list->next;
2733 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002734
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002735 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002736 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002737}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002739
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002740/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002741 * Check the signature of a certificate by its parent
2742 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002743static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002744 mbedtls_x509_crt *parent,
2745 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002746{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002747 int ret;
2748 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002749
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002750 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002751 if( ret != 0 )
2752 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2753
2754 /* Skip expensive computation on obvious mismatch */
2755 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002756 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002757 ret = -1;
2758 goto exit;
2759 }
2760
2761#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2762 ((void) rs_ctx);
2763#else
2764 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2765 {
2766 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002767 sig_info->sig_md,
2768 sig_info->crt_hash, sig_info->crt_hash_len,
2769 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002770 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002771 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002772 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002773#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002774 {
2775 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2776 sig_info->sig_opts,
2777 pk,
2778 sig_info->sig_md,
2779 sig_info->crt_hash, sig_info->crt_hash_len,
2780 sig_info->sig.p, sig_info->sig.len );
2781 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002782
Hanno Beckere449e2d2019-02-25 14:45:31 +00002783exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002784 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002785 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002786}
2787
2788/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002789 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2790 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002791 *
2792 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002793 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002794static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2795 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002796 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002797{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002798 int need_ca_bit;
2799
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002800 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002801 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2802 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002803 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002804 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002805 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002806 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002807
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002808 /* Parent must have the basicConstraints CA bit set as a general rule */
2809 need_ca_bit = 1;
2810
2811 /* Exception: v1/v2 certificates that are locally trusted. */
2812 if( top && parent->version < 3 )
2813 need_ca_bit = 0;
2814
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002815 if( need_ca_bit && ! parent->ca_istrue )
2816 return( -1 );
2817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002819 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002820 x509_crt_check_key_usage_frame( parent,
2821 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002822 {
2823 return( -1 );
2824 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002825#endif
2826
2827 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002828}
2829
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002830/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002831 * Find a suitable parent for child in candidates, or return NULL.
2832 *
2833 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002834 * 1. subject name matches child's issuer
2835 * 2. if necessary, the CA bit is set and key usage allows signing certs
2836 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002837 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002838 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002839 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002840 * If there's a suitable candidate which is also time-valid, return the first
2841 * such. Otherwise, return the first suitable candidate (or NULL if there is
2842 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002843 *
2844 * The rationale for this rule is that someone could have a list of trusted
2845 * roots with two versions on the same root with different validity periods.
2846 * (At least one user reported having such a list and wanted it to just work.)
2847 * The reason we don't just require time-validity is that generally there is
2848 * only one version, and if it's expired we want the flags to state that
2849 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002850 *
2851 * The rationale for rule 3 (signature for trusted roots) is that users might
2852 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002853 * way we select the correct one is by checking the signature (as we don't
2854 * rely on key identifier extensions). (This is one way users might choose to
2855 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002856 *
2857 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002858 * - [in] child: certificate for which we're looking for a parent
2859 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002860 * - [out] r_parent: parent found (or NULL)
2861 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002862 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2863 * of the chain, 0 otherwise
2864 * - [in] path_cnt: number of intermediates seen so far
2865 * - [in] self_cnt: number of self-signed intermediates seen so far
2866 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002867 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002868 *
2869 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002870 * - 0 on success
2871 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002872 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002873static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002874 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002875 mbedtls_x509_crt *candidates,
2876 mbedtls_x509_crt **r_parent,
2877 int *r_signature_is_good,
2878 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002879 unsigned path_cnt,
2880 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002881 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002882{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002883 int ret;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002884 mbedtls_x509_crt *parent_crt;
2885 int signature_is_good;
2886
2887#if defined(MBEDTLS_HAVE_TIME_DATE)
2888 mbedtls_x509_crt *fallback_parent;
2889 int fallback_signature_is_good;
2890#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002891
2892#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002893 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002894 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2895 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002896 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002897 parent_crt = rs_ctx->parent;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002898#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002899 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002900 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002901#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002902
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002903 /* clear saved state */
2904 rs_ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002905#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002906 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002907 rs_ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002908#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002909
2910 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002911 goto check_signature;
2912 }
2913#endif
2914
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002915#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002916 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002917 fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002918#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002919
Hanno Becker43bf9002019-02-25 14:46:49 +00002920 for( parent_crt = candidates; parent_crt != NULL;
2921 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002922 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002923 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002924
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002925#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2926check_signature:
2927#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002928
2929 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002930 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002931 mbedtls_x509_crt_frame const *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002932
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002933 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002934 if( ret != 0 )
2935 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002936
Hanno Becker843b71a2019-06-25 09:39:21 +01002937#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker040c5642019-06-10 11:14:24 +01002938 if( !mbedtls_x509_time_is_past( &parent->valid_to ) &&
2939 !mbedtls_x509_time_is_future( &parent->valid_from ) )
Hanno Becker43bf9002019-02-25 14:46:49 +00002940 {
2941 parent_valid = 1;
2942 }
Hanno Becker843b71a2019-06-25 09:39:21 +01002943#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker43bf9002019-02-25 14:46:49 +00002944
2945 /* basic parenting skills (name, CA bit, key usage) */
2946 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2947 parent_match = 1;
2948
2949 /* +1 because the stored max_pathlen is 1 higher
2950 * than the actual value */
2951 if( !( parent->max_pathlen > 0 &&
2952 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2953 {
2954 path_len_ok = 1;
2955 }
2956
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002957 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002958 }
2959
2960 if( parent_match == 0 || path_len_ok == 0 )
2961 continue;
2962
2963 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002964 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002965
2966#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002967 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2968 {
2969 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002970 rs_ctx->parent = parent_crt;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002971#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002972 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002973 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002974#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002975
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002976 return( ret );
2977 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002978#else
2979 (void) ret;
2980#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002981
2982 signature_is_good = ret == 0;
2983 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002984 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002985
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002986 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002987 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002988 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002989#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002990 if( fallback_parent == NULL )
2991 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002992 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002993 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002994 }
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002995#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002996
2997 continue;
2998 }
2999
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003000 break;
3001 }
3002
Hanno Becker43bf9002019-02-25 14:46:49 +00003003 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003004 {
Hanno Becker43bf9002019-02-25 14:46:49 +00003005 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003006 *r_signature_is_good = signature_is_good;
3007 }
3008 else
3009 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003010#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003011 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003012 *r_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003013#else /* MBEDTLS_HAVE_TIME_DATE */
3014 *r_parent = NULL;
3015#endif /* !MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003016 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003017
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003018 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003019}
3020
3021/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003022 * Find a parent in trusted CAs or the provided chain, or return NULL.
3023 *
3024 * Searches in trusted CAs first, and return the first suitable parent found
3025 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003026 *
3027 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003028 * - [in] child: certificate for which we're looking for a parent, followed
3029 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003030 * - [in] trust_ca: list of locally trusted certificates
3031 * - [out] parent: parent found (or NULL)
3032 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
3033 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
3034 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
3035 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003036 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003037 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003038 *
3039 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003040 * - 0 on success
3041 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003042 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003043static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00003044 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00003045 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003046 mbedtls_x509_crt *trust_ca,
3047 mbedtls_x509_crt **parent,
3048 int *parent_is_trusted,
3049 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003050 unsigned path_cnt,
3051 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003052 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003053{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003054 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003055 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003056
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003057 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003058
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003059#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003060 /* restore then clear saved state if we have some stored */
3061 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
3062 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003063 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003064 rs_ctx->parent_is_trusted = -1;
3065 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003066#endif
3067
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003068 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00003069 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003070
Hanno Becker5299cf82019-02-25 13:50:41 +00003071 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003072 parent, signature_is_good,
3073 *parent_is_trusted,
3074 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003075
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003076#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003077 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3078 {
3079 /* save state */
3080 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003081 return( ret );
3082 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003083#else
3084 (void) ret;
3085#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003086
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003087 /* stop here if found or already in second iteration */
3088 if( *parent != NULL || *parent_is_trusted == 0 )
3089 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003090
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003091 /* prepare second iteration */
3092 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003093 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003094
3095 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01003096 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003097 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02003098 *parent_is_trusted = 0;
3099 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003100 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003101
3102 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003103}
3104
3105/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003106 * Check if an end-entity certificate is locally trusted
3107 *
3108 * Currently we require such certificates to be self-signed (actually only
3109 * check for self-issued as self-signatures are not checked)
3110 */
3111static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003112 mbedtls_x509_crt_frame const *crt,
3113 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003114{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003115 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003116
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003117 /* look for an exact match with trusted cert */
3118 for( cur = trust_ca; cur != NULL; cur = cur->next )
3119 {
3120 if( crt->raw.len == cur->raw.len &&
3121 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3122 {
3123 return( 0 );
3124 }
3125 }
3126
3127 /* too bad */
3128 return( -1 );
3129}
3130
3131/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003132 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003133 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003134 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3135 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003136 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003137 * such that every cert in the chain is a child of the next one,
3138 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003139 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003140 * Verify that chain and return it with flags for all issues found.
3141 *
3142 * Special cases:
3143 * - EE == Rj -> return a one-element list containing it
3144 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3145 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003146 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003147 * Tests for (aspects of) this function should include at least:
3148 * - trusted EE
3149 * - EE -> trusted root
Antonin Décimod5f47592019-01-23 15:24:37 +01003150 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003151 * - if relevant: EE untrusted
3152 * - if relevant: EE -> intermediate, untrusted
3153 * with the aspect under test checked at each relevant level (EE, int, root).
3154 * For some aspects longer chains are required, but usually length 2 is
3155 * enough (but length 1 is not in general).
3156 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003157 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003158 * - [in] crt: the cert list EE, C1, ..., Cn
3159 * - [in] trust_ca: the trusted list R1, ..., Rp
3160 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003161 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003162 * Only valid when return value is 0, may contain garbage otherwise!
3163 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003164 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003165 *
3166 * Return value:
3167 * - non-zero if the chain could not be fully built and examined
3168 * - 0 is the chain was successfully built and examined,
3169 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003170 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003171static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003172 mbedtls_x509_crt *crt,
3173 mbedtls_x509_crt *trust_ca,
3174 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003175 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003176 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003177 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003178{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003179 /* Don't initialize any of those variables here, so that the compiler can
3180 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003181 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003182 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003183 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003184 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003185 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003186 int parent_is_trusted;
3187 int child_is_trusted;
3188 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003189 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003190
3191#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3192 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003193 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003194 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003195 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003196 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003197 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003198
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003199 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003200 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003201 child_crt = cur->crt;
3202
3203 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003204 goto find_parent;
3205 }
3206#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003207
Hanno Becker1e0677a2019-02-25 14:58:22 +00003208 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003209 self_cnt = 0;
3210 parent_is_trusted = 0;
3211 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003212
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003213 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003214#if defined(MBEDTLS_X509_CRL_PARSE_C)
3215 mbedtls_x509_buf_raw child_serial;
3216#endif /* MBEDTLS_X509_CRL_PARSE_C */
3217 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003218
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003219 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003220 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003221 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003222 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003223 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003224
3225#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3226find_parent:
3227#endif
3228
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003229 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003230
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003231 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003232 mbedtls_x509_crt_sig_info child_sig;
3233 {
Hanno Becker5f268b32019-05-20 16:26:34 +01003234 mbedtls_x509_crt_frame const *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003235
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003236 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003237 if( ret != 0 )
3238 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3239
Hanno Becker843b71a2019-06-25 09:39:21 +01003240#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker5299cf82019-02-25 13:50:41 +00003241 /* Check time-validity (all certificates) */
3242 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3243 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3244 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3245 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Hanno Becker843b71a2019-06-25 09:39:21 +01003246#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker5299cf82019-02-25 13:50:41 +00003247
3248 /* Stop here for trusted roots (but not for trusted EE certs) */
3249 if( child_is_trusted )
3250 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003251 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003252 return( 0 );
3253 }
3254
3255 self_issued = 0;
3256 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3257 &child->subject_raw,
3258 NULL, NULL ) == 0 )
3259 {
3260 self_issued = 1;
3261 }
3262
3263 /* Check signature algorithm: MD & PK algs */
3264 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3265 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3266
3267 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3268 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3269
3270 /* Special case: EE certs that are locally trusted */
3271 if( ver_chain->len == 1 && self_issued &&
3272 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3273 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003274 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003275 return( 0 );
3276 }
3277
3278#if defined(MBEDTLS_X509_CRL_PARSE_C)
3279 child_serial = child->serial;
3280#endif /* MBEDTLS_X509_CRL_PARSE_C */
3281
3282 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003283 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003284
Hanno Becker5299cf82019-02-25 13:50:41 +00003285 if( ret != 0 )
3286 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3287 }
3288
3289 /* Look for a parent in trusted CAs or up the chain */
3290 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003291 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003292 &parent_is_trusted, &signature_is_good,
3293 ver_chain->len - 1, self_cnt, rs_ctx );
3294
3295 x509_crt_free_sig_info( &child_sig );
3296 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003297
3298#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003299 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3300 {
3301 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003302 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003303 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003304 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003305 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003306 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003307#else
3308 (void) ret;
3309#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003310
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003311 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003312 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003313 {
3314 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003315 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003316 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003317
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003318 /* Count intermediate self-issued (not necessarily self-signed) certs.
3319 * These can occur with some strategies for key rollover, see [SIRO],
3320 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003321 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003322 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003323
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003324 /* path_cnt is 0 for the first intermediate CA,
3325 * and if parent is trusted it's not an intermediate CA */
3326 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003327 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003328 {
3329 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003330 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003331 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003332
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003333 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003334 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003335 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3336
Hanno Becker58c35642019-02-25 18:13:46 +00003337 {
3338 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003339 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003340 if( ret != 0 )
3341 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3342
3343 /* check size of signing key */
3344 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3345 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3346
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003347 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003348 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003351 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003352 *flags |= x509_crt_verifycrl( child_serial.p,
3353 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003354 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003355#else
3356 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003357#endif
3358
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003359 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003360 child_crt = parent_crt;
3361 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003362 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003363 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003364 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003365}
3366
3367/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003368 * Check for CN match
3369 */
Hanno Becker24926222019-02-21 13:10:55 +00003370static int x509_crt_check_cn( unsigned char const *buf,
3371 size_t buflen,
3372 const char *cn,
3373 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003374{
Hanno Becker24926222019-02-21 13:10:55 +00003375 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003376 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003377 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003378
3379 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003380 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003381 {
3382 return( 0 );
3383 }
3384
3385 return( -1 );
3386}
3387
Hanno Becker8b543b32019-02-21 11:50:44 +00003388/* Returns 1 on a match and 0 on a mismatch.
3389 * This is because this function is used as a callback for
3390 * mbedtls_x509_name_cmp_raw(), which continues the name
3391 * traversal as long as the callback returns 0. */
3392static int x509_crt_check_name( void *ctx,
3393 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003394 mbedtls_x509_buf *val,
3395 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003396{
3397 char const *cn = (char const*) ctx;
3398 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003399 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003400
3401 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003402 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003403 {
3404 return( 1 );
3405 }
3406
3407 return( 0 );
3408}
3409
Hanno Beckerda410822019-02-21 13:36:59 +00003410/* Returns 1 on a match and 0 on a mismatch.
3411 * This is because this function is used as a callback for
3412 * mbedtls_asn1_traverse_sequence_of(), which continues the
3413 * traversal as long as the callback returns 0. */
3414static int x509_crt_subject_alt_check_name( void *ctx,
3415 int tag,
3416 unsigned char *data,
3417 size_t data_len )
3418{
3419 char const *cn = (char const*) ctx;
3420 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003421 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003422
3423 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3424 return( 1 );
3425
3426 return( 0 );
3427}
3428
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003429/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003430 * Verify the requested CN - only call this if cn is not NULL!
3431 */
Hanno Becker082435c2019-02-25 18:14:40 +00003432static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3433 const char *cn,
3434 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003435{
Hanno Beckerda410822019-02-21 13:36:59 +00003436 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01003437 mbedtls_x509_crt_frame const *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003438
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003439 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003440 if( ret != 0 )
3441 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3442
3443 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003444 {
Hanno Becker90b94082019-02-21 21:13:21 +00003445 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003446 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003447 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003448 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003449
Hanno Becker90b94082019-02-21 21:13:21 +00003450 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3451 MBEDTLS_ASN1_TAG_CLASS_MASK,
3452 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3453 MBEDTLS_ASN1_TAG_VALUE_MASK,
3454 2 /* SubjectAlt DNS */,
3455 x509_crt_subject_alt_check_name,
Hanno Becker484caf02019-05-29 14:41:44 +01003456 (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003457 }
3458 else
3459 {
Hanno Becker082435c2019-02-25 18:14:40 +00003460 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3461 &frame->subject_raw,
Hanno Becker484caf02019-05-29 14:41:44 +01003462 x509_crt_check_name, (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003463 }
Hanno Beckerda410822019-02-21 13:36:59 +00003464
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003465 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003466
3467 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3468 * return 1 when finding a name component matching `cn`. */
3469 if( ret == 1 )
3470 return( 0 );
3471
3472 if( ret != 0 )
3473 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3474
3475 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3476 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003477}
3478
3479/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003480 * Merge the flags for all certs in the chain, after calling callback
3481 */
3482static int x509_crt_merge_flags_with_cb(
3483 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003484 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003485 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3486 void *p_vrfy )
3487{
3488 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003489 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003490 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003491 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003492
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003493 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003494 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003495 cur = &ver_chain->items[i-1];
3496 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003497
3498 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003499 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003500 return( ret );
3501
3502 *flags |= cur_flags;
3503 }
3504
3505 return( 0 );
3506}
3507
3508/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003509 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3512 mbedtls_x509_crt *trust_ca,
3513 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003514 const char *cn, uint32_t *flags,
3515 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003516 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003517{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003518 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3519 &mbedtls_x509_crt_profile_default, cn, flags,
3520 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003521}
3522
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003523/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003524 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003525 */
3526int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3527 mbedtls_x509_crt *trust_ca,
3528 mbedtls_x509_crl *ca_crl,
3529 const mbedtls_x509_crt_profile *profile,
3530 const char *cn, uint32_t *flags,
3531 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3532 void *p_vrfy )
3533{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003534 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3535 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3536}
3537
3538/*
3539 * Verify the certificate validity, with profile, restartable version
3540 *
3541 * This function:
3542 * - checks the requested CN (if any)
3543 * - checks the type and size of the EE cert's key,
3544 * as that isn't done as part of chain building/verification currently
3545 * - builds and verifies the chain
3546 * - then calls the callback and merges the flags
3547 */
3548int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3549 mbedtls_x509_crt *trust_ca,
3550 mbedtls_x509_crl *ca_crl,
3551 const mbedtls_x509_crt_profile *profile,
3552 const char *cn, uint32_t *flags,
3553 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3554 void *p_vrfy,
3555 mbedtls_x509_crt_restart_ctx *rs_ctx )
3556{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003557 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003558 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003559 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003560
3561 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003562 ee_flags = 0;
3563 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003564
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003565 if( profile == NULL )
3566 {
3567 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3568 goto exit;
3569 }
3570
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003571 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003572 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003573 {
3574 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3575 if( ret != 0 )
3576 return( ret );
3577 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003578
Hanno Becker87233362019-02-25 18:15:33 +00003579 {
3580 mbedtls_pk_context *pk;
3581 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003582
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003583 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003584 if( ret != 0 )
3585 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003586
Hanno Becker87233362019-02-25 18:15:33 +00003587 /* Check the type and size of the key */
3588 pk_type = mbedtls_pk_get_type( pk );
3589
3590 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3591 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3592
3593 if( x509_profile_check_key( profile, pk ) != 0 )
3594 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3595
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003596 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003597 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003598
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003599 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003600 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003601 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003602
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003603 if( ret != 0 )
3604 goto exit;
3605
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003606 /* Merge end-entity flags */
3607 ver_chain.items[0].flags |= ee_flags;
3608
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003609 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003610 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003611
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003612exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003613#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3614 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3615 mbedtls_x509_crt_restart_free( rs_ctx );
3616#endif
3617
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003618 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3619 * the SSL module for authmode optional, but non-zero return from the
3620 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003621 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3622 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3623
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003624 if( ret != 0 )
3625 {
3626 *flags = (uint32_t) -1;
3627 return( ret );
3628 }
3629
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003630 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003632
3633 return( 0 );
3634}
3635
3636/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003637 * Initialize a certificate chain
3638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003642}
3643
3644/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003645 * Unallocate all certificate data
3646 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 mbedtls_x509_crt *cert_cur = crt;
3651 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003652
3653 if( crt == NULL )
3654 return;
3655
3656 do
3657 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003658 x509_crt_cache_free( cert_cur->cache );
3659 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003660
3661#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3665 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003666#endif
3667
Hanno Becker2bcc7642019-02-26 19:01:00 +00003668 mbedtls_x509_name_free( cert_cur->issuer.next );
3669 mbedtls_x509_name_free( cert_cur->subject.next );
3670 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3671 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003672#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003673
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003674 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003675 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003676 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003678 }
3679
3680 cert_cur = cert_cur->next;
3681 }
3682 while( cert_cur != NULL );
3683
3684 cert_cur = crt;
3685 do
3686 {
3687 cert_prv = cert_cur;
3688 cert_cur = cert_cur->next;
3689
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003690 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003691 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003693 }
3694 while( cert_cur != NULL );
3695}
3696
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003697#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3698/*
3699 * Initialize a restart context
3700 */
3701void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3702{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003703 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003704
3705 ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003706#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003707 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003708 ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003709#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003710
3711 ctx->parent_is_trusted = -1;
3712
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003713 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003714 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003715 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003716}
3717
3718/*
3719 * Free the components of a restart context
3720 */
3721void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3722{
3723 if( ctx == NULL )
3724 return;
3725
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003726 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003727 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003728}
3729#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#endif /* MBEDTLS_X509_CRT_PARSE_C */