blob: 4e5ff43bdf9df2f0bdb38ca3a85709198eee5074 [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
746 end = *p + len;
747
748 if( *p != end )
749 return( MBEDTLS_ERR_X509_INVALID_DATE +
750 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
751
752 return( 0 );
753}
754#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755
Hanno Beckerd07614c2019-06-25 10:19:58 +0100756#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757/*
758 * X.509 v2/v3 unique identifier (not parsed)
759 */
760static int x509_get_uid( unsigned char **p,
761 const unsigned char *end,
Hanno Beckere9084122019-06-07 12:04:39 +0100762 mbedtls_x509_buf_raw *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763{
764 int ret;
765
766 if( *p == end )
767 return( 0 );
768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
770 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 return( 0 );
774
Hanno Becker2f472142019-02-12 11:52:10 +0000775 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776 }
777
778 uid->p = *p;
779 *p += uid->len;
780
781 return( 0 );
782}
Hanno Beckerd07614c2019-06-25 10:19:58 +0100783#else /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
784static int x509_skip_uid( unsigned char **p,
785 const unsigned char *end,
786 int n )
787{
788 int ret;
789 size_t len;
790
791 if( *p == end )
792 return( 0 );
793
794 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
795 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
796 {
797 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
798 return( 0 );
799
800 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
801 }
802
803 *p += len;
804 return( 0 );
805}
806#endif /* MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807
808static int x509_get_basic_constraints( unsigned char **p,
809 const unsigned char *end,
810 int *ca_istrue,
811 int *max_pathlen )
812{
813 int ret;
814 size_t len;
815
816 /*
817 * BasicConstraints ::= SEQUENCE {
818 * cA BOOLEAN DEFAULT FALSE,
819 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
820 */
821 *ca_istrue = 0; /* DEFAULT FALSE */
822 *max_pathlen = 0; /* endless */
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
825 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000826 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827
828 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200829 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
834 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835
836 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000837 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838
839 if( *ca_istrue != 0 )
840 *ca_istrue = 1;
841 }
842
843 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200844 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000847 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848
849 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000850 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851
852 (*max_pathlen)++;
853
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200854 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855}
856
857static int x509_get_ns_cert_type( unsigned char **p,
858 const unsigned char *end,
859 unsigned char *ns_cert_type)
860{
861 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000865 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866
867 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000868 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869
870 /* Get actual bitstring */
871 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200872 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873}
874
875static int x509_get_key_usage( unsigned char **p,
876 const unsigned char *end,
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100877 uint16_t *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878{
879 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200880 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000884 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885
886 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000887 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888
889 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200890 *key_usage = 0;
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100891 for( i = 0; i < bs.len && i < sizeof( *key_usage ); i++ )
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200892 {
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100893 *key_usage |= (uint16_t) bs.p[i] << ( 8*i );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200894 }
895
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200896 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897}
898
Hanno Becker529f25d2019-05-02 14:48:25 +0100899static int asn1_build_sequence_cb( void *ctx,
900 int tag,
901 unsigned char *data,
902 size_t data_len )
Hanno Becker15b73b42019-05-02 13:21:27 +0100903{
904 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
905 mbedtls_asn1_sequence *cur = *cur_ptr;
906
907 /* Allocate and assign next pointer */
908 if( cur->buf.p != NULL )
909 {
910 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
911 if( cur->next == NULL )
912 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
913 cur = cur->next;
914 }
915
916 cur->buf.tag = tag;
917 cur->buf.p = data;
918 cur->buf.len = data_len;
919
920 *cur_ptr = cur;
921 return( 0 );
922}
923
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924/*
Hanno Becker529f25d2019-05-02 14:48:25 +0100925 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
926 *
927 * KeyPurposeId ::= OBJECT IDENTIFIER
928 */
929static int x509_get_ext_key_usage( unsigned char **p,
930 const unsigned char *end,
931 mbedtls_x509_sequence *ext_key_usage)
932{
933 return( mbedtls_asn1_traverse_sequence_of( p, end,
934 0xFF, MBEDTLS_ASN1_OID,
935 0, 0,
936 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100937 (void *) &ext_key_usage ) );
Hanno Becker529f25d2019-05-02 14:48:25 +0100938}
939
940/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941 * SubjectAltName ::= GeneralNames
942 *
943 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
944 *
945 * GeneralName ::= CHOICE {
946 * otherName [0] OtherName,
947 * rfc822Name [1] IA5String,
948 * dNSName [2] IA5String,
949 * x400Address [3] ORAddress,
950 * directoryName [4] Name,
951 * ediPartyName [5] EDIPartyName,
952 * uniformResourceIdentifier [6] IA5String,
953 * iPAddress [7] OCTET STRING,
954 * registeredID [8] OBJECT IDENTIFIER }
955 *
956 * OtherName ::= SEQUENCE {
957 * type-id OBJECT IDENTIFIER,
958 * value [0] EXPLICIT ANY DEFINED BY type-id }
959 *
960 * EDIPartyName ::= SEQUENCE {
961 * nameAssigner [0] DirectoryString OPTIONAL,
962 * partyName [1] DirectoryString }
963 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000964 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 */
Hanno Becker5984d302019-02-21 14:46:54 +0000966static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000970 return( mbedtls_asn1_traverse_sequence_of( &p, end,
971 MBEDTLS_ASN1_TAG_CLASS_MASK,
972 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
973 MBEDTLS_ASN1_TAG_VALUE_MASK,
974 2 /* SubjectAlt DNS */,
Hanno Becker529f25d2019-05-02 14:48:25 +0100975 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100976 (void *) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977}
978
979/*
980 * X.509 v3 extensions
981 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000983static int x509_crt_get_ext_cb( void *ctx,
984 int tag,
985 unsigned char *p,
986 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987{
988 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000989 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000991 unsigned char *end, *end_ext_octet;
992 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
993 int is_critical = 0; /* DEFAULT FALSE */
994 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000996 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000997
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000998 /*
999 * Extension ::= SEQUENCE {
1000 * extnID OBJECT IDENTIFIER,
1001 * critical BOOLEAN DEFAULT FALSE,
1002 * extnValue OCTET STRING }
1003 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001005 end = p + ext_len;
1006
1007 /* Get extension ID */
1008 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
1009 MBEDTLS_ASN1_OID ) ) != 0 )
1010 goto err;
1011
1012 extn_oid.tag = MBEDTLS_ASN1_OID;
1013 extn_oid.p = p;
1014 p += extn_oid.len;
1015
1016 /* Get optional critical */
1017 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
1018 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
1019 goto err;
1020
1021 /* Data should be octet string type */
1022 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1023 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1024 goto err;
1025
1026 end_ext_octet = p + len;
1027 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001029 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
1030 goto err;
1031 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001033 /*
1034 * Detect supported extensions
1035 */
1036 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
1037 if( ret != 0 )
1038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001040 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001042 /* Data is marked as critical: fail */
1043 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
1044 goto err;
1045 }
Hanno Beckerb36a2452019-05-29 14:43:17 +01001046#endif /* MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001047 return( 0 );
1048 }
1049
1050 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +00001051 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001052 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
1053
Hanno Becker21f55672019-02-15 15:27:59 +00001054 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001055 switch( ext_type )
1056 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001057 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001058 {
1059 int ca_istrue;
1060 int max_pathlen;
1061
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001063 ret = x509_get_basic_constraints( &p, end_ext_octet,
1064 &ca_istrue,
1065 &max_pathlen );
1066 if( ret != 0 )
1067 goto err;
1068
Hanno Becker21f55672019-02-15 15:27:59 +00001069 frame->ca_istrue = ca_istrue;
1070 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001072 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001076 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001077 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001078 if( ret != 0 )
1079 goto err;
1080 break;
1081
1082 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1083 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +00001084 frame->subject_alt_raw.p = p;
1085 frame->subject_alt_raw.len = end_ext_octet - p;
1086
1087 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1088 MBEDTLS_ASN1_TAG_CLASS_MASK,
1089 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
1090 MBEDTLS_ASN1_TAG_VALUE_MASK,
1091 2 /* SubjectAlt DNS */,
1092 NULL, NULL );
1093 if( ret != 0 )
1094 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095 break;
1096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001099 frame->ext_key_usage_raw.p = p;
1100 frame->ext_key_usage_raw.len = end_ext_octet - p;
1101 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001102 {
Hanno Becker21f55672019-02-15 15:27:59 +00001103 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1104 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001105 }
Hanno Becker21f55672019-02-15 15:27:59 +00001106
1107 /* Check structural sanity of extension. */
1108 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1109 0xFF, MBEDTLS_ASN1_OID,
1110 0, 0, NULL, NULL );
1111 if( ret != 0 )
1112 goto err;
1113
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114 break;
1115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001118 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001119 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001120 if( ret != 0 )
1121 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 break;
1123
1124 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001125 /*
1126 * If this is a non-critical extension, which the oid layer
1127 * supports, but there isn't an X.509 parser for it,
1128 * skip the extension.
1129 */
1130#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1131 if( is_critical )
1132 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1133#endif
1134 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135 }
1136
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001138
1139err:
1140 return( ret );
1141}
1142
Hanno Becker21f55672019-02-15 15:27:59 +00001143static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001144{
1145 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001146 unsigned char *p = frame->v3_ext.p;
1147 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001148
Hanno Becker21f55672019-02-15 15:27:59 +00001149 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001150 return( 0 );
1151
Hanno Becker21f55672019-02-15 15:27:59 +00001152 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001153 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001154 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001155
1156 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1157 return( ret );
1158 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1159 return( ret );
1160
1161 if( ret != 0 )
1162 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1163
1164 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001165}
1166
Hanno Becker21f55672019-02-15 15:27:59 +00001167static int x509_crt_parse_frame( unsigned char *start,
1168 unsigned char *end,
1169 mbedtls_x509_crt_frame *frame )
1170{
1171 int ret;
1172 unsigned char *p;
1173 size_t len;
1174
1175 mbedtls_x509_buf tmp;
1176 unsigned char *tbs_start;
1177
1178 mbedtls_x509_buf outer_sig_alg;
1179 size_t inner_sig_alg_len;
1180 unsigned char *inner_sig_alg_start;
1181
1182 memset( frame, 0, sizeof( *frame ) );
1183
1184 /*
1185 * Certificate ::= SEQUENCE {
1186 * tbsCertificate TBSCertificate,
1187 * signatureAlgorithm AlgorithmIdentifier,
1188 * signatureValue BIT STRING
1189 * }
1190 *
1191 */
1192 p = start;
1193
1194 frame->raw.p = p;
1195 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1196 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1197 {
1198 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1199 }
1200
1201 /* NOTE: We are currently not checking that the `Certificate`
1202 * structure spans the entire buffer. */
1203 end = p + len;
1204 frame->raw.len = end - frame->raw.p;
1205
1206 /*
1207 * TBSCertificate ::= SEQUENCE { ...
1208 */
1209 frame->tbs.p = p;
1210 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1211 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1212 {
1213 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1214 }
1215 tbs_start = p;
1216
1217 /* Breadth-first parsing: Jump over TBS for now. */
1218 p += len;
1219 frame->tbs.len = p - frame->tbs.p;
1220
1221 /*
1222 * AlgorithmIdentifier ::= SEQUENCE { ...
1223 */
1224 outer_sig_alg.p = p;
1225 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1226 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1227 {
1228 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1229 }
1230 p += len;
1231 outer_sig_alg.len = p - outer_sig_alg.p;
1232
1233 /*
1234 * signatureValue BIT STRING
1235 */
1236 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1237 if( ret != 0 )
1238 return( ret );
1239 frame->sig.p = tmp.p;
1240 frame->sig.len = tmp.len;
1241
1242 /* Check that we consumed the entire `Certificate` structure. */
1243 if( p != end )
1244 {
1245 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1246 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1247 }
1248
1249 /* Parse TBSCertificate structure
1250 *
1251 * TBSCertificate ::= SEQUENCE {
1252 * version [0] EXPLICIT Version DEFAULT v1,
1253 * serialNumber CertificateSerialNumber,
1254 * signature AlgorithmIdentifier,
1255 * issuer Name,
1256 * validity Validity,
1257 * subject Name,
1258 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1259 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1260 * -- If present, version MUST be v2 or v3
1261 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1262 * -- If present, version MUST be v2 or v3
1263 * extensions [3] EXPLICIT Extensions OPTIONAL
1264 * -- If present, version MUST be v3
1265 * }
1266 */
1267 end = frame->tbs.p + frame->tbs.len;
1268 p = tbs_start;
1269
1270 /*
1271 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1272 */
1273 {
1274 int version;
1275 ret = x509_get_version( &p, end, &version );
1276 if( ret != 0 )
1277 return( ret );
1278
1279 if( version < 0 || version > 2 )
1280 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1281
1282 frame->version = version + 1;
1283 }
1284
1285 /*
1286 * CertificateSerialNumber ::= INTEGER
1287 */
1288 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1289 if( ret != 0 )
1290 return( ret );
1291
1292 frame->serial.p = tmp.p;
1293 frame->serial.len = tmp.len;
1294
1295 /*
1296 * signature AlgorithmIdentifier
1297 */
1298 inner_sig_alg_start = p;
1299 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1300 &frame->sig_pk, NULL );
1301 if( ret != 0 )
1302 return( ret );
1303 inner_sig_alg_len = p - inner_sig_alg_start;
1304
1305 frame->sig_alg.p = inner_sig_alg_start;
1306 frame->sig_alg.len = inner_sig_alg_len;
1307
1308 /* Consistency check:
1309 * Inner and outer AlgorithmIdentifier structures must coincide:
1310 *
1311 * Quoting RFC 5280, Section 4.1.1.2:
1312 * This field MUST contain the same algorithm identifier as the
1313 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1314 */
1315 if( outer_sig_alg.len != inner_sig_alg_len ||
1316 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1317 {
1318 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1319 }
1320
1321 /*
1322 * issuer Name
1323 *
1324 * Name ::= CHOICE { -- only one possibility for now --
1325 * rdnSequence RDNSequence }
1326 *
1327 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1328 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001329 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001330
1331 ret = mbedtls_asn1_get_tag( &p, end, &len,
1332 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1333 if( ret != 0 )
1334 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001335 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001336 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001337
Hanno Becker3aa12162019-07-02 16:47:40 +01001338 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001339 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1340 &frame->issuer_raw,
1341 NULL, NULL );
1342 if( ret != 0 )
1343 return( ret );
1344
Hanno Becker21f55672019-02-15 15:27:59 +00001345 /*
1346 * Validity ::= SEQUENCE { ...
1347 */
Hanno Becker843b71a2019-06-25 09:39:21 +01001348#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker21f55672019-02-15 15:27:59 +00001349 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1350 if( ret != 0 )
1351 return( ret );
Hanno Becker843b71a2019-06-25 09:39:21 +01001352#else /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1353 ret = x509_skip_dates( &p, end );
1354 if( ret != 0 )
1355 return( ret );
1356#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker21f55672019-02-15 15:27:59 +00001357
1358 /*
1359 * subject Name
1360 *
1361 * Name ::= CHOICE { -- only one possibility for now --
1362 * rdnSequence RDNSequence }
1363 *
1364 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1365 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001366 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001367
1368 ret = mbedtls_asn1_get_tag( &p, end, &len,
1369 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1370 if( ret != 0 )
1371 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001372 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001373 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001374
Hanno Becker3aa12162019-07-02 16:47:40 +01001375 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001376 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1377 &frame->subject_raw,
1378 NULL, NULL );
1379 if( ret != 0 )
1380 return( ret );
1381
Hanno Becker21f55672019-02-15 15:27:59 +00001382 /*
1383 * SubjectPublicKeyInfo
1384 */
1385 frame->pubkey_raw.p = p;
1386 ret = mbedtls_asn1_get_tag( &p, end, &len,
1387 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1388 if( ret != 0 )
1389 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1390 p += len;
1391 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1392
Hanno Becker97aa4362019-06-08 07:38:20 +01001393 if( frame->version != 1 )
Hanno Becker21f55672019-02-15 15:27:59 +00001394 {
Hanno Beckerd07614c2019-06-25 10:19:58 +01001395#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Beckerfd64f142019-06-07 11:47:12 +01001396 /*
1397 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1398 * -- If present, version shall be v2 or v3
1399 */
Hanno Beckere9084122019-06-07 12:04:39 +01001400 ret = x509_get_uid( &p, end, &frame->issuer_id, 1 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001401 if( ret != 0 )
1402 return( ret );
1403
Hanno Beckerfd64f142019-06-07 11:47:12 +01001404 /*
1405 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1406 * -- If present, version shall be v2 or v3
1407 */
Hanno Beckere9084122019-06-07 12:04:39 +01001408 ret = x509_get_uid( &p, end, &frame->subject_id, 2 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001409 if( ret != 0 )
1410 return( ret );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001411#else /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
1412 ret = x509_skip_uid( &p, end, 1 /* implicit tag */ );
1413 if( ret != 0 )
1414 return( ret );
1415 ret = x509_skip_uid( &p, end, 2 /* implicit tag */ );
1416 if( ret != 0 )
1417 return( ret );
1418#endif /* MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker21f55672019-02-15 15:27:59 +00001419 }
1420
1421 /*
1422 * extensions [3] EXPLICIT Extensions OPTIONAL
1423 * -- If present, version shall be v3
1424 */
1425#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1426 if( frame->version == 3 )
1427#endif
1428 {
1429 if( p != end )
1430 {
1431 ret = mbedtls_asn1_get_tag( &p, end, &len,
1432 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1433 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1434 if( len == 0 )
1435 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1436 if( ret != 0 )
1437 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1438
1439 frame->v3_ext.p = p;
1440 frame->v3_ext.len = len;
1441
1442 p += len;
1443 }
1444
1445 ret = x509_crt_frame_parse_ext( frame );
1446 if( ret != 0 )
1447 return( ret );
1448 }
1449
1450 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1451 if( p != end )
1452 {
1453 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1454 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1455 }
1456
1457 return( 0 );
1458}
1459
Hanno Becker12506232019-05-13 13:53:21 +01001460static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001461 mbedtls_x509_name *subject )
1462{
Hanno Becker1e11f212019-03-04 14:43:43 +00001463 return( mbedtls_x509_get_name( frame->subject_raw.p,
1464 frame->subject_raw.len,
1465 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001466}
1467
Hanno Becker12506232019-05-13 13:53:21 +01001468static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001469 mbedtls_x509_name *issuer )
1470{
Hanno Becker1e11f212019-03-04 14:43:43 +00001471 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1472 frame->issuer_raw.len,
1473 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001474}
1475
Hanno Becker12506232019-05-13 13:53:21 +01001476static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001477 mbedtls_x509_sequence *subject_alt )
1478{
1479 int ret;
1480 unsigned char *p = frame->subject_alt_raw.p;
1481 unsigned char *end = p + frame->subject_alt_raw.len;
1482
1483 memset( subject_alt, 0, sizeof( *subject_alt ) );
1484
1485 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1486 return( 0 );
1487
1488 ret = x509_get_subject_alt_name( p, end, subject_alt );
1489 if( ret != 0 )
1490 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1491 return( ret );
1492}
1493
Hanno Becker12506232019-05-13 13:53:21 +01001494static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001495 mbedtls_x509_sequence *ext_key_usage )
1496{
1497 int ret;
1498 unsigned char *p = frame->ext_key_usage_raw.p;
1499 unsigned char *end = p + frame->ext_key_usage_raw.len;
1500
1501 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1502
1503 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1504 return( 0 );
1505
1506 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1507 if( ret != 0 )
1508 {
1509 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1510 return( ret );
1511 }
1512
1513 return( 0 );
1514}
1515
Hanno Becker180f7bf2019-02-28 13:23:38 +00001516#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001517static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1518 mbedtls_pk_context *pk )
1519{
1520 unsigned char *p = frame->pubkey_raw.p;
1521 unsigned char *end = p + frame->pubkey_raw.len;
1522 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1523}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001524#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001525
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526/*
1527 * Parse and fill a single X.509 certificate in DER format
1528 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001529static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1530 const unsigned char *buf,
1531 size_t buflen,
1532 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533{
1534 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001535 mbedtls_x509_crt_frame *frame;
1536 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001537
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540
Hanno Becker21f55672019-02-15 15:27:59 +00001541 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542 {
Hanno Becker21f55672019-02-15 15:27:59 +00001543 crt->raw.p = (unsigned char*) buf;
1544 crt->raw.len = buflen;
1545 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001546 }
1547 else
1548 {
Hanno Becker7b8e11e2019-05-03 12:37:12 +01001549 /* Call mbedtls_calloc with buflen + 1 in order to avoid potential
1550 * return of NULL in case of length 0 certificates, which we want
1551 * to cleanly fail with MBEDTLS_ERR_X509_INVALID_FORMAT in the
1552 * core parsing routine, but not here. */
1553 crt->raw.p = mbedtls_calloc( 1, buflen + 1 );
Hanno Becker21f55672019-02-15 15:27:59 +00001554 if( crt->raw.p == NULL )
1555 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1556 crt->raw.len = buflen;
1557 memcpy( crt->raw.p, buf, buflen );
1558
1559 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001560 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001561
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001562 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1563 if( cache == NULL )
1564 {
1565 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1566 goto exit;
1567 }
1568 crt->cache = cache;
1569 x509_crt_cache_init( cache );
1570
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001571#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1572
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001573 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001574 if( ret != 0 )
1575 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
Hanno Becker76428352019-03-05 15:29:23 +00001577 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001578
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001579#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001581 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1582 if( frame == NULL )
1583 {
1584 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1585 goto exit;
1586 }
1587 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001588
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001589 ret = x509_crt_parse_frame( crt->raw.p,
1590 crt->raw.p + crt->raw.len,
1591 frame );
1592 if( ret != 0 )
1593 goto exit;
1594
Hanno Becker21f55672019-02-15 15:27:59 +00001595 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1596 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001597 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1598 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1599 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1600 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001601#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Becker38f0cb42019-03-04 15:13:45 +00001602 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1603 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001604#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001605 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1606 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1607 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Becker843b71a2019-06-25 09:39:21 +01001608
1609#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001610 crt->valid_from = frame->valid_from;
1611 crt->valid_to = frame->valid_to;
Hanno Becker843b71a2019-06-25 09:39:21 +01001612#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1613
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001614 crt->version = frame->version;
1615 crt->ca_istrue = frame->ca_istrue;
1616 crt->max_pathlen = frame->max_pathlen;
1617 crt->ext_types = frame->ext_types;
1618 crt->key_usage = frame->key_usage;
1619 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620
1621 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001622 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625 {
Hanno Becker21f55672019-02-15 15:27:59 +00001626 /* sig_oid: Previously, needed for convenience in
1627 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001628 unsigned char *tmp = frame->sig_alg.p;
1629 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001630 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631
Hanno Becker21f55672019-02-15 15:27:59 +00001632 ret = mbedtls_x509_get_alg( &tmp, end,
1633 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634 if( ret != 0 )
1635 {
Hanno Becker21f55672019-02-15 15:27:59 +00001636 /* This should never happen, because we check
1637 * the sanity of the AlgorithmIdentifier structure
1638 * during frame parsing. */
1639 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1640 goto exit;
1641 }
1642 crt->sig_oid = sig_oid;
1643
1644 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001645 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001646 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1647 &crt->sig_md, &crt->sig_pk,
1648 &crt->sig_opts );
1649 if( ret != 0 )
1650 {
1651 /* Again, this should never happen. */
1652 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1653 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654 }
1655 }
1656
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001657 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001658 if( ret != 0 )
1659 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001661 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001662 if( ret != 0 )
1663 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001665 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001666 if( ret != 0 )
1667 goto exit;
1668
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001669 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001670 if( ret != 0 )
1671 goto exit;
1672
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001673 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1674 if( ret != 0 )
1675 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001676#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001677
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001678 /* Currently, we accept DER encoded CRTs with trailing garbage
1679 * and promise to not account for the garbage in the `raw` field.
1680 *
1681 * Note that this means that `crt->raw.len` is not necessarily the
1682 * full size of the heap buffer allocated at `crt->raw.p` in case
1683 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1684 * need the size, and the garbage data doesn't need zeroization. */
1685 crt->raw.len = frame->raw.len;
1686
1687 cache->pk_raw = frame->pubkey_raw;
1688
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001689 /* Free the frame before parsing the public key to
1690 * keep peak RAM usage low. This is slightly inefficient
1691 * because the frame will need to be parsed again on the
1692 * first usage of the CRT, but that seems acceptable.
1693 * As soon as the frame gets used multiple times, it
1694 * will be cached by default. */
1695 x509_crt_cache_clear_frame( crt->cache );
1696
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001697 /* The cache just references the PK structure from the legacy
1698 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001699 * the cache.
1700 *
1701 * We're not actually using the parsed PK context here;
1702 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001703 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001704 if( ret != 0 )
1705 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001706 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001707
1708exit:
1709 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711
Hanno Becker21f55672019-02-15 15:27:59 +00001712 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713}
1714
1715/*
1716 * Parse one X.509 certificate in DER format from a buffer and add them to a
1717 * chained list
1718 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001719static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1720 const unsigned char *buf,
1721 size_t buflen,
1722 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723{
1724 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726
1727 /*
1728 * Check for valid input
1729 */
1730 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732
Hanno Becker371e0e42019-02-25 18:08:59 +00001733 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734 {
1735 prev = crt;
1736 crt = crt->next;
1737 }
1738
1739 /*
1740 * Add new certificate on the end of the chain if needed.
1741 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001742 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001744 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745
1746 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001747 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001748
1749 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752 }
1753
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001754 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755 {
1756 if( prev )
1757 prev->next = NULL;
1758
1759 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761
1762 return( ret );
1763 }
1764
1765 return( 0 );
1766}
1767
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001768int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1769 const unsigned char *buf,
1770 size_t buflen )
1771{
1772 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1773}
1774
1775int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1776 const unsigned char *buf,
1777 size_t buflen )
1778{
1779 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1780}
1781
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001783 * Parse one or more PEM certificates from a buffer and add them to the chained
1784 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001786int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1787 const unsigned char *buf,
1788 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789{
Janos Follath98e28a72016-05-31 14:03:54 +01001790#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001791 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001793#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794
1795 /*
1796 * Check for valid input
1797 */
1798 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001800
1801 /*
1802 * Determine buffer content. Buffer contains either one DER certificate or
1803 * one or more PEM certificates.
1804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001806 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001807 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001810 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1813 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001814#else
1815 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1816#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_PEM_PARSE_C)
1819 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820 {
1821 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001823
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001824 /* 1 rather than 0 since the terminating NULL byte is counted in */
1825 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826 {
1827 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001830 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001832 "-----BEGIN CERTIFICATE-----",
1833 "-----END CERTIFICATE-----",
1834 buf, NULL, 0, &use_len );
1835
1836 if( ret == 0 )
1837 {
1838 /*
1839 * Was PEM encoded
1840 */
1841 buflen -= use_len;
1842 buf += use_len;
1843 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001845 {
1846 return( ret );
1847 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001851
1852 /*
1853 * PEM header and footer were found
1854 */
1855 buflen -= use_len;
1856 buf += use_len;
1857
1858 if( first_error == 0 )
1859 first_error = ret;
1860
Paul Bakker5a5fa922014-09-26 14:53:04 +02001861 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001862 continue;
1863 }
1864 else
1865 break;
1866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001870
1871 if( ret != 0 )
1872 {
1873 /*
1874 * Quit parsing on a memory error
1875 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001876 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001877 return( ret );
1878
1879 if( first_error == 0 )
1880 first_error = ret;
1881
1882 total_failed++;
1883 continue;
1884 }
1885
1886 success = 1;
1887 }
1888 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001889
1890 if( success )
1891 return( total_failed );
1892 else if( first_error )
1893 return( first_error );
1894 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001896#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001897}
1898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001900/*
1901 * Load one or more certificates and add them to the chained list
1902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001904{
1905 int ret;
1906 size_t n;
1907 unsigned char *buf;
1908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001910 return( ret );
1911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001913
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001914 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001916
1917 return( ret );
1918}
1919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001921{
1922 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001923#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001924 int w_ret;
1925 WCHAR szDir[MAX_PATH];
1926 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001927 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001928 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929
Paul Bakker9af723c2014-05-01 13:03:14 +02001930 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931 HANDLE hFind;
1932
1933 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001935
Paul Bakker9af723c2014-05-01 13:03:14 +02001936 memset( szDir, 0, sizeof(szDir) );
1937 memset( filename, 0, MAX_PATH );
1938 memcpy( filename, path, len );
1939 filename[len++] = '\\';
1940 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001941 filename[len++] = '*';
1942
Simon B3c6b18d2016-11-03 01:11:37 +00001943 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001944 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001945 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001947
1948 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001949 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951
1952 len = MAX_PATH - len;
1953 do
1954 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001955 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001956
1957 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1958 continue;
1959
Paul Bakker9af723c2014-05-01 13:03:14 +02001960 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001961 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001962 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001963 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001964 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001965 {
1966 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1967 goto cleanup;
1968 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001971 if( w_ret < 0 )
1972 ret++;
1973 else
1974 ret += w_ret;
1975 }
1976 while( FindNextFileW( hFind, &file_data ) != 0 );
1977
Paul Bakker66d5d072014-06-17 16:39:18 +02001978 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001980
Ron Eldor36d90422017-01-09 15:09:16 +02001981cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001982 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001983#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001984 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001985 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001987 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001988 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001989 DIR *dir = opendir( path );
1990
Paul Bakker66d5d072014-06-17 16:39:18 +02001991 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001993
Ron Eldor63140682017-01-09 19:27:59 +02001994#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001995 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001996 {
1997 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001998 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001999 }
Ron Eldor63140682017-01-09 19:27:59 +02002000#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002001
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01002002 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002003 {
Andres AGf9113192016-09-02 14:06:04 +01002004 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
2005 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002006
Andres AGf9113192016-09-02 14:06:04 +01002007 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002008 {
Andres AGf9113192016-09-02 14:06:04 +01002009 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
2010 goto cleanup;
2011 }
2012 else if( stat( entry_name, &sb ) == -1 )
2013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002015 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016 }
2017
2018 if( !S_ISREG( sb.st_mode ) )
2019 continue;
2020
2021 // Ignore parse errors
2022 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024 if( t_ret < 0 )
2025 ret++;
2026 else
2027 ret += t_ret;
2028 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002029
2030cleanup:
Andres AGf9113192016-09-02 14:06:04 +01002031 closedir( dir );
2032
Ron Eldor63140682017-01-09 19:27:59 +02002033#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02002034 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02002036#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002037
Paul Bakkerbe089b02013-10-14 15:51:50 +02002038#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002039
2040 return( ret );
2041}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043
Hanno Becker08d34122019-06-25 09:42:57 +01002044typedef struct mbedtls_x509_crt_sig_info
2045{
2046 mbedtls_md_type_t sig_md;
2047 mbedtls_pk_type_t sig_pk;
2048 void *sig_opts;
2049 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
2050 size_t crt_hash_len;
2051 mbedtls_x509_buf_raw sig;
2052 mbedtls_x509_buf_raw issuer_raw;
2053} mbedtls_x509_crt_sig_info;
2054
2055static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2056{
2057#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2058 mbedtls_free( info->sig_opts );
2059#else
2060 ((void) info);
2061#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2062}
2063
2064static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2065 mbedtls_x509_crt_sig_info *info )
2066{
2067 const mbedtls_md_info_t *md_info;
2068
2069 md_info = mbedtls_md_info_from_type( frame->sig_md );
2070 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2071 info->crt_hash ) != 0 )
2072 {
2073 /* Note: this can't happen except after an internal error */
2074 return( -1 );
2075 }
2076
2077 info->crt_hash_len = mbedtls_md_get_size( md_info );
2078
2079 /* Make sure that this function leaves the target structure
2080 * ready to be freed, regardless of success of failure. */
2081 info->sig_opts = NULL;
2082
2083#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2084 {
2085 int ret;
2086 unsigned char *alg_start = frame->sig_alg.p;
2087 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2088
2089 /* Get signature options -- currently only
2090 * necessary for RSASSA-PSS. */
2091 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2092 &info->sig_pk, &info->sig_opts );
2093 if( ret != 0 )
2094 {
2095 /* Note: this can't happen except after an internal error */
2096 return( -1 );
2097 }
2098 }
2099#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2100 info->sig_md = frame->sig_md;
2101 info->sig_pk = frame->sig_pk;
2102#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2103
2104 info->issuer_raw = frame->issuer_raw;
2105 info->sig = frame->sig;
2106 return( 0 );
2107}
2108
Hanno Becker02a21932019-06-10 15:08:43 +01002109#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002110static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002112{
2113 size_t i;
2114 size_t n = *size;
2115 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002117 const char *sep = "";
2118 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002119
2120 while( cur != NULL )
2121 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002122 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002123 {
2124 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002125 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002126 }
2127
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002128 n -= cur->buf.len + sep_len;
2129 for( i = 0; i < sep_len; i++ )
2130 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002131 for( i = 0; i < cur->buf.len; i++ )
2132 *p++ = cur->buf.p[i];
2133
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002134 sep = ", ";
2135 sep_len = 2;
2136
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002137 cur = cur->next;
2138 }
2139
2140 *p = '\0';
2141
2142 *size = n;
2143 *buf = p;
2144
2145 return( 0 );
2146}
2147
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002148#define PRINT_ITEM(i) \
2149 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002151 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002152 sep = ", "; \
2153 }
2154
2155#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002156 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002157 PRINT_ITEM( name );
2158
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002159static int x509_info_cert_type( char **buf, size_t *size,
2160 unsigned char ns_cert_type )
2161{
2162 int ret;
2163 size_t n = *size;
2164 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002165 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002168 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2170 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2171 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002172 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2173 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2174 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002175
2176 *size = n;
2177 *buf = p;
2178
2179 return( 0 );
2180}
2181
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002182#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002183 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002184 PRINT_ITEM( name );
2185
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002186static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002187 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002188{
2189 int ret;
2190 size_t n = *size;
2191 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002192 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2195 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002196 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2197 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2198 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2200 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002201 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2202 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002203
2204 *size = n;
2205 *buf = p;
2206
2207 return( 0 );
2208}
2209
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002210static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002212{
2213 int ret;
2214 const char *desc;
2215 size_t n = *size;
2216 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002218 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002219
2220 while( cur != NULL )
2221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002223 desc = "???";
2224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002226 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002227
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002228 sep = ", ";
2229
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002230 cur = cur->next;
2231 }
2232
2233 *size = n;
2234 *buf = p;
2235
2236 return( 0 );
2237}
2238
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002239/*
2240 * Return an informational string about the certificate.
2241 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002242#define BEFORE_COLON 18
2243#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002245 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002246{
2247 int ret;
2248 size_t n;
2249 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002250 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002251 mbedtls_x509_crt_frame frame;
2252 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002253
Hanno Becker5226c532019-02-27 17:38:40 +00002254 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2255 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002256 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002257
2258 p = buf;
2259 n = size;
2260
Hanno Becker4f869ed2019-02-24 16:47:57 +00002261 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002262 mbedtls_pk_init( &pk );
2263
2264 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002265 {
2266 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002267 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Janos Follath98e28a72016-05-31 14:03:54 +01002268
2269 return( (int) ( size - n ) );
2270 }
2271
Hanno Becker5226c532019-02-27 17:38:40 +00002272 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002273 if( ret != 0 )
2274 {
2275 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2276 goto cleanup;
2277 }
2278
Hanno Becker5226c532019-02-27 17:38:40 +00002279 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002280 if( ret != 0 )
2281 {
2282 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2283 goto cleanup;
2284 }
2285
Hanno Becker5226c532019-02-27 17:38:40 +00002286 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002287 if( ret != 0 )
2288 {
2289 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2290 goto cleanup;
2291 }
2292
Hanno Becker5226c532019-02-27 17:38:40 +00002293 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002294 if( ret != 0 )
2295 {
2296 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2297 goto cleanup;
2298 }
2299
Hanno Becker5226c532019-02-27 17:38:40 +00002300 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002301 if( ret != 0 )
2302 {
2303 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2304 goto cleanup;
2305 }
2306
Hanno Becker5226c532019-02-27 17:38:40 +00002307 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2308 if( ret != 0 )
2309 {
2310 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2311 goto cleanup;
2312 }
2313
2314 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002315 if( ret != 0 )
2316 {
2317 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2318 goto cleanup;
2319 }
2320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002322 prefix, frame.version );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002323 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002324
Hanno Becker4f869ed2019-02-24 16:47:57 +00002325 {
2326 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002327 serial.p = frame.serial.p;
2328 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002329 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2330 prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002331 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002332 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002333 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002334 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002337 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002338 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002339 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002342 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002343 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002344 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345
Hanno Becker843b71a2019-06-25 09:39:21 +01002346#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002348 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002349 frame.valid_from.year, frame.valid_from.mon,
2350 frame.valid_from.day, frame.valid_from.hour,
2351 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002352 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002356 frame.valid_to.year, frame.valid_to.mon,
2357 frame.valid_to.day, frame.valid_to.hour,
2358 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002359 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker843b71a2019-06-25 09:39:21 +01002360#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002363 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002364
Hanno Becker83cd8672019-02-21 17:13:46 +00002365 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2366 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002367 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002368
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002369 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002371 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002372 {
2373 return( ret );
2374 }
2375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002377 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002378 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002379
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002380 /*
2381 * Optional extensions
2382 */
2383
Hanno Becker5226c532019-02-27 17:38:40 +00002384 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002387 frame.ca_istrue ? "true" : "false" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002388 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002389
Hanno Becker5226c532019-02-27 17:38:40 +00002390 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002391 {
Hanno Becker5226c532019-02-27 17:38:40 +00002392 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002393 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002394 }
2395 }
2396
Hanno Becker5226c532019-02-27 17:38:40 +00002397 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002400 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002401
2402 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002403 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002404 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002405 }
2406
Hanno Becker5226c532019-02-27 17:38:40 +00002407 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002410 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002411
Hanno Becker5226c532019-02-27 17:38:40 +00002412 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002413 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002414 }
2415
Hanno Becker5226c532019-02-27 17:38:40 +00002416 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002419 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002420
Hanno Becker5226c532019-02-27 17:38:40 +00002421 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002422 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002423 }
2424
Hanno Becker5226c532019-02-27 17:38:40 +00002425 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002428 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002429
2430 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002431 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002432 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002433 }
2434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002436 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002437
Hanno Becker4f869ed2019-02-24 16:47:57 +00002438 ret = (int) ( size - n );
2439
2440cleanup:
2441
Hanno Becker4f869ed2019-02-24 16:47:57 +00002442 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002443 mbedtls_pk_free( &pk );
2444 mbedtls_x509_name_free( issuer );
2445 mbedtls_x509_name_free( subject );
2446 mbedtls_x509_sequence_free( ext_key_usage );
2447 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002448
2449 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002450}
2451
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002452struct x509_crt_verify_string {
2453 int code;
2454 const char *string;
2455};
2456
2457static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002458 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002459 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2460 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2461 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2462 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2463 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002464 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2465 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2466 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002467 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002468 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2469 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2470 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2471 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002472 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2473 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2474 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2475 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2476 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2477 { 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 +01002478 { 0, NULL }
2479};
2480
2481int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002482 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002483{
2484 int ret;
2485 const struct x509_crt_verify_string *cur;
2486 char *p = buf;
2487 size_t n = size;
2488
2489 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2490 {
2491 if( ( flags & cur->code ) == 0 )
2492 continue;
2493
2494 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002495 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002496 flags ^= cur->code;
2497 }
2498
2499 if( flags != 0 )
2500 {
2501 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2502 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002503 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002504 }
2505
2506 return( (int) ( size - n ) );
2507}
Hanno Becker02a21932019-06-10 15:08:43 +01002508#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002511static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2512 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002513{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002514 unsigned int usage_must, usage_may;
2515 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2516 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2517
2518 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2519 return( 0 );
2520
2521 usage_must = usage & ~may_mask;
2522
2523 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2524 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2525
2526 usage_may = usage & may_mask;
2527
2528 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002530
2531 return( 0 );
2532}
Hanno Becker45eedf12019-02-25 13:55:33 +00002533
2534int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2535 unsigned int usage )
2536{
2537 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002538 mbedtls_x509_crt_frame const *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002539 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002540 if( ret != 0 )
2541 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2542
2543 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002544 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002545
2546 return( ret );
2547}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002548#endif
2549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002551typedef struct
2552{
2553 const char *oid;
2554 size_t oid_len;
2555} x509_crt_check_ext_key_usage_cb_ctx_t;
2556
2557static int x509_crt_check_ext_key_usage_cb( void *ctx,
2558 int tag,
2559 unsigned char *data,
2560 size_t data_len )
2561{
2562 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2563 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2564 ((void) tag);
2565
2566 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2567 data, data_len ) == 0 )
2568 {
2569 return( 1 );
2570 }
2571
2572 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2573 data_len ) == 0 )
2574 {
2575 return( 1 );
2576 }
2577
2578 return( 0 );
2579}
2580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002582 const char *usage_oid,
2583 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002584{
Hanno Beckere1956af2019-02-21 14:28:12 +00002585 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002586 mbedtls_x509_crt_frame const *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002587 unsigned ext_types;
2588 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002589 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002590
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002591 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002592 if( ret != 0 )
2593 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2594
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002595 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002596 ext_types = frame->ext_types;
2597 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2598 {
2599 p = frame->ext_key_usage_raw.p;
2600 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002601
Hanno Beckere9718b42019-02-25 18:11:42 +00002602 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2603 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2604 x509_crt_check_ext_key_usage_cb,
2605 &cb_ctx );
2606 if( ret == 1 )
2607 ret = 0;
2608 else
2609 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2610 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002611
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002612 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002613 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002618/*
2619 * Return 1 if the certificate is revoked, or 0 otherwise.
2620 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002621static int x509_serial_is_revoked( unsigned char const *serial,
2622 size_t serial_len,
2623 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002626
2627 while( cur != NULL && cur->serial.len != 0 )
2628 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002629 if( serial_len == cur->serial.len &&
2630 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002631 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002632 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002633 return( 1 );
2634 }
2635
2636 cur = cur->next;
2637 }
2638
2639 return( 0 );
2640}
2641
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002642int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2643 const mbedtls_x509_crl *crl )
2644{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002645 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002646 mbedtls_x509_crt_frame const *frame;
Hanno Becker79ae5b62019-02-25 18:12:00 +00002647
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002648 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002649 if( ret != 0 )
2650 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2651
2652 ret = x509_serial_is_revoked( frame->serial.p,
2653 frame->serial.len,
2654 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002655 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002656 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002657}
2658
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002659/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002660 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002661 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002662 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002663static int x509_crt_verifycrl( unsigned char *crt_serial,
2664 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002665 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002666 mbedtls_x509_crl *crl_list,
2667 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002668{
Hanno Beckerbb266132019-02-25 18:12:46 +00002669 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002670 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2672 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002673 mbedtls_x509_buf_raw ca_subject;
2674 mbedtls_pk_context *pk;
2675 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002676
Hanno Beckerbb266132019-02-25 18:12:46 +00002677 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002678 return( flags );
2679
Hanno Beckerbb266132019-02-25 18:12:46 +00002680 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002681 mbedtls_x509_crt_frame const *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002682 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002683 if( ret != 0 )
2684 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2685
2686 ca_subject = ca->subject_raw;
2687
2688 can_sign = 0;
2689 if( x509_crt_check_key_usage_frame( ca,
2690 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2691 {
2692 can_sign = 1;
2693 }
2694
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002695 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002696 }
2697
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002698 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002699 if( ret != 0 )
2700 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2701
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002702 while( crl_list != NULL )
2703 {
2704 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002705 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002706 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002707 {
2708 crl_list = crl_list->next;
2709 continue;
2710 }
2711
2712 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002713 * Check if the CA is configured to sign CRLs
2714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002716 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002719 break;
2720 }
2721#endif
2722
2723 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002724 * Check if CRL is correctly signed by the trusted CA
2725 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002726 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2727 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2728
2729 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2730 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002733 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002734 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002735 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002737 break;
2738 }
2739
Hanno Beckerbb266132019-02-25 18:12:46 +00002740 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002741 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002742
Hanno Beckerbb266132019-02-25 18:12:46 +00002743 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002745 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002748 break;
2749 }
2750
2751 /*
2752 * Check for validity of CRL (Do not drop out)
2753 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002754 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002756
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002757 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002758 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002759
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002760 /*
2761 * Check if certificate is revoked
2762 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002763 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2764 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002767 break;
2768 }
2769
2770 crl_list = crl_list->next;
2771 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002772
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002773 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002774 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002777
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002778/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002779 * Check the signature of a certificate by its parent
2780 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002781static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002782 mbedtls_x509_crt *parent,
2783 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002784{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002785 int ret;
2786 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002787
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002788 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002789 if( ret != 0 )
2790 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2791
2792 /* Skip expensive computation on obvious mismatch */
2793 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002794 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002795 ret = -1;
2796 goto exit;
2797 }
2798
2799#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2800 ((void) rs_ctx);
2801#else
2802 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2803 {
2804 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002805 sig_info->sig_md,
2806 sig_info->crt_hash, sig_info->crt_hash_len,
2807 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002808 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002809 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002810 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002811#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002812 {
2813 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2814 sig_info->sig_opts,
2815 pk,
2816 sig_info->sig_md,
2817 sig_info->crt_hash, sig_info->crt_hash_len,
2818 sig_info->sig.p, sig_info->sig.len );
2819 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002820
Hanno Beckere449e2d2019-02-25 14:45:31 +00002821exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002822 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002823 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002824}
2825
2826/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002827 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2828 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002829 *
2830 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002831 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002832static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2833 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002834 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002835{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002836 int need_ca_bit;
2837
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002838 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002839 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2840 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002841 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002842 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002843 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002844 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002845
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002846 /* Parent must have the basicConstraints CA bit set as a general rule */
2847 need_ca_bit = 1;
2848
2849 /* Exception: v1/v2 certificates that are locally trusted. */
2850 if( top && parent->version < 3 )
2851 need_ca_bit = 0;
2852
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002853 if( need_ca_bit && ! parent->ca_istrue )
2854 return( -1 );
2855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002857 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002858 x509_crt_check_key_usage_frame( parent,
2859 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002860 {
2861 return( -1 );
2862 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002863#endif
2864
2865 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002866}
2867
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002868/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002869 * Find a suitable parent for child in candidates, or return NULL.
2870 *
2871 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002872 * 1. subject name matches child's issuer
2873 * 2. if necessary, the CA bit is set and key usage allows signing certs
2874 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002875 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002876 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002877 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002878 * If there's a suitable candidate which is also time-valid, return the first
2879 * such. Otherwise, return the first suitable candidate (or NULL if there is
2880 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002881 *
2882 * The rationale for this rule is that someone could have a list of trusted
2883 * roots with two versions on the same root with different validity periods.
2884 * (At least one user reported having such a list and wanted it to just work.)
2885 * The reason we don't just require time-validity is that generally there is
2886 * only one version, and if it's expired we want the flags to state that
2887 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002888 *
2889 * The rationale for rule 3 (signature for trusted roots) is that users might
2890 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002891 * way we select the correct one is by checking the signature (as we don't
2892 * rely on key identifier extensions). (This is one way users might choose to
2893 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002894 *
2895 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002896 * - [in] child: certificate for which we're looking for a parent
2897 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002898 * - [out] r_parent: parent found (or NULL)
2899 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002900 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2901 * of the chain, 0 otherwise
2902 * - [in] path_cnt: number of intermediates seen so far
2903 * - [in] self_cnt: number of self-signed intermediates seen so far
2904 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002905 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002906 *
2907 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002908 * - 0 on success
2909 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002910 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002911static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002912 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002913 mbedtls_x509_crt *candidates,
2914 mbedtls_x509_crt **r_parent,
2915 int *r_signature_is_good,
2916 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002917 unsigned path_cnt,
2918 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002919 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002920{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002921 int ret;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002922 mbedtls_x509_crt *parent_crt;
2923 int signature_is_good;
2924
2925#if defined(MBEDTLS_HAVE_TIME_DATE)
2926 mbedtls_x509_crt *fallback_parent;
2927 int fallback_signature_is_good;
2928#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002929
2930#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002931 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002932 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2933 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002934 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002935 parent_crt = rs_ctx->parent;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002936#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002937 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002938 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002939#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002940
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002941 /* clear saved state */
2942 rs_ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002943#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002944 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002945 rs_ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002946#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002947
2948 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002949 goto check_signature;
2950 }
2951#endif
2952
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002953#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002954 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002955 fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002956#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002957
Hanno Becker43bf9002019-02-25 14:46:49 +00002958 for( parent_crt = candidates; parent_crt != NULL;
2959 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002960 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002961 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002962
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002963#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2964check_signature:
2965#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002966
2967 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002968 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002969 mbedtls_x509_crt_frame const *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002970
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002971 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002972 if( ret != 0 )
2973 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002974
Hanno Becker843b71a2019-06-25 09:39:21 +01002975#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker040c5642019-06-10 11:14:24 +01002976 if( !mbedtls_x509_time_is_past( &parent->valid_to ) &&
2977 !mbedtls_x509_time_is_future( &parent->valid_from ) )
Hanno Becker43bf9002019-02-25 14:46:49 +00002978 {
2979 parent_valid = 1;
2980 }
Hanno Becker843b71a2019-06-25 09:39:21 +01002981#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker43bf9002019-02-25 14:46:49 +00002982
2983 /* basic parenting skills (name, CA bit, key usage) */
2984 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2985 parent_match = 1;
2986
2987 /* +1 because the stored max_pathlen is 1 higher
2988 * than the actual value */
2989 if( !( parent->max_pathlen > 0 &&
2990 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2991 {
2992 path_len_ok = 1;
2993 }
2994
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002995 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002996 }
2997
2998 if( parent_match == 0 || path_len_ok == 0 )
2999 continue;
3000
3001 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00003002 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003003
3004#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003005 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3006 {
3007 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00003008 rs_ctx->parent = parent_crt;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003009#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003010 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003011 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003012#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003013
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003014 return( ret );
3015 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003016#else
3017 (void) ret;
3018#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003019
3020 signature_is_good = ret == 0;
3021 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02003022 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02003023
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003024 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00003025 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003026 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003027#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003028 if( fallback_parent == NULL )
3029 {
Hanno Becker43bf9002019-02-25 14:46:49 +00003030 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003031 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003032 }
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003033#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003034
3035 continue;
3036 }
3037
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003038 break;
3039 }
3040
Hanno Becker43bf9002019-02-25 14:46:49 +00003041 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003042 {
Hanno Becker43bf9002019-02-25 14:46:49 +00003043 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003044 *r_signature_is_good = signature_is_good;
3045 }
3046 else
3047 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003048#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003049 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003050 *r_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003051#else /* MBEDTLS_HAVE_TIME_DATE */
3052 *r_parent = NULL;
3053#endif /* !MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003054 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003055
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003056 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003057}
3058
3059/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003060 * Find a parent in trusted CAs or the provided chain, or return NULL.
3061 *
3062 * Searches in trusted CAs first, and return the first suitable parent found
3063 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003064 *
3065 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003066 * - [in] child: certificate for which we're looking for a parent, followed
3067 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003068 * - [in] trust_ca: list of locally trusted certificates
3069 * - [out] parent: parent found (or NULL)
3070 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
3071 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
3072 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
3073 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003074 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003075 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003076 *
3077 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003078 * - 0 on success
3079 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003080 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003081static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00003082 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00003083 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003084 mbedtls_x509_crt *trust_ca,
3085 mbedtls_x509_crt **parent,
3086 int *parent_is_trusted,
3087 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003088 unsigned path_cnt,
3089 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003090 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003091{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003092 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003093 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003094
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003095 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003096
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003097#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003098 /* restore then clear saved state if we have some stored */
3099 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
3100 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003101 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003102 rs_ctx->parent_is_trusted = -1;
3103 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003104#endif
3105
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003106 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00003107 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003108
Hanno Becker5299cf82019-02-25 13:50:41 +00003109 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003110 parent, signature_is_good,
3111 *parent_is_trusted,
3112 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003113
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003114#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003115 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3116 {
3117 /* save state */
3118 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003119 return( ret );
3120 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003121#else
3122 (void) ret;
3123#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003124
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003125 /* stop here if found or already in second iteration */
3126 if( *parent != NULL || *parent_is_trusted == 0 )
3127 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003128
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003129 /* prepare second iteration */
3130 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003131 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003132
3133 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01003134 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003135 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02003136 *parent_is_trusted = 0;
3137 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003138 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003139
3140 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003141}
3142
3143/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003144 * Check if an end-entity certificate is locally trusted
3145 *
3146 * Currently we require such certificates to be self-signed (actually only
3147 * check for self-issued as self-signatures are not checked)
3148 */
3149static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003150 mbedtls_x509_crt_frame const *crt,
3151 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003152{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003153 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003154
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003155 /* look for an exact match with trusted cert */
3156 for( cur = trust_ca; cur != NULL; cur = cur->next )
3157 {
3158 if( crt->raw.len == cur->raw.len &&
3159 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3160 {
3161 return( 0 );
3162 }
3163 }
3164
3165 /* too bad */
3166 return( -1 );
3167}
3168
3169/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003170 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003171 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003172 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3173 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003174 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003175 * such that every cert in the chain is a child of the next one,
3176 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003177 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003178 * Verify that chain and return it with flags for all issues found.
3179 *
3180 * Special cases:
3181 * - EE == Rj -> return a one-element list containing it
3182 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3183 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003184 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003185 * Tests for (aspects of) this function should include at least:
3186 * - trusted EE
3187 * - EE -> trusted root
Antonin Décimod5f47592019-01-23 15:24:37 +01003188 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003189 * - if relevant: EE untrusted
3190 * - if relevant: EE -> intermediate, untrusted
3191 * with the aspect under test checked at each relevant level (EE, int, root).
3192 * For some aspects longer chains are required, but usually length 2 is
3193 * enough (but length 1 is not in general).
3194 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003195 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003196 * - [in] crt: the cert list EE, C1, ..., Cn
3197 * - [in] trust_ca: the trusted list R1, ..., Rp
3198 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003199 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003200 * Only valid when return value is 0, may contain garbage otherwise!
3201 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003202 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003203 *
3204 * Return value:
3205 * - non-zero if the chain could not be fully built and examined
3206 * - 0 is the chain was successfully built and examined,
3207 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003208 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003209static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003210 mbedtls_x509_crt *crt,
3211 mbedtls_x509_crt *trust_ca,
3212 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003213 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003214 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003215 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003216{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003217 /* Don't initialize any of those variables here, so that the compiler can
3218 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003219 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003220 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003221 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003222 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003223 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003224 int parent_is_trusted;
3225 int child_is_trusted;
3226 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003227 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003228
3229#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3230 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003231 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003232 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003233 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003234 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003235 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003236
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003237 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003238 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003239 child_crt = cur->crt;
3240
3241 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003242 goto find_parent;
3243 }
3244#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003245
Hanno Becker1e0677a2019-02-25 14:58:22 +00003246 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003247 self_cnt = 0;
3248 parent_is_trusted = 0;
3249 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003250
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003251 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003252#if defined(MBEDTLS_X509_CRL_PARSE_C)
3253 mbedtls_x509_buf_raw child_serial;
3254#endif /* MBEDTLS_X509_CRL_PARSE_C */
3255 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003256
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003257 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003258 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003259 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003260 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003261 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003262
3263#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3264find_parent:
3265#endif
3266
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003267 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003268
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003269 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003270 mbedtls_x509_crt_sig_info child_sig;
3271 {
Hanno Becker5f268b32019-05-20 16:26:34 +01003272 mbedtls_x509_crt_frame const *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003273
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003274 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003275 if( ret != 0 )
3276 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3277
Hanno Becker843b71a2019-06-25 09:39:21 +01003278#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker5299cf82019-02-25 13:50:41 +00003279 /* Check time-validity (all certificates) */
3280 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3281 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3282 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3283 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Hanno Becker843b71a2019-06-25 09:39:21 +01003284#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker5299cf82019-02-25 13:50:41 +00003285
3286 /* Stop here for trusted roots (but not for trusted EE certs) */
3287 if( child_is_trusted )
3288 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003289 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003290 return( 0 );
3291 }
3292
3293 self_issued = 0;
3294 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3295 &child->subject_raw,
3296 NULL, NULL ) == 0 )
3297 {
3298 self_issued = 1;
3299 }
3300
3301 /* Check signature algorithm: MD & PK algs */
3302 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3303 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3304
3305 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3306 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3307
3308 /* Special case: EE certs that are locally trusted */
3309 if( ver_chain->len == 1 && self_issued &&
3310 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3311 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003312 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003313 return( 0 );
3314 }
3315
3316#if defined(MBEDTLS_X509_CRL_PARSE_C)
3317 child_serial = child->serial;
3318#endif /* MBEDTLS_X509_CRL_PARSE_C */
3319
3320 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003321 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003322
Hanno Becker5299cf82019-02-25 13:50:41 +00003323 if( ret != 0 )
3324 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3325 }
3326
3327 /* Look for a parent in trusted CAs or up the chain */
3328 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003329 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003330 &parent_is_trusted, &signature_is_good,
3331 ver_chain->len - 1, self_cnt, rs_ctx );
3332
3333 x509_crt_free_sig_info( &child_sig );
3334 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003335
3336#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003337 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3338 {
3339 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003340 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003341 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003342 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003343 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003344 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003345#else
3346 (void) ret;
3347#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003348
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003349 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003350 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003351 {
3352 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003353 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003354 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003355
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003356 /* Count intermediate self-issued (not necessarily self-signed) certs.
3357 * These can occur with some strategies for key rollover, see [SIRO],
3358 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003359 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003360 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003361
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003362 /* path_cnt is 0 for the first intermediate CA,
3363 * and if parent is trusted it's not an intermediate CA */
3364 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003365 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003366 {
3367 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003368 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003369 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003370
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003371 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003372 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003373 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3374
Hanno Becker58c35642019-02-25 18:13:46 +00003375 {
3376 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003377 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003378 if( ret != 0 )
3379 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3380
3381 /* check size of signing key */
3382 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3383 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3384
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003385 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003386 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003389 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003390 *flags |= x509_crt_verifycrl( child_serial.p,
3391 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003392 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003393#else
3394 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003395#endif
3396
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003397 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003398 child_crt = parent_crt;
3399 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003400 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003401 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003402 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003403}
3404
3405/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003406 * Check for CN match
3407 */
Hanno Becker24926222019-02-21 13:10:55 +00003408static int x509_crt_check_cn( unsigned char const *buf,
3409 size_t buflen,
3410 const char *cn,
3411 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003412{
Hanno Becker24926222019-02-21 13:10:55 +00003413 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003414 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003415 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003416
3417 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003418 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003419 {
3420 return( 0 );
3421 }
3422
3423 return( -1 );
3424}
3425
Hanno Becker8b543b32019-02-21 11:50:44 +00003426/* Returns 1 on a match and 0 on a mismatch.
3427 * This is because this function is used as a callback for
3428 * mbedtls_x509_name_cmp_raw(), which continues the name
3429 * traversal as long as the callback returns 0. */
3430static int x509_crt_check_name( void *ctx,
3431 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003432 mbedtls_x509_buf *val,
3433 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003434{
3435 char const *cn = (char const*) ctx;
3436 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003437 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003438
3439 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003440 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003441 {
3442 return( 1 );
3443 }
3444
3445 return( 0 );
3446}
3447
Hanno Beckerda410822019-02-21 13:36:59 +00003448/* Returns 1 on a match and 0 on a mismatch.
3449 * This is because this function is used as a callback for
3450 * mbedtls_asn1_traverse_sequence_of(), which continues the
3451 * traversal as long as the callback returns 0. */
3452static int x509_crt_subject_alt_check_name( void *ctx,
3453 int tag,
3454 unsigned char *data,
3455 size_t data_len )
3456{
3457 char const *cn = (char const*) ctx;
3458 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003459 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003460
3461 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3462 return( 1 );
3463
3464 return( 0 );
3465}
3466
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003467/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003468 * Verify the requested CN - only call this if cn is not NULL!
3469 */
Hanno Becker082435c2019-02-25 18:14:40 +00003470static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3471 const char *cn,
3472 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003473{
Hanno Beckerda410822019-02-21 13:36:59 +00003474 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01003475 mbedtls_x509_crt_frame const *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003476
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003477 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003478 if( ret != 0 )
3479 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3480
3481 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003482 {
Hanno Becker90b94082019-02-21 21:13:21 +00003483 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003484 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003485 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003486 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003487
Hanno Becker90b94082019-02-21 21:13:21 +00003488 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3489 MBEDTLS_ASN1_TAG_CLASS_MASK,
3490 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3491 MBEDTLS_ASN1_TAG_VALUE_MASK,
3492 2 /* SubjectAlt DNS */,
3493 x509_crt_subject_alt_check_name,
Hanno Becker484caf02019-05-29 14:41:44 +01003494 (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003495 }
3496 else
3497 {
Hanno Becker082435c2019-02-25 18:14:40 +00003498 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3499 &frame->subject_raw,
Hanno Becker484caf02019-05-29 14:41:44 +01003500 x509_crt_check_name, (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003501 }
Hanno Beckerda410822019-02-21 13:36:59 +00003502
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003503 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003504
3505 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3506 * return 1 when finding a name component matching `cn`. */
3507 if( ret == 1 )
3508 return( 0 );
3509
3510 if( ret != 0 )
3511 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3512
3513 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3514 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003515}
3516
3517/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003518 * Merge the flags for all certs in the chain, after calling callback
3519 */
3520static int x509_crt_merge_flags_with_cb(
3521 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003522 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003523 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3524 void *p_vrfy )
3525{
3526 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003527 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003528 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003529 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003530
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003531 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003532 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003533 cur = &ver_chain->items[i-1];
3534 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003535
3536 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003537 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003538 return( ret );
3539
3540 *flags |= cur_flags;
3541 }
3542
3543 return( 0 );
3544}
3545
3546/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003547 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3550 mbedtls_x509_crt *trust_ca,
3551 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003552 const char *cn, uint32_t *flags,
3553 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003554 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003555{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003556 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3557 &mbedtls_x509_crt_profile_default, cn, flags,
3558 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003559}
3560
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003561/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003562 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003563 */
3564int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3565 mbedtls_x509_crt *trust_ca,
3566 mbedtls_x509_crl *ca_crl,
3567 const mbedtls_x509_crt_profile *profile,
3568 const char *cn, uint32_t *flags,
3569 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3570 void *p_vrfy )
3571{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003572 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3573 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3574}
3575
3576/*
3577 * Verify the certificate validity, with profile, restartable version
3578 *
3579 * This function:
3580 * - checks the requested CN (if any)
3581 * - checks the type and size of the EE cert's key,
3582 * as that isn't done as part of chain building/verification currently
3583 * - builds and verifies the chain
3584 * - then calls the callback and merges the flags
3585 */
3586int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3587 mbedtls_x509_crt *trust_ca,
3588 mbedtls_x509_crl *ca_crl,
3589 const mbedtls_x509_crt_profile *profile,
3590 const char *cn, uint32_t *flags,
3591 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3592 void *p_vrfy,
3593 mbedtls_x509_crt_restart_ctx *rs_ctx )
3594{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003595 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003596 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003597 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003598
3599 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003600 ee_flags = 0;
3601 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003602
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003603 if( profile == NULL )
3604 {
3605 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3606 goto exit;
3607 }
3608
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003609 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003610 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003611 {
3612 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3613 if( ret != 0 )
3614 return( ret );
3615 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003616
Hanno Becker87233362019-02-25 18:15:33 +00003617 {
3618 mbedtls_pk_context *pk;
3619 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003620
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003621 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003622 if( ret != 0 )
3623 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003624
Hanno Becker87233362019-02-25 18:15:33 +00003625 /* Check the type and size of the key */
3626 pk_type = mbedtls_pk_get_type( pk );
3627
3628 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3629 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3630
3631 if( x509_profile_check_key( profile, pk ) != 0 )
3632 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3633
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003634 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003635 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003636
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003637 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003638 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003639 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003640
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003641 if( ret != 0 )
3642 goto exit;
3643
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003644 /* Merge end-entity flags */
3645 ver_chain.items[0].flags |= ee_flags;
3646
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003647 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003648 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003649
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003650exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003651#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3652 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3653 mbedtls_x509_crt_restart_free( rs_ctx );
3654#endif
3655
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003656 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3657 * the SSL module for authmode optional, but non-zero return from the
3658 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003659 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3660 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3661
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003662 if( ret != 0 )
3663 {
3664 *flags = (uint32_t) -1;
3665 return( ret );
3666 }
3667
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003668 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003670
3671 return( 0 );
3672}
3673
3674/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003675 * Initialize a certificate chain
3676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003680}
3681
3682/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003683 * Unallocate all certificate data
3684 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003687{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 mbedtls_x509_crt *cert_cur = crt;
3689 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003690
3691 if( crt == NULL )
3692 return;
3693
3694 do
3695 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003696 x509_crt_cache_free( cert_cur->cache );
3697 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003698
3699#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3703 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003704#endif
3705
Hanno Becker2bcc7642019-02-26 19:01:00 +00003706 mbedtls_x509_name_free( cert_cur->issuer.next );
3707 mbedtls_x509_name_free( cert_cur->subject.next );
3708 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3709 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003710#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003711
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003712 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003713 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003714 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003716 }
3717
3718 cert_cur = cert_cur->next;
3719 }
3720 while( cert_cur != NULL );
3721
3722 cert_cur = crt;
3723 do
3724 {
3725 cert_prv = cert_cur;
3726 cert_cur = cert_cur->next;
3727
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003728 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003729 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003731 }
3732 while( cert_cur != NULL );
3733}
3734
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003735#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3736/*
3737 * Initialize a restart context
3738 */
3739void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3740{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003741 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003742
3743 ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003744#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003745 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003746 ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003747#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003748
3749 ctx->parent_is_trusted = -1;
3750
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003751 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003752 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003753 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003754}
3755
3756/*
3757 * Free the components of a restart context
3758 */
3759void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3760{
3761 if( ctx == NULL )
3762 return;
3763
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003764 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003765 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003766}
3767#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769#endif /* MBEDTLS_X509_CRT_PARSE_C */