blob: e1e98df5255b26d81b949642e365a2a8fcc9e050 [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 );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300105#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker12506232019-05-13 13:53:21 +0100106static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000107 mbedtls_x509_sequence *subject_alt );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300108#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker12506232019-05-13 13:53:21 +0100109static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000110 mbedtls_x509_sequence *ext_key_usage );
111
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300112static int mbedtls_x509_crt_flush_cache_pk( mbedtls_x509_crt const *crt )
Hanno Beckerbc685192019-03-05 15:35:31 +0000113{
114#if defined(MBEDTLS_THREADING_C)
115 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
116 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100117#endif
Hanno Beckerfc99a092019-06-28 14:45:26 +0100118
119#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
120 defined(MBEDTLS_THREADING_C)
121 /* Can only free the PK context if nobody is using it.
122 * If MBEDTLS_X509_ALWAYS_FLUSH is set, nested uses
123 * of xxx_acquire() are prohibited, and no reference
124 * counting is needed. Also, notice that the code-path
125 * below is safe if the cache isn't filled. */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100126 if( crt->cache->pk_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100127#endif /* !MBEDTLS_X509_ALWAYS_FLUSH ||
128 MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100129 {
Hanno Beckerbc685192019-03-05 15:35:31 +0000130#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100131 /* The cache holds a shallow copy of the PK context
132 * in the legacy struct, so don't free PK context. */
133 mbedtls_free( crt->cache->pk );
Hanno Beckerbc685192019-03-05 15:35:31 +0000134#else
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100135 mbedtls_pk_free( crt->cache->pk );
136 mbedtls_free( crt->cache->pk );
Hanno Beckerbc685192019-03-05 15:35:31 +0000137#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100138 crt->cache->pk = NULL;
139 }
Hanno Beckerbc685192019-03-05 15:35:31 +0000140
141#if defined(MBEDTLS_THREADING_C)
142 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
143 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
144#endif
145 return( 0 );
146}
147
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300148static int mbedtls_x509_crt_flush_cache_frame( mbedtls_x509_crt const *crt )
Hanno Beckerbc685192019-03-05 15:35:31 +0000149{
150#if defined(MBEDTLS_THREADING_C)
151 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
152 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100153#endif
Hanno Beckerbc685192019-03-05 15:35:31 +0000154
Hanno Beckerfc99a092019-06-28 14:45:26 +0100155#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
156 defined(MBEDTLS_THREADING_C)
157 /* Can only free the PK context if nobody is using it.
158 * If MBEDTLS_X509_ALWAYS_FLUSH is set, nested uses
159 * of xxx_acquire() are prohibited, and no reference
160 * counting is needed. Also, notice that the code-path
161 * below is safe if the cache isn't filled. */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100162 if( crt->cache->frame_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100163#endif /* !MBEDTLS_X509_ALWAYS_FLUSH ||
164 MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100165 {
166 mbedtls_free( crt->cache->frame );
167 crt->cache->frame = NULL;
168 }
Hanno Beckerbc685192019-03-05 15:35:31 +0000169
170#if defined(MBEDTLS_THREADING_C)
171 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
172 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
173#endif
174 return( 0 );
175}
176
177int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt )
178{
179 int ret;
180 ret = mbedtls_x509_crt_flush_cache_frame( crt );
181 if( ret != 0 )
182 return( ret );
183 ret = mbedtls_x509_crt_flush_cache_pk( crt );
184 if( ret != 0 )
185 return( ret );
186 return( 0 );
187}
188
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000189static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame );
Hanno Beckered058882019-06-28 10:46:43 +0100190
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300191static int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt )
Hanno Becker337088a2019-02-25 14:53:14 +0000192{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000193 mbedtls_x509_crt_cache *cache = crt->cache;
Hanno Becker337088a2019-02-25 14:53:14 +0000194 mbedtls_x509_crt_frame *frame;
195
Hanno Becker76428352019-03-05 15:29:23 +0000196 if( cache->frame != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100197 {
Hanno Becker410322f2019-07-02 13:37:12 +0100198#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
199 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000200 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100201#else
202 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
203 * allow nested uses of acquire. */
204 return( MBEDTLS_ERR_X509_FATAL_ERROR );
205#endif
206 }
Hanno Becker76428352019-03-05 15:29:23 +0000207
Hanno Becker337088a2019-02-25 14:53:14 +0000208 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
209 if( frame == NULL )
210 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000211 cache->frame = frame;
Hanno Becker337088a2019-02-25 14:53:14 +0000212
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000213#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
214 /* This would work with !MBEDTLS_X509_ON_DEMAND_PARSING, too,
215 * but is inefficient compared to copying the respective fields
216 * from the legacy mbedtls_x509_crt. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000217 return( x509_crt_parse_frame( crt->raw.p,
218 crt->raw.p + crt->raw.len,
219 frame ) );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000220#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
221 /* Make sure all extension related fields are properly initialized. */
222 frame->ca_istrue = 0;
223 frame->max_pathlen = 0;
224 frame->ext_types = 0;
225 frame->version = crt->version;
226 frame->sig_md = crt->sig_md;
227 frame->sig_pk = crt->sig_pk;
Hanno Becker843b71a2019-06-25 09:39:21 +0100228
229#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000230 frame->valid_from = crt->valid_from;
231 frame->valid_to = crt->valid_to;
Hanno Becker843b71a2019-06-25 09:39:21 +0100232#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
233
Hanno Becker38f0cb42019-03-04 15:13:45 +0000234 x509_buf_to_buf_raw( &frame->raw, &crt->raw );
235 x509_buf_to_buf_raw( &frame->tbs, &crt->tbs );
236 x509_buf_to_buf_raw( &frame->serial, &crt->serial );
237 x509_buf_to_buf_raw( &frame->pubkey_raw, &crt->pk_raw );
238 x509_buf_to_buf_raw( &frame->issuer_raw, &crt->issuer_raw );
239 x509_buf_to_buf_raw( &frame->subject_raw, &crt->subject_raw );
Hanno Beckerd07614c2019-06-25 10:19:58 +0100240#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Becker38f0cb42019-03-04 15:13:45 +0000241 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
242 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
Hanno Beckerd07614c2019-06-25 10:19:58 +0100243#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker38f0cb42019-03-04 15:13:45 +0000244 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
245 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000246
247 /* The legacy CRT structure doesn't explicitly contain
248 * the `AlgorithmIdentifier` bounds; however, those can
249 * be inferred from the surrounding (mandatory) `SerialNumber`
250 * and `Issuer` fields. */
251 frame->sig_alg.p = crt->serial.p + crt->serial.len;
252 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
253
254 return( x509_crt_frame_parse_ext( frame ) );
255#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000256}
Hanno Becker337088a2019-02-25 14:53:14 +0000257
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300258static int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000259{
260 mbedtls_x509_crt_cache *cache = crt->cache;
261 mbedtls_pk_context *pk;
262
Hanno Becker76428352019-03-05 15:29:23 +0000263 if( cache->pk != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100264 {
Hanno Becker410322f2019-07-02 13:37:12 +0100265#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
266 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000267 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100268#else
269 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
270 * allow nested uses of acquire. */
271 return( MBEDTLS_ERR_X509_FATAL_ERROR );
272#endif
273 }
Hanno Becker76428352019-03-05 15:29:23 +0000274
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000275 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
276 if( pk == NULL )
277 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000278 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000279
280#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
281 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000282 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000283#else
284 {
285 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
286 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
287 pk_raw.p + pk_raw.len,
288 pk ) );
289 }
290#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000291}
292
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000293static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000294{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000295 memset( cache, 0, sizeof( *cache ) );
296#if defined(MBEDTLS_THREADING_C)
297 mbedtls_mutex_init( &cache->frame_mutex );
298 mbedtls_mutex_init( &cache->pk_mutex );
299#endif
300}
301
302static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
303{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000304#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000305 /* The cache holds a shallow copy of the PK context
306 * in the legacy struct, so don't free PK context. */
307 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000308#else
309 mbedtls_pk_free( cache->pk );
310 mbedtls_free( cache->pk );
311#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
312
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000313 cache->pk = NULL;
314}
315
316static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
317{
318 mbedtls_free( cache->frame );
319 cache->frame = NULL;
320}
321
322static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
323{
324 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000325 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000326
327#if defined(MBEDTLS_THREADING_C)
328 mbedtls_mutex_free( &cache->frame_mutex );
329 mbedtls_mutex_free( &cache->pk_mutex );
330#endif
331
332 x509_crt_cache_clear_frame( cache );
333 x509_crt_cache_clear_pk( cache );
334
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200335 mbedtls_platform_memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000336}
337
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300338#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000339int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
340 mbedtls_x509_sequence **subj_alt )
341{
342 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100343 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000344 mbedtls_x509_sequence *seq;
345
346 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
347 if( ret != 0 )
348 return( ret );
349
350 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
351 if( seq == NULL )
352 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
353 else
354 ret = x509_crt_subject_alt_from_frame( frame, seq );
355
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000356 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000357
358 *subj_alt = seq;
359 return( ret );
360}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300361#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000362
363int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
364 mbedtls_x509_sequence **ext_key_usage )
365{
366 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100367 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000368 mbedtls_x509_sequence *seq;
369
370 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
371 if( ret != 0 )
372 return( ret );
373
374 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
375 if( seq == NULL )
376 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
377 else
378 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
379
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000380 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000381
382 *ext_key_usage = seq;
383 return( ret );
384}
385
Hanno Becker63e69982019-02-26 18:50:49 +0000386int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
387 mbedtls_x509_name **subject )
388{
389 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100390 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000391 mbedtls_x509_name *name;
392
393 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
394 if( ret != 0 )
395 return( ret );
396
397 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
398 if( name == NULL )
399 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
400 else
401 ret = x509_crt_subject_from_frame( frame, name );
402
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000403 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000404
405 *subject = name;
406 return( ret );
407}
408
409int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
410 mbedtls_x509_name **issuer )
411{
412 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100413 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000414 mbedtls_x509_name *name;
415
416 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
417 if( ret != 0 )
418 return( ret );
419
420 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
421 if( name == NULL )
422 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
423 else
424 ret = x509_crt_issuer_from_frame( frame, name );
425
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000426 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000427
428 *issuer = name;
429 return( ret );
430}
431
Hanno Becker823efad2019-02-28 13:23:58 +0000432int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
433 mbedtls_x509_crt_frame *dst )
434{
435 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100436 mbedtls_x509_crt_frame const *frame;
Hanno Becker823efad2019-02-28 13:23:58 +0000437 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
438 if( ret != 0 )
439 return( ret );
440 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000441 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000442 return( 0 );
443}
444
445int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
446 mbedtls_pk_context *dst )
447{
448#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
449 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
450 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
451 pk_raw.p + pk_raw.len,
452 dst ) );
453#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
454 int ret;
455 mbedtls_pk_context *pk;
456 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
457 if( ret != 0 )
458 return( ret );
459
460 /* Move PK from CRT cache to destination pointer
461 * to avoid a copy. */
462 *dst = *pk;
463 mbedtls_free( crt->cache->pk );
464 crt->cache->pk = NULL;
465
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000466 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000467 return( 0 );
468#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
469}
470
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200471/*
472 * Item in a verification chain: cert and flags for it
473 */
474typedef struct {
475 mbedtls_x509_crt *crt;
476 uint32_t flags;
477} x509_crt_verify_chain_item;
478
479/*
480 * Max size of verification chain: end-entity + intermediates + trusted root
481 */
482#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200483
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200485 * Default profile
486 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200487const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
488{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200489#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200490 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200491 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200492#endif
493 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200494 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
495 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
496 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
497 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
498 0xFFFFFFF, /* Any PK alg */
499 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200500 2048,
501};
502
503/*
504 * Next-default profile
505 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200506const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
507{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200508 /* Hashes from SHA-256 and above */
509 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
510 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
511 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
512 0xFFFFFFF, /* Any PK alg */
Hanno Becker59e7b082019-08-23 13:21:21 +0100513#if defined(MBEDTLS_USE_TINYCRYPT)
514 MBEDTLS_X509_ID_FLAG( MBEDTLS_UECC_DP_SECP256R1 ),
515#elif defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200516 /* Curves at or above 128-bit security level */
517 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
518 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
519 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
520 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
521 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
522 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
523 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
524#else
525 0,
526#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200527 2048,
528};
529
530/*
531 * NSA Suite B Profile
532 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200533const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
534{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200535 /* Only SHA-256 and 384 */
536 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
537 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
538 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200539 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
540 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Hanno Becker59e7b082019-08-23 13:21:21 +0100541#if defined(MBEDTLS_USE_TINYCRYPT)
542 MBEDTLS_X509_ID_FLAG( MBEDTLS_UECC_DP_SECP256R1 ),
543#elif defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200544 /* Only NIST P-256 and P-384 */
545 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
546 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
547#else
548 0,
549#endif
550 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200551};
552
553/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200554 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200555 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200556 */
557static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
558 mbedtls_md_type_t md_alg )
559{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200560 if( md_alg == MBEDTLS_MD_NONE )
561 return( -1 );
562
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200563 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
564 return( 0 );
565
566 return( -1 );
567}
568
569/*
570 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200571 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200572 */
573static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
574 mbedtls_pk_type_t pk_alg )
575{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200576 if( pk_alg == MBEDTLS_PK_NONE )
577 return( -1 );
578
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200579 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
580 return( 0 );
581
582 return( -1 );
583}
584
585/*
586 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200587 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200588 */
589static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200590 const mbedtls_pk_context *pk )
591{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200592 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200593
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200594#if defined(MBEDTLS_RSA_C)
595 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
596 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200597 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200598 return( 0 );
599
600 return( -1 );
601 }
602#endif
603
Hanno Beckerd931ad22019-08-21 15:25:22 +0100604#if defined(MBEDTLS_USE_TINYCRYPT)
605 if( pk_alg == MBEDTLS_PK_ECKEY )
606 {
607 if( ( profile->allowed_curves & MBEDTLS_UECC_DP_SECP256R1 ) != 0 )
608 return( 0 );
609
610 return( -1 );
611 }
612#endif /* MBEDTLS_USE_TINYCRYPT */
613
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200614#if defined(MBEDTLS_ECP_C)
615 if( pk_alg == MBEDTLS_PK_ECDSA ||
616 pk_alg == MBEDTLS_PK_ECKEY ||
617 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200618 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200619 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200620
Philippe Antoineb5b25432018-05-11 11:06:29 +0200621 if( gid == MBEDTLS_ECP_DP_NONE )
622 return( -1 );
623
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200624 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
625 return( 0 );
626
627 return( -1 );
628 }
629#endif
630
631 return( -1 );
632}
633
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300634#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200635/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000636 * Return 0 if name matches wildcard, -1 otherwise
637 */
Hanno Becker24926222019-02-21 13:10:55 +0000638static int x509_check_wildcard( char const *cn,
639 size_t cn_len,
640 unsigned char const *buf,
641 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000642{
643 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000644 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000645
646 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000647 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000648 return( -1 );
649
650 for( i = 0; i < cn_len; ++i )
651 {
652 if( cn[i] == '.' )
653 {
654 cn_idx = i;
655 break;
656 }
657 }
658
659 if( cn_idx == 0 )
660 return( -1 );
661
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000662 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
663 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000664 {
665 return( 0 );
666 }
667
668 return( -1 );
669}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300670#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker1f8527f2018-11-02 09:19:16 +0000671
672/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
674 */
675static int x509_get_version( unsigned char **p,
676 const unsigned char *end,
677 int *ver )
678{
679 int ret;
680 size_t len;
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
683 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 {
687 *ver = 0;
688 return( 0 );
689 }
690
Hanno Becker2f472142019-02-12 11:52:10 +0000691 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692 }
693
694 end = *p + len;
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
697 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
699 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 return( MBEDTLS_ERR_X509_INVALID_VERSION +
701 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
703 return( 0 );
704}
705
Hanno Becker843b71a2019-06-25 09:39:21 +0100706#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707/*
708 * Validity ::= SEQUENCE {
709 * notBefore Time,
710 * notAfter Time }
711 */
712static int x509_get_dates( unsigned char **p,
713 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_x509_time *from,
715 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716{
717 int ret;
718 size_t len;
719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
721 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
722 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723
724 end = *p + len;
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 return( ret );
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730 return( ret );
731
732 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 return( MBEDTLS_ERR_X509_INVALID_DATE +
734 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735
736 return( 0 );
737}
Hanno Becker843b71a2019-06-25 09:39:21 +0100738#else /* !MBEDTLS_X509_CRT_REMOVE_TIME */
739static int x509_skip_dates( unsigned char **p,
740 const unsigned char *end )
741{
742 int ret;
743 size_t len;
744
745 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
746 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
747 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
748
Manuel Pégourié-Gonnard0d1db202019-07-30 14:11:25 +0200749 /* skip contents of the sequence */
750 *p += len;
Hanno Becker843b71a2019-06-25 09:39:21 +0100751
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
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300940#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker529f25d2019-05-02 14:48:25 +0100941/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942 * SubjectAltName ::= GeneralNames
943 *
944 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
945 *
946 * GeneralName ::= CHOICE {
947 * otherName [0] OtherName,
948 * rfc822Name [1] IA5String,
949 * dNSName [2] IA5String,
950 * x400Address [3] ORAddress,
951 * directoryName [4] Name,
952 * ediPartyName [5] EDIPartyName,
953 * uniformResourceIdentifier [6] IA5String,
954 * iPAddress [7] OCTET STRING,
955 * registeredID [8] OBJECT IDENTIFIER }
956 *
957 * OtherName ::= SEQUENCE {
958 * type-id OBJECT IDENTIFIER,
959 * value [0] EXPLICIT ANY DEFINED BY type-id }
960 *
961 * EDIPartyName ::= SEQUENCE {
962 * nameAssigner [0] DirectoryString OPTIONAL,
963 * partyName [1] DirectoryString }
964 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000965 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966 */
Hanno Becker5984d302019-02-21 14:46:54 +0000967static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000971 return( mbedtls_asn1_traverse_sequence_of( &p, end,
972 MBEDTLS_ASN1_TAG_CLASS_MASK,
973 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
974 MBEDTLS_ASN1_TAG_VALUE_MASK,
975 2 /* SubjectAlt DNS */,
Hanno Becker529f25d2019-05-02 14:48:25 +0100976 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100977 (void *) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300979#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
981/*
982 * X.509 v3 extensions
983 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000985static int x509_crt_get_ext_cb( void *ctx,
986 int tag,
987 unsigned char *p,
988 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989{
990 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000991 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000993 unsigned char *end, *end_ext_octet;
994 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
995 int is_critical = 0; /* DEFAULT FALSE */
996 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000998 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000999
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001000 /*
1001 * Extension ::= SEQUENCE {
1002 * extnID OBJECT IDENTIFIER,
1003 * critical BOOLEAN DEFAULT FALSE,
1004 * extnValue OCTET STRING }
1005 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001007 end = p + ext_len;
1008
1009 /* Get extension ID */
1010 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
1011 MBEDTLS_ASN1_OID ) ) != 0 )
1012 goto err;
1013
1014 extn_oid.tag = MBEDTLS_ASN1_OID;
1015 extn_oid.p = p;
1016 p += extn_oid.len;
1017
1018 /* Get optional critical */
1019 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
1020 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
1021 goto err;
1022
1023 /* Data should be octet string type */
1024 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1025 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1026 goto err;
1027
1028 end_ext_octet = p + len;
1029 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001031 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
1032 goto err;
1033 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001035 /*
1036 * Detect supported extensions
1037 */
1038 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
1039 if( ret != 0 )
1040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001042 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001044 /* Data is marked as critical: fail */
1045 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
1046 goto err;
1047 }
Hanno Beckerb36a2452019-05-29 14:43:17 +01001048#endif /* MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001049 return( 0 );
1050 }
1051
1052 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +00001053 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001054 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
1055
Hanno Becker21f55672019-02-15 15:27:59 +00001056 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001057 switch( ext_type )
1058 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001059 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001060 {
1061 int ca_istrue;
1062 int max_pathlen;
1063
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001065 ret = x509_get_basic_constraints( &p, end_ext_octet,
1066 &ca_istrue,
1067 &max_pathlen );
1068 if( ret != 0 )
1069 goto err;
1070
Hanno Becker21f55672019-02-15 15:27:59 +00001071 frame->ca_istrue = ca_istrue;
1072 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001074 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001078 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001079 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001080 if( ret != 0 )
1081 goto err;
1082 break;
1083
1084 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001085#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001086 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +00001087 frame->subject_alt_raw.p = p;
1088 frame->subject_alt_raw.len = end_ext_octet - p;
Hanno Becker21f55672019-02-15 15:27:59 +00001089 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1090 MBEDTLS_ASN1_TAG_CLASS_MASK,
1091 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
1092 MBEDTLS_ASN1_TAG_VALUE_MASK,
1093 2 /* SubjectAlt DNS */,
1094 NULL, NULL );
1095 if( ret != 0 )
1096 goto err;
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001097#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 break;
1099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001102 frame->ext_key_usage_raw.p = p;
1103 frame->ext_key_usage_raw.len = end_ext_octet - p;
1104 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001105 {
Hanno Becker21f55672019-02-15 15:27:59 +00001106 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1107 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001108 }
Hanno Becker21f55672019-02-15 15:27:59 +00001109
1110 /* Check structural sanity of extension. */
1111 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1112 0xFF, MBEDTLS_ASN1_OID,
1113 0, 0, NULL, NULL );
1114 if( ret != 0 )
1115 goto err;
1116
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117 break;
1118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001121 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001122 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001123 if( ret != 0 )
1124 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125 break;
1126
1127 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001128 /*
1129 * If this is a non-critical extension, which the oid layer
1130 * supports, but there isn't an X.509 parser for it,
1131 * skip the extension.
1132 */
1133#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1134 if( is_critical )
1135 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1136#endif
1137 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138 }
1139
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001141
1142err:
1143 return( ret );
1144}
1145
Hanno Becker21f55672019-02-15 15:27:59 +00001146static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001147{
1148 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001149 unsigned char *p = frame->v3_ext.p;
1150 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001151
Hanno Becker21f55672019-02-15 15:27:59 +00001152 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001153 return( 0 );
1154
Hanno Becker21f55672019-02-15 15:27:59 +00001155 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001156 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001157 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001158
1159 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1160 return( ret );
1161 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1162 return( ret );
1163
1164 if( ret != 0 )
1165 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1166
1167 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168}
1169
Hanno Becker21f55672019-02-15 15:27:59 +00001170static int x509_crt_parse_frame( unsigned char *start,
1171 unsigned char *end,
1172 mbedtls_x509_crt_frame *frame )
1173{
1174 int ret;
1175 unsigned char *p;
1176 size_t len;
1177
1178 mbedtls_x509_buf tmp;
1179 unsigned char *tbs_start;
1180
1181 mbedtls_x509_buf outer_sig_alg;
1182 size_t inner_sig_alg_len;
1183 unsigned char *inner_sig_alg_start;
1184
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001185 mbedtls_platform_memset( frame, 0, sizeof( *frame ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001186
1187 /*
1188 * Certificate ::= SEQUENCE {
1189 * tbsCertificate TBSCertificate,
1190 * signatureAlgorithm AlgorithmIdentifier,
1191 * signatureValue BIT STRING
1192 * }
1193 *
1194 */
1195 p = start;
1196
1197 frame->raw.p = p;
1198 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1199 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1200 {
1201 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1202 }
1203
1204 /* NOTE: We are currently not checking that the `Certificate`
1205 * structure spans the entire buffer. */
1206 end = p + len;
1207 frame->raw.len = end - frame->raw.p;
1208
1209 /*
1210 * TBSCertificate ::= SEQUENCE { ...
1211 */
1212 frame->tbs.p = p;
1213 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1214 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1215 {
1216 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1217 }
1218 tbs_start = p;
1219
1220 /* Breadth-first parsing: Jump over TBS for now. */
1221 p += len;
1222 frame->tbs.len = p - frame->tbs.p;
1223
1224 /*
1225 * AlgorithmIdentifier ::= SEQUENCE { ...
1226 */
1227 outer_sig_alg.p = p;
1228 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1229 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1230 {
1231 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1232 }
1233 p += len;
1234 outer_sig_alg.len = p - outer_sig_alg.p;
1235
1236 /*
1237 * signatureValue BIT STRING
1238 */
1239 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1240 if( ret != 0 )
1241 return( ret );
1242 frame->sig.p = tmp.p;
1243 frame->sig.len = tmp.len;
1244
1245 /* Check that we consumed the entire `Certificate` structure. */
1246 if( p != end )
1247 {
1248 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1249 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1250 }
1251
1252 /* Parse TBSCertificate structure
1253 *
1254 * TBSCertificate ::= SEQUENCE {
1255 * version [0] EXPLICIT Version DEFAULT v1,
1256 * serialNumber CertificateSerialNumber,
1257 * signature AlgorithmIdentifier,
1258 * issuer Name,
1259 * validity Validity,
1260 * subject Name,
1261 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1262 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1263 * -- If present, version MUST be v2 or v3
1264 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1265 * -- If present, version MUST be v2 or v3
1266 * extensions [3] EXPLICIT Extensions OPTIONAL
1267 * -- If present, version MUST be v3
1268 * }
1269 */
1270 end = frame->tbs.p + frame->tbs.len;
1271 p = tbs_start;
1272
1273 /*
1274 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1275 */
1276 {
1277 int version;
1278 ret = x509_get_version( &p, end, &version );
1279 if( ret != 0 )
1280 return( ret );
1281
1282 if( version < 0 || version > 2 )
1283 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1284
1285 frame->version = version + 1;
1286 }
1287
1288 /*
1289 * CertificateSerialNumber ::= INTEGER
1290 */
1291 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1292 if( ret != 0 )
1293 return( ret );
1294
1295 frame->serial.p = tmp.p;
1296 frame->serial.len = tmp.len;
1297
1298 /*
1299 * signature AlgorithmIdentifier
1300 */
1301 inner_sig_alg_start = p;
1302 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1303 &frame->sig_pk, NULL );
1304 if( ret != 0 )
1305 return( ret );
1306 inner_sig_alg_len = p - inner_sig_alg_start;
1307
1308 frame->sig_alg.p = inner_sig_alg_start;
1309 frame->sig_alg.len = inner_sig_alg_len;
1310
1311 /* Consistency check:
1312 * Inner and outer AlgorithmIdentifier structures must coincide:
1313 *
1314 * Quoting RFC 5280, Section 4.1.1.2:
1315 * This field MUST contain the same algorithm identifier as the
1316 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1317 */
1318 if( outer_sig_alg.len != inner_sig_alg_len ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03001319 mbedtls_platform_memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
Hanno Becker21f55672019-02-15 15:27:59 +00001320 {
1321 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1322 }
1323
1324 /*
1325 * issuer Name
1326 *
1327 * Name ::= CHOICE { -- only one possibility for now --
1328 * rdnSequence RDNSequence }
1329 *
1330 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1331 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001332 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001333
1334 ret = mbedtls_asn1_get_tag( &p, end, &len,
1335 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1336 if( ret != 0 )
1337 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001338 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001339 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001340
Hanno Becker3aa12162019-07-02 16:47:40 +01001341 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001342 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1343 &frame->issuer_raw,
1344 NULL, NULL );
1345 if( ret != 0 )
1346 return( ret );
1347
Hanno Becker21f55672019-02-15 15:27:59 +00001348 /*
1349 * Validity ::= SEQUENCE { ...
1350 */
Hanno Becker843b71a2019-06-25 09:39:21 +01001351#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker21f55672019-02-15 15:27:59 +00001352 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1353 if( ret != 0 )
1354 return( ret );
Hanno Becker843b71a2019-06-25 09:39:21 +01001355#else /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1356 ret = x509_skip_dates( &p, end );
1357 if( ret != 0 )
1358 return( ret );
1359#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker21f55672019-02-15 15:27:59 +00001360
1361 /*
1362 * subject Name
1363 *
1364 * Name ::= CHOICE { -- only one possibility for now --
1365 * rdnSequence RDNSequence }
1366 *
1367 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1368 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001369 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001370
1371 ret = mbedtls_asn1_get_tag( &p, end, &len,
1372 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1373 if( ret != 0 )
1374 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001375 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001376 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001377
Hanno Becker3aa12162019-07-02 16:47:40 +01001378 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001379 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1380 &frame->subject_raw,
1381 NULL, NULL );
1382 if( ret != 0 )
1383 return( ret );
1384
Hanno Becker21f55672019-02-15 15:27:59 +00001385 /*
1386 * SubjectPublicKeyInfo
1387 */
1388 frame->pubkey_raw.p = p;
1389 ret = mbedtls_asn1_get_tag( &p, end, &len,
1390 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1391 if( ret != 0 )
1392 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1393 p += len;
1394 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1395
Hanno Becker97aa4362019-06-08 07:38:20 +01001396 if( frame->version != 1 )
Hanno Becker21f55672019-02-15 15:27:59 +00001397 {
Hanno Beckerd07614c2019-06-25 10:19:58 +01001398#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Beckerfd64f142019-06-07 11:47:12 +01001399 /*
1400 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1401 * -- If present, version shall be v2 or v3
1402 */
Hanno Beckere9084122019-06-07 12:04:39 +01001403 ret = x509_get_uid( &p, end, &frame->issuer_id, 1 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001404 if( ret != 0 )
1405 return( ret );
1406
Hanno Beckerfd64f142019-06-07 11:47:12 +01001407 /*
1408 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1409 * -- If present, version shall be v2 or v3
1410 */
Hanno Beckere9084122019-06-07 12:04:39 +01001411 ret = x509_get_uid( &p, end, &frame->subject_id, 2 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001412 if( ret != 0 )
1413 return( ret );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001414#else /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
1415 ret = x509_skip_uid( &p, end, 1 /* implicit tag */ );
1416 if( ret != 0 )
1417 return( ret );
1418 ret = x509_skip_uid( &p, end, 2 /* implicit tag */ );
1419 if( ret != 0 )
1420 return( ret );
1421#endif /* MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker21f55672019-02-15 15:27:59 +00001422 }
1423
1424 /*
1425 * extensions [3] EXPLICIT Extensions OPTIONAL
1426 * -- If present, version shall be v3
1427 */
1428#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1429 if( frame->version == 3 )
1430#endif
1431 {
1432 if( p != end )
1433 {
1434 ret = mbedtls_asn1_get_tag( &p, end, &len,
1435 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1436 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1437 if( len == 0 )
1438 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1439 if( ret != 0 )
1440 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1441
1442 frame->v3_ext.p = p;
1443 frame->v3_ext.len = len;
1444
1445 p += len;
1446 }
1447
1448 ret = x509_crt_frame_parse_ext( frame );
1449 if( ret != 0 )
1450 return( ret );
1451 }
1452
1453 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1454 if( p != end )
1455 {
1456 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1457 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1458 }
1459
1460 return( 0 );
1461}
1462
Hanno Becker12506232019-05-13 13:53:21 +01001463static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001464 mbedtls_x509_name *subject )
1465{
Hanno Becker1e11f212019-03-04 14:43:43 +00001466 return( mbedtls_x509_get_name( frame->subject_raw.p,
1467 frame->subject_raw.len,
1468 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001469}
1470
Hanno Becker12506232019-05-13 13:53:21 +01001471static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001472 mbedtls_x509_name *issuer )
1473{
Hanno Becker1e11f212019-03-04 14:43:43 +00001474 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1475 frame->issuer_raw.len,
1476 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001477}
1478
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001479#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker12506232019-05-13 13:53:21 +01001480static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001481 mbedtls_x509_sequence *subject_alt )
1482{
1483 int ret;
1484 unsigned char *p = frame->subject_alt_raw.p;
1485 unsigned char *end = p + frame->subject_alt_raw.len;
1486
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001487 mbedtls_platform_memset( subject_alt, 0, sizeof( *subject_alt ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001488
1489 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1490 return( 0 );
1491
1492 ret = x509_get_subject_alt_name( p, end, subject_alt );
1493 if( ret != 0 )
1494 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1495 return( ret );
1496}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001497#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker21f55672019-02-15 15:27:59 +00001498
Hanno Becker12506232019-05-13 13:53:21 +01001499static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001500 mbedtls_x509_sequence *ext_key_usage )
1501{
1502 int ret;
1503 unsigned char *p = frame->ext_key_usage_raw.p;
1504 unsigned char *end = p + frame->ext_key_usage_raw.len;
1505
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001506 mbedtls_platform_memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001507
1508 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1509 return( 0 );
1510
1511 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1512 if( ret != 0 )
1513 {
1514 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1515 return( ret );
1516 }
1517
1518 return( 0 );
1519}
1520
Hanno Becker180f7bf2019-02-28 13:23:38 +00001521#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001522static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1523 mbedtls_pk_context *pk )
1524{
1525 unsigned char *p = frame->pubkey_raw.p;
1526 unsigned char *end = p + frame->pubkey_raw.len;
1527 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1528}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001529#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001530
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531/*
1532 * Parse and fill a single X.509 certificate in DER format
1533 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001534static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1535 const unsigned char *buf,
1536 size_t buflen,
1537 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538{
1539 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001540 mbedtls_x509_crt_frame *frame;
1541 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001542
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545
Hanno Becker21f55672019-02-15 15:27:59 +00001546 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547 {
Hanno Becker21f55672019-02-15 15:27:59 +00001548 crt->raw.p = (unsigned char*) buf;
1549 crt->raw.len = buflen;
1550 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001551 }
1552 else
1553 {
Hanno Becker7b8e11e2019-05-03 12:37:12 +01001554 /* Call mbedtls_calloc with buflen + 1 in order to avoid potential
1555 * return of NULL in case of length 0 certificates, which we want
1556 * to cleanly fail with MBEDTLS_ERR_X509_INVALID_FORMAT in the
1557 * core parsing routine, but not here. */
1558 crt->raw.p = mbedtls_calloc( 1, buflen + 1 );
Hanno Becker21f55672019-02-15 15:27:59 +00001559 if( crt->raw.p == NULL )
1560 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1561 crt->raw.len = buflen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001562 mbedtls_platform_memcpy( crt->raw.p, buf, buflen );
Hanno Becker21f55672019-02-15 15:27:59 +00001563
1564 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001565 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001566
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001567 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1568 if( cache == NULL )
1569 {
1570 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1571 goto exit;
1572 }
1573 crt->cache = cache;
1574 x509_crt_cache_init( cache );
1575
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001576#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1577
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001578 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001579 if( ret != 0 )
1580 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581
Hanno Becker76428352019-03-05 15:29:23 +00001582 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001583
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001584#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001586 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1587 if( frame == NULL )
1588 {
1589 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1590 goto exit;
1591 }
1592 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001593
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001594 ret = x509_crt_parse_frame( crt->raw.p,
1595 crt->raw.p + crt->raw.len,
1596 frame );
1597 if( ret != 0 )
1598 goto exit;
1599
Hanno Becker21f55672019-02-15 15:27:59 +00001600 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1601 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001602 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1603 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1604 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1605 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001606#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Becker38f0cb42019-03-04 15:13:45 +00001607 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1608 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001609#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001610 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1611 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1612 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Becker843b71a2019-06-25 09:39:21 +01001613
1614#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001615 crt->valid_from = frame->valid_from;
1616 crt->valid_to = frame->valid_to;
Hanno Becker843b71a2019-06-25 09:39:21 +01001617#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1618
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001619 crt->version = frame->version;
1620 crt->ca_istrue = frame->ca_istrue;
1621 crt->max_pathlen = frame->max_pathlen;
1622 crt->ext_types = frame->ext_types;
1623 crt->key_usage = frame->key_usage;
1624 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625
1626 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001627 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630 {
Hanno Becker21f55672019-02-15 15:27:59 +00001631 /* sig_oid: Previously, needed for convenience in
1632 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001633 unsigned char *tmp = frame->sig_alg.p;
1634 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001635 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636
Hanno Becker21f55672019-02-15 15:27:59 +00001637 ret = mbedtls_x509_get_alg( &tmp, end,
1638 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639 if( ret != 0 )
1640 {
Hanno Becker21f55672019-02-15 15:27:59 +00001641 /* This should never happen, because we check
1642 * the sanity of the AlgorithmIdentifier structure
1643 * during frame parsing. */
1644 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1645 goto exit;
1646 }
1647 crt->sig_oid = sig_oid;
1648
1649 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001650 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001651 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1652 &crt->sig_md, &crt->sig_pk,
1653 &crt->sig_opts );
1654 if( ret != 0 )
1655 {
1656 /* Again, this should never happen. */
1657 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1658 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 }
1660 }
1661
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001662 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001663 if( ret != 0 )
1664 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001665
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001666 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001667 if( ret != 0 )
1668 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001670 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001671 if( ret != 0 )
1672 goto exit;
1673
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001674#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001675 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001676 if( ret != 0 )
1677 goto exit;
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001678#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker21f55672019-02-15 15:27:59 +00001679
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001680 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1681 if( ret != 0 )
1682 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001683#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001684
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001685 /* Currently, we accept DER encoded CRTs with trailing garbage
1686 * and promise to not account for the garbage in the `raw` field.
1687 *
1688 * Note that this means that `crt->raw.len` is not necessarily the
1689 * full size of the heap buffer allocated at `crt->raw.p` in case
1690 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1691 * need the size, and the garbage data doesn't need zeroization. */
1692 crt->raw.len = frame->raw.len;
1693
1694 cache->pk_raw = frame->pubkey_raw;
1695
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001696 /* Free the frame before parsing the public key to
1697 * keep peak RAM usage low. This is slightly inefficient
1698 * because the frame will need to be parsed again on the
1699 * first usage of the CRT, but that seems acceptable.
1700 * As soon as the frame gets used multiple times, it
1701 * will be cached by default. */
1702 x509_crt_cache_clear_frame( crt->cache );
1703
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001704 /* The cache just references the PK structure from the legacy
1705 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001706 * the cache.
1707 *
1708 * We're not actually using the parsed PK context here;
1709 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001710 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001711 if( ret != 0 )
1712 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001713 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001714
1715exit:
1716 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001718
Hanno Becker21f55672019-02-15 15:27:59 +00001719 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001720}
1721
1722/*
1723 * Parse one X.509 certificate in DER format from a buffer and add them to a
1724 * chained list
1725 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001726static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1727 const unsigned char *buf,
1728 size_t buflen,
1729 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730{
1731 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001733
1734 /*
1735 * Check for valid input
1736 */
1737 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739
Hanno Becker371e0e42019-02-25 18:08:59 +00001740 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741 {
1742 prev = crt;
1743 crt = crt->next;
1744 }
1745
1746 /*
1747 * Add new certificate on the end of the chain if needed.
1748 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001749 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001751 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752
1753 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001754 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755
1756 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759 }
1760
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001761 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001762 {
1763 if( prev )
1764 prev->next = NULL;
1765
1766 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768
1769 return( ret );
1770 }
1771
1772 return( 0 );
1773}
1774
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001775int mbedtls_x509_crt_parse_der_nocopy( 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, 0 ) );
1780}
1781
1782int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1783 const unsigned char *buf,
1784 size_t buflen )
1785{
1786 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1787}
1788
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001790 * Parse one or more PEM certificates from a buffer and add them to the chained
1791 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001792 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001793int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1794 const unsigned char *buf,
1795 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001796{
Janos Follath98e28a72016-05-31 14:03:54 +01001797#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001798 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001800#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801
1802 /*
1803 * Check for valid input
1804 */
1805 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001807
1808 /*
1809 * Determine buffer content. Buffer contains either one DER certificate or
1810 * one or more PEM certificates.
1811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001813 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001814 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001817 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1820 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001821#else
1822 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1823#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#if defined(MBEDTLS_PEM_PARSE_C)
1826 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827 {
1828 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001830
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001831 /* 1 rather than 0 since the terminating NULL byte is counted in */
1832 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833 {
1834 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001836
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001837 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001839 "-----BEGIN CERTIFICATE-----",
1840 "-----END CERTIFICATE-----",
1841 buf, NULL, 0, &use_len );
1842
1843 if( ret == 0 )
1844 {
1845 /*
1846 * Was PEM encoded
1847 */
1848 buflen -= use_len;
1849 buf += use_len;
1850 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001852 {
1853 return( ret );
1854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001858
1859 /*
1860 * PEM header and footer were found
1861 */
1862 buflen -= use_len;
1863 buf += use_len;
1864
1865 if( first_error == 0 )
1866 first_error = ret;
1867
Paul Bakker5a5fa922014-09-26 14:53:04 +02001868 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001869 continue;
1870 }
1871 else
1872 break;
1873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001877
1878 if( ret != 0 )
1879 {
1880 /*
1881 * Quit parsing on a memory error
1882 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001883 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001884 return( ret );
1885
1886 if( first_error == 0 )
1887 first_error = ret;
1888
1889 total_failed++;
1890 continue;
1891 }
1892
1893 success = 1;
1894 }
1895 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001896
1897 if( success )
1898 return( total_failed );
1899 else if( first_error )
1900 return( first_error );
1901 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001903#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001904}
1905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001907/*
1908 * Load one or more certificates and add them to the chained list
1909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001911{
1912 int ret;
1913 size_t n;
1914 unsigned char *buf;
1915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001917 return( ret );
1918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001920
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001921 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001923
1924 return( ret );
1925}
1926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928{
1929 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001930#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931 int w_ret;
1932 WCHAR szDir[MAX_PATH];
1933 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001934 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001935 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001936
Paul Bakker9af723c2014-05-01 13:03:14 +02001937 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001938 HANDLE hFind;
1939
1940 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001942
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001943 mbedtls_platform_memset( szDir, 0, sizeof(szDir) );
1944 mbedtls_platform_memset( filename, 0, MAX_PATH );
Teppo Järvelin91d79382019-10-02 09:09:31 +03001945 mbedtls_platform_memcpy( filename, path, len );
Paul Bakker9af723c2014-05-01 13:03:14 +02001946 filename[len++] = '\\';
1947 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948 filename[len++] = '*';
1949
Simon B3c6b18d2016-11-03 01:11:37 +00001950 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001951 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001952 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001954
1955 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001956 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001958
1959 len = MAX_PATH - len;
1960 do
1961 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001962 mbedtls_platform_memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001963
1964 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1965 continue;
1966
Paul Bakker9af723c2014-05-01 13:03:14 +02001967 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001968 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001969 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001970 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001971 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001972 {
1973 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1974 goto cleanup;
1975 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001978 if( w_ret < 0 )
1979 ret++;
1980 else
1981 ret += w_ret;
1982 }
1983 while( FindNextFileW( hFind, &file_data ) != 0 );
1984
Paul Bakker66d5d072014-06-17 16:39:18 +02001985 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987
Ron Eldor36d90422017-01-09 15:09:16 +02001988cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001989 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001990#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001991 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001992 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001993 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001994 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001995 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001996 DIR *dir = opendir( path );
1997
Paul Bakker66d5d072014-06-17 16:39:18 +02001998 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002000
Ron Eldor63140682017-01-09 19:27:59 +02002001#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02002002 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02002003 {
2004 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002005 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02002006 }
Ron Eldor63140682017-01-09 19:27:59 +02002007#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002008
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01002009 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002010 {
Andres AGf9113192016-09-02 14:06:04 +01002011 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
2012 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002013
Andres AGf9113192016-09-02 14:06:04 +01002014 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002015 {
Andres AGf9113192016-09-02 14:06:04 +01002016 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
2017 goto cleanup;
2018 }
2019 else if( stat( entry_name, &sb ) == -1 )
2020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002022 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023 }
2024
2025 if( !S_ISREG( sb.st_mode ) )
2026 continue;
2027
2028 // Ignore parse errors
2029 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002031 if( t_ret < 0 )
2032 ret++;
2033 else
2034 ret += t_ret;
2035 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002036
2037cleanup:
Andres AGf9113192016-09-02 14:06:04 +01002038 closedir( dir );
2039
Ron Eldor63140682017-01-09 19:27:59 +02002040#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02002041 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02002043#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002044
Paul Bakkerbe089b02013-10-14 15:51:50 +02002045#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002046
2047 return( ret );
2048}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050
Hanno Becker08d34122019-06-25 09:42:57 +01002051typedef struct mbedtls_x509_crt_sig_info
2052{
2053 mbedtls_md_type_t sig_md;
2054 mbedtls_pk_type_t sig_pk;
2055 void *sig_opts;
Hanno Becker08d34122019-06-25 09:42:57 +01002056 size_t crt_hash_len;
2057 mbedtls_x509_buf_raw sig;
2058 mbedtls_x509_buf_raw issuer_raw;
Teppo Järvelinc3e57162019-08-30 11:25:15 +03002059 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
Hanno Becker08d34122019-06-25 09:42:57 +01002060} mbedtls_x509_crt_sig_info;
2061
2062static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2063{
2064#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2065 mbedtls_free( info->sig_opts );
2066#else
2067 ((void) info);
2068#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2069}
2070
2071static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2072 mbedtls_x509_crt_sig_info *info )
2073{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01002074 mbedtls_md_handle_t md_info;
Hanno Becker08d34122019-06-25 09:42:57 +01002075
2076 md_info = mbedtls_md_info_from_type( frame->sig_md );
2077 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2078 info->crt_hash ) != 0 )
2079 {
2080 /* Note: this can't happen except after an internal error */
2081 return( -1 );
2082 }
2083
2084 info->crt_hash_len = mbedtls_md_get_size( md_info );
2085
2086 /* Make sure that this function leaves the target structure
2087 * ready to be freed, regardless of success of failure. */
2088 info->sig_opts = NULL;
2089
2090#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2091 {
2092 int ret;
2093 unsigned char *alg_start = frame->sig_alg.p;
2094 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2095
2096 /* Get signature options -- currently only
2097 * necessary for RSASSA-PSS. */
2098 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2099 &info->sig_pk, &info->sig_opts );
2100 if( ret != 0 )
2101 {
2102 /* Note: this can't happen except after an internal error */
2103 return( -1 );
2104 }
2105 }
2106#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2107 info->sig_md = frame->sig_md;
2108 info->sig_pk = frame->sig_pk;
2109#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2110
2111 info->issuer_raw = frame->issuer_raw;
2112 info->sig = frame->sig;
2113 return( 0 );
2114}
2115
Hanno Becker02a21932019-06-10 15:08:43 +01002116#if !defined(MBEDTLS_X509_REMOVE_INFO)
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002117#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002118static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002120{
2121 size_t i;
2122 size_t n = *size;
2123 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002125 const char *sep = "";
2126 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002127
2128 while( cur != NULL )
2129 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002130 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002131 {
2132 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002133 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002134 }
2135
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002136 n -= cur->buf.len + sep_len;
2137 for( i = 0; i < sep_len; i++ )
2138 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002139 for( i = 0; i < cur->buf.len; i++ )
2140 *p++ = cur->buf.p[i];
2141
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002142 sep = ", ";
2143 sep_len = 2;
2144
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002145 cur = cur->next;
2146 }
2147
2148 *p = '\0';
2149
2150 *size = n;
2151 *buf = p;
2152
2153 return( 0 );
2154}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002155#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002156
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002157#define PRINT_ITEM(i) \
2158 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002160 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002161 sep = ", "; \
2162 }
2163
2164#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002165 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002166 PRINT_ITEM( name );
2167
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002168static int x509_info_cert_type( char **buf, size_t *size,
2169 unsigned char ns_cert_type )
2170{
2171 int ret;
2172 size_t n = *size;
2173 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002174 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002177 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2179 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2180 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002181 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2182 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2183 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002184
2185 *size = n;
2186 *buf = p;
2187
2188 return( 0 );
2189}
2190
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002191#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002192 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002193 PRINT_ITEM( name );
2194
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002195static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002196 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002197{
2198 int ret;
2199 size_t n = *size;
2200 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002201 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2204 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002205 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2206 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2207 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2209 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002210 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2211 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002212
2213 *size = n;
2214 *buf = p;
2215
2216 return( 0 );
2217}
2218
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002219static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002221{
2222 int ret;
2223 const char *desc;
2224 size_t n = *size;
2225 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002227 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002228
2229 while( cur != NULL )
2230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002232 desc = "???";
2233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002235 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002236
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002237 sep = ", ";
2238
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002239 cur = cur->next;
2240 }
2241
2242 *size = n;
2243 *buf = p;
2244
2245 return( 0 );
2246}
2247
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002248/*
2249 * Return an informational string about the certificate.
2250 */
Teppo Järvelinffaba552019-09-03 12:33:16 +03002251#define BEFORE_COLON_CRT 18
2252#define BC_CRT "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002254 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002255{
2256 int ret;
2257 size_t n;
2258 char *p;
Teppo Järvelinffaba552019-09-03 12:33:16 +03002259 char key_size_str[BEFORE_COLON_CRT];
Hanno Becker5226c532019-02-27 17:38:40 +00002260 mbedtls_x509_crt_frame frame;
2261 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002262
Hanno Becker5226c532019-02-27 17:38:40 +00002263 mbedtls_x509_name *issuer = NULL, *subject = NULL;
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002264 mbedtls_x509_sequence *ext_key_usage = NULL;
2265#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
2266 mbedtls_x509_sequence *subject_alt_names = NULL;
2267#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
2268
Hanno Becker4f869ed2019-02-24 16:47:57 +00002269 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002270
2271 p = buf;
2272 n = size;
2273
Hanno Becker4f869ed2019-02-24 16:47:57 +00002274 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002275 mbedtls_pk_init( &pk );
2276
2277 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002278 {
2279 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002280 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Janos Follath98e28a72016-05-31 14:03:54 +01002281
2282 return( (int) ( size - n ) );
2283 }
2284
Hanno Becker5226c532019-02-27 17:38:40 +00002285 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002286 if( ret != 0 )
2287 {
2288 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2289 goto cleanup;
2290 }
2291
Hanno Becker5226c532019-02-27 17:38:40 +00002292 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002293 if( ret != 0 )
2294 {
2295 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2296 goto cleanup;
2297 }
2298
Hanno Becker5226c532019-02-27 17:38:40 +00002299 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002300 if( ret != 0 )
2301 {
2302 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2303 goto cleanup;
2304 }
2305
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002306#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker5226c532019-02-27 17:38:40 +00002307 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002308 if( ret != 0 )
2309 {
2310 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2311 goto cleanup;
2312 }
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002313#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker4f869ed2019-02-24 16:47:57 +00002314
Hanno Becker5226c532019-02-27 17:38:40 +00002315 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002316 if( ret != 0 )
2317 {
2318 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2319 goto cleanup;
2320 }
2321
Hanno Becker5226c532019-02-27 17:38:40 +00002322 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2323 if( ret != 0 )
2324 {
2325 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2326 goto cleanup;
2327 }
2328
2329 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002330 if( ret != 0 )
2331 {
2332 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2333 goto cleanup;
2334 }
2335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002337 prefix, frame.version );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002338 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339
Hanno Becker4f869ed2019-02-24 16:47:57 +00002340 {
2341 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002342 serial.p = frame.serial.p;
2343 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002344 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2345 prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002346 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002347 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002348 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002349 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002352 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002353 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002354 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002357 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002358 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002359 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002360
Hanno Becker843b71a2019-06-25 09:39:21 +01002361#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002363 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002364 frame.valid_from.year, frame.valid_from.mon,
2365 frame.valid_from.day, frame.valid_from.hour,
2366 frame.valid_from.min, frame.valid_from.sec );
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002370 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002371 frame.valid_to.year, frame.valid_to.mon,
2372 frame.valid_to.day, frame.valid_to.hour,
2373 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002374 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker843b71a2019-06-25 09:39:21 +01002375#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002378 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002379
Hanno Becker83cd8672019-02-21 17:13:46 +00002380 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2381 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002382 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002383
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002384 /* Key size */
Teppo Järvelinffaba552019-09-03 12:33:16 +03002385 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON_CRT,
Hanno Becker5226c532019-02-27 17:38:40 +00002386 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002387 {
2388 return( ret );
2389 }
2390
Teppo Järvelinffaba552019-09-03 12:33:16 +03002391 ret = mbedtls_snprintf( p, n, "\n%s%-" BC_CRT "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002392 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002393 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002394
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002395 /*
2396 * Optional extensions
2397 */
2398
Hanno Becker5226c532019-02-27 17:38:40 +00002399 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002402 frame.ca_istrue ? "true" : "false" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002403 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002404
Hanno Becker5226c532019-02-27 17:38:40 +00002405 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002406 {
Hanno Becker5226c532019-02-27 17:38:40 +00002407 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002408 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002409 }
2410 }
2411
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002412#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker5226c532019-02-27 17:38:40 +00002413 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002416 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002417
2418 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002419 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002420 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002421 }
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002422#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002423
Hanno Becker5226c532019-02-27 17:38:40 +00002424 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002427 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002428
Hanno Becker5226c532019-02-27 17:38:40 +00002429 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002430 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002431 }
2432
Hanno Becker5226c532019-02-27 17:38:40 +00002433 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002436 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002437
Hanno Becker5226c532019-02-27 17:38:40 +00002438 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002439 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002440 }
2441
Hanno Becker5226c532019-02-27 17:38:40 +00002442 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002445 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002446
2447 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002448 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002449 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002450 }
2451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002453 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002454
Hanno Becker4f869ed2019-02-24 16:47:57 +00002455 ret = (int) ( size - n );
2456
2457cleanup:
2458
Hanno Becker4f869ed2019-02-24 16:47:57 +00002459 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002460 mbedtls_pk_free( &pk );
2461 mbedtls_x509_name_free( issuer );
2462 mbedtls_x509_name_free( subject );
2463 mbedtls_x509_sequence_free( ext_key_usage );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002464#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker5226c532019-02-27 17:38:40 +00002465 mbedtls_x509_sequence_free( subject_alt_names );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002466#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker4f869ed2019-02-24 16:47:57 +00002467
2468 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002469}
2470
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002471struct x509_crt_verify_string {
2472 int code;
2473 const char *string;
2474};
2475
2476static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002477 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002478 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2479 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2480 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2481 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2482 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002483 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2484 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2485 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002486 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002487 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2488 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2489 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2490 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002491 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2492 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2493 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2494 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2495 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2496 { 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 +01002497 { 0, NULL }
2498};
2499
2500int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002501 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002502{
2503 int ret;
2504 const struct x509_crt_verify_string *cur;
2505 char *p = buf;
2506 size_t n = size;
2507
2508 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2509 {
2510 if( ( flags & cur->code ) == 0 )
2511 continue;
2512
2513 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002514 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002515 flags ^= cur->code;
2516 }
2517
2518 if( flags != 0 )
2519 {
2520 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2521 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002522 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002523 }
2524
2525 return( (int) ( size - n ) );
2526}
Hanno Becker02a21932019-06-10 15:08:43 +01002527#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002530static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2531 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002532{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002533 unsigned int usage_must, usage_may;
2534 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2535 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2536
2537 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2538 return( 0 );
2539
2540 usage_must = usage & ~may_mask;
2541
2542 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2543 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2544
2545 usage_may = usage & may_mask;
2546
2547 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002549
2550 return( 0 );
2551}
Hanno Becker45eedf12019-02-25 13:55:33 +00002552
2553int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2554 unsigned int usage )
2555{
2556 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002557 mbedtls_x509_crt_frame const *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002558 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002559 if( ret != 0 )
2560 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2561
2562 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002563 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002564
2565 return( ret );
2566}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002567#endif
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002570typedef struct
2571{
2572 const char *oid;
2573 size_t oid_len;
2574} x509_crt_check_ext_key_usage_cb_ctx_t;
2575
2576static int x509_crt_check_ext_key_usage_cb( void *ctx,
2577 int tag,
2578 unsigned char *data,
2579 size_t data_len )
2580{
2581 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2582 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2583 ((void) tag);
2584
2585 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2586 data, data_len ) == 0 )
2587 {
2588 return( 1 );
2589 }
2590
Teppo Järvelin61f412e2019-10-03 12:25:22 +03002591 if( data_len == cb_ctx->oid_len && mbedtls_platform_memcmp( data, cb_ctx->oid,
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002592 data_len ) == 0 )
2593 {
2594 return( 1 );
2595 }
2596
2597 return( 0 );
2598}
2599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002601 const char *usage_oid,
2602 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002603{
Hanno Beckere1956af2019-02-21 14:28:12 +00002604 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002605 mbedtls_x509_crt_frame const *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002606 unsigned ext_types;
2607 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002608 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002609
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002610 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002611 if( ret != 0 )
2612 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2613
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002614 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002615 ext_types = frame->ext_types;
2616 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2617 {
2618 p = frame->ext_key_usage_raw.p;
2619 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002620
Hanno Beckere9718b42019-02-25 18:11:42 +00002621 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2622 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2623 x509_crt_check_ext_key_usage_cb,
2624 &cb_ctx );
2625 if( ret == 1 )
2626 ret = 0;
2627 else
2628 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2629 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002630
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002631 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002632 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002633}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002637/*
2638 * Return 1 if the certificate is revoked, or 0 otherwise.
2639 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002640static int x509_serial_is_revoked( unsigned char const *serial,
2641 size_t serial_len,
2642 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002645
2646 while( cur != NULL && cur->serial.len != 0 )
2647 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002648 if( serial_len == cur->serial.len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +03002649 mbedtls_platform_memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002650 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002651 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002652 return( 1 );
2653 }
2654
2655 cur = cur->next;
2656 }
2657
2658 return( 0 );
2659}
2660
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002661int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2662 const mbedtls_x509_crl *crl )
2663{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002664 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002665 mbedtls_x509_crt_frame const *frame;
Hanno Becker79ae5b62019-02-25 18:12:00 +00002666
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002667 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002668 if( ret != 0 )
2669 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2670
2671 ret = x509_serial_is_revoked( frame->serial.p,
2672 frame->serial.len,
2673 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002674 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002675 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002676}
2677
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002678/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002679 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002680 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002681 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002682static int x509_crt_verifycrl( unsigned char *crt_serial,
2683 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002684 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002685 mbedtls_x509_crl *crl_list,
2686 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002687{
Hanno Beckerbb266132019-02-25 18:12:46 +00002688 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002689 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +01002691 mbedtls_md_handle_t md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002692 mbedtls_x509_buf_raw ca_subject;
2693 mbedtls_pk_context *pk;
2694 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002695
Hanno Beckerbb266132019-02-25 18:12:46 +00002696 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002697 return( flags );
2698
Hanno Beckerbb266132019-02-25 18:12:46 +00002699 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002700 mbedtls_x509_crt_frame const *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002701 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002702 if( ret != 0 )
2703 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2704
2705 ca_subject = ca->subject_raw;
2706
2707 can_sign = 0;
2708 if( x509_crt_check_key_usage_frame( ca,
2709 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2710 {
2711 can_sign = 1;
2712 }
2713
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002714 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002715 }
2716
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002717 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002718 if( ret != 0 )
2719 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2720
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002721 while( crl_list != NULL )
2722 {
2723 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002724 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002725 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002726 {
2727 crl_list = crl_list->next;
2728 continue;
2729 }
2730
2731 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002732 * Check if the CA is configured to sign CRLs
2733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002735 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002738 break;
2739 }
2740#endif
2741
2742 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002743 * Check if CRL is correctly signed by the trusted CA
2744 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002745 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2746 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2747
2748 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2749 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002752 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002753 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002754 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002756 break;
2757 }
2758
Hanno Beckerbb266132019-02-25 18:12:46 +00002759 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002760 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002761
Hanno Beckerbb266132019-02-25 18:12:46 +00002762 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002764 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002767 break;
2768 }
2769
2770 /*
2771 * Check for validity of CRL (Do not drop out)
2772 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002773 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002775
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002776 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002777 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002778
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002779 /*
2780 * Check if certificate is revoked
2781 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002782 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2783 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002786 break;
2787 }
2788
2789 crl_list = crl_list->next;
2790 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002791
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002792 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002793 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002796
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002797/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002798 * Check the signature of a certificate by its parent
2799 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002800static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002801 mbedtls_x509_crt *parent,
2802 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002803{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002804 int ret;
2805 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002806
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002807 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002808 if( ret != 0 )
2809 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2810
2811 /* Skip expensive computation on obvious mismatch */
2812 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002813 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002814 ret = -1;
2815 goto exit;
2816 }
2817
2818#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2819 ((void) rs_ctx);
2820#else
2821 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2822 {
2823 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002824 sig_info->sig_md,
2825 sig_info->crt_hash, sig_info->crt_hash_len,
2826 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002827 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002828 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002829 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002830#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002831 {
2832 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2833 sig_info->sig_opts,
2834 pk,
2835 sig_info->sig_md,
2836 sig_info->crt_hash, sig_info->crt_hash_len,
2837 sig_info->sig.p, sig_info->sig.len );
2838 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002839
Hanno Beckere449e2d2019-02-25 14:45:31 +00002840exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002841 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002842 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002843}
2844
2845/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002846 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2847 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002848 *
2849 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002850 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002851static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2852 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002853 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002854{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002855 int need_ca_bit;
2856
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002857 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002858 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2859 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002860 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002861 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002862 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002863 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002864
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002865 /* Parent must have the basicConstraints CA bit set as a general rule */
2866 need_ca_bit = 1;
2867
2868 /* Exception: v1/v2 certificates that are locally trusted. */
2869 if( top && parent->version < 3 )
2870 need_ca_bit = 0;
2871
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002872 if( need_ca_bit && ! parent->ca_istrue )
2873 return( -1 );
2874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002876 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002877 x509_crt_check_key_usage_frame( parent,
2878 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002879 {
2880 return( -1 );
2881 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002882#endif
2883
2884 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002885}
2886
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002887/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002888 * Find a suitable parent for child in candidates, or return NULL.
2889 *
2890 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002891 * 1. subject name matches child's issuer
2892 * 2. if necessary, the CA bit is set and key usage allows signing certs
2893 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002894 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002895 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002896 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002897 * If there's a suitable candidate which is also time-valid, return the first
2898 * such. Otherwise, return the first suitable candidate (or NULL if there is
2899 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002900 *
2901 * The rationale for this rule is that someone could have a list of trusted
2902 * roots with two versions on the same root with different validity periods.
2903 * (At least one user reported having such a list and wanted it to just work.)
2904 * The reason we don't just require time-validity is that generally there is
2905 * only one version, and if it's expired we want the flags to state that
2906 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002907 *
2908 * The rationale for rule 3 (signature for trusted roots) is that users might
2909 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002910 * way we select the correct one is by checking the signature (as we don't
2911 * rely on key identifier extensions). (This is one way users might choose to
2912 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002913 *
2914 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002915 * - [in] child: certificate for which we're looking for a parent
2916 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002917 * - [out] r_parent: parent found (or NULL)
2918 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002919 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2920 * of the chain, 0 otherwise
2921 * - [in] path_cnt: number of intermediates seen so far
2922 * - [in] self_cnt: number of self-signed intermediates seen so far
2923 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002924 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002925 *
2926 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002927 * - 0 on success
2928 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002929 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002930static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002931 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002932 mbedtls_x509_crt *candidates,
2933 mbedtls_x509_crt **r_parent,
2934 int *r_signature_is_good,
2935 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002936 unsigned path_cnt,
2937 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002938 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002939{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002940 int ret;
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01002941 volatile int ret_fi;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002942 mbedtls_x509_crt *parent_crt;
2943 int signature_is_good;
2944
2945#if defined(MBEDTLS_HAVE_TIME_DATE)
2946 mbedtls_x509_crt *fallback_parent;
2947 int fallback_signature_is_good;
2948#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002949
2950#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002951 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002952 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2953 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002954 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002955 parent_crt = rs_ctx->parent;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002956#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002957 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002958 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002959#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002960
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002961 /* clear saved state */
2962 rs_ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002963#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002964 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002965 rs_ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002966#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002967
2968 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002969 goto check_signature;
2970 }
2971#endif
2972
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002973#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002974 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002975 fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002976#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002977
Hanno Becker43bf9002019-02-25 14:46:49 +00002978 for( parent_crt = candidates; parent_crt != NULL;
2979 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002980 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002981 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002982
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002983#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2984check_signature:
2985#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002986
2987 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002988 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002989 mbedtls_x509_crt_frame const *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002990
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002991 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002992 if( ret != 0 )
2993 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002994
Hanno Becker843b71a2019-06-25 09:39:21 +01002995#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker040c5642019-06-10 11:14:24 +01002996 if( !mbedtls_x509_time_is_past( &parent->valid_to ) &&
2997 !mbedtls_x509_time_is_future( &parent->valid_from ) )
Manuel Pégourié-Gonnardf1358ac2019-07-30 16:03:06 +02002998#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker43bf9002019-02-25 14:46:49 +00002999 {
3000 parent_valid = 1;
3001 }
3002
3003 /* basic parenting skills (name, CA bit, key usage) */
3004 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
3005 parent_match = 1;
3006
3007 /* +1 because the stored max_pathlen is 1 higher
3008 * than the actual value */
3009 if( !( parent->max_pathlen > 0 &&
3010 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
3011 {
3012 path_len_ok = 1;
3013 }
3014
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003015 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00003016 }
3017
3018 if( parent_match == 0 || path_len_ok == 0 )
3019 continue;
3020
3021 /* Signature */
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003022 ret_fi = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003023
3024#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003025 if( rs_ctx != NULL && ret_fi == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003026 {
3027 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00003028 rs_ctx->parent = parent_crt;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003029#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003030 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003031 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003032#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003033
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003034 return( ret_fi );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003035 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003036#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003037
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003038 signature_is_good = 0;
3039 if( ret_fi == 0 )
3040 {
3041 mbedtls_platform_enforce_volatile_reads();
3042 if( ret_fi == 0 )
3043 signature_is_good = 1;
3044 }
3045
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003046 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02003047 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02003048
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003049 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00003050 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003051 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003052#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003053 if( fallback_parent == NULL )
3054 {
Hanno Becker43bf9002019-02-25 14:46:49 +00003055 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003056 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003057 }
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003058#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003059
3060 continue;
3061 }
3062
Manuel Pégourié-Gonnard8abd0a02019-09-10 11:24:29 +02003063 *r_parent = parent_crt;
Andy Gross3fc6f9d2019-01-30 10:25:53 -06003064 *r_signature_is_good = signature_is_good;
3065
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003066 break;
3067 }
3068
Manuel Pégourié-Gonnard8abd0a02019-09-10 11:24:29 +02003069 if( parent_crt == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003070 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003071#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003072 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003073 *r_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003074#else /* MBEDTLS_HAVE_TIME_DATE */
3075 *r_parent = NULL;
3076#endif /* !MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003077 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003078
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003079 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003080}
3081
3082/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003083 * Find a parent in trusted CAs or the provided chain, or return NULL.
3084 *
3085 * Searches in trusted CAs first, and return the first suitable parent found
3086 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003087 *
3088 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003089 * - [in] child: certificate for which we're looking for a parent, followed
3090 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003091 * - [in] trust_ca: list of locally trusted certificates
3092 * - [out] parent: parent found (or NULL)
3093 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
3094 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
3095 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
3096 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003097 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003098 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003099 *
3100 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003101 * - 0 on success
3102 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003103 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003104static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00003105 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00003106 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003107 mbedtls_x509_crt *trust_ca,
3108 mbedtls_x509_crt **parent,
3109 int *parent_is_trusted,
3110 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003111 unsigned path_cnt,
3112 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003113 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003114{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003115 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003116 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003117
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003118 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003119
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003120#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003121 /* restore then clear saved state if we have some stored */
3122 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
3123 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003124 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003125 rs_ctx->parent_is_trusted = -1;
3126 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003127#endif
3128
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003129 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00003130 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003131
Hanno Becker5299cf82019-02-25 13:50:41 +00003132 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003133 parent, signature_is_good,
3134 *parent_is_trusted,
3135 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003136
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003137#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003138 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3139 {
3140 /* save state */
3141 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003142 return( ret );
3143 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003144#else
3145 (void) ret;
3146#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003147
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003148 /* stop here if found or already in second iteration */
3149 if( *parent != NULL || *parent_is_trusted == 0 )
3150 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003151
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003152 /* prepare second iteration */
3153 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003154 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003155
3156 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01003157 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003158 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02003159 *parent_is_trusted = 0;
3160 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003161 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003162
3163 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003164}
3165
3166/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003167 * Check if an end-entity certificate is locally trusted
3168 *
3169 * Currently we require such certificates to be self-signed (actually only
3170 * check for self-issued as self-signatures are not checked)
3171 */
3172static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003173 mbedtls_x509_crt_frame const *crt,
3174 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003175{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003176 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003177
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003178 /* look for an exact match with trusted cert */
3179 for( cur = trust_ca; cur != NULL; cur = cur->next )
3180 {
3181 if( crt->raw.len == cur->raw.len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +03003182 mbedtls_platform_memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003183 {
3184 return( 0 );
3185 }
3186 }
3187
3188 /* too bad */
3189 return( -1 );
3190}
3191
Hanno Becker8d6d3202019-08-16 17:18:15 +01003192#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3193
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003194/*
Hanno Beckeradc282a2019-08-16 17:14:25 +01003195 * Reset (init or clear) a verify_chain
3196 */
3197static void x509_crt_verify_chain_reset(
3198 mbedtls_x509_crt_verify_chain *ver_chain )
3199{
3200 size_t i;
3201
3202 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
3203 {
3204 ver_chain->items[i].crt = NULL;
3205 ver_chain->items[i].flags = (uint32_t) -1;
3206 }
3207
3208 ver_chain->len = 0;
3209}
3210
3211/*
3212 * Merge the flags for all certs in the chain, after calling callback
3213 */
3214static int x509_crt_verify_chain_get_flags(
3215 const mbedtls_x509_crt_verify_chain *ver_chain,
3216 uint32_t *flags,
3217 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3218 void *p_vrfy )
3219{
3220 int ret;
3221 unsigned i;
3222 uint32_t cur_flags;
3223 const mbedtls_x509_crt_verify_chain_item *cur;
3224
3225 for( i = ver_chain->len; i != 0; --i )
3226 {
3227 cur = &ver_chain->items[i-1];
3228 cur_flags = cur->flags;
3229
3230 if( NULL != f_vrfy )
3231 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
3232 return( ret );
3233
3234 *flags |= cur_flags;
3235 }
3236
3237 return( 0 );
3238}
3239
3240static void x509_crt_verify_chain_add_ee_flags(
3241 mbedtls_x509_crt_verify_chain *chain,
3242 uint32_t ee_flags )
3243{
3244 chain->items[0].flags |= ee_flags;
3245}
3246
3247static void x509_crt_verify_chain_add_crt(
3248 mbedtls_x509_crt_verify_chain *chain,
3249 mbedtls_x509_crt *crt )
3250{
3251 mbedtls_x509_crt_verify_chain_item *cur;
3252 cur = &chain->items[chain->len];
3253 cur->crt = crt;
3254 cur->flags = 0;
3255 chain->len++;
3256}
3257
3258static uint32_t* x509_crt_verify_chain_get_cur_flags(
3259 mbedtls_x509_crt_verify_chain *chain )
3260{
3261 return( &chain->items[chain->len - 1].flags );
3262}
3263
3264static unsigned x509_crt_verify_chain_len(
3265 mbedtls_x509_crt_verify_chain const *chain )
3266{
3267 return( chain->len );
3268}
3269
Hanno Becker14b0a682019-08-29 15:26:15 +01003270#else /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Hanno Becker8d6d3202019-08-16 17:18:15 +01003271
3272/*
3273 * Reset (init or clear) a verify_chain
3274 */
3275static void x509_crt_verify_chain_reset(
3276 mbedtls_x509_crt_verify_chain *ver_chain )
3277{
3278 ver_chain->len = 0;
3279 ver_chain->flags = 0;
3280}
3281
3282/*
3283 * Merge the flags for all certs in the chain, after calling callback
3284 */
3285static int x509_crt_verify_chain_get_flags(
3286 const mbedtls_x509_crt_verify_chain *ver_chain,
3287 uint32_t *flags,
3288 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3289 void *p_vrfy )
3290{
3291 ((void) f_vrfy);
3292 ((void) p_vrfy);
3293 *flags = ver_chain->flags;
3294 return( 0 );
3295}
3296
3297static void x509_crt_verify_chain_add_ee_flags(
3298 mbedtls_x509_crt_verify_chain *chain,
3299 uint32_t ee_flags )
3300{
3301 chain->flags |= ee_flags;
3302}
3303
3304static void x509_crt_verify_chain_add_crt(
3305 mbedtls_x509_crt_verify_chain *chain,
3306 mbedtls_x509_crt *crt )
3307{
3308 ((void) crt);
3309 chain->len++;
3310}
3311
3312static uint32_t* x509_crt_verify_chain_get_cur_flags(
3313 mbedtls_x509_crt_verify_chain *chain )
3314{
3315 return( &chain->flags );
3316}
3317
3318static unsigned x509_crt_verify_chain_len(
3319 mbedtls_x509_crt_verify_chain const *chain )
3320{
3321 return( chain->len );
3322}
3323
3324#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3325
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003326/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003327 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003328 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003329 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3330 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003331 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003332 * such that every cert in the chain is a child of the next one,
3333 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003334 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003335 * Verify that chain and return it with flags for all issues found.
3336 *
3337 * Special cases:
3338 * - EE == Rj -> return a one-element list containing it
3339 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3340 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003341 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003342 * Tests for (aspects of) this function should include at least:
3343 * - trusted EE
3344 * - EE -> trusted root
Antonin Décimod5f47592019-01-23 15:24:37 +01003345 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003346 * - if relevant: EE untrusted
3347 * - if relevant: EE -> intermediate, untrusted
3348 * with the aspect under test checked at each relevant level (EE, int, root).
3349 * For some aspects longer chains are required, but usually length 2 is
3350 * enough (but length 1 is not in general).
3351 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003352 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003353 * - [in] crt: the cert list EE, C1, ..., Cn
3354 * - [in] trust_ca: the trusted list R1, ..., Rp
3355 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003356 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003357 * Only valid when return value is 0, may contain garbage otherwise!
3358 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003359 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003360 *
3361 * Return value:
3362 * - non-zero if the chain could not be fully built and examined
3363 * - 0 is the chain was successfully built and examined,
3364 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003365 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003366static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003367 mbedtls_x509_crt *crt,
3368 mbedtls_x509_crt *trust_ca,
3369 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003370 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003371 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003372 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003373{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003374 /* Don't initialize any of those variables here, so that the compiler can
3375 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003376 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003377 uint32_t *flags;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003378 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003379 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003380 int parent_is_trusted;
3381 int child_is_trusted;
3382 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003383 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003384
3385#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3386 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003387 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003388 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003389 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003390 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003391 self_cnt = rs_ctx->self_cnt;
Hanno Beckeradc282a2019-08-16 17:14:25 +01003392 child_crt = rs_ctx->cur_crt;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003393
3394 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003395 goto find_parent;
3396 }
3397#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003398
Hanno Becker1e0677a2019-02-25 14:58:22 +00003399 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003400 self_cnt = 0;
3401 parent_is_trusted = 0;
3402 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003403
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003404 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003405#if defined(MBEDTLS_X509_CRL_PARSE_C)
3406 mbedtls_x509_buf_raw child_serial;
3407#endif /* MBEDTLS_X509_CRL_PARSE_C */
3408 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003409
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003410 /* Add certificate to the verification chain */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003411 x509_crt_verify_chain_add_crt( ver_chain, child_crt );
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003412
3413#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3414find_parent:
3415#endif
3416
Hanno Beckeradc282a2019-08-16 17:14:25 +01003417 flags = x509_crt_verify_chain_get_cur_flags( ver_chain );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003418
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003419 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003420 mbedtls_x509_crt_sig_info child_sig;
3421 {
Hanno Becker5f268b32019-05-20 16:26:34 +01003422 mbedtls_x509_crt_frame const *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003423
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003424 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003425 if( ret != 0 )
3426 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3427
Hanno Becker843b71a2019-06-25 09:39:21 +01003428#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker5299cf82019-02-25 13:50:41 +00003429 /* Check time-validity (all certificates) */
3430 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3431 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3432 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3433 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Hanno Becker843b71a2019-06-25 09:39:21 +01003434#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker5299cf82019-02-25 13:50:41 +00003435
3436 /* Stop here for trusted roots (but not for trusted EE certs) */
3437 if( child_is_trusted )
3438 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003439 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003440 return( 0 );
3441 }
3442
3443 self_issued = 0;
3444 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3445 &child->subject_raw,
3446 NULL, NULL ) == 0 )
3447 {
3448 self_issued = 1;
3449 }
3450
3451 /* Check signature algorithm: MD & PK algs */
3452 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3453 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3454
3455 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3456 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3457
3458 /* Special case: EE certs that are locally trusted */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003459 if( x509_crt_verify_chain_len( ver_chain ) == 1 && self_issued &&
Hanno Becker5299cf82019-02-25 13:50:41 +00003460 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3461 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003462 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003463 return( 0 );
3464 }
3465
3466#if defined(MBEDTLS_X509_CRL_PARSE_C)
3467 child_serial = child->serial;
3468#endif /* MBEDTLS_X509_CRL_PARSE_C */
3469
3470 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003471 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003472
Hanno Becker5299cf82019-02-25 13:50:41 +00003473 if( ret != 0 )
3474 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3475 }
3476
3477 /* Look for a parent in trusted CAs or up the chain */
3478 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003479 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003480 &parent_is_trusted, &signature_is_good,
Hanno Beckeradc282a2019-08-16 17:14:25 +01003481 x509_crt_verify_chain_len( ver_chain ) - 1,
3482 self_cnt, rs_ctx );
Hanno Becker5299cf82019-02-25 13:50:41 +00003483
3484 x509_crt_free_sig_info( &child_sig );
3485 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003486
3487#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003488 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3489 {
3490 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003491 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003492 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003493 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003494 rs_ctx->cur_crt = child_crt;
Hanno Becker5299cf82019-02-25 13:50:41 +00003495 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003496 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003497#else
3498 (void) ret;
3499#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003500
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003501 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003502 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003503 {
3504 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003505 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003506 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003507
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003508 /* Count intermediate self-issued (not necessarily self-signed) certs.
3509 * These can occur with some strategies for key rollover, see [SIRO],
3510 * and should be excluded from max_pathlen checks. */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003511 if( x509_crt_verify_chain_len( ver_chain ) != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003512 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003513
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003514 /* path_cnt is 0 for the first intermediate CA,
3515 * and if parent is trusted it's not an intermediate CA */
3516 if( ! parent_is_trusted &&
Hanno Beckeradc282a2019-08-16 17:14:25 +01003517 x509_crt_verify_chain_len( ver_chain ) >
3518 MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003519 {
3520 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003521 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003522 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003523
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003524 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003525 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003526 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3527
Hanno Becker58c35642019-02-25 18:13:46 +00003528 {
3529 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003530 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003531 if( ret != 0 )
3532 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3533
3534 /* check size of signing key */
3535 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3536 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3537
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003538 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003539 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003542 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003543 *flags |= x509_crt_verifycrl( child_serial.p,
3544 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003545 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003546#else
3547 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003548#endif
3549
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003550 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003551 child_crt = parent_crt;
3552 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003553 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003554 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003555 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003556}
3557
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003558#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003559/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003560 * Check for CN match
3561 */
Hanno Becker24926222019-02-21 13:10:55 +00003562static int x509_crt_check_cn( unsigned char const *buf,
3563 size_t buflen,
3564 const char *cn,
3565 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003566{
Hanno Becker24926222019-02-21 13:10:55 +00003567 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003568 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003569 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003570
3571 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003572 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003573 {
3574 return( 0 );
3575 }
3576
3577 return( -1 );
3578}
3579
Hanno Becker8b543b32019-02-21 11:50:44 +00003580/* Returns 1 on a match and 0 on a mismatch.
3581 * This is because this function is used as a callback for
3582 * mbedtls_x509_name_cmp_raw(), which continues the name
3583 * traversal as long as the callback returns 0. */
3584static int x509_crt_check_name( void *ctx,
3585 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003586 mbedtls_x509_buf *val,
3587 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003588{
3589 char const *cn = (char const*) ctx;
3590 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003591 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003592
3593 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003594 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003595 {
3596 return( 1 );
3597 }
3598
3599 return( 0 );
3600}
3601
Hanno Beckerda410822019-02-21 13:36:59 +00003602/* Returns 1 on a match and 0 on a mismatch.
3603 * This is because this function is used as a callback for
3604 * mbedtls_asn1_traverse_sequence_of(), which continues the
3605 * traversal as long as the callback returns 0. */
3606static int x509_crt_subject_alt_check_name( void *ctx,
3607 int tag,
3608 unsigned char *data,
3609 size_t data_len )
3610{
3611 char const *cn = (char const*) ctx;
3612 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003613 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003614
3615 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3616 return( 1 );
3617
3618 return( 0 );
3619}
3620
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003621/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003622 * Verify the requested CN - only call this if cn is not NULL!
3623 */
Hanno Becker082435c2019-02-25 18:14:40 +00003624static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3625 const char *cn,
3626 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003627{
Hanno Beckerda410822019-02-21 13:36:59 +00003628 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01003629 mbedtls_x509_crt_frame const *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003630
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003631 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003632 if( ret != 0 )
3633 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3634
3635 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003636 {
Hanno Becker90b94082019-02-21 21:13:21 +00003637 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003638 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003639 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003640 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003641
Hanno Becker90b94082019-02-21 21:13:21 +00003642 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3643 MBEDTLS_ASN1_TAG_CLASS_MASK,
3644 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3645 MBEDTLS_ASN1_TAG_VALUE_MASK,
3646 2 /* SubjectAlt DNS */,
3647 x509_crt_subject_alt_check_name,
Hanno Becker484caf02019-05-29 14:41:44 +01003648 (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003649 }
3650 else
3651 {
Hanno Becker082435c2019-02-25 18:14:40 +00003652 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3653 &frame->subject_raw,
Hanno Becker484caf02019-05-29 14:41:44 +01003654 x509_crt_check_name, (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003655 }
Hanno Beckerda410822019-02-21 13:36:59 +00003656
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003657 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003658
3659 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3660 * return 1 when finding a name component matching `cn`. */
3661 if( ret == 1 )
3662 return( 0 );
3663
3664 if( ret != 0 )
3665 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3666
3667 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3668 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003669}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003670#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003671
3672/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003673 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3676 mbedtls_x509_crt *trust_ca,
3677 mbedtls_x509_crl *ca_crl,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003678#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3679 const char *cn,
3680#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003681 uint32_t *flags
3682#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3683 , int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *)
3684 , void *p_vrfy
3685#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3686 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003687{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003688 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003689 &mbedtls_x509_crt_profile_default,
3690#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3691 cn,
3692#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
3693 flags,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003694#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3695 f_vrfy, p_vrfy,
3696#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3697 NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003698}
3699
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003700/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003701 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003702 */
3703int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3704 mbedtls_x509_crt *trust_ca,
3705 mbedtls_x509_crl *ca_crl,
3706 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003707#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3708 const char *cn,
3709#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003710 uint32_t *flags
3711#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3712 , int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *)
3713 , void *p_vrfy
3714#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3715 )
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003716{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003717 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003718 profile,
3719#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3720 cn,
3721#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003722 flags,
3723#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3724 f_vrfy, p_vrfy,
3725#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3726 NULL ) );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003727}
3728
3729/*
3730 * Verify the certificate validity, with profile, restartable version
3731 *
3732 * This function:
3733 * - checks the requested CN (if any)
3734 * - checks the type and size of the EE cert's key,
3735 * as that isn't done as part of chain building/verification currently
3736 * - builds and verifies the chain
3737 * - then calls the callback and merges the flags
3738 */
3739int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3740 mbedtls_x509_crt *trust_ca,
3741 mbedtls_x509_crl *ca_crl,
3742 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003743#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3744 const char *cn,
3745#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
3746 uint32_t *flags,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003747#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003748 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3749 void *p_vrfy,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003750#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003751 mbedtls_x509_crt_restart_ctx *rs_ctx )
3752{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003753 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003754 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003755 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003756
3757 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003758 ee_flags = 0;
3759 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003760
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003761 if( profile == NULL )
3762 {
3763 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3764 goto exit;
3765 }
3766
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003767#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003768 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003769 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003770 {
3771 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3772 if( ret != 0 )
3773 return( ret );
3774 }
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003775#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003776
Hanno Becker87233362019-02-25 18:15:33 +00003777 {
3778 mbedtls_pk_context *pk;
3779 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003780
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003781 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003782 if( ret != 0 )
3783 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003784
Hanno Becker87233362019-02-25 18:15:33 +00003785 /* Check the type and size of the key */
3786 pk_type = mbedtls_pk_get_type( pk );
3787
3788 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3789 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3790
3791 if( x509_profile_check_key( profile, pk ) != 0 )
3792 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3793
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003794 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003795 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003796
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003797 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003798 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003799 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003800
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003801 if( ret != 0 )
3802 goto exit;
3803
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003804 /* Merge end-entity flags */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003805 x509_crt_verify_chain_add_ee_flags( &ver_chain, ee_flags );
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003806
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003807 /* Build final flags, calling callback on the way if any */
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003808#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Beckeradc282a2019-08-16 17:14:25 +01003809 ret = x509_crt_verify_chain_get_flags( &ver_chain, flags, f_vrfy, p_vrfy );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003810#else
Hanno Beckeradc282a2019-08-16 17:14:25 +01003811 ret = x509_crt_verify_chain_get_flags( &ver_chain, flags, NULL, NULL );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003812#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003813
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003814exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003815#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3816 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3817 mbedtls_x509_crt_restart_free( rs_ctx );
3818#endif
3819
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003820 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3821 * the SSL module for authmode optional, but non-zero return from the
3822 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003823 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3824 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3825
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003826 if( ret != 0 )
3827 {
3828 *flags = (uint32_t) -1;
3829 return( ret );
3830 }
3831
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003832 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003834
3835 return( 0 );
3836}
3837
3838/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003839 * Initialize a certificate chain
3840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003842{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003844}
3845
3846/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003847 * Unallocate all certificate data
3848 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 mbedtls_x509_crt *cert_cur = crt;
3853 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003854
3855 if( crt == NULL )
3856 return;
3857
3858 do
3859 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003860 x509_crt_cache_free( cert_cur->cache );
3861 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003862
3863#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3867 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003868#endif
3869
Hanno Becker2bcc7642019-02-26 19:01:00 +00003870 mbedtls_x509_name_free( cert_cur->issuer.next );
3871 mbedtls_x509_name_free( cert_cur->subject.next );
3872 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003873#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker2bcc7642019-02-26 19:01:00 +00003874 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003875#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
3876
Hanno Becker180f7bf2019-02-28 13:23:38 +00003877#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003878
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003879 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003880 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003881 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003883 }
3884
3885 cert_cur = cert_cur->next;
3886 }
3887 while( cert_cur != NULL );
3888
3889 cert_cur = crt;
3890 do
3891 {
3892 cert_prv = cert_cur;
3893 cert_cur = cert_cur->next;
3894
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003895 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003896 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003898 }
3899 while( cert_cur != NULL );
3900}
3901
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003902#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3903/*
3904 * Initialize a restart context
3905 */
3906void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3907{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003908 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003909
3910 ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003911#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003912 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003913 ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003914#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003915
3916 ctx->parent_is_trusted = -1;
3917
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003918 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003919 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003920 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003921}
3922
3923/*
3924 * Free the components of a restart context
3925 */
3926void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3927{
3928 if( ctx == NULL )
3929 return;
3930
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003931 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003932 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003933}
3934#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3935
Teppo Järvelinffaba552019-09-03 12:33:16 +03003936int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
3937 mbedtls_x509_crt_frame const **dst )
3938{
3939 int ret = 0;
3940#if defined(MBEDTLS_THREADING_C)
3941 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
3942 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
3943#endif /* MBEDTLS_THREADING_C */
3944
3945#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
3946 defined(MBEDTLS_THREADING_C)
3947 if( crt->cache->frame_readers == 0 )
3948#endif
3949 ret = mbedtls_x509_crt_cache_provide_frame( crt );
3950
3951#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
3952 defined(MBEDTLS_THREADING_C)
3953 if( crt->cache->frame_readers == MBEDTLS_X509_CACHE_FRAME_READERS_MAX )
3954 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
3955
3956 crt->cache->frame_readers++;
3957#endif
3958
3959#if defined(MBEDTLS_THREADING_C)
3960 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
3961 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
3962#endif /* MBEDTLS_THREADING_C */
3963
3964 *dst = crt->cache->frame;
3965 return( ret );
3966}
3967
3968int mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt )
3969{
3970#if defined(MBEDTLS_THREADING_C)
3971 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
3972 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
3973#endif /* MBEDTLS_THREADING_C */
3974
3975#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
3976 defined(MBEDTLS_THREADING_C)
3977 if( crt->cache->frame_readers == 0 )
3978 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3979
3980 crt->cache->frame_readers--;
3981#endif
3982
3983#if defined(MBEDTLS_THREADING_C)
3984 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
3985#endif /* MBEDTLS_THREADING_C */
3986
3987#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
3988 (void) mbedtls_x509_crt_flush_cache_frame( crt );
3989#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
3990
3991#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
3992 !defined(MBEDTLS_THREADING_C)
3993 ((void) crt);
3994#endif
3995
3996 return( 0 );
3997}
3998
3999int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
4000 mbedtls_pk_context **dst )
4001{
4002 int ret = 0;
4003#if defined(MBEDTLS_THREADING_C)
4004 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
4005 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4006#endif /* MBEDTLS_THREADING_C */
4007
4008#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
4009 defined(MBEDTLS_THREADING_C)
4010 if( crt->cache->pk_readers == 0 )
4011#endif
4012 ret = mbedtls_x509_crt_cache_provide_pk( crt );
4013
4014#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
4015 defined(MBEDTLS_THREADING_C)
4016 if( crt->cache->pk_readers == MBEDTLS_X509_CACHE_PK_READERS_MAX )
4017 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4018
4019 crt->cache->pk_readers++;
4020#endif
4021
4022#if defined(MBEDTLS_THREADING_C)
4023 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
4024 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4025#endif /* MBEDTLS_THREADING_C */
4026
4027 *dst = crt->cache->pk;
4028 return( ret );
4029}
4030
4031int mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt )
4032{
4033#if defined(MBEDTLS_THREADING_C)
4034 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
4035 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4036#endif /* MBEDTLS_THREADING_C */
4037
4038#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
4039 defined(MBEDTLS_THREADING_C)
4040 if( crt->cache->pk_readers == 0 )
4041 return( MBEDTLS_ERR_X509_FATAL_ERROR );
4042
4043 crt->cache->pk_readers--;
4044#endif
4045
4046#if defined(MBEDTLS_THREADING_C)
4047 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
4048#endif /* MBEDTLS_THREADING_C */
4049
4050#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
4051 (void) mbedtls_x509_crt_flush_cache_pk( crt );
4052#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
4053
4054#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
4055 !defined(MBEDTLS_THREADING_C)
4056 ((void) crt);
4057#endif
4058
4059 return( 0 );
4060}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061#endif /* MBEDTLS_X509_CRT_PARSE_C */