blob: 0c158f83601bf9163e9d8d7a5990727c79d27409 [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 );
Hanno Beckerd07614c2019-06-25 10:19:58 +0100238#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Becker38f0cb42019-03-04 15:13:45 +0000239 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
240 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
Hanno Beckerd07614c2019-06-25 10:19:58 +0100241#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker38f0cb42019-03-04 15:13:45 +0000242 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
243 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000244
245 /* The legacy CRT structure doesn't explicitly contain
246 * the `AlgorithmIdentifier` bounds; however, those can
247 * be inferred from the surrounding (mandatory) `SerialNumber`
248 * and `Issuer` fields. */
249 frame->sig_alg.p = crt->serial.p + crt->serial.len;
250 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
251
252 return( x509_crt_frame_parse_ext( frame ) );
253#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000254}
Hanno Becker337088a2019-02-25 14:53:14 +0000255
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000256int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
257{
258 mbedtls_x509_crt_cache *cache = crt->cache;
259 mbedtls_pk_context *pk;
260
Hanno Becker76428352019-03-05 15:29:23 +0000261 if( cache->pk != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100262 {
Hanno Becker410322f2019-07-02 13:37:12 +0100263#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
264 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000265 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100266#else
267 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
268 * allow nested uses of acquire. */
269 return( MBEDTLS_ERR_X509_FATAL_ERROR );
270#endif
271 }
Hanno Becker76428352019-03-05 15:29:23 +0000272
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000273 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
274 if( pk == NULL )
275 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000276 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000277
278#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
279 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000280 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000281#else
282 {
283 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
284 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
285 pk_raw.p + pk_raw.len,
286 pk ) );
287 }
288#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000289}
290
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000291static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000292{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000293 memset( cache, 0, sizeof( *cache ) );
294#if defined(MBEDTLS_THREADING_C)
295 mbedtls_mutex_init( &cache->frame_mutex );
296 mbedtls_mutex_init( &cache->pk_mutex );
297#endif
298}
299
300static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
301{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000302#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000303 /* The cache holds a shallow copy of the PK context
304 * in the legacy struct, so don't free PK context. */
305 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000306#else
307 mbedtls_pk_free( cache->pk );
308 mbedtls_free( cache->pk );
309#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
310
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000311 cache->pk = NULL;
312}
313
314static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
315{
316 mbedtls_free( cache->frame );
317 cache->frame = NULL;
318}
319
320static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
321{
322 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000323 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000324
325#if defined(MBEDTLS_THREADING_C)
326 mbedtls_mutex_free( &cache->frame_mutex );
327 mbedtls_mutex_free( &cache->pk_mutex );
328#endif
329
330 x509_crt_cache_clear_frame( cache );
331 x509_crt_cache_clear_pk( cache );
332
333 memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000334}
335
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000336int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
337 mbedtls_x509_sequence **subj_alt )
338{
339 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100340 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000341 mbedtls_x509_sequence *seq;
342
343 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
344 if( ret != 0 )
345 return( ret );
346
347 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
348 if( seq == NULL )
349 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
350 else
351 ret = x509_crt_subject_alt_from_frame( frame, seq );
352
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000353 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000354
355 *subj_alt = seq;
356 return( ret );
357}
358
359int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
360 mbedtls_x509_sequence **ext_key_usage )
361{
362 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100363 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000364 mbedtls_x509_sequence *seq;
365
366 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
367 if( ret != 0 )
368 return( ret );
369
370 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
371 if( seq == NULL )
372 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
373 else
374 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
375
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000376 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000377
378 *ext_key_usage = seq;
379 return( ret );
380}
381
Hanno Becker63e69982019-02-26 18:50:49 +0000382int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
383 mbedtls_x509_name **subject )
384{
385 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100386 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000387 mbedtls_x509_name *name;
388
389 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
390 if( ret != 0 )
391 return( ret );
392
393 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
394 if( name == NULL )
395 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
396 else
397 ret = x509_crt_subject_from_frame( frame, name );
398
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000399 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000400
401 *subject = name;
402 return( ret );
403}
404
405int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
406 mbedtls_x509_name **issuer )
407{
408 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100409 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000410 mbedtls_x509_name *name;
411
412 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
413 if( ret != 0 )
414 return( ret );
415
416 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
417 if( name == NULL )
418 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
419 else
420 ret = x509_crt_issuer_from_frame( frame, name );
421
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000422 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000423
424 *issuer = name;
425 return( ret );
426}
427
Hanno Becker823efad2019-02-28 13:23:58 +0000428int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
429 mbedtls_x509_crt_frame *dst )
430{
431 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100432 mbedtls_x509_crt_frame const *frame;
Hanno Becker823efad2019-02-28 13:23:58 +0000433 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
434 if( ret != 0 )
435 return( ret );
436 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000437 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000438 return( 0 );
439}
440
441int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
442 mbedtls_pk_context *dst )
443{
444#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
445 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
446 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
447 pk_raw.p + pk_raw.len,
448 dst ) );
449#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
450 int ret;
451 mbedtls_pk_context *pk;
452 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
453 if( ret != 0 )
454 return( ret );
455
456 /* Move PK from CRT cache to destination pointer
457 * to avoid a copy. */
458 *dst = *pk;
459 mbedtls_free( crt->cache->pk );
460 crt->cache->pk = NULL;
461
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000462 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000463 return( 0 );
464#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
465}
466
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200467/*
468 * Item in a verification chain: cert and flags for it
469 */
470typedef struct {
471 mbedtls_x509_crt *crt;
472 uint32_t flags;
473} x509_crt_verify_chain_item;
474
475/*
476 * Max size of verification chain: end-entity + intermediates + trusted root
477 */
478#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200479
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200481 * Default profile
482 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200483const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
484{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200485#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200486 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200487 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200488#endif
489 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200490 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
491 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
492 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
493 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
494 0xFFFFFFF, /* Any PK alg */
495 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200496 2048,
497};
498
499/*
500 * Next-default profile
501 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200502const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
503{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200504 /* Hashes from SHA-256 and above */
505 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
506 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
507 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
508 0xFFFFFFF, /* Any PK alg */
509#if defined(MBEDTLS_ECP_C)
510 /* Curves at or above 128-bit security level */
511 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
512 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
513 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
514 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
515 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
516 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
517 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
518#else
519 0,
520#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200521 2048,
522};
523
524/*
525 * NSA Suite B Profile
526 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200527const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
528{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200529 /* Only SHA-256 and 384 */
530 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
531 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
532 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200533 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
534 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200535#if defined(MBEDTLS_ECP_C)
536 /* Only NIST P-256 and P-384 */
537 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
538 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
539#else
540 0,
541#endif
542 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200543};
544
545/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200546 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200547 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200548 */
549static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
550 mbedtls_md_type_t md_alg )
551{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200552 if( md_alg == MBEDTLS_MD_NONE )
553 return( -1 );
554
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200555 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
556 return( 0 );
557
558 return( -1 );
559}
560
561/*
562 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200563 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200564 */
565static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
566 mbedtls_pk_type_t pk_alg )
567{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200568 if( pk_alg == MBEDTLS_PK_NONE )
569 return( -1 );
570
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200571 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
572 return( 0 );
573
574 return( -1 );
575}
576
577/*
578 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200579 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200580 */
581static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200582 const mbedtls_pk_context *pk )
583{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200584 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200585
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200586#if defined(MBEDTLS_RSA_C)
587 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
588 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200589 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200590 return( 0 );
591
592 return( -1 );
593 }
594#endif
595
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200596#if defined(MBEDTLS_ECP_C)
597 if( pk_alg == MBEDTLS_PK_ECDSA ||
598 pk_alg == MBEDTLS_PK_ECKEY ||
599 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200600 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200601 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200602
Philippe Antoineb5b25432018-05-11 11:06:29 +0200603 if( gid == MBEDTLS_ECP_DP_NONE )
604 return( -1 );
605
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200606 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
607 return( 0 );
608
609 return( -1 );
610 }
611#endif
612
613 return( -1 );
614}
615
616/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000617 * Return 0 if name matches wildcard, -1 otherwise
618 */
Hanno Becker24926222019-02-21 13:10:55 +0000619static int x509_check_wildcard( char const *cn,
620 size_t cn_len,
621 unsigned char const *buf,
622 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000623{
624 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000625 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000626
627 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000628 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000629 return( -1 );
630
631 for( i = 0; i < cn_len; ++i )
632 {
633 if( cn[i] == '.' )
634 {
635 cn_idx = i;
636 break;
637 }
638 }
639
640 if( cn_idx == 0 )
641 return( -1 );
642
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000643 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
644 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000645 {
646 return( 0 );
647 }
648
649 return( -1 );
650}
651
652/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200653 * Reset (init or clear) a verify_chain
654 */
655static void x509_crt_verify_chain_reset(
656 mbedtls_x509_crt_verify_chain *ver_chain )
657{
658 size_t i;
659
660 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
661 {
662 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000663 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200664 }
665
666 ver_chain->len = 0;
667}
668
669/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
671 */
672static int x509_get_version( unsigned char **p,
673 const unsigned char *end,
674 int *ver )
675{
676 int ret;
677 size_t len;
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
680 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 {
684 *ver = 0;
685 return( 0 );
686 }
687
Hanno Becker2f472142019-02-12 11:52:10 +0000688 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 }
690
691 end = *p + len;
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
694 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695
696 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 return( MBEDTLS_ERR_X509_INVALID_VERSION +
698 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
700 return( 0 );
701}
702
Hanno Becker843b71a2019-06-25 09:39:21 +0100703#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704/*
705 * Validity ::= SEQUENCE {
706 * notBefore Time,
707 * notAfter Time }
708 */
709static int x509_get_dates( unsigned char **p,
710 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_x509_time *from,
712 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713{
714 int ret;
715 size_t len;
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
718 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
719 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720
721 end = *p + len;
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 return( ret );
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 return( ret );
728
729 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( MBEDTLS_ERR_X509_INVALID_DATE +
731 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732
733 return( 0 );
734}
Hanno Becker843b71a2019-06-25 09:39:21 +0100735#else /* !MBEDTLS_X509_CRT_REMOVE_TIME */
736static int x509_skip_dates( unsigned char **p,
737 const unsigned char *end )
738{
739 int ret;
740 size_t len;
741
742 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
743 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
744 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
745
Manuel Pégourié-Gonnard0d1db202019-07-30 14:11:25 +0200746 /* skip contents of the sequence */
747 *p += len;
Hanno Becker843b71a2019-06-25 09:39:21 +0100748
749 return( 0 );
750}
751#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752
Hanno Beckerd07614c2019-06-25 10:19:58 +0100753#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754/*
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}
Hanno Beckerd07614c2019-06-25 10:19:58 +0100780#else /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
781static int x509_skip_uid( unsigned char **p,
782 const unsigned char *end,
783 int n )
784{
785 int ret;
786 size_t len;
787
788 if( *p == end )
789 return( 0 );
790
791 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
792 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
793 {
794 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
795 return( 0 );
796
797 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
798 }
799
800 *p += len;
801 return( 0 );
802}
803#endif /* MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804
805static int x509_get_basic_constraints( unsigned char **p,
806 const unsigned char *end,
807 int *ca_istrue,
808 int *max_pathlen )
809{
810 int ret;
811 size_t len;
812
813 /*
814 * BasicConstraints ::= SEQUENCE {
815 * cA BOOLEAN DEFAULT FALSE,
816 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
817 */
818 *ca_istrue = 0; /* DEFAULT FALSE */
819 *max_pathlen = 0; /* endless */
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
822 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000823 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824
825 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200826 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
831 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832
833 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000834 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835
836 if( *ca_istrue != 0 )
837 *ca_istrue = 1;
838 }
839
840 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200841 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000844 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845
846 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000847 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848
849 (*max_pathlen)++;
850
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200851 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852}
853
854static int x509_get_ns_cert_type( unsigned char **p,
855 const unsigned char *end,
856 unsigned char *ns_cert_type)
857{
858 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000862 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863
864 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000865 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866
867 /* Get actual bitstring */
868 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200869 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870}
871
872static int x509_get_key_usage( unsigned char **p,
873 const unsigned char *end,
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100874 uint16_t *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875{
876 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200877 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000881 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882
883 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000884 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885
886 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200887 *key_usage = 0;
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100888 for( i = 0; i < bs.len && i < sizeof( *key_usage ); i++ )
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200889 {
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100890 *key_usage |= (uint16_t) bs.p[i] << ( 8*i );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200891 }
892
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200893 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894}
895
Hanno Becker529f25d2019-05-02 14:48:25 +0100896static int asn1_build_sequence_cb( void *ctx,
897 int tag,
898 unsigned char *data,
899 size_t data_len )
Hanno Becker15b73b42019-05-02 13:21:27 +0100900{
901 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
902 mbedtls_asn1_sequence *cur = *cur_ptr;
903
904 /* Allocate and assign next pointer */
905 if( cur->buf.p != NULL )
906 {
907 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
908 if( cur->next == NULL )
909 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
910 cur = cur->next;
911 }
912
913 cur->buf.tag = tag;
914 cur->buf.p = data;
915 cur->buf.len = data_len;
916
917 *cur_ptr = cur;
918 return( 0 );
919}
920
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921/*
Hanno Becker529f25d2019-05-02 14:48:25 +0100922 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
923 *
924 * KeyPurposeId ::= OBJECT IDENTIFIER
925 */
926static int x509_get_ext_key_usage( unsigned char **p,
927 const unsigned char *end,
928 mbedtls_x509_sequence *ext_key_usage)
929{
930 return( mbedtls_asn1_traverse_sequence_of( p, end,
931 0xFF, MBEDTLS_ASN1_OID,
932 0, 0,
933 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100934 (void *) &ext_key_usage ) );
Hanno Becker529f25d2019-05-02 14:48:25 +0100935}
936
937/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 * SubjectAltName ::= GeneralNames
939 *
940 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
941 *
942 * GeneralName ::= CHOICE {
943 * otherName [0] OtherName,
944 * rfc822Name [1] IA5String,
945 * dNSName [2] IA5String,
946 * x400Address [3] ORAddress,
947 * directoryName [4] Name,
948 * ediPartyName [5] EDIPartyName,
949 * uniformResourceIdentifier [6] IA5String,
950 * iPAddress [7] OCTET STRING,
951 * registeredID [8] OBJECT IDENTIFIER }
952 *
953 * OtherName ::= SEQUENCE {
954 * type-id OBJECT IDENTIFIER,
955 * value [0] EXPLICIT ANY DEFINED BY type-id }
956 *
957 * EDIPartyName ::= SEQUENCE {
958 * nameAssigner [0] DirectoryString OPTIONAL,
959 * partyName [1] DirectoryString }
960 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000961 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 */
Hanno Becker5984d302019-02-21 14:46:54 +0000963static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000967 return( mbedtls_asn1_traverse_sequence_of( &p, end,
968 MBEDTLS_ASN1_TAG_CLASS_MASK,
969 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
970 MBEDTLS_ASN1_TAG_VALUE_MASK,
971 2 /* SubjectAlt DNS */,
Hanno Becker529f25d2019-05-02 14:48:25 +0100972 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100973 (void *) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974}
975
976/*
977 * X.509 v3 extensions
978 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000980static int x509_crt_get_ext_cb( void *ctx,
981 int tag,
982 unsigned char *p,
983 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984{
985 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000986 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000988 unsigned char *end, *end_ext_octet;
989 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
990 int is_critical = 0; /* DEFAULT FALSE */
991 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000993 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000994
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000995 /*
996 * Extension ::= SEQUENCE {
997 * extnID OBJECT IDENTIFIER,
998 * critical BOOLEAN DEFAULT FALSE,
999 * extnValue OCTET STRING }
1000 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001002 end = p + ext_len;
1003
1004 /* Get extension ID */
1005 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
1006 MBEDTLS_ASN1_OID ) ) != 0 )
1007 goto err;
1008
1009 extn_oid.tag = MBEDTLS_ASN1_OID;
1010 extn_oid.p = p;
1011 p += extn_oid.len;
1012
1013 /* Get optional critical */
1014 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
1015 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
1016 goto err;
1017
1018 /* Data should be octet string type */
1019 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1020 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1021 goto err;
1022
1023 end_ext_octet = p + len;
1024 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001026 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
1027 goto err;
1028 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001030 /*
1031 * Detect supported extensions
1032 */
1033 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
1034 if( ret != 0 )
1035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001037 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001039 /* Data is marked as critical: fail */
1040 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
1041 goto err;
1042 }
Hanno Beckerb36a2452019-05-29 14:43:17 +01001043#endif /* MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001044 return( 0 );
1045 }
1046
1047 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +00001048 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001049 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
1050
Hanno Becker21f55672019-02-15 15:27:59 +00001051 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001052 switch( ext_type )
1053 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001054 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001055 {
1056 int ca_istrue;
1057 int max_pathlen;
1058
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001060 ret = x509_get_basic_constraints( &p, end_ext_octet,
1061 &ca_istrue,
1062 &max_pathlen );
1063 if( ret != 0 )
1064 goto err;
1065
Hanno Becker21f55672019-02-15 15:27:59 +00001066 frame->ca_istrue = ca_istrue;
1067 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001069 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001073 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001074 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001075 if( ret != 0 )
1076 goto err;
1077 break;
1078
1079 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1080 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +00001081 frame->subject_alt_raw.p = p;
1082 frame->subject_alt_raw.len = end_ext_octet - p;
1083
1084 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1085 MBEDTLS_ASN1_TAG_CLASS_MASK,
1086 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
1087 MBEDTLS_ASN1_TAG_VALUE_MASK,
1088 2 /* SubjectAlt DNS */,
1089 NULL, NULL );
1090 if( ret != 0 )
1091 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092 break;
1093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001096 frame->ext_key_usage_raw.p = p;
1097 frame->ext_key_usage_raw.len = end_ext_octet - p;
1098 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001099 {
Hanno Becker21f55672019-02-15 15:27:59 +00001100 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1101 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001102 }
Hanno Becker21f55672019-02-15 15:27:59 +00001103
1104 /* Check structural sanity of extension. */
1105 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1106 0xFF, MBEDTLS_ASN1_OID,
1107 0, 0, NULL, NULL );
1108 if( ret != 0 )
1109 goto err;
1110
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 break;
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001115 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001116 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001117 if( ret != 0 )
1118 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119 break;
1120
1121 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001122 /*
1123 * If this is a non-critical extension, which the oid layer
1124 * supports, but there isn't an X.509 parser for it,
1125 * skip the extension.
1126 */
1127#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1128 if( is_critical )
1129 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1130#endif
1131 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 }
1133
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001135
1136err:
1137 return( ret );
1138}
1139
Hanno Becker21f55672019-02-15 15:27:59 +00001140static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001141{
1142 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001143 unsigned char *p = frame->v3_ext.p;
1144 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001145
Hanno Becker21f55672019-02-15 15:27:59 +00001146 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001147 return( 0 );
1148
Hanno Becker21f55672019-02-15 15:27:59 +00001149 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001150 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001151 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001152
1153 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1154 return( ret );
1155 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1156 return( ret );
1157
1158 if( ret != 0 )
1159 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1160
1161 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162}
1163
Hanno Becker21f55672019-02-15 15:27:59 +00001164static int x509_crt_parse_frame( unsigned char *start,
1165 unsigned char *end,
1166 mbedtls_x509_crt_frame *frame )
1167{
1168 int ret;
1169 unsigned char *p;
1170 size_t len;
1171
1172 mbedtls_x509_buf tmp;
1173 unsigned char *tbs_start;
1174
1175 mbedtls_x509_buf outer_sig_alg;
1176 size_t inner_sig_alg_len;
1177 unsigned char *inner_sig_alg_start;
1178
1179 memset( frame, 0, sizeof( *frame ) );
1180
1181 /*
1182 * Certificate ::= SEQUENCE {
1183 * tbsCertificate TBSCertificate,
1184 * signatureAlgorithm AlgorithmIdentifier,
1185 * signatureValue BIT STRING
1186 * }
1187 *
1188 */
1189 p = start;
1190
1191 frame->raw.p = p;
1192 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1193 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1194 {
1195 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1196 }
1197
1198 /* NOTE: We are currently not checking that the `Certificate`
1199 * structure spans the entire buffer. */
1200 end = p + len;
1201 frame->raw.len = end - frame->raw.p;
1202
1203 /*
1204 * TBSCertificate ::= SEQUENCE { ...
1205 */
1206 frame->tbs.p = p;
1207 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1209 {
1210 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1211 }
1212 tbs_start = p;
1213
1214 /* Breadth-first parsing: Jump over TBS for now. */
1215 p += len;
1216 frame->tbs.len = p - frame->tbs.p;
1217
1218 /*
1219 * AlgorithmIdentifier ::= SEQUENCE { ...
1220 */
1221 outer_sig_alg.p = p;
1222 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1223 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1224 {
1225 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1226 }
1227 p += len;
1228 outer_sig_alg.len = p - outer_sig_alg.p;
1229
1230 /*
1231 * signatureValue BIT STRING
1232 */
1233 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1234 if( ret != 0 )
1235 return( ret );
1236 frame->sig.p = tmp.p;
1237 frame->sig.len = tmp.len;
1238
1239 /* Check that we consumed the entire `Certificate` structure. */
1240 if( p != end )
1241 {
1242 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1243 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1244 }
1245
1246 /* Parse TBSCertificate structure
1247 *
1248 * TBSCertificate ::= SEQUENCE {
1249 * version [0] EXPLICIT Version DEFAULT v1,
1250 * serialNumber CertificateSerialNumber,
1251 * signature AlgorithmIdentifier,
1252 * issuer Name,
1253 * validity Validity,
1254 * subject Name,
1255 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1256 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1257 * -- If present, version MUST be v2 or v3
1258 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1259 * -- If present, version MUST be v2 or v3
1260 * extensions [3] EXPLICIT Extensions OPTIONAL
1261 * -- If present, version MUST be v3
1262 * }
1263 */
1264 end = frame->tbs.p + frame->tbs.len;
1265 p = tbs_start;
1266
1267 /*
1268 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1269 */
1270 {
1271 int version;
1272 ret = x509_get_version( &p, end, &version );
1273 if( ret != 0 )
1274 return( ret );
1275
1276 if( version < 0 || version > 2 )
1277 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1278
1279 frame->version = version + 1;
1280 }
1281
1282 /*
1283 * CertificateSerialNumber ::= INTEGER
1284 */
1285 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1286 if( ret != 0 )
1287 return( ret );
1288
1289 frame->serial.p = tmp.p;
1290 frame->serial.len = tmp.len;
1291
1292 /*
1293 * signature AlgorithmIdentifier
1294 */
1295 inner_sig_alg_start = p;
1296 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1297 &frame->sig_pk, NULL );
1298 if( ret != 0 )
1299 return( ret );
1300 inner_sig_alg_len = p - inner_sig_alg_start;
1301
1302 frame->sig_alg.p = inner_sig_alg_start;
1303 frame->sig_alg.len = inner_sig_alg_len;
1304
1305 /* Consistency check:
1306 * Inner and outer AlgorithmIdentifier structures must coincide:
1307 *
1308 * Quoting RFC 5280, Section 4.1.1.2:
1309 * This field MUST contain the same algorithm identifier as the
1310 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1311 */
1312 if( outer_sig_alg.len != inner_sig_alg_len ||
1313 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1314 {
1315 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1316 }
1317
1318 /*
1319 * issuer Name
1320 *
1321 * Name ::= CHOICE { -- only one possibility for now --
1322 * rdnSequence RDNSequence }
1323 *
1324 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1325 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001326 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001327
1328 ret = mbedtls_asn1_get_tag( &p, end, &len,
1329 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1330 if( ret != 0 )
1331 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001332 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001333 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001334
Hanno Becker3aa12162019-07-02 16:47:40 +01001335 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001336 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1337 &frame->issuer_raw,
1338 NULL, NULL );
1339 if( ret != 0 )
1340 return( ret );
1341
Hanno Becker21f55672019-02-15 15:27:59 +00001342 /*
1343 * Validity ::= SEQUENCE { ...
1344 */
Hanno Becker843b71a2019-06-25 09:39:21 +01001345#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker21f55672019-02-15 15:27:59 +00001346 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1347 if( ret != 0 )
1348 return( ret );
Hanno Becker843b71a2019-06-25 09:39:21 +01001349#else /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1350 ret = x509_skip_dates( &p, end );
1351 if( ret != 0 )
1352 return( ret );
1353#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker21f55672019-02-15 15:27:59 +00001354
1355 /*
1356 * subject Name
1357 *
1358 * Name ::= CHOICE { -- only one possibility for now --
1359 * rdnSequence RDNSequence }
1360 *
1361 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1362 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001363 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001364
1365 ret = mbedtls_asn1_get_tag( &p, end, &len,
1366 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1367 if( ret != 0 )
1368 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001369 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001370 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001371
Hanno Becker3aa12162019-07-02 16:47:40 +01001372 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001373 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1374 &frame->subject_raw,
1375 NULL, NULL );
1376 if( ret != 0 )
1377 return( ret );
1378
Hanno Becker21f55672019-02-15 15:27:59 +00001379 /*
1380 * SubjectPublicKeyInfo
1381 */
1382 frame->pubkey_raw.p = p;
1383 ret = mbedtls_asn1_get_tag( &p, end, &len,
1384 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1385 if( ret != 0 )
1386 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1387 p += len;
1388 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1389
Hanno Becker97aa4362019-06-08 07:38:20 +01001390 if( frame->version != 1 )
Hanno Becker21f55672019-02-15 15:27:59 +00001391 {
Hanno Beckerd07614c2019-06-25 10:19:58 +01001392#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Beckerfd64f142019-06-07 11:47:12 +01001393 /*
1394 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1395 * -- If present, version shall be v2 or v3
1396 */
Hanno Beckere9084122019-06-07 12:04:39 +01001397 ret = x509_get_uid( &p, end, &frame->issuer_id, 1 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001398 if( ret != 0 )
1399 return( ret );
1400
Hanno Beckerfd64f142019-06-07 11:47:12 +01001401 /*
1402 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1403 * -- If present, version shall be v2 or v3
1404 */
Hanno Beckere9084122019-06-07 12:04:39 +01001405 ret = x509_get_uid( &p, end, &frame->subject_id, 2 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001406 if( ret != 0 )
1407 return( ret );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001408#else /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
1409 ret = x509_skip_uid( &p, end, 1 /* implicit tag */ );
1410 if( ret != 0 )
1411 return( ret );
1412 ret = x509_skip_uid( &p, end, 2 /* implicit tag */ );
1413 if( ret != 0 )
1414 return( ret );
1415#endif /* MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker21f55672019-02-15 15:27:59 +00001416 }
1417
1418 /*
1419 * extensions [3] EXPLICIT Extensions OPTIONAL
1420 * -- If present, version shall be v3
1421 */
1422#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1423 if( frame->version == 3 )
1424#endif
1425 {
1426 if( p != end )
1427 {
1428 ret = mbedtls_asn1_get_tag( &p, end, &len,
1429 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1430 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1431 if( len == 0 )
1432 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1433 if( ret != 0 )
1434 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1435
1436 frame->v3_ext.p = p;
1437 frame->v3_ext.len = len;
1438
1439 p += len;
1440 }
1441
1442 ret = x509_crt_frame_parse_ext( frame );
1443 if( ret != 0 )
1444 return( ret );
1445 }
1446
1447 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1448 if( p != end )
1449 {
1450 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1451 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1452 }
1453
1454 return( 0 );
1455}
1456
Hanno Becker12506232019-05-13 13:53:21 +01001457static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001458 mbedtls_x509_name *subject )
1459{
Hanno Becker1e11f212019-03-04 14:43:43 +00001460 return( mbedtls_x509_get_name( frame->subject_raw.p,
1461 frame->subject_raw.len,
1462 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001463}
1464
Hanno Becker12506232019-05-13 13:53:21 +01001465static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001466 mbedtls_x509_name *issuer )
1467{
Hanno Becker1e11f212019-03-04 14:43:43 +00001468 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1469 frame->issuer_raw.len,
1470 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001471}
1472
Hanno Becker12506232019-05-13 13:53:21 +01001473static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001474 mbedtls_x509_sequence *subject_alt )
1475{
1476 int ret;
1477 unsigned char *p = frame->subject_alt_raw.p;
1478 unsigned char *end = p + frame->subject_alt_raw.len;
1479
1480 memset( subject_alt, 0, sizeof( *subject_alt ) );
1481
1482 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1483 return( 0 );
1484
1485 ret = x509_get_subject_alt_name( p, end, subject_alt );
1486 if( ret != 0 )
1487 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1488 return( ret );
1489}
1490
Hanno Becker12506232019-05-13 13:53:21 +01001491static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001492 mbedtls_x509_sequence *ext_key_usage )
1493{
1494 int ret;
1495 unsigned char *p = frame->ext_key_usage_raw.p;
1496 unsigned char *end = p + frame->ext_key_usage_raw.len;
1497
1498 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1499
1500 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1501 return( 0 );
1502
1503 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1504 if( ret != 0 )
1505 {
1506 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1507 return( ret );
1508 }
1509
1510 return( 0 );
1511}
1512
Hanno Becker180f7bf2019-02-28 13:23:38 +00001513#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001514static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1515 mbedtls_pk_context *pk )
1516{
1517 unsigned char *p = frame->pubkey_raw.p;
1518 unsigned char *end = p + frame->pubkey_raw.len;
1519 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1520}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001521#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001522
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523/*
1524 * Parse and fill a single X.509 certificate in DER format
1525 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001526static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1527 const unsigned char *buf,
1528 size_t buflen,
1529 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530{
1531 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001532 mbedtls_x509_crt_frame *frame;
1533 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001534
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001537
Hanno Becker21f55672019-02-15 15:27:59 +00001538 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539 {
Hanno Becker21f55672019-02-15 15:27:59 +00001540 crt->raw.p = (unsigned char*) buf;
1541 crt->raw.len = buflen;
1542 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001543 }
1544 else
1545 {
Hanno Becker7b8e11e2019-05-03 12:37:12 +01001546 /* Call mbedtls_calloc with buflen + 1 in order to avoid potential
1547 * return of NULL in case of length 0 certificates, which we want
1548 * to cleanly fail with MBEDTLS_ERR_X509_INVALID_FORMAT in the
1549 * core parsing routine, but not here. */
1550 crt->raw.p = mbedtls_calloc( 1, buflen + 1 );
Hanno Becker21f55672019-02-15 15:27:59 +00001551 if( crt->raw.p == NULL )
1552 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1553 crt->raw.len = buflen;
1554 memcpy( crt->raw.p, buf, buflen );
1555
1556 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001557 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001558
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001559 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1560 if( cache == NULL )
1561 {
1562 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1563 goto exit;
1564 }
1565 crt->cache = cache;
1566 x509_crt_cache_init( cache );
1567
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001568#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1569
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001570 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001571 if( ret != 0 )
1572 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573
Hanno Becker76428352019-03-05 15:29:23 +00001574 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001575
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001576#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001578 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1579 if( frame == NULL )
1580 {
1581 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1582 goto exit;
1583 }
1584 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001585
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001586 ret = x509_crt_parse_frame( crt->raw.p,
1587 crt->raw.p + crt->raw.len,
1588 frame );
1589 if( ret != 0 )
1590 goto exit;
1591
Hanno Becker21f55672019-02-15 15:27:59 +00001592 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1593 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001594 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1595 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1596 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1597 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001598#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Becker38f0cb42019-03-04 15:13:45 +00001599 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1600 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001601#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001602 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1603 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1604 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Becker843b71a2019-06-25 09:39:21 +01001605
1606#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001607 crt->valid_from = frame->valid_from;
1608 crt->valid_to = frame->valid_to;
Hanno Becker843b71a2019-06-25 09:39:21 +01001609#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1610
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001611 crt->version = frame->version;
1612 crt->ca_istrue = frame->ca_istrue;
1613 crt->max_pathlen = frame->max_pathlen;
1614 crt->ext_types = frame->ext_types;
1615 crt->key_usage = frame->key_usage;
1616 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617
1618 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001619 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622 {
Hanno Becker21f55672019-02-15 15:27:59 +00001623 /* sig_oid: Previously, needed for convenience in
1624 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001625 unsigned char *tmp = frame->sig_alg.p;
1626 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001627 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628
Hanno Becker21f55672019-02-15 15:27:59 +00001629 ret = mbedtls_x509_get_alg( &tmp, end,
1630 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 if( ret != 0 )
1632 {
Hanno Becker21f55672019-02-15 15:27:59 +00001633 /* This should never happen, because we check
1634 * the sanity of the AlgorithmIdentifier structure
1635 * during frame parsing. */
1636 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1637 goto exit;
1638 }
1639 crt->sig_oid = sig_oid;
1640
1641 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001642 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001643 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1644 &crt->sig_md, &crt->sig_pk,
1645 &crt->sig_opts );
1646 if( ret != 0 )
1647 {
1648 /* Again, this should never happen. */
1649 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1650 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651 }
1652 }
1653
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001654 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001655 if( ret != 0 )
1656 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001658 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001659 if( ret != 0 )
1660 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001662 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001663 if( ret != 0 )
1664 goto exit;
1665
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001666 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001667 if( ret != 0 )
1668 goto exit;
1669
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001670 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1671 if( ret != 0 )
1672 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001673#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001674
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001675 /* Currently, we accept DER encoded CRTs with trailing garbage
1676 * and promise to not account for the garbage in the `raw` field.
1677 *
1678 * Note that this means that `crt->raw.len` is not necessarily the
1679 * full size of the heap buffer allocated at `crt->raw.p` in case
1680 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1681 * need the size, and the garbage data doesn't need zeroization. */
1682 crt->raw.len = frame->raw.len;
1683
1684 cache->pk_raw = frame->pubkey_raw;
1685
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001686 /* Free the frame before parsing the public key to
1687 * keep peak RAM usage low. This is slightly inefficient
1688 * because the frame will need to be parsed again on the
1689 * first usage of the CRT, but that seems acceptable.
1690 * As soon as the frame gets used multiple times, it
1691 * will be cached by default. */
1692 x509_crt_cache_clear_frame( crt->cache );
1693
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001694 /* The cache just references the PK structure from the legacy
1695 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001696 * the cache.
1697 *
1698 * We're not actually using the parsed PK context here;
1699 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001700 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001701 if( ret != 0 )
1702 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001703 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001704
1705exit:
1706 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001708
Hanno Becker21f55672019-02-15 15:27:59 +00001709 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001710}
1711
1712/*
1713 * Parse one X.509 certificate in DER format from a buffer and add them to a
1714 * chained list
1715 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001716static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1717 const unsigned char *buf,
1718 size_t buflen,
1719 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001720{
1721 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723
1724 /*
1725 * Check for valid input
1726 */
1727 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001729
Hanno Becker371e0e42019-02-25 18:08:59 +00001730 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001731 {
1732 prev = crt;
1733 crt = crt->next;
1734 }
1735
1736 /*
1737 * Add new certificate on the end of the chain if needed.
1738 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001739 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001740 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001741 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742
1743 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001744 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745
1746 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001748 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749 }
1750
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001751 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752 {
1753 if( prev )
1754 prev->next = NULL;
1755
1756 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758
1759 return( ret );
1760 }
1761
1762 return( 0 );
1763}
1764
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001765int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1766 const unsigned char *buf,
1767 size_t buflen )
1768{
1769 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1770}
1771
1772int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1773 const unsigned char *buf,
1774 size_t buflen )
1775{
1776 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1777}
1778
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001780 * Parse one or more PEM certificates from a buffer and add them to the chained
1781 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001783int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1784 const unsigned char *buf,
1785 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786{
Janos Follath98e28a72016-05-31 14:03:54 +01001787#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001788 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001790#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001791
1792 /*
1793 * Check for valid input
1794 */
1795 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797
1798 /*
1799 * Determine buffer content. Buffer contains either one DER certificate or
1800 * one or more PEM certificates.
1801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001803 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001804 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001807 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1810 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001811#else
1812 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1813#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_PEM_PARSE_C)
1816 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817 {
1818 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001821 /* 1 rather than 0 since the terminating NULL byte is counted in */
1822 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001823 {
1824 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001827 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829 "-----BEGIN CERTIFICATE-----",
1830 "-----END CERTIFICATE-----",
1831 buf, NULL, 0, &use_len );
1832
1833 if( ret == 0 )
1834 {
1835 /*
1836 * Was PEM encoded
1837 */
1838 buflen -= use_len;
1839 buf += use_len;
1840 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001842 {
1843 return( ret );
1844 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001848
1849 /*
1850 * PEM header and footer were found
1851 */
1852 buflen -= use_len;
1853 buf += use_len;
1854
1855 if( first_error == 0 )
1856 first_error = ret;
1857
Paul Bakker5a5fa922014-09-26 14:53:04 +02001858 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001859 continue;
1860 }
1861 else
1862 break;
1863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001867
1868 if( ret != 0 )
1869 {
1870 /*
1871 * Quit parsing on a memory error
1872 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001873 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001874 return( ret );
1875
1876 if( first_error == 0 )
1877 first_error = ret;
1878
1879 total_failed++;
1880 continue;
1881 }
1882
1883 success = 1;
1884 }
1885 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001886
1887 if( success )
1888 return( total_failed );
1889 else if( first_error )
1890 return( first_error );
1891 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001893#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001894}
1895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001897/*
1898 * Load one or more certificates and add them to the chained list
1899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001901{
1902 int ret;
1903 size_t n;
1904 unsigned char *buf;
1905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001907 return( ret );
1908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001910
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001911 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001913
1914 return( ret );
1915}
1916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918{
1919 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001920#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001921 int w_ret;
1922 WCHAR szDir[MAX_PATH];
1923 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001924 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001925 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001926
Paul Bakker9af723c2014-05-01 13:03:14 +02001927 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928 HANDLE hFind;
1929
1930 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001932
Paul Bakker9af723c2014-05-01 13:03:14 +02001933 memset( szDir, 0, sizeof(szDir) );
1934 memset( filename, 0, MAX_PATH );
1935 memcpy( filename, path, len );
1936 filename[len++] = '\\';
1937 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001938 filename[len++] = '*';
1939
Simon B3c6b18d2016-11-03 01:11:37 +00001940 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001941 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001942 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001944
1945 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001946 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948
1949 len = MAX_PATH - len;
1950 do
1951 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001952 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001953
1954 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1955 continue;
1956
Paul Bakker9af723c2014-05-01 13:03:14 +02001957 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001958 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001959 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001960 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001961 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001962 {
1963 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1964 goto cleanup;
1965 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001968 if( w_ret < 0 )
1969 ret++;
1970 else
1971 ret += w_ret;
1972 }
1973 while( FindNextFileW( hFind, &file_data ) != 0 );
1974
Paul Bakker66d5d072014-06-17 16:39:18 +02001975 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001977
Ron Eldor36d90422017-01-09 15:09:16 +02001978cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001979 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001980#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001981 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001982 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001983 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001984 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001985 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986 DIR *dir = opendir( path );
1987
Paul Bakker66d5d072014-06-17 16:39:18 +02001988 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990
Ron Eldor63140682017-01-09 19:27:59 +02001991#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001992 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001993 {
1994 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001995 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001996 }
Ron Eldor63140682017-01-09 19:27:59 +02001997#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001998
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001999 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002000 {
Andres AGf9113192016-09-02 14:06:04 +01002001 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
2002 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002003
Andres AGf9113192016-09-02 14:06:04 +01002004 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002005 {
Andres AGf9113192016-09-02 14:06:04 +01002006 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
2007 goto cleanup;
2008 }
2009 else if( stat( entry_name, &sb ) == -1 )
2010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002012 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002013 }
2014
2015 if( !S_ISREG( sb.st_mode ) )
2016 continue;
2017
2018 // Ignore parse errors
2019 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002021 if( t_ret < 0 )
2022 ret++;
2023 else
2024 ret += t_ret;
2025 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002026
2027cleanup:
Andres AGf9113192016-09-02 14:06:04 +01002028 closedir( dir );
2029
Ron Eldor63140682017-01-09 19:27:59 +02002030#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02002031 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02002033#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002034
Paul Bakkerbe089b02013-10-14 15:51:50 +02002035#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002036
2037 return( ret );
2038}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002040
Hanno Becker08d34122019-06-25 09:42:57 +01002041typedef struct mbedtls_x509_crt_sig_info
2042{
2043 mbedtls_md_type_t sig_md;
2044 mbedtls_pk_type_t sig_pk;
2045 void *sig_opts;
2046 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
2047 size_t crt_hash_len;
2048 mbedtls_x509_buf_raw sig;
2049 mbedtls_x509_buf_raw issuer_raw;
2050} mbedtls_x509_crt_sig_info;
2051
2052static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2053{
2054#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2055 mbedtls_free( info->sig_opts );
2056#else
2057 ((void) info);
2058#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2059}
2060
2061static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2062 mbedtls_x509_crt_sig_info *info )
2063{
2064 const mbedtls_md_info_t *md_info;
2065
2066 md_info = mbedtls_md_info_from_type( frame->sig_md );
2067 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2068 info->crt_hash ) != 0 )
2069 {
2070 /* Note: this can't happen except after an internal error */
2071 return( -1 );
2072 }
2073
2074 info->crt_hash_len = mbedtls_md_get_size( md_info );
2075
2076 /* Make sure that this function leaves the target structure
2077 * ready to be freed, regardless of success of failure. */
2078 info->sig_opts = NULL;
2079
2080#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2081 {
2082 int ret;
2083 unsigned char *alg_start = frame->sig_alg.p;
2084 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2085
2086 /* Get signature options -- currently only
2087 * necessary for RSASSA-PSS. */
2088 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2089 &info->sig_pk, &info->sig_opts );
2090 if( ret != 0 )
2091 {
2092 /* Note: this can't happen except after an internal error */
2093 return( -1 );
2094 }
2095 }
2096#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2097 info->sig_md = frame->sig_md;
2098 info->sig_pk = frame->sig_pk;
2099#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2100
2101 info->issuer_raw = frame->issuer_raw;
2102 info->sig = frame->sig;
2103 return( 0 );
2104}
2105
Hanno Becker02a21932019-06-10 15:08:43 +01002106#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002107static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002109{
2110 size_t i;
2111 size_t n = *size;
2112 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002114 const char *sep = "";
2115 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002116
2117 while( cur != NULL )
2118 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002119 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002120 {
2121 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002122 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002123 }
2124
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002125 n -= cur->buf.len + sep_len;
2126 for( i = 0; i < sep_len; i++ )
2127 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002128 for( i = 0; i < cur->buf.len; i++ )
2129 *p++ = cur->buf.p[i];
2130
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002131 sep = ", ";
2132 sep_len = 2;
2133
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002134 cur = cur->next;
2135 }
2136
2137 *p = '\0';
2138
2139 *size = n;
2140 *buf = p;
2141
2142 return( 0 );
2143}
2144
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002145#define PRINT_ITEM(i) \
2146 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002148 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002149 sep = ", "; \
2150 }
2151
2152#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002153 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002154 PRINT_ITEM( name );
2155
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002156static int x509_info_cert_type( char **buf, size_t *size,
2157 unsigned char ns_cert_type )
2158{
2159 int ret;
2160 size_t n = *size;
2161 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002162 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002165 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2167 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2168 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002169 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2170 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2171 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002172
2173 *size = n;
2174 *buf = p;
2175
2176 return( 0 );
2177}
2178
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002179#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002180 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002181 PRINT_ITEM( name );
2182
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002183static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002184 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002185{
2186 int ret;
2187 size_t n = *size;
2188 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002189 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2192 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002193 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2194 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2195 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2197 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002198 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2199 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002200
2201 *size = n;
2202 *buf = p;
2203
2204 return( 0 );
2205}
2206
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002207static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002209{
2210 int ret;
2211 const char *desc;
2212 size_t n = *size;
2213 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002215 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002216
2217 while( cur != NULL )
2218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002220 desc = "???";
2221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002223 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002224
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002225 sep = ", ";
2226
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002227 cur = cur->next;
2228 }
2229
2230 *size = n;
2231 *buf = p;
2232
2233 return( 0 );
2234}
2235
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002236/*
2237 * Return an informational string about the certificate.
2238 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002239#define BEFORE_COLON 18
2240#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002242 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002243{
2244 int ret;
2245 size_t n;
2246 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002247 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002248 mbedtls_x509_crt_frame frame;
2249 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002250
Hanno Becker5226c532019-02-27 17:38:40 +00002251 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2252 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002253 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002254
2255 p = buf;
2256 n = size;
2257
Hanno Becker4f869ed2019-02-24 16:47:57 +00002258 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002259 mbedtls_pk_init( &pk );
2260
2261 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002262 {
2263 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002264 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Janos Follath98e28a72016-05-31 14:03:54 +01002265
2266 return( (int) ( size - n ) );
2267 }
2268
Hanno Becker5226c532019-02-27 17:38:40 +00002269 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002270 if( ret != 0 )
2271 {
2272 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2273 goto cleanup;
2274 }
2275
Hanno Becker5226c532019-02-27 17:38:40 +00002276 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002277 if( ret != 0 )
2278 {
2279 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2280 goto cleanup;
2281 }
2282
Hanno Becker5226c532019-02-27 17:38:40 +00002283 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002284 if( ret != 0 )
2285 {
2286 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2287 goto cleanup;
2288 }
2289
Hanno Becker5226c532019-02-27 17:38:40 +00002290 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002291 if( ret != 0 )
2292 {
2293 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2294 goto cleanup;
2295 }
2296
Hanno Becker5226c532019-02-27 17:38:40 +00002297 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002298 if( ret != 0 )
2299 {
2300 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2301 goto cleanup;
2302 }
2303
Hanno Becker5226c532019-02-27 17:38:40 +00002304 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2305 if( ret != 0 )
2306 {
2307 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2308 goto cleanup;
2309 }
2310
2311 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002312 if( ret != 0 )
2313 {
2314 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2315 goto cleanup;
2316 }
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002319 prefix, frame.version );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002320 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321
Hanno Becker4f869ed2019-02-24 16:47:57 +00002322 {
2323 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002324 serial.p = frame.serial.p;
2325 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002326 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2327 prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002328 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002329 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002330 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002331 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002334 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002335 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002336 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002339 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002340 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002341 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002342
Hanno Becker843b71a2019-06-25 09:39:21 +01002343#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002346 frame.valid_from.year, frame.valid_from.mon,
2347 frame.valid_from.day, frame.valid_from.hour,
2348 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002349 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002352 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002353 frame.valid_to.year, frame.valid_to.mon,
2354 frame.valid_to.day, frame.valid_to.hour,
2355 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002356 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker843b71a2019-06-25 09:39:21 +01002357#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002360 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002361
Hanno Becker83cd8672019-02-21 17:13:46 +00002362 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2363 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002364 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002365
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002366 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002368 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369 {
2370 return( ret );
2371 }
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002374 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002375 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002376
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002377 /*
2378 * Optional extensions
2379 */
2380
Hanno Becker5226c532019-02-27 17:38:40 +00002381 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002384 frame.ca_istrue ? "true" : "false" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002385 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002386
Hanno Becker5226c532019-02-27 17:38:40 +00002387 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002388 {
Hanno Becker5226c532019-02-27 17:38:40 +00002389 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002390 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002391 }
2392 }
2393
Hanno Becker5226c532019-02-27 17:38:40 +00002394 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002397 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002398
2399 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002400 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002401 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002402 }
2403
Hanno Becker5226c532019-02-27 17:38:40 +00002404 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002407 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002408
Hanno Becker5226c532019-02-27 17:38:40 +00002409 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002410 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002411 }
2412
Hanno Becker5226c532019-02-27 17:38:40 +00002413 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002416 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002417
Hanno Becker5226c532019-02-27 17:38:40 +00002418 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002419 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002420 }
2421
Hanno Becker5226c532019-02-27 17:38:40 +00002422 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002425 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002426
2427 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002428 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002429 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002430 }
2431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002433 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002434
Hanno Becker4f869ed2019-02-24 16:47:57 +00002435 ret = (int) ( size - n );
2436
2437cleanup:
2438
Hanno Becker4f869ed2019-02-24 16:47:57 +00002439 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002440 mbedtls_pk_free( &pk );
2441 mbedtls_x509_name_free( issuer );
2442 mbedtls_x509_name_free( subject );
2443 mbedtls_x509_sequence_free( ext_key_usage );
2444 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002445
2446 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002447}
2448
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002449struct x509_crt_verify_string {
2450 int code;
2451 const char *string;
2452};
2453
2454static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002455 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002456 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2457 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2458 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2459 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2460 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002461 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2462 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2463 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002464 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002465 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2466 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2467 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2468 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002469 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2470 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2471 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2472 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2473 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2474 { 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 +01002475 { 0, NULL }
2476};
2477
2478int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002479 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002480{
2481 int ret;
2482 const struct x509_crt_verify_string *cur;
2483 char *p = buf;
2484 size_t n = size;
2485
2486 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2487 {
2488 if( ( flags & cur->code ) == 0 )
2489 continue;
2490
2491 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002492 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002493 flags ^= cur->code;
2494 }
2495
2496 if( flags != 0 )
2497 {
2498 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2499 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002500 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002501 }
2502
2503 return( (int) ( size - n ) );
2504}
Hanno Becker02a21932019-06-10 15:08:43 +01002505#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002508static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2509 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002510{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002511 unsigned int usage_must, usage_may;
2512 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2513 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2514
2515 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2516 return( 0 );
2517
2518 usage_must = usage & ~may_mask;
2519
2520 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2521 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2522
2523 usage_may = usage & may_mask;
2524
2525 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002527
2528 return( 0 );
2529}
Hanno Becker45eedf12019-02-25 13:55:33 +00002530
2531int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2532 unsigned int usage )
2533{
2534 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002535 mbedtls_x509_crt_frame const *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002536 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002537 if( ret != 0 )
2538 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2539
2540 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002541 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002542
2543 return( ret );
2544}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002545#endif
2546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002548typedef struct
2549{
2550 const char *oid;
2551 size_t oid_len;
2552} x509_crt_check_ext_key_usage_cb_ctx_t;
2553
2554static int x509_crt_check_ext_key_usage_cb( void *ctx,
2555 int tag,
2556 unsigned char *data,
2557 size_t data_len )
2558{
2559 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2560 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2561 ((void) tag);
2562
2563 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2564 data, data_len ) == 0 )
2565 {
2566 return( 1 );
2567 }
2568
2569 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2570 data_len ) == 0 )
2571 {
2572 return( 1 );
2573 }
2574
2575 return( 0 );
2576}
2577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002579 const char *usage_oid,
2580 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002581{
Hanno Beckere1956af2019-02-21 14:28:12 +00002582 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002583 mbedtls_x509_crt_frame const *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002584 unsigned ext_types;
2585 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002586 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002587
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002588 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002589 if( ret != 0 )
2590 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2591
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002592 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002593 ext_types = frame->ext_types;
2594 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2595 {
2596 p = frame->ext_key_usage_raw.p;
2597 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002598
Hanno Beckere9718b42019-02-25 18:11:42 +00002599 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2600 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2601 x509_crt_check_ext_key_usage_cb,
2602 &cb_ctx );
2603 if( ret == 1 )
2604 ret = 0;
2605 else
2606 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2607 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002608
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002609 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002610 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002615/*
2616 * Return 1 if the certificate is revoked, or 0 otherwise.
2617 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002618static int x509_serial_is_revoked( unsigned char const *serial,
2619 size_t serial_len,
2620 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002621{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002623
2624 while( cur != NULL && cur->serial.len != 0 )
2625 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002626 if( serial_len == cur->serial.len &&
2627 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002628 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002629 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002630 return( 1 );
2631 }
2632
2633 cur = cur->next;
2634 }
2635
2636 return( 0 );
2637}
2638
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002639int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2640 const mbedtls_x509_crl *crl )
2641{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002642 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002643 mbedtls_x509_crt_frame const *frame;
Hanno Becker79ae5b62019-02-25 18:12:00 +00002644
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002645 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002646 if( ret != 0 )
2647 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2648
2649 ret = x509_serial_is_revoked( frame->serial.p,
2650 frame->serial.len,
2651 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002652 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002653 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002654}
2655
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002656/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002657 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002658 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002659 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002660static int x509_crt_verifycrl( unsigned char *crt_serial,
2661 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002662 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002663 mbedtls_x509_crl *crl_list,
2664 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002665{
Hanno Beckerbb266132019-02-25 18:12:46 +00002666 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002667 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2669 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002670 mbedtls_x509_buf_raw ca_subject;
2671 mbedtls_pk_context *pk;
2672 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002673
Hanno Beckerbb266132019-02-25 18:12:46 +00002674 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002675 return( flags );
2676
Hanno Beckerbb266132019-02-25 18:12:46 +00002677 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002678 mbedtls_x509_crt_frame const *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002679 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002680 if( ret != 0 )
2681 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2682
2683 ca_subject = ca->subject_raw;
2684
2685 can_sign = 0;
2686 if( x509_crt_check_key_usage_frame( ca,
2687 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2688 {
2689 can_sign = 1;
2690 }
2691
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002692 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002693 }
2694
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002695 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002696 if( ret != 0 )
2697 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2698
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002699 while( crl_list != NULL )
2700 {
2701 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002702 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002703 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002704 {
2705 crl_list = crl_list->next;
2706 continue;
2707 }
2708
2709 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002710 * Check if the CA is configured to sign CRLs
2711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002713 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002716 break;
2717 }
2718#endif
2719
2720 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002721 * Check if CRL is correctly signed by the trusted CA
2722 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002723 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2724 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2725
2726 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2727 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002730 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002731 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002732 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002734 break;
2735 }
2736
Hanno Beckerbb266132019-02-25 18:12:46 +00002737 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002738 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002739
Hanno Beckerbb266132019-02-25 18:12:46 +00002740 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002742 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002745 break;
2746 }
2747
2748 /*
2749 * Check for validity of CRL (Do not drop out)
2750 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002751 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002753
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002754 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002755 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002756
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002757 /*
2758 * Check if certificate is revoked
2759 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002760 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2761 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002764 break;
2765 }
2766
2767 crl_list = crl_list->next;
2768 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002769
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002770 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002771 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002774
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002775/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002776 * Check the signature of a certificate by its parent
2777 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002778static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002779 mbedtls_x509_crt *parent,
2780 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002781{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002782 int ret;
2783 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002784
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002785 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002786 if( ret != 0 )
2787 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2788
2789 /* Skip expensive computation on obvious mismatch */
2790 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002791 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002792 ret = -1;
2793 goto exit;
2794 }
2795
2796#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2797 ((void) rs_ctx);
2798#else
2799 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2800 {
2801 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002802 sig_info->sig_md,
2803 sig_info->crt_hash, sig_info->crt_hash_len,
2804 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002805 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002806 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002807 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002808#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002809 {
2810 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2811 sig_info->sig_opts,
2812 pk,
2813 sig_info->sig_md,
2814 sig_info->crt_hash, sig_info->crt_hash_len,
2815 sig_info->sig.p, sig_info->sig.len );
2816 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002817
Hanno Beckere449e2d2019-02-25 14:45:31 +00002818exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002819 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002820 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002821}
2822
2823/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002824 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2825 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002826 *
2827 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002828 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002829static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2830 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002831 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002832{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002833 int need_ca_bit;
2834
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002835 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002836 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2837 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002838 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002839 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002840 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002841 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002842
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002843 /* Parent must have the basicConstraints CA bit set as a general rule */
2844 need_ca_bit = 1;
2845
2846 /* Exception: v1/v2 certificates that are locally trusted. */
2847 if( top && parent->version < 3 )
2848 need_ca_bit = 0;
2849
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002850 if( need_ca_bit && ! parent->ca_istrue )
2851 return( -1 );
2852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002854 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002855 x509_crt_check_key_usage_frame( parent,
2856 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002857 {
2858 return( -1 );
2859 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002860#endif
2861
2862 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002863}
2864
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002865/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002866 * Find a suitable parent for child in candidates, or return NULL.
2867 *
2868 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002869 * 1. subject name matches child's issuer
2870 * 2. if necessary, the CA bit is set and key usage allows signing certs
2871 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002872 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002873 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002874 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002875 * If there's a suitable candidate which is also time-valid, return the first
2876 * such. Otherwise, return the first suitable candidate (or NULL if there is
2877 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002878 *
2879 * The rationale for this rule is that someone could have a list of trusted
2880 * roots with two versions on the same root with different validity periods.
2881 * (At least one user reported having such a list and wanted it to just work.)
2882 * The reason we don't just require time-validity is that generally there is
2883 * only one version, and if it's expired we want the flags to state that
2884 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002885 *
2886 * The rationale for rule 3 (signature for trusted roots) is that users might
2887 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002888 * way we select the correct one is by checking the signature (as we don't
2889 * rely on key identifier extensions). (This is one way users might choose to
2890 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002891 *
2892 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002893 * - [in] child: certificate for which we're looking for a parent
2894 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002895 * - [out] r_parent: parent found (or NULL)
2896 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002897 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2898 * of the chain, 0 otherwise
2899 * - [in] path_cnt: number of intermediates seen so far
2900 * - [in] self_cnt: number of self-signed intermediates seen so far
2901 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002902 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002903 *
2904 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002905 * - 0 on success
2906 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002907 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002908static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002909 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002910 mbedtls_x509_crt *candidates,
2911 mbedtls_x509_crt **r_parent,
2912 int *r_signature_is_good,
2913 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002914 unsigned path_cnt,
2915 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002916 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002917{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002918 int ret;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002919 mbedtls_x509_crt *parent_crt;
2920 int signature_is_good;
2921
2922#if defined(MBEDTLS_HAVE_TIME_DATE)
2923 mbedtls_x509_crt *fallback_parent;
2924 int fallback_signature_is_good;
2925#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002926
2927#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002928 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002929 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2930 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002931 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002932 parent_crt = rs_ctx->parent;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002933#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002934 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002935 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002936#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002937
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002938 /* clear saved state */
2939 rs_ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002940#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002941 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002942 rs_ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002943#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002944
2945 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002946 goto check_signature;
2947 }
2948#endif
2949
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002950#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002951 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002952 fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002953#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002954
Hanno Becker43bf9002019-02-25 14:46:49 +00002955 for( parent_crt = candidates; parent_crt != NULL;
2956 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002957 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002958 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002959
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002960#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2961check_signature:
2962#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002963
2964 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002965 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002966 mbedtls_x509_crt_frame const *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002967
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002968 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002969 if( ret != 0 )
2970 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002971
Hanno Becker843b71a2019-06-25 09:39:21 +01002972#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker040c5642019-06-10 11:14:24 +01002973 if( !mbedtls_x509_time_is_past( &parent->valid_to ) &&
2974 !mbedtls_x509_time_is_future( &parent->valid_from ) )
Manuel Pégourié-Gonnardf1358ac2019-07-30 16:03:06 +02002975#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker43bf9002019-02-25 14:46:49 +00002976 {
2977 parent_valid = 1;
2978 }
2979
2980 /* basic parenting skills (name, CA bit, key usage) */
2981 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2982 parent_match = 1;
2983
2984 /* +1 because the stored max_pathlen is 1 higher
2985 * than the actual value */
2986 if( !( parent->max_pathlen > 0 &&
2987 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2988 {
2989 path_len_ok = 1;
2990 }
2991
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002992 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002993 }
2994
2995 if( parent_match == 0 || path_len_ok == 0 )
2996 continue;
2997
2998 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002999 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003000
3001#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003002 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3003 {
3004 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00003005 rs_ctx->parent = parent_crt;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003006#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003007 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003008 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003009#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003010
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003011 return( ret );
3012 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003013#else
3014 (void) ret;
3015#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003016
3017 signature_is_good = ret == 0;
3018 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02003019 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02003020
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003021 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00003022 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003023 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003024#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003025 if( fallback_parent == NULL )
3026 {
Hanno Becker43bf9002019-02-25 14:46:49 +00003027 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003028 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003029 }
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003030#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003031
3032 continue;
3033 }
3034
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003035 break;
3036 }
3037
Hanno Becker43bf9002019-02-25 14:46:49 +00003038 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003039 {
Hanno Becker43bf9002019-02-25 14:46:49 +00003040 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003041 *r_signature_is_good = signature_is_good;
3042 }
3043 else
3044 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003045#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003046 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003047 *r_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003048#else /* MBEDTLS_HAVE_TIME_DATE */
3049 *r_parent = NULL;
3050#endif /* !MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003051 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003052
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003053 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003054}
3055
3056/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003057 * Find a parent in trusted CAs or the provided chain, or return NULL.
3058 *
3059 * Searches in trusted CAs first, and return the first suitable parent found
3060 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003061 *
3062 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003063 * - [in] child: certificate for which we're looking for a parent, followed
3064 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003065 * - [in] trust_ca: list of locally trusted certificates
3066 * - [out] parent: parent found (or NULL)
3067 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
3068 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
3069 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
3070 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003071 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003072 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003073 *
3074 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003075 * - 0 on success
3076 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003077 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003078static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00003079 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00003080 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003081 mbedtls_x509_crt *trust_ca,
3082 mbedtls_x509_crt **parent,
3083 int *parent_is_trusted,
3084 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003085 unsigned path_cnt,
3086 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003087 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003088{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003089 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003090 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003091
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003092 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003093
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003094#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003095 /* restore then clear saved state if we have some stored */
3096 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
3097 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003098 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003099 rs_ctx->parent_is_trusted = -1;
3100 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003101#endif
3102
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003103 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00003104 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003105
Hanno Becker5299cf82019-02-25 13:50:41 +00003106 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003107 parent, signature_is_good,
3108 *parent_is_trusted,
3109 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003110
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003111#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003112 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3113 {
3114 /* save state */
3115 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003116 return( ret );
3117 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003118#else
3119 (void) ret;
3120#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003121
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003122 /* stop here if found or already in second iteration */
3123 if( *parent != NULL || *parent_is_trusted == 0 )
3124 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003125
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003126 /* prepare second iteration */
3127 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003128 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003129
3130 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01003131 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003132 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02003133 *parent_is_trusted = 0;
3134 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003135 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003136
3137 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003138}
3139
3140/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003141 * Check if an end-entity certificate is locally trusted
3142 *
3143 * Currently we require such certificates to be self-signed (actually only
3144 * check for self-issued as self-signatures are not checked)
3145 */
3146static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003147 mbedtls_x509_crt_frame const *crt,
3148 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003149{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003150 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003151
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003152 /* look for an exact match with trusted cert */
3153 for( cur = trust_ca; cur != NULL; cur = cur->next )
3154 {
3155 if( crt->raw.len == cur->raw.len &&
3156 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3157 {
3158 return( 0 );
3159 }
3160 }
3161
3162 /* too bad */
3163 return( -1 );
3164}
3165
3166/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003167 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003168 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003169 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3170 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003171 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003172 * such that every cert in the chain is a child of the next one,
3173 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003174 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003175 * Verify that chain and return it with flags for all issues found.
3176 *
3177 * Special cases:
3178 * - EE == Rj -> return a one-element list containing it
3179 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3180 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003181 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003182 * Tests for (aspects of) this function should include at least:
3183 * - trusted EE
3184 * - EE -> trusted root
Antonin Décimod5f47592019-01-23 15:24:37 +01003185 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003186 * - if relevant: EE untrusted
3187 * - if relevant: EE -> intermediate, untrusted
3188 * with the aspect under test checked at each relevant level (EE, int, root).
3189 * For some aspects longer chains are required, but usually length 2 is
3190 * enough (but length 1 is not in general).
3191 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003192 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003193 * - [in] crt: the cert list EE, C1, ..., Cn
3194 * - [in] trust_ca: the trusted list R1, ..., Rp
3195 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003196 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003197 * Only valid when return value is 0, may contain garbage otherwise!
3198 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003199 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003200 *
3201 * Return value:
3202 * - non-zero if the chain could not be fully built and examined
3203 * - 0 is the chain was successfully built and examined,
3204 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003205 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003206static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003207 mbedtls_x509_crt *crt,
3208 mbedtls_x509_crt *trust_ca,
3209 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003210 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003211 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003212 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003213{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003214 /* Don't initialize any of those variables here, so that the compiler can
3215 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003216 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003217 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003218 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003219 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003220 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003221 int parent_is_trusted;
3222 int child_is_trusted;
3223 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003224 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003225
3226#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3227 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003228 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003229 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003230 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003231 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003232 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003233
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003234 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003235 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003236 child_crt = cur->crt;
3237
3238 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003239 goto find_parent;
3240 }
3241#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003242
Hanno Becker1e0677a2019-02-25 14:58:22 +00003243 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003244 self_cnt = 0;
3245 parent_is_trusted = 0;
3246 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003247
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003248 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003249#if defined(MBEDTLS_X509_CRL_PARSE_C)
3250 mbedtls_x509_buf_raw child_serial;
3251#endif /* MBEDTLS_X509_CRL_PARSE_C */
3252 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003253
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003254 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003255 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003256 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003257 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003258 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003259
3260#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3261find_parent:
3262#endif
3263
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003264 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003265
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003266 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003267 mbedtls_x509_crt_sig_info child_sig;
3268 {
Hanno Becker5f268b32019-05-20 16:26:34 +01003269 mbedtls_x509_crt_frame const *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003270
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003271 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003272 if( ret != 0 )
3273 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3274
Hanno Becker843b71a2019-06-25 09:39:21 +01003275#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker5299cf82019-02-25 13:50:41 +00003276 /* Check time-validity (all certificates) */
3277 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3278 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3279 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3280 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Hanno Becker843b71a2019-06-25 09:39:21 +01003281#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker5299cf82019-02-25 13:50:41 +00003282
3283 /* Stop here for trusted roots (but not for trusted EE certs) */
3284 if( child_is_trusted )
3285 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003286 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003287 return( 0 );
3288 }
3289
3290 self_issued = 0;
3291 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3292 &child->subject_raw,
3293 NULL, NULL ) == 0 )
3294 {
3295 self_issued = 1;
3296 }
3297
3298 /* Check signature algorithm: MD & PK algs */
3299 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3300 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3301
3302 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3303 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3304
3305 /* Special case: EE certs that are locally trusted */
3306 if( ver_chain->len == 1 && self_issued &&
3307 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3308 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003309 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003310 return( 0 );
3311 }
3312
3313#if defined(MBEDTLS_X509_CRL_PARSE_C)
3314 child_serial = child->serial;
3315#endif /* MBEDTLS_X509_CRL_PARSE_C */
3316
3317 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003318 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003319
Hanno Becker5299cf82019-02-25 13:50:41 +00003320 if( ret != 0 )
3321 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3322 }
3323
3324 /* Look for a parent in trusted CAs or up the chain */
3325 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003326 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003327 &parent_is_trusted, &signature_is_good,
3328 ver_chain->len - 1, self_cnt, rs_ctx );
3329
3330 x509_crt_free_sig_info( &child_sig );
3331 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003332
3333#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003334 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3335 {
3336 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003337 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003338 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003339 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003340 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003341 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003342#else
3343 (void) ret;
3344#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003345
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003346 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003347 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003348 {
3349 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003350 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003351 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003352
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003353 /* Count intermediate self-issued (not necessarily self-signed) certs.
3354 * These can occur with some strategies for key rollover, see [SIRO],
3355 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003356 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003357 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003358
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003359 /* path_cnt is 0 for the first intermediate CA,
3360 * and if parent is trusted it's not an intermediate CA */
3361 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003362 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003363 {
3364 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003365 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003366 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003367
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003368 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003369 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003370 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3371
Hanno Becker58c35642019-02-25 18:13:46 +00003372 {
3373 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003374 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003375 if( ret != 0 )
3376 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3377
3378 /* check size of signing key */
3379 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3380 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3381
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003382 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003383 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003386 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003387 *flags |= x509_crt_verifycrl( child_serial.p,
3388 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003389 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003390#else
3391 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003392#endif
3393
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003394 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003395 child_crt = parent_crt;
3396 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003397 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003398 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003399 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003400}
3401
3402/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003403 * Check for CN match
3404 */
Hanno Becker24926222019-02-21 13:10:55 +00003405static int x509_crt_check_cn( unsigned char const *buf,
3406 size_t buflen,
3407 const char *cn,
3408 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003409{
Hanno Becker24926222019-02-21 13:10:55 +00003410 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003411 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003412 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003413
3414 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003415 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003416 {
3417 return( 0 );
3418 }
3419
3420 return( -1 );
3421}
3422
Hanno Becker8b543b32019-02-21 11:50:44 +00003423/* Returns 1 on a match and 0 on a mismatch.
3424 * This is because this function is used as a callback for
3425 * mbedtls_x509_name_cmp_raw(), which continues the name
3426 * traversal as long as the callback returns 0. */
3427static int x509_crt_check_name( void *ctx,
3428 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003429 mbedtls_x509_buf *val,
3430 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003431{
3432 char const *cn = (char const*) ctx;
3433 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003434 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003435
3436 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003437 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003438 {
3439 return( 1 );
3440 }
3441
3442 return( 0 );
3443}
3444
Hanno Beckerda410822019-02-21 13:36:59 +00003445/* Returns 1 on a match and 0 on a mismatch.
3446 * This is because this function is used as a callback for
3447 * mbedtls_asn1_traverse_sequence_of(), which continues the
3448 * traversal as long as the callback returns 0. */
3449static int x509_crt_subject_alt_check_name( void *ctx,
3450 int tag,
3451 unsigned char *data,
3452 size_t data_len )
3453{
3454 char const *cn = (char const*) ctx;
3455 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003456 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003457
3458 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3459 return( 1 );
3460
3461 return( 0 );
3462}
3463
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003464/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003465 * Verify the requested CN - only call this if cn is not NULL!
3466 */
Hanno Becker082435c2019-02-25 18:14:40 +00003467static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3468 const char *cn,
3469 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003470{
Hanno Beckerda410822019-02-21 13:36:59 +00003471 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01003472 mbedtls_x509_crt_frame const *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003473
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003474 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003475 if( ret != 0 )
3476 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3477
3478 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003479 {
Hanno Becker90b94082019-02-21 21:13:21 +00003480 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003481 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003482 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003483 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003484
Hanno Becker90b94082019-02-21 21:13:21 +00003485 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3486 MBEDTLS_ASN1_TAG_CLASS_MASK,
3487 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3488 MBEDTLS_ASN1_TAG_VALUE_MASK,
3489 2 /* SubjectAlt DNS */,
3490 x509_crt_subject_alt_check_name,
Hanno Becker484caf02019-05-29 14:41:44 +01003491 (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003492 }
3493 else
3494 {
Hanno Becker082435c2019-02-25 18:14:40 +00003495 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3496 &frame->subject_raw,
Hanno Becker484caf02019-05-29 14:41:44 +01003497 x509_crt_check_name, (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003498 }
Hanno Beckerda410822019-02-21 13:36:59 +00003499
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003500 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003501
3502 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3503 * return 1 when finding a name component matching `cn`. */
3504 if( ret == 1 )
3505 return( 0 );
3506
3507 if( ret != 0 )
3508 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3509
3510 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3511 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003512}
3513
3514/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003515 * Merge the flags for all certs in the chain, after calling callback
3516 */
3517static int x509_crt_merge_flags_with_cb(
3518 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003519 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003520 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3521 void *p_vrfy )
3522{
3523 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003524 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003525 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003526 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003527
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003528 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003529 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003530 cur = &ver_chain->items[i-1];
3531 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003532
3533 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003534 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003535 return( ret );
3536
3537 *flags |= cur_flags;
3538 }
3539
3540 return( 0 );
3541}
3542
3543/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003544 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3547 mbedtls_x509_crt *trust_ca,
3548 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003549 const char *cn, uint32_t *flags,
3550 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003551 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003552{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003553 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3554 &mbedtls_x509_crt_profile_default, cn, flags,
3555 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003556}
3557
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003558/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003559 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003560 */
3561int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3562 mbedtls_x509_crt *trust_ca,
3563 mbedtls_x509_crl *ca_crl,
3564 const mbedtls_x509_crt_profile *profile,
3565 const char *cn, uint32_t *flags,
3566 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3567 void *p_vrfy )
3568{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003569 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3570 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3571}
3572
3573/*
3574 * Verify the certificate validity, with profile, restartable version
3575 *
3576 * This function:
3577 * - checks the requested CN (if any)
3578 * - checks the type and size of the EE cert's key,
3579 * as that isn't done as part of chain building/verification currently
3580 * - builds and verifies the chain
3581 * - then calls the callback and merges the flags
3582 */
3583int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3584 mbedtls_x509_crt *trust_ca,
3585 mbedtls_x509_crl *ca_crl,
3586 const mbedtls_x509_crt_profile *profile,
3587 const char *cn, uint32_t *flags,
3588 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3589 void *p_vrfy,
3590 mbedtls_x509_crt_restart_ctx *rs_ctx )
3591{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003592 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003593 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003594 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003595
3596 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003597 ee_flags = 0;
3598 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003599
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003600 if( profile == NULL )
3601 {
3602 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3603 goto exit;
3604 }
3605
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003606 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003607 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003608 {
3609 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3610 if( ret != 0 )
3611 return( ret );
3612 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003613
Hanno Becker87233362019-02-25 18:15:33 +00003614 {
3615 mbedtls_pk_context *pk;
3616 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003617
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003618 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003619 if( ret != 0 )
3620 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003621
Hanno Becker87233362019-02-25 18:15:33 +00003622 /* Check the type and size of the key */
3623 pk_type = mbedtls_pk_get_type( pk );
3624
3625 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3626 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3627
3628 if( x509_profile_check_key( profile, pk ) != 0 )
3629 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3630
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003631 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003632 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003633
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003634 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003635 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003636 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003637
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003638 if( ret != 0 )
3639 goto exit;
3640
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003641 /* Merge end-entity flags */
3642 ver_chain.items[0].flags |= ee_flags;
3643
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003644 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003645 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003646
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003647exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003648#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3649 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3650 mbedtls_x509_crt_restart_free( rs_ctx );
3651#endif
3652
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003653 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3654 * the SSL module for authmode optional, but non-zero return from the
3655 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003656 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3657 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3658
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003659 if( ret != 0 )
3660 {
3661 *flags = (uint32_t) -1;
3662 return( ret );
3663 }
3664
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003665 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003667
3668 return( 0 );
3669}
3670
3671/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003672 * Initialize a certificate chain
3673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003677}
3678
3679/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003680 * Unallocate all certificate data
3681 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003684{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 mbedtls_x509_crt *cert_cur = crt;
3686 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003687
3688 if( crt == NULL )
3689 return;
3690
3691 do
3692 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003693 x509_crt_cache_free( cert_cur->cache );
3694 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003695
3696#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3700 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003701#endif
3702
Hanno Becker2bcc7642019-02-26 19:01:00 +00003703 mbedtls_x509_name_free( cert_cur->issuer.next );
3704 mbedtls_x509_name_free( cert_cur->subject.next );
3705 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3706 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003707#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003708
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003709 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003710 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003711 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003713 }
3714
3715 cert_cur = cert_cur->next;
3716 }
3717 while( cert_cur != NULL );
3718
3719 cert_cur = crt;
3720 do
3721 {
3722 cert_prv = cert_cur;
3723 cert_cur = cert_cur->next;
3724
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003725 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003726 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003728 }
3729 while( cert_cur != NULL );
3730}
3731
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003732#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3733/*
3734 * Initialize a restart context
3735 */
3736void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3737{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003738 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003739
3740 ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003741#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003742 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003743 ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003744#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003745
3746 ctx->parent_is_trusted = -1;
3747
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003748 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003749 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003750 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003751}
3752
3753/*
3754 * Free the components of a restart context
3755 */
3756void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3757{
3758 if( ctx == NULL )
3759 return;
3760
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003761 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003762 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003763}
3764#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766#endif /* MBEDTLS_X509_CRT_PARSE_C */