blob: af8f1d67fdb513b5a5d36cd8984fa567e4795adf [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"
Manuel Pégourié-Gonnard9ca11fc2019-11-28 12:07:01 +010046#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000047
Rich Evans00ab4702015-02-06 13:43:58 +000048#include <string.h>
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#endif
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056#else
Simon Butcherd2642582018-10-03 15:11:19 +010057#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020060#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010066#endif
67
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#include <windows.h>
70#else
71#include <time.h>
72#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020076#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <sys/types.h>
78#include <sys/stat.h>
79#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000080#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081#endif
82
Hanno Becker38f0cb42019-03-04 15:13:45 +000083#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
84static void x509_buf_to_buf_raw( mbedtls_x509_buf_raw *dst,
85 mbedtls_x509_buf const *src )
86{
87 dst->p = src->p;
88 dst->len = src->len;
89}
90
91static void x509_buf_raw_to_buf( mbedtls_x509_buf *dst,
92 mbedtls_x509_buf_raw const *src )
93{
94 dst->p = src->p;
95 dst->len = src->len;
96}
97#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
98
Hanno Becker21f55672019-02-15 15:27:59 +000099static int x509_crt_parse_frame( unsigned char *start,
100 unsigned char *end,
101 mbedtls_x509_crt_frame *frame );
Hanno Becker12506232019-05-13 13:53:21 +0100102static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000103 mbedtls_x509_name *subject );
Hanno Becker12506232019-05-13 13:53:21 +0100104static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000105 mbedtls_x509_name *issuer );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300106#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker12506232019-05-13 13:53:21 +0100107static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000108 mbedtls_x509_sequence *subject_alt );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300109#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker12506232019-05-13 13:53:21 +0100110static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000111 mbedtls_x509_sequence *ext_key_usage );
112
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300113static int mbedtls_x509_crt_flush_cache_pk( mbedtls_x509_crt const *crt )
Hanno Beckerbc685192019-03-05 15:35:31 +0000114{
115#if defined(MBEDTLS_THREADING_C)
116 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
117 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100118#endif
Hanno Beckerfc99a092019-06-28 14:45:26 +0100119
120#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
121 defined(MBEDTLS_THREADING_C)
122 /* Can only free the PK context if nobody is using it.
123 * If MBEDTLS_X509_ALWAYS_FLUSH is set, nested uses
124 * of xxx_acquire() are prohibited, and no reference
125 * counting is needed. Also, notice that the code-path
126 * below is safe if the cache isn't filled. */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100127 if( crt->cache->pk_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100128#endif /* !MBEDTLS_X509_ALWAYS_FLUSH ||
129 MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100130 {
Hanno Beckerbc685192019-03-05 15:35:31 +0000131#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100132 /* The cache holds a shallow copy of the PK context
133 * in the legacy struct, so don't free PK context. */
134 mbedtls_free( crt->cache->pk );
Hanno Beckerbc685192019-03-05 15:35:31 +0000135#else
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100136 mbedtls_pk_free( crt->cache->pk );
137 mbedtls_free( crt->cache->pk );
Hanno Beckerbc685192019-03-05 15:35:31 +0000138#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100139 crt->cache->pk = NULL;
140 }
Hanno Beckerbc685192019-03-05 15:35:31 +0000141
142#if defined(MBEDTLS_THREADING_C)
143 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
144 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
145#endif
146 return( 0 );
147}
148
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300149static int mbedtls_x509_crt_flush_cache_frame( mbedtls_x509_crt const *crt )
Hanno Beckerbc685192019-03-05 15:35:31 +0000150{
151#if defined(MBEDTLS_THREADING_C)
152 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
153 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100154#endif
Hanno Beckerbc685192019-03-05 15:35:31 +0000155
Hanno Beckerfc99a092019-06-28 14:45:26 +0100156#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
157 defined(MBEDTLS_THREADING_C)
158 /* Can only free the PK context if nobody is using it.
159 * If MBEDTLS_X509_ALWAYS_FLUSH is set, nested uses
160 * of xxx_acquire() are prohibited, and no reference
161 * counting is needed. Also, notice that the code-path
162 * below is safe if the cache isn't filled. */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100163 if( crt->cache->frame_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100164#endif /* !MBEDTLS_X509_ALWAYS_FLUSH ||
165 MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100166 {
167 mbedtls_free( crt->cache->frame );
168 crt->cache->frame = NULL;
169 }
Hanno Beckerbc685192019-03-05 15:35:31 +0000170
171#if defined(MBEDTLS_THREADING_C)
172 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
173 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
174#endif
175 return( 0 );
176}
177
178int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt )
179{
180 int ret;
181 ret = mbedtls_x509_crt_flush_cache_frame( crt );
182 if( ret != 0 )
183 return( ret );
184 ret = mbedtls_x509_crt_flush_cache_pk( crt );
185 if( ret != 0 )
186 return( ret );
187 return( 0 );
188}
189
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000190static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame );
Hanno Beckered058882019-06-28 10:46:43 +0100191
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300192static int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt )
Hanno Becker337088a2019-02-25 14:53:14 +0000193{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000194 mbedtls_x509_crt_cache *cache = crt->cache;
Hanno Becker337088a2019-02-25 14:53:14 +0000195 mbedtls_x509_crt_frame *frame;
196
Hanno Becker76428352019-03-05 15:29:23 +0000197 if( cache->frame != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100198 {
Hanno Becker410322f2019-07-02 13:37:12 +0100199#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
200 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000201 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100202#else
203 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
204 * allow nested uses of acquire. */
205 return( MBEDTLS_ERR_X509_FATAL_ERROR );
206#endif
207 }
Hanno Becker76428352019-03-05 15:29:23 +0000208
Hanno Becker337088a2019-02-25 14:53:14 +0000209 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
210 if( frame == NULL )
211 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000212 cache->frame = frame;
Hanno Becker337088a2019-02-25 14:53:14 +0000213
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000214#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
215 /* This would work with !MBEDTLS_X509_ON_DEMAND_PARSING, too,
216 * but is inefficient compared to copying the respective fields
217 * from the legacy mbedtls_x509_crt. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000218 return( x509_crt_parse_frame( crt->raw.p,
219 crt->raw.p + crt->raw.len,
220 frame ) );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000221#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
222 /* Make sure all extension related fields are properly initialized. */
223 frame->ca_istrue = 0;
224 frame->max_pathlen = 0;
225 frame->ext_types = 0;
226 frame->version = crt->version;
227 frame->sig_md = crt->sig_md;
228 frame->sig_pk = crt->sig_pk;
Hanno Becker843b71a2019-06-25 09:39:21 +0100229
230#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000231 frame->valid_from = crt->valid_from;
232 frame->valid_to = crt->valid_to;
Hanno Becker843b71a2019-06-25 09:39:21 +0100233#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
234
Hanno Becker38f0cb42019-03-04 15:13:45 +0000235 x509_buf_to_buf_raw( &frame->raw, &crt->raw );
236 x509_buf_to_buf_raw( &frame->tbs, &crt->tbs );
237 x509_buf_to_buf_raw( &frame->serial, &crt->serial );
238 x509_buf_to_buf_raw( &frame->pubkey_raw, &crt->pk_raw );
239 x509_buf_to_buf_raw( &frame->issuer_raw, &crt->issuer_raw );
240 x509_buf_to_buf_raw( &frame->subject_raw, &crt->subject_raw );
Hanno Beckerd07614c2019-06-25 10:19:58 +0100241#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Becker38f0cb42019-03-04 15:13:45 +0000242 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
243 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
Hanno Beckerd07614c2019-06-25 10:19:58 +0100244#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker38f0cb42019-03-04 15:13:45 +0000245 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
246 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000247
248 /* The legacy CRT structure doesn't explicitly contain
249 * the `AlgorithmIdentifier` bounds; however, those can
250 * be inferred from the surrounding (mandatory) `SerialNumber`
251 * and `Issuer` fields. */
252 frame->sig_alg.p = crt->serial.p + crt->serial.len;
253 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
254
255 return( x509_crt_frame_parse_ext( frame ) );
256#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000257}
Hanno Becker337088a2019-02-25 14:53:14 +0000258
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300259static int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000260{
261 mbedtls_x509_crt_cache *cache = crt->cache;
262 mbedtls_pk_context *pk;
263
Hanno Becker76428352019-03-05 15:29:23 +0000264 if( cache->pk != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100265 {
Hanno Becker410322f2019-07-02 13:37:12 +0100266#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
267 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000268 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100269#else
270 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
271 * allow nested uses of acquire. */
272 return( MBEDTLS_ERR_X509_FATAL_ERROR );
273#endif
274 }
Hanno Becker76428352019-03-05 15:29:23 +0000275
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000276 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
277 if( pk == NULL )
278 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000279 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000280
281#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
282 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000283 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000284#else
285 {
286 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
287 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
288 pk_raw.p + pk_raw.len,
289 pk ) );
290 }
291#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000292}
293
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000294static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000295{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000296 memset( cache, 0, sizeof( *cache ) );
297#if defined(MBEDTLS_THREADING_C)
298 mbedtls_mutex_init( &cache->frame_mutex );
299 mbedtls_mutex_init( &cache->pk_mutex );
300#endif
301}
302
303static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
304{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000305#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000306 /* The cache holds a shallow copy of the PK context
307 * in the legacy struct, so don't free PK context. */
308 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000309#else
310 mbedtls_pk_free( cache->pk );
311 mbedtls_free( cache->pk );
312#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
313
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000314 cache->pk = NULL;
315}
316
317static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
318{
319 mbedtls_free( cache->frame );
320 cache->frame = NULL;
321}
322
323static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
324{
325 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000326 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000327
328#if defined(MBEDTLS_THREADING_C)
329 mbedtls_mutex_free( &cache->frame_mutex );
330 mbedtls_mutex_free( &cache->pk_mutex );
331#endif
332
333 x509_crt_cache_clear_frame( cache );
334 x509_crt_cache_clear_pk( cache );
335
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200336 mbedtls_platform_memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000337}
338
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300339#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000340int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
341 mbedtls_x509_sequence **subj_alt )
342{
343 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100344 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000345 mbedtls_x509_sequence *seq;
346
347 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
348 if( ret != 0 )
349 return( ret );
350
351 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
352 if( seq == NULL )
353 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
354 else
355 ret = x509_crt_subject_alt_from_frame( frame, seq );
356
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000357 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000358
359 *subj_alt = seq;
360 return( ret );
361}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300362#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000363
364int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
365 mbedtls_x509_sequence **ext_key_usage )
366{
367 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100368 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000369 mbedtls_x509_sequence *seq;
370
371 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
372 if( ret != 0 )
373 return( ret );
374
375 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
376 if( seq == NULL )
377 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
378 else
379 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
380
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000381 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000382
383 *ext_key_usage = seq;
384 return( ret );
385}
386
Hanno Becker63e69982019-02-26 18:50:49 +0000387int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
388 mbedtls_x509_name **subject )
389{
390 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100391 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000392 mbedtls_x509_name *name;
393
394 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
395 if( ret != 0 )
396 return( ret );
397
398 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
399 if( name == NULL )
400 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
401 else
402 ret = x509_crt_subject_from_frame( frame, name );
403
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000404 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000405
406 *subject = name;
407 return( ret );
408}
409
410int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
411 mbedtls_x509_name **issuer )
412{
413 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100414 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000415 mbedtls_x509_name *name;
416
417 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
418 if( ret != 0 )
419 return( ret );
420
421 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
422 if( name == NULL )
423 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
424 else
425 ret = x509_crt_issuer_from_frame( frame, name );
426
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000427 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000428
429 *issuer = name;
430 return( ret );
431}
432
Hanno Becker823efad2019-02-28 13:23:58 +0000433int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
434 mbedtls_x509_crt_frame *dst )
435{
436 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100437 mbedtls_x509_crt_frame const *frame;
Hanno Becker823efad2019-02-28 13:23:58 +0000438 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
439 if( ret != 0 )
440 return( ret );
441 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000442 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000443 return( 0 );
444}
445
446int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
447 mbedtls_pk_context *dst )
448{
449#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
450 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
451 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
452 pk_raw.p + pk_raw.len,
453 dst ) );
454#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
455 int ret;
456 mbedtls_pk_context *pk;
457 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
458 if( ret != 0 )
459 return( ret );
460
461 /* Move PK from CRT cache to destination pointer
462 * to avoid a copy. */
463 *dst = *pk;
464 mbedtls_free( crt->cache->pk );
465 crt->cache->pk = NULL;
466
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000467 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000468 return( 0 );
469#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
470}
471
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200472/*
473 * Item in a verification chain: cert and flags for it
474 */
475typedef struct {
476 mbedtls_x509_crt *crt;
477 uint32_t flags;
478} x509_crt_verify_chain_item;
479
480/*
481 * Max size of verification chain: end-entity + intermediates + trusted root
482 */
483#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200484
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200486 * Default profile
487 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200488const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
489{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200490#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200491 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200492 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200493#endif
494 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200495 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
496 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
497 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
498 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
499 0xFFFFFFF, /* Any PK alg */
500 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200501 2048,
502};
503
504/*
505 * Next-default profile
506 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200507const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
508{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200509 /* Hashes from SHA-256 and above */
510 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
511 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
512 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
513 0xFFFFFFF, /* Any PK alg */
Hanno Becker59e7b082019-08-23 13:21:21 +0100514#if defined(MBEDTLS_USE_TINYCRYPT)
515 MBEDTLS_X509_ID_FLAG( MBEDTLS_UECC_DP_SECP256R1 ),
516#elif defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200517 /* Curves at or above 128-bit security level */
518 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
519 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
520 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
521 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
522 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
523 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
524 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
525#else
526 0,
527#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200528 2048,
529};
530
531/*
532 * NSA Suite B Profile
533 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200534const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
535{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200536 /* Only SHA-256 and 384 */
537 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
538 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
539 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200540 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
541 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Hanno Becker59e7b082019-08-23 13:21:21 +0100542#if defined(MBEDTLS_USE_TINYCRYPT)
543 MBEDTLS_X509_ID_FLAG( MBEDTLS_UECC_DP_SECP256R1 ),
544#elif defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200545 /* Only NIST P-256 and P-384 */
546 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
547 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
548#else
549 0,
550#endif
551 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200552};
553
554/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200555 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200556 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200557 */
558static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
559 mbedtls_md_type_t md_alg )
560{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200561 if( md_alg == MBEDTLS_MD_NONE )
562 return( -1 );
563
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200564 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
565 return( 0 );
566
567 return( -1 );
568}
569
570/*
571 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200572 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200573 */
574static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
575 mbedtls_pk_type_t pk_alg )
576{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200577 if( pk_alg == MBEDTLS_PK_NONE )
578 return( -1 );
579
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200580 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
581 return( 0 );
582
583 return( -1 );
584}
585
586/*
587 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200588 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200589 */
590static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200591 const mbedtls_pk_context *pk )
592{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200593 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200594
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200595#if defined(MBEDTLS_RSA_C)
596 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
597 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200598 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200599 return( 0 );
600
601 return( -1 );
602 }
603#endif
604
Hanno Beckerd931ad22019-08-21 15:25:22 +0100605#if defined(MBEDTLS_USE_TINYCRYPT)
606 if( pk_alg == MBEDTLS_PK_ECKEY )
607 {
608 if( ( profile->allowed_curves & MBEDTLS_UECC_DP_SECP256R1 ) != 0 )
609 return( 0 );
610
611 return( -1 );
612 }
613#endif /* MBEDTLS_USE_TINYCRYPT */
614
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200615#if defined(MBEDTLS_ECP_C)
616 if( pk_alg == MBEDTLS_PK_ECDSA ||
617 pk_alg == MBEDTLS_PK_ECKEY ||
618 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200619 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200620 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200621
Philippe Antoineb5b25432018-05-11 11:06:29 +0200622 if( gid == MBEDTLS_ECP_DP_NONE )
623 return( -1 );
624
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200625 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
626 return( 0 );
627
628 return( -1 );
629 }
630#endif
631
632 return( -1 );
633}
634
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300635#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200636/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000637 * Return 0 if name matches wildcard, -1 otherwise
638 */
Hanno Becker24926222019-02-21 13:10:55 +0000639static int x509_check_wildcard( char const *cn,
640 size_t cn_len,
641 unsigned char const *buf,
642 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000643{
644 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000645 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000646
647 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000648 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000649 return( -1 );
650
651 for( i = 0; i < cn_len; ++i )
652 {
653 if( cn[i] == '.' )
654 {
655 cn_idx = i;
656 break;
657 }
658 }
659
660 if( cn_idx == 0 )
661 return( -1 );
662
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000663 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
664 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000665 {
666 return( 0 );
667 }
668
669 return( -1 );
670}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300671#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker1f8527f2018-11-02 09:19:16 +0000672
673/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
675 */
676static int x509_get_version( unsigned char **p,
677 const unsigned char *end,
678 int *ver )
679{
680 int ret;
681 size_t len;
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
684 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 {
688 *ver = 0;
689 return( 0 );
690 }
691
Hanno Becker2f472142019-02-12 11:52:10 +0000692 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693 }
694
695 end = *p + len;
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
698 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
700 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 return( MBEDTLS_ERR_X509_INVALID_VERSION +
702 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703
704 return( 0 );
705}
706
Hanno Becker843b71a2019-06-25 09:39:21 +0100707#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708/*
709 * Validity ::= SEQUENCE {
710 * notBefore Time,
711 * notAfter Time }
712 */
713static int x509_get_dates( unsigned char **p,
714 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_x509_time *from,
716 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717{
718 int ret;
719 size_t len;
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
722 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
723 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724
725 end = *p + len;
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 return( ret );
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731 return( ret );
732
733 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 return( MBEDTLS_ERR_X509_INVALID_DATE +
735 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736
737 return( 0 );
738}
Hanno Becker843b71a2019-06-25 09:39:21 +0100739#else /* !MBEDTLS_X509_CRT_REMOVE_TIME */
740static int x509_skip_dates( unsigned char **p,
741 const unsigned char *end )
742{
743 int ret;
744 size_t len;
745
746 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
747 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
748 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
749
Manuel Pégourié-Gonnard0d1db202019-07-30 14:11:25 +0200750 /* skip contents of the sequence */
751 *p += len;
Hanno Becker843b71a2019-06-25 09:39:21 +0100752
753 return( 0 );
754}
755#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756
Hanno Beckerd07614c2019-06-25 10:19:58 +0100757#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758/*
759 * X.509 v2/v3 unique identifier (not parsed)
760 */
761static int x509_get_uid( unsigned char **p,
762 const unsigned char *end,
Hanno Beckere9084122019-06-07 12:04:39 +0100763 mbedtls_x509_buf_raw *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764{
765 int ret;
766
767 if( *p == end )
768 return( 0 );
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
771 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 return( 0 );
775
Hanno Becker2f472142019-02-12 11:52:10 +0000776 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 }
778
779 uid->p = *p;
780 *p += uid->len;
781
782 return( 0 );
783}
Hanno Beckerd07614c2019-06-25 10:19:58 +0100784#else /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
785static int x509_skip_uid( unsigned char **p,
786 const unsigned char *end,
787 int n )
788{
789 int ret;
790 size_t len;
791
792 if( *p == end )
793 return( 0 );
794
795 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
796 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
797 {
798 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
799 return( 0 );
800
801 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
802 }
803
804 *p += len;
805 return( 0 );
806}
807#endif /* MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808
809static int x509_get_basic_constraints( unsigned char **p,
810 const unsigned char *end,
811 int *ca_istrue,
812 int *max_pathlen )
813{
814 int ret;
815 size_t len;
816
817 /*
818 * BasicConstraints ::= SEQUENCE {
819 * cA BOOLEAN DEFAULT FALSE,
820 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
821 */
822 *ca_istrue = 0; /* DEFAULT FALSE */
823 *max_pathlen = 0; /* endless */
824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
826 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000827 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828
829 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200830 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
835 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
837 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000838 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839
840 if( *ca_istrue != 0 )
841 *ca_istrue = 1;
842 }
843
844 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200845 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000848 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849
850 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000851 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852
853 (*max_pathlen)++;
854
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200855 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856}
857
858static int x509_get_ns_cert_type( unsigned char **p,
859 const unsigned char *end,
860 unsigned char *ns_cert_type)
861{
862 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000866 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867
868 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000869 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870
871 /* Get actual bitstring */
872 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200873 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874}
875
876static int x509_get_key_usage( unsigned char **p,
877 const unsigned char *end,
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100878 uint16_t *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879{
880 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200881 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000885 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886
887 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000888 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889
890 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200891 *key_usage = 0;
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100892 for( i = 0; i < bs.len && i < sizeof( *key_usage ); i++ )
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200893 {
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100894 *key_usage |= (uint16_t) bs.p[i] << ( 8*i );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200895 }
896
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200897 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898}
899
Hanno Becker529f25d2019-05-02 14:48:25 +0100900static int asn1_build_sequence_cb( void *ctx,
901 int tag,
902 unsigned char *data,
903 size_t data_len )
Hanno Becker15b73b42019-05-02 13:21:27 +0100904{
905 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
906 mbedtls_asn1_sequence *cur = *cur_ptr;
907
908 /* Allocate and assign next pointer */
909 if( cur->buf.p != NULL )
910 {
911 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
912 if( cur->next == NULL )
913 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
914 cur = cur->next;
915 }
916
917 cur->buf.tag = tag;
918 cur->buf.p = data;
919 cur->buf.len = data_len;
920
921 *cur_ptr = cur;
922 return( 0 );
923}
924
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925/*
Hanno Becker529f25d2019-05-02 14:48:25 +0100926 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
927 *
928 * KeyPurposeId ::= OBJECT IDENTIFIER
929 */
930static int x509_get_ext_key_usage( unsigned char **p,
931 const unsigned char *end,
932 mbedtls_x509_sequence *ext_key_usage)
933{
934 return( mbedtls_asn1_traverse_sequence_of( p, end,
935 0xFF, MBEDTLS_ASN1_OID,
936 0, 0,
937 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100938 (void *) &ext_key_usage ) );
Hanno Becker529f25d2019-05-02 14:48:25 +0100939}
940
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300941#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker529f25d2019-05-02 14:48:25 +0100942/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 * SubjectAltName ::= GeneralNames
944 *
945 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
946 *
947 * GeneralName ::= CHOICE {
948 * otherName [0] OtherName,
949 * rfc822Name [1] IA5String,
950 * dNSName [2] IA5String,
951 * x400Address [3] ORAddress,
952 * directoryName [4] Name,
953 * ediPartyName [5] EDIPartyName,
954 * uniformResourceIdentifier [6] IA5String,
955 * iPAddress [7] OCTET STRING,
956 * registeredID [8] OBJECT IDENTIFIER }
957 *
958 * OtherName ::= SEQUENCE {
959 * type-id OBJECT IDENTIFIER,
960 * value [0] EXPLICIT ANY DEFINED BY type-id }
961 *
962 * EDIPartyName ::= SEQUENCE {
963 * nameAssigner [0] DirectoryString OPTIONAL,
964 * partyName [1] DirectoryString }
965 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000966 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 */
Hanno Becker5984d302019-02-21 14:46:54 +0000968static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000972 return( mbedtls_asn1_traverse_sequence_of( &p, end,
973 MBEDTLS_ASN1_TAG_CLASS_MASK,
974 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
975 MBEDTLS_ASN1_TAG_VALUE_MASK,
976 2 /* SubjectAlt DNS */,
Hanno Becker529f25d2019-05-02 14:48:25 +0100977 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100978 (void *) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300980#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981
982/*
983 * X.509 v3 extensions
984 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000986static int x509_crt_get_ext_cb( void *ctx,
987 int tag,
988 unsigned char *p,
989 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990{
991 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000992 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000994 unsigned char *end, *end_ext_octet;
995 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
996 int is_critical = 0; /* DEFAULT FALSE */
997 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000999 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +00001000
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001001 /*
1002 * Extension ::= SEQUENCE {
1003 * extnID OBJECT IDENTIFIER,
1004 * critical BOOLEAN DEFAULT FALSE,
1005 * extnValue OCTET STRING }
1006 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001008 end = p + ext_len;
1009
1010 /* Get extension ID */
1011 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
1012 MBEDTLS_ASN1_OID ) ) != 0 )
1013 goto err;
1014
1015 extn_oid.tag = MBEDTLS_ASN1_OID;
1016 extn_oid.p = p;
1017 p += extn_oid.len;
1018
1019 /* Get optional critical */
1020 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
1021 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
1022 goto err;
1023
1024 /* Data should be octet string type */
1025 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1026 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1027 goto err;
1028
1029 end_ext_octet = p + len;
1030 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001032 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
1033 goto err;
1034 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001036 /*
1037 * Detect supported extensions
1038 */
1039 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
1040 if( ret != 0 )
1041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001043 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001045 /* Data is marked as critical: fail */
1046 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
1047 goto err;
1048 }
Hanno Beckerb36a2452019-05-29 14:43:17 +01001049#endif /* MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001050 return( 0 );
1051 }
1052
1053 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +00001054 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001055 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
1056
Hanno Becker21f55672019-02-15 15:27:59 +00001057 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001058 switch( ext_type )
1059 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001060 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001061 {
1062 int ca_istrue;
1063 int max_pathlen;
1064
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001066 ret = x509_get_basic_constraints( &p, end_ext_octet,
1067 &ca_istrue,
1068 &max_pathlen );
1069 if( ret != 0 )
1070 goto err;
1071
Hanno Becker21f55672019-02-15 15:27:59 +00001072 frame->ca_istrue = ca_istrue;
1073 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001075 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001079 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001080 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001081 if( ret != 0 )
1082 goto err;
1083 break;
1084
1085 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001086#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001087 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +00001088 frame->subject_alt_raw.p = p;
1089 frame->subject_alt_raw.len = end_ext_octet - p;
Hanno Becker21f55672019-02-15 15:27:59 +00001090 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1091 MBEDTLS_ASN1_TAG_CLASS_MASK,
1092 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
1093 MBEDTLS_ASN1_TAG_VALUE_MASK,
1094 2 /* SubjectAlt DNS */,
1095 NULL, NULL );
1096 if( ret != 0 )
1097 goto err;
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001098#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 break;
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001103 frame->ext_key_usage_raw.p = p;
1104 frame->ext_key_usage_raw.len = end_ext_octet - p;
1105 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001106 {
Hanno Becker21f55672019-02-15 15:27:59 +00001107 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1108 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001109 }
Hanno Becker21f55672019-02-15 15:27:59 +00001110
1111 /* Check structural sanity of extension. */
1112 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1113 0xFF, MBEDTLS_ASN1_OID,
1114 0, 0, NULL, NULL );
1115 if( ret != 0 )
1116 goto err;
1117
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 break;
1119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001122 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001123 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001124 if( ret != 0 )
1125 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126 break;
1127
1128 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001129 /*
1130 * If this is a non-critical extension, which the oid layer
1131 * supports, but there isn't an X.509 parser for it,
1132 * skip the extension.
1133 */
1134#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1135 if( is_critical )
1136 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1137#endif
1138 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139 }
1140
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001142
1143err:
1144 return( ret );
1145}
1146
Hanno Becker21f55672019-02-15 15:27:59 +00001147static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001148{
1149 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001150 unsigned char *p = frame->v3_ext.p;
1151 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001152
Hanno Becker21f55672019-02-15 15:27:59 +00001153 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001154 return( 0 );
1155
Hanno Becker21f55672019-02-15 15:27:59 +00001156 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001157 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001158 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001159
1160 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1161 return( ret );
1162 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1163 return( ret );
1164
1165 if( ret != 0 )
1166 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1167
1168 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169}
1170
Hanno Becker21f55672019-02-15 15:27:59 +00001171static int x509_crt_parse_frame( unsigned char *start,
1172 unsigned char *end,
1173 mbedtls_x509_crt_frame *frame )
1174{
1175 int ret;
1176 unsigned char *p;
1177 size_t len;
1178
1179 mbedtls_x509_buf tmp;
1180 unsigned char *tbs_start;
1181
1182 mbedtls_x509_buf outer_sig_alg;
1183 size_t inner_sig_alg_len;
1184 unsigned char *inner_sig_alg_start;
1185
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001186 mbedtls_platform_memset( frame, 0, sizeof( *frame ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001187
1188 /*
1189 * Certificate ::= SEQUENCE {
1190 * tbsCertificate TBSCertificate,
1191 * signatureAlgorithm AlgorithmIdentifier,
1192 * signatureValue BIT STRING
1193 * }
1194 *
1195 */
1196 p = start;
1197
1198 frame->raw.p = p;
1199 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1201 {
1202 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1203 }
1204
1205 /* NOTE: We are currently not checking that the `Certificate`
1206 * structure spans the entire buffer. */
1207 end = p + len;
1208 frame->raw.len = end - frame->raw.p;
1209
1210 /*
1211 * TBSCertificate ::= SEQUENCE { ...
1212 */
1213 frame->tbs.p = p;
1214 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1215 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1216 {
1217 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1218 }
1219 tbs_start = p;
1220
1221 /* Breadth-first parsing: Jump over TBS for now. */
1222 p += len;
1223 frame->tbs.len = p - frame->tbs.p;
1224
1225 /*
1226 * AlgorithmIdentifier ::= SEQUENCE { ...
1227 */
1228 outer_sig_alg.p = p;
1229 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1230 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1231 {
1232 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1233 }
1234 p += len;
1235 outer_sig_alg.len = p - outer_sig_alg.p;
1236
1237 /*
1238 * signatureValue BIT STRING
1239 */
1240 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1241 if( ret != 0 )
1242 return( ret );
1243 frame->sig.p = tmp.p;
1244 frame->sig.len = tmp.len;
1245
1246 /* Check that we consumed the entire `Certificate` structure. */
1247 if( p != end )
1248 {
1249 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1250 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1251 }
1252
1253 /* Parse TBSCertificate structure
1254 *
1255 * TBSCertificate ::= SEQUENCE {
1256 * version [0] EXPLICIT Version DEFAULT v1,
1257 * serialNumber CertificateSerialNumber,
1258 * signature AlgorithmIdentifier,
1259 * issuer Name,
1260 * validity Validity,
1261 * subject Name,
1262 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1263 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1264 * -- If present, version MUST be v2 or v3
1265 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1266 * -- If present, version MUST be v2 or v3
1267 * extensions [3] EXPLICIT Extensions OPTIONAL
1268 * -- If present, version MUST be v3
1269 * }
1270 */
1271 end = frame->tbs.p + frame->tbs.len;
1272 p = tbs_start;
1273
1274 /*
1275 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1276 */
1277 {
1278 int version;
1279 ret = x509_get_version( &p, end, &version );
1280 if( ret != 0 )
1281 return( ret );
1282
1283 if( version < 0 || version > 2 )
1284 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1285
1286 frame->version = version + 1;
1287 }
1288
1289 /*
1290 * CertificateSerialNumber ::= INTEGER
1291 */
1292 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1293 if( ret != 0 )
1294 return( ret );
1295
1296 frame->serial.p = tmp.p;
1297 frame->serial.len = tmp.len;
1298
1299 /*
1300 * signature AlgorithmIdentifier
1301 */
1302 inner_sig_alg_start = p;
1303 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1304 &frame->sig_pk, NULL );
1305 if( ret != 0 )
1306 return( ret );
1307 inner_sig_alg_len = p - inner_sig_alg_start;
1308
1309 frame->sig_alg.p = inner_sig_alg_start;
1310 frame->sig_alg.len = inner_sig_alg_len;
1311
1312 /* Consistency check:
1313 * Inner and outer AlgorithmIdentifier structures must coincide:
1314 *
1315 * Quoting RFC 5280, Section 4.1.1.2:
1316 * This field MUST contain the same algorithm identifier as the
1317 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1318 */
1319 if( outer_sig_alg.len != inner_sig_alg_len ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03001320 mbedtls_platform_memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
Hanno Becker21f55672019-02-15 15:27:59 +00001321 {
1322 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1323 }
1324
1325 /*
1326 * issuer Name
1327 *
1328 * Name ::= CHOICE { -- only one possibility for now --
1329 * rdnSequence RDNSequence }
1330 *
1331 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1332 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001333 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001334
1335 ret = mbedtls_asn1_get_tag( &p, end, &len,
1336 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1337 if( ret != 0 )
1338 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001339 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001340 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001341
Hanno Becker3aa12162019-07-02 16:47:40 +01001342 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001343 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1344 &frame->issuer_raw,
1345 NULL, NULL );
1346 if( ret != 0 )
1347 return( ret );
1348
Hanno Becker21f55672019-02-15 15:27:59 +00001349 /*
1350 * Validity ::= SEQUENCE { ...
1351 */
Hanno Becker843b71a2019-06-25 09:39:21 +01001352#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker21f55672019-02-15 15:27:59 +00001353 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1354 if( ret != 0 )
1355 return( ret );
Hanno Becker843b71a2019-06-25 09:39:21 +01001356#else /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1357 ret = x509_skip_dates( &p, end );
1358 if( ret != 0 )
1359 return( ret );
1360#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker21f55672019-02-15 15:27:59 +00001361
1362 /*
1363 * subject Name
1364 *
1365 * Name ::= CHOICE { -- only one possibility for now --
1366 * rdnSequence RDNSequence }
1367 *
1368 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1369 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001370 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001371
1372 ret = mbedtls_asn1_get_tag( &p, end, &len,
1373 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1374 if( ret != 0 )
1375 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001376 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001377 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001378
Hanno Becker3aa12162019-07-02 16:47:40 +01001379 /* Comparing the raw buffer to itself amounts to structural validation. */
Hanno Becker21f55672019-02-15 15:27:59 +00001380 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1381 &frame->subject_raw,
1382 NULL, NULL );
1383 if( ret != 0 )
1384 return( ret );
1385
Hanno Becker21f55672019-02-15 15:27:59 +00001386 /*
1387 * SubjectPublicKeyInfo
1388 */
1389 frame->pubkey_raw.p = p;
1390 ret = mbedtls_asn1_get_tag( &p, end, &len,
1391 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1392 if( ret != 0 )
1393 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1394 p += len;
1395 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1396
Hanno Becker97aa4362019-06-08 07:38:20 +01001397 if( frame->version != 1 )
Hanno Becker21f55672019-02-15 15:27:59 +00001398 {
Hanno Beckerd07614c2019-06-25 10:19:58 +01001399#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Beckerfd64f142019-06-07 11:47:12 +01001400 /*
1401 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1402 * -- If present, version shall be v2 or v3
1403 */
Hanno Beckere9084122019-06-07 12:04:39 +01001404 ret = x509_get_uid( &p, end, &frame->issuer_id, 1 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001405 if( ret != 0 )
1406 return( ret );
1407
Hanno Beckerfd64f142019-06-07 11:47:12 +01001408 /*
1409 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1410 * -- If present, version shall be v2 or v3
1411 */
Hanno Beckere9084122019-06-07 12:04:39 +01001412 ret = x509_get_uid( &p, end, &frame->subject_id, 2 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001413 if( ret != 0 )
1414 return( ret );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001415#else /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
1416 ret = x509_skip_uid( &p, end, 1 /* implicit tag */ );
1417 if( ret != 0 )
1418 return( ret );
1419 ret = x509_skip_uid( &p, end, 2 /* implicit tag */ );
1420 if( ret != 0 )
1421 return( ret );
1422#endif /* MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker21f55672019-02-15 15:27:59 +00001423 }
1424
1425 /*
1426 * extensions [3] EXPLICIT Extensions OPTIONAL
1427 * -- If present, version shall be v3
1428 */
1429#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1430 if( frame->version == 3 )
1431#endif
1432 {
1433 if( p != end )
1434 {
1435 ret = mbedtls_asn1_get_tag( &p, end, &len,
1436 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1437 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1438 if( len == 0 )
1439 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1440 if( ret != 0 )
1441 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1442
1443 frame->v3_ext.p = p;
1444 frame->v3_ext.len = len;
1445
1446 p += len;
1447 }
1448
1449 ret = x509_crt_frame_parse_ext( frame );
1450 if( ret != 0 )
1451 return( ret );
1452 }
1453
1454 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1455 if( p != end )
1456 {
1457 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1458 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1459 }
1460
1461 return( 0 );
1462}
1463
Hanno Becker12506232019-05-13 13:53:21 +01001464static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001465 mbedtls_x509_name *subject )
1466{
Hanno Becker1e11f212019-03-04 14:43:43 +00001467 return( mbedtls_x509_get_name( frame->subject_raw.p,
1468 frame->subject_raw.len,
1469 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001470}
1471
Hanno Becker12506232019-05-13 13:53:21 +01001472static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001473 mbedtls_x509_name *issuer )
1474{
Hanno Becker1e11f212019-03-04 14:43:43 +00001475 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1476 frame->issuer_raw.len,
1477 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001478}
1479
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001480#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker12506232019-05-13 13:53:21 +01001481static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001482 mbedtls_x509_sequence *subject_alt )
1483{
1484 int ret;
1485 unsigned char *p = frame->subject_alt_raw.p;
1486 unsigned char *end = p + frame->subject_alt_raw.len;
1487
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001488 mbedtls_platform_memset( subject_alt, 0, sizeof( *subject_alt ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001489
1490 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1491 return( 0 );
1492
1493 ret = x509_get_subject_alt_name( p, end, subject_alt );
1494 if( ret != 0 )
1495 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1496 return( ret );
1497}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001498#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker21f55672019-02-15 15:27:59 +00001499
Hanno Becker12506232019-05-13 13:53:21 +01001500static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001501 mbedtls_x509_sequence *ext_key_usage )
1502{
1503 int ret;
1504 unsigned char *p = frame->ext_key_usage_raw.p;
1505 unsigned char *end = p + frame->ext_key_usage_raw.len;
1506
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001507 mbedtls_platform_memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001508
1509 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1510 return( 0 );
1511
1512 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1513 if( ret != 0 )
1514 {
1515 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1516 return( ret );
1517 }
1518
1519 return( 0 );
1520}
1521
Hanno Becker180f7bf2019-02-28 13:23:38 +00001522#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001523static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1524 mbedtls_pk_context *pk )
1525{
1526 unsigned char *p = frame->pubkey_raw.p;
1527 unsigned char *end = p + frame->pubkey_raw.len;
1528 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1529}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001530#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001531
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532/*
1533 * Parse and fill a single X.509 certificate in DER format
1534 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001535static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1536 const unsigned char *buf,
1537 size_t buflen,
1538 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539{
1540 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001541 mbedtls_x509_crt_frame *frame;
1542 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001543
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546
Hanno Becker21f55672019-02-15 15:27:59 +00001547 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548 {
Hanno Becker21f55672019-02-15 15:27:59 +00001549 crt->raw.p = (unsigned char*) buf;
1550 crt->raw.len = buflen;
1551 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001552 }
1553 else
1554 {
Hanno Becker7b8e11e2019-05-03 12:37:12 +01001555 /* Call mbedtls_calloc with buflen + 1 in order to avoid potential
1556 * return of NULL in case of length 0 certificates, which we want
1557 * to cleanly fail with MBEDTLS_ERR_X509_INVALID_FORMAT in the
1558 * core parsing routine, but not here. */
1559 crt->raw.p = mbedtls_calloc( 1, buflen + 1 );
Hanno Becker21f55672019-02-15 15:27:59 +00001560 if( crt->raw.p == NULL )
1561 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1562 crt->raw.len = buflen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001563 mbedtls_platform_memcpy( crt->raw.p, buf, buflen );
Hanno Becker21f55672019-02-15 15:27:59 +00001564
1565 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001566 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001567
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001568 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1569 if( cache == NULL )
1570 {
1571 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1572 goto exit;
1573 }
1574 crt->cache = cache;
1575 x509_crt_cache_init( cache );
1576
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001577#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1578
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001579 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001580 if( ret != 0 )
1581 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
Hanno Becker76428352019-03-05 15:29:23 +00001583 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001584
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001585#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001587 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1588 if( frame == NULL )
1589 {
1590 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1591 goto exit;
1592 }
1593 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001594
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001595 ret = x509_crt_parse_frame( crt->raw.p,
1596 crt->raw.p + crt->raw.len,
1597 frame );
1598 if( ret != 0 )
1599 goto exit;
1600
Hanno Becker21f55672019-02-15 15:27:59 +00001601 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1602 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001603 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1604 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1605 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1606 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001607#if !defined(MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID)
Hanno Becker38f0cb42019-03-04 15:13:45 +00001608 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1609 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
Hanno Beckerd07614c2019-06-25 10:19:58 +01001610#endif /* !MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001611 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1612 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1613 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Becker843b71a2019-06-25 09:39:21 +01001614
1615#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001616 crt->valid_from = frame->valid_from;
1617 crt->valid_to = frame->valid_to;
Hanno Becker843b71a2019-06-25 09:39:21 +01001618#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
1619
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001620 crt->version = frame->version;
1621 crt->ca_istrue = frame->ca_istrue;
1622 crt->max_pathlen = frame->max_pathlen;
1623 crt->ext_types = frame->ext_types;
1624 crt->key_usage = frame->key_usage;
1625 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626
1627 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001628 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 {
Hanno Becker21f55672019-02-15 15:27:59 +00001632 /* sig_oid: Previously, needed for convenience in
1633 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001634 unsigned char *tmp = frame->sig_alg.p;
1635 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001636 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637
Hanno Becker21f55672019-02-15 15:27:59 +00001638 ret = mbedtls_x509_get_alg( &tmp, end,
1639 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640 if( ret != 0 )
1641 {
Hanno Becker21f55672019-02-15 15:27:59 +00001642 /* This should never happen, because we check
1643 * the sanity of the AlgorithmIdentifier structure
1644 * during frame parsing. */
1645 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1646 goto exit;
1647 }
1648 crt->sig_oid = sig_oid;
1649
1650 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001651 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001652 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1653 &crt->sig_md, &crt->sig_pk,
1654 &crt->sig_opts );
1655 if( ret != 0 )
1656 {
1657 /* Again, this should never happen. */
1658 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1659 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660 }
1661 }
1662
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001663 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001664 if( ret != 0 )
1665 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001666
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001667 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001668 if( ret != 0 )
1669 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001671 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001672 if( ret != 0 )
1673 goto exit;
1674
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001675#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001676 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001677 if( ret != 0 )
1678 goto exit;
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03001679#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker21f55672019-02-15 15:27:59 +00001680
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001681 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1682 if( ret != 0 )
1683 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001684#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001685
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001686 /* Currently, we accept DER encoded CRTs with trailing garbage
1687 * and promise to not account for the garbage in the `raw` field.
1688 *
1689 * Note that this means that `crt->raw.len` is not necessarily the
1690 * full size of the heap buffer allocated at `crt->raw.p` in case
1691 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1692 * need the size, and the garbage data doesn't need zeroization. */
1693 crt->raw.len = frame->raw.len;
1694
1695 cache->pk_raw = frame->pubkey_raw;
1696
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001697 /* Free the frame before parsing the public key to
1698 * keep peak RAM usage low. This is slightly inefficient
1699 * because the frame will need to be parsed again on the
1700 * first usage of the CRT, but that seems acceptable.
1701 * As soon as the frame gets used multiple times, it
1702 * will be cached by default. */
1703 x509_crt_cache_clear_frame( crt->cache );
1704
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001705 /* The cache just references the PK structure from the legacy
1706 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001707 * the cache.
1708 *
1709 * We're not actually using the parsed PK context here;
1710 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001711 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001712 if( ret != 0 )
1713 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001714 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001715
1716exit:
1717 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719
Hanno Becker21f55672019-02-15 15:27:59 +00001720 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721}
1722
1723/*
1724 * Parse one X.509 certificate in DER format from a buffer and add them to a
1725 * chained list
1726 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001727static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1728 const unsigned char *buf,
1729 size_t buflen,
1730 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001731{
1732 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734
1735 /*
1736 * Check for valid input
1737 */
1738 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001740
Hanno Becker371e0e42019-02-25 18:08:59 +00001741 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742 {
1743 prev = crt;
1744 crt = crt->next;
1745 }
1746
1747 /*
1748 * Add new certificate on the end of the chain if needed.
1749 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001750 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001752 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753
1754 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001755 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756
1757 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760 }
1761
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001762 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001763 {
1764 if( prev )
1765 prev->next = NULL;
1766
1767 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001769
1770 return( ret );
1771 }
1772
1773 return( 0 );
1774}
1775
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001776int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1777 const unsigned char *buf,
1778 size_t buflen )
1779{
1780 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1781}
1782
1783int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1784 const unsigned char *buf,
1785 size_t buflen )
1786{
1787 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1788}
1789
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001790/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001791 * Parse one or more PEM certificates from a buffer and add them to the chained
1792 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001794int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1795 const unsigned char *buf,
1796 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797{
Janos Follath98e28a72016-05-31 14:03:54 +01001798#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001799 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001801#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802
1803 /*
1804 * Check for valid input
1805 */
1806 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808
1809 /*
1810 * Determine buffer content. Buffer contains either one DER certificate or
1811 * one or more PEM certificates.
1812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001814 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001815 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001818 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1821 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001822#else
1823 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1824#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_PEM_PARSE_C)
1827 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001828 {
1829 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001831
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001832 /* 1 rather than 0 since the terminating NULL byte is counted in */
1833 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001834 {
1835 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001837
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001838 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001840 "-----BEGIN CERTIFICATE-----",
1841 "-----END CERTIFICATE-----",
1842 buf, NULL, 0, &use_len );
1843
1844 if( ret == 0 )
1845 {
1846 /*
1847 * Was PEM encoded
1848 */
1849 buflen -= use_len;
1850 buf += use_len;
1851 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001853 {
1854 return( ret );
1855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001859
1860 /*
1861 * PEM header and footer were found
1862 */
1863 buflen -= use_len;
1864 buf += use_len;
1865
1866 if( first_error == 0 )
1867 first_error = ret;
1868
Paul Bakker5a5fa922014-09-26 14:53:04 +02001869 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001870 continue;
1871 }
1872 else
1873 break;
1874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001878
1879 if( ret != 0 )
1880 {
1881 /*
1882 * Quit parsing on a memory error
1883 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001884 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001885 return( ret );
1886
1887 if( first_error == 0 )
1888 first_error = ret;
1889
1890 total_failed++;
1891 continue;
1892 }
1893
1894 success = 1;
1895 }
1896 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001897
1898 if( success )
1899 return( total_failed );
1900 else if( first_error )
1901 return( first_error );
1902 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001904#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001905}
1906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001908/*
1909 * Load one or more certificates and add them to the chained list
1910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001912{
1913 int ret;
1914 size_t n;
1915 unsigned char *buf;
1916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918 return( ret );
1919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001921
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001922 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001924
1925 return( ret );
1926}
1927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929{
1930 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001931#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001932 int w_ret;
1933 WCHAR szDir[MAX_PATH];
1934 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001935 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001936 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001937
Paul Bakker9af723c2014-05-01 13:03:14 +02001938 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001939 HANDLE hFind;
1940
1941 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001943
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001944 mbedtls_platform_memset( szDir, 0, sizeof(szDir) );
1945 mbedtls_platform_memset( filename, 0, MAX_PATH );
Teppo Järvelin91d79382019-10-02 09:09:31 +03001946 mbedtls_platform_memcpy( filename, path, len );
Paul Bakker9af723c2014-05-01 13:03:14 +02001947 filename[len++] = '\\';
1948 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001949 filename[len++] = '*';
1950
Simon B3c6b18d2016-11-03 01:11:37 +00001951 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001952 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001953 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001955
1956 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001957 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001959
1960 len = MAX_PATH - len;
1961 do
1962 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001963 mbedtls_platform_memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001964
1965 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1966 continue;
1967
Paul Bakker9af723c2014-05-01 13:03:14 +02001968 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001969 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001970 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001971 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001972 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001973 {
1974 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1975 goto cleanup;
1976 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001979 if( w_ret < 0 )
1980 ret++;
1981 else
1982 ret += w_ret;
1983 }
1984 while( FindNextFileW( hFind, &file_data ) != 0 );
1985
Paul Bakker66d5d072014-06-17 16:39:18 +02001986 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001988
Ron Eldor36d90422017-01-09 15:09:16 +02001989cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001991#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001992 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001993 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001994 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001995 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001996 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001997 DIR *dir = opendir( path );
1998
Paul Bakker66d5d072014-06-17 16:39:18 +02001999 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002001
Ron Eldor63140682017-01-09 19:27:59 +02002002#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02002003 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02002004 {
2005 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002006 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02002007 }
Ron Eldor63140682017-01-09 19:27:59 +02002008#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002009
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01002010 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002011 {
Andres AGf9113192016-09-02 14:06:04 +01002012 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
2013 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002014
Andres AGf9113192016-09-02 14:06:04 +01002015 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016 {
Andres AGf9113192016-09-02 14:06:04 +01002017 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
2018 goto cleanup;
2019 }
2020 else if( stat( entry_name, &sb ) == -1 )
2021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002023 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024 }
2025
2026 if( !S_ISREG( sb.st_mode ) )
2027 continue;
2028
2029 // Ignore parse errors
2030 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002032 if( t_ret < 0 )
2033 ret++;
2034 else
2035 ret += t_ret;
2036 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002037
2038cleanup:
Andres AGf9113192016-09-02 14:06:04 +01002039 closedir( dir );
2040
Ron Eldor63140682017-01-09 19:27:59 +02002041#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02002042 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02002044#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01002045
Paul Bakkerbe089b02013-10-14 15:51:50 +02002046#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002047
2048 return( ret );
2049}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002051
Hanno Becker08d34122019-06-25 09:42:57 +01002052typedef struct mbedtls_x509_crt_sig_info
2053{
2054 mbedtls_md_type_t sig_md;
2055 mbedtls_pk_type_t sig_pk;
2056 void *sig_opts;
Hanno Becker08d34122019-06-25 09:42:57 +01002057 size_t crt_hash_len;
2058 mbedtls_x509_buf_raw sig;
2059 mbedtls_x509_buf_raw issuer_raw;
Teppo Järvelinc3e57162019-08-30 11:25:15 +03002060 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
Hanno Becker08d34122019-06-25 09:42:57 +01002061} mbedtls_x509_crt_sig_info;
2062
2063static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2064{
2065#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2066 mbedtls_free( info->sig_opts );
2067#else
2068 ((void) info);
2069#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2070}
2071
2072static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2073 mbedtls_x509_crt_sig_info *info )
2074{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01002075 mbedtls_md_handle_t md_info;
Hanno Becker08d34122019-06-25 09:42:57 +01002076
2077 md_info = mbedtls_md_info_from_type( frame->sig_md );
2078 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2079 info->crt_hash ) != 0 )
2080 {
2081 /* Note: this can't happen except after an internal error */
2082 return( -1 );
2083 }
2084
2085 info->crt_hash_len = mbedtls_md_get_size( md_info );
2086
2087 /* Make sure that this function leaves the target structure
2088 * ready to be freed, regardless of success of failure. */
2089 info->sig_opts = NULL;
2090
2091#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2092 {
2093 int ret;
2094 unsigned char *alg_start = frame->sig_alg.p;
2095 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2096
2097 /* Get signature options -- currently only
2098 * necessary for RSASSA-PSS. */
2099 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2100 &info->sig_pk, &info->sig_opts );
2101 if( ret != 0 )
2102 {
2103 /* Note: this can't happen except after an internal error */
2104 return( -1 );
2105 }
2106 }
2107#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2108 info->sig_md = frame->sig_md;
2109 info->sig_pk = frame->sig_pk;
2110#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2111
2112 info->issuer_raw = frame->issuer_raw;
2113 info->sig = frame->sig;
2114 return( 0 );
2115}
2116
Hanno Becker02a21932019-06-10 15:08:43 +01002117#if !defined(MBEDTLS_X509_REMOVE_INFO)
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002118#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002119static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002121{
2122 size_t i;
2123 size_t n = *size;
2124 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002126 const char *sep = "";
2127 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002128
2129 while( cur != NULL )
2130 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002131 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002132 {
2133 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002134 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002135 }
2136
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002137 n -= cur->buf.len + sep_len;
2138 for( i = 0; i < sep_len; i++ )
2139 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002140 for( i = 0; i < cur->buf.len; i++ )
2141 *p++ = cur->buf.p[i];
2142
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002143 sep = ", ";
2144 sep_len = 2;
2145
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002146 cur = cur->next;
2147 }
2148
2149 *p = '\0';
2150
2151 *size = n;
2152 *buf = p;
2153
2154 return( 0 );
2155}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002156#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002157
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002158#define PRINT_ITEM(i) \
2159 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002161 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002162 sep = ", "; \
2163 }
2164
2165#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002166 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002167 PRINT_ITEM( name );
2168
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002169static int x509_info_cert_type( char **buf, size_t *size,
2170 unsigned char ns_cert_type )
2171{
2172 int ret;
2173 size_t n = *size;
2174 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002175 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002178 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2180 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2181 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002182 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2183 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2184 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002185
2186 *size = n;
2187 *buf = p;
2188
2189 return( 0 );
2190}
2191
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002192#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002193 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002194 PRINT_ITEM( name );
2195
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002196static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002197 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002198{
2199 int ret;
2200 size_t n = *size;
2201 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002202 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2205 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002206 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2207 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2208 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2210 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002211 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2212 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002213
2214 *size = n;
2215 *buf = p;
2216
2217 return( 0 );
2218}
2219
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002220static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002222{
2223 int ret;
2224 const char *desc;
2225 size_t n = *size;
2226 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002228 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002229
2230 while( cur != NULL )
2231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002233 desc = "???";
2234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002236 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002237
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002238 sep = ", ";
2239
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002240 cur = cur->next;
2241 }
2242
2243 *size = n;
2244 *buf = p;
2245
2246 return( 0 );
2247}
2248
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002249/*
2250 * Return an informational string about the certificate.
2251 */
Teppo Järvelinffaba552019-09-03 12:33:16 +03002252#define BEFORE_COLON_CRT 18
2253#define BC_CRT "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002255 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002256{
2257 int ret;
2258 size_t n;
2259 char *p;
Teppo Järvelinffaba552019-09-03 12:33:16 +03002260 char key_size_str[BEFORE_COLON_CRT];
Hanno Becker5226c532019-02-27 17:38:40 +00002261 mbedtls_x509_crt_frame frame;
2262 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002263
Hanno Becker5226c532019-02-27 17:38:40 +00002264 mbedtls_x509_name *issuer = NULL, *subject = NULL;
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002265 mbedtls_x509_sequence *ext_key_usage = NULL;
2266#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
2267 mbedtls_x509_sequence *subject_alt_names = NULL;
2268#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
2269
Hanno Becker4f869ed2019-02-24 16:47:57 +00002270 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002271
2272 p = buf;
2273 n = size;
2274
Hanno Becker4f869ed2019-02-24 16:47:57 +00002275 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002276 mbedtls_pk_init( &pk );
2277
2278 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002279 {
2280 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002281 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Janos Follath98e28a72016-05-31 14:03:54 +01002282
2283 return( (int) ( size - n ) );
2284 }
2285
Hanno Becker5226c532019-02-27 17:38:40 +00002286 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002287 if( ret != 0 )
2288 {
2289 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2290 goto cleanup;
2291 }
2292
Hanno Becker5226c532019-02-27 17:38:40 +00002293 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002294 if( ret != 0 )
2295 {
2296 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2297 goto cleanup;
2298 }
2299
Hanno Becker5226c532019-02-27 17:38:40 +00002300 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002301 if( ret != 0 )
2302 {
2303 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2304 goto cleanup;
2305 }
2306
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002307#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker5226c532019-02-27 17:38:40 +00002308 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002309 if( ret != 0 )
2310 {
2311 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2312 goto cleanup;
2313 }
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002314#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker4f869ed2019-02-24 16:47:57 +00002315
Hanno Becker5226c532019-02-27 17:38:40 +00002316 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002317 if( ret != 0 )
2318 {
2319 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2320 goto cleanup;
2321 }
2322
Hanno Becker5226c532019-02-27 17:38:40 +00002323 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2324 if( ret != 0 )
2325 {
2326 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2327 goto cleanup;
2328 }
2329
2330 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002331 if( ret != 0 )
2332 {
2333 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2334 goto cleanup;
2335 }
2336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002338 prefix, frame.version );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002339 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002340
Hanno Becker4f869ed2019-02-24 16:47:57 +00002341 {
2342 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002343 serial.p = frame.serial.p;
2344 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002345 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2346 prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002347 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002348 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002349 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002350 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002353 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002354 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002355 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002358 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002359 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002360 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002361
Hanno Becker843b71a2019-06-25 09:39:21 +01002362#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002364 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002365 frame.valid_from.year, frame.valid_from.mon,
2366 frame.valid_from.day, frame.valid_from.hour,
2367 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002368 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002372 frame.valid_to.year, frame.valid_to.mon,
2373 frame.valid_to.day, frame.valid_to.hour,
2374 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002375 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker843b71a2019-06-25 09:39:21 +01002376#endif /* MBEDTLS_X509_CRT_REMOVE_TIME */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002379 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002380
Hanno Becker83cd8672019-02-21 17:13:46 +00002381 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2382 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002383 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002384
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002385 /* Key size */
Teppo Järvelinffaba552019-09-03 12:33:16 +03002386 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON_CRT,
Hanno Becker5226c532019-02-27 17:38:40 +00002387 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002388 {
2389 return( ret );
2390 }
2391
Teppo Järvelinffaba552019-09-03 12:33:16 +03002392 ret = mbedtls_snprintf( p, n, "\n%s%-" BC_CRT "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002393 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002394 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002395
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002396 /*
2397 * Optional extensions
2398 */
2399
Hanno Becker5226c532019-02-27 17:38:40 +00002400 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002403 frame.ca_istrue ? "true" : "false" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002404 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002405
Hanno Becker5226c532019-02-27 17:38:40 +00002406 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002407 {
Hanno Becker5226c532019-02-27 17:38:40 +00002408 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002409 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002410 }
2411 }
2412
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002413#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker5226c532019-02-27 17:38:40 +00002414 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002417 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002418
2419 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002420 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002421 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002422 }
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002423#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002424
Hanno Becker5226c532019-02-27 17:38:40 +00002425 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002428 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002429
Hanno Becker5226c532019-02-27 17:38:40 +00002430 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002431 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002432 }
2433
Hanno Becker5226c532019-02-27 17:38:40 +00002434 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002437 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002438
Hanno Becker5226c532019-02-27 17:38:40 +00002439 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002440 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002441 }
2442
Hanno Becker5226c532019-02-27 17:38:40 +00002443 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002446 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002447
2448 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002449 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002450 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002451 }
2452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002454 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002455
Hanno Becker4f869ed2019-02-24 16:47:57 +00002456 ret = (int) ( size - n );
2457
2458cleanup:
2459
Hanno Becker4f869ed2019-02-24 16:47:57 +00002460 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002461 mbedtls_pk_free( &pk );
2462 mbedtls_x509_name_free( issuer );
2463 mbedtls_x509_name_free( subject );
2464 mbedtls_x509_sequence_free( ext_key_usage );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002465#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker5226c532019-02-27 17:38:40 +00002466 mbedtls_x509_sequence_free( subject_alt_names );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03002467#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker4f869ed2019-02-24 16:47:57 +00002468
2469 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002470}
2471
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002472struct x509_crt_verify_string {
2473 int code;
2474 const char *string;
2475};
2476
2477static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002478 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002479 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2480 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2481 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2482 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2483 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002484 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2485 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2486 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002487 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002488 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2489 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2490 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2491 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002492 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2493 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2494 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2495 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2496 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2497 { 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 +01002498 { 0, NULL }
2499};
2500
2501int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002502 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002503{
2504 int ret;
2505 const struct x509_crt_verify_string *cur;
2506 char *p = buf;
2507 size_t n = size;
2508
2509 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2510 {
2511 if( ( flags & cur->code ) == 0 )
2512 continue;
2513
2514 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002515 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002516 flags ^= cur->code;
2517 }
2518
2519 if( flags != 0 )
2520 {
2521 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2522 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002523 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002524 }
2525
2526 return( (int) ( size - n ) );
2527}
Hanno Becker02a21932019-06-10 15:08:43 +01002528#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002531static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2532 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002533{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002534 unsigned int usage_must, usage_may;
2535 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2536 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2537
2538 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2539 return( 0 );
2540
2541 usage_must = usage & ~may_mask;
2542
2543 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2544 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2545
2546 usage_may = usage & may_mask;
2547
2548 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002550
2551 return( 0 );
2552}
Hanno Becker45eedf12019-02-25 13:55:33 +00002553
2554int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2555 unsigned int usage )
2556{
2557 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002558 mbedtls_x509_crt_frame const *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002559 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002560 if( ret != 0 )
2561 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2562
2563 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002564 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002565
2566 return( ret );
2567}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002568#endif
2569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002571typedef struct
2572{
2573 const char *oid;
2574 size_t oid_len;
2575} x509_crt_check_ext_key_usage_cb_ctx_t;
2576
2577static int x509_crt_check_ext_key_usage_cb( void *ctx,
2578 int tag,
2579 unsigned char *data,
2580 size_t data_len )
2581{
2582 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2583 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2584 ((void) tag);
2585
2586 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2587 data, data_len ) == 0 )
2588 {
2589 return( 1 );
2590 }
2591
Teppo Järvelin61f412e2019-10-03 12:25:22 +03002592 if( data_len == cb_ctx->oid_len && mbedtls_platform_memcmp( data, cb_ctx->oid,
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002593 data_len ) == 0 )
2594 {
2595 return( 1 );
2596 }
2597
2598 return( 0 );
2599}
2600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002602 const char *usage_oid,
2603 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002604{
Hanno Beckere1956af2019-02-21 14:28:12 +00002605 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002606 mbedtls_x509_crt_frame const *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002607 unsigned ext_types;
2608 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002609 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002610
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002611 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002612 if( ret != 0 )
2613 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2614
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002615 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002616 ext_types = frame->ext_types;
2617 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2618 {
2619 p = frame->ext_key_usage_raw.p;
2620 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002621
Hanno Beckere9718b42019-02-25 18:11:42 +00002622 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2623 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2624 x509_crt_check_ext_key_usage_cb,
2625 &cb_ctx );
2626 if( ret == 1 )
2627 ret = 0;
2628 else
2629 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2630 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002631
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002632 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002633 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002634}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002638/*
2639 * Return 1 if the certificate is revoked, or 0 otherwise.
2640 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002641static int x509_serial_is_revoked( unsigned char const *serial,
2642 size_t serial_len,
2643 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002646
2647 while( cur != NULL && cur->serial.len != 0 )
2648 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002649 if( serial_len == cur->serial.len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +03002650 mbedtls_platform_memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002651 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002652 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002653 return( 1 );
2654 }
2655
2656 cur = cur->next;
2657 }
2658
2659 return( 0 );
2660}
2661
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002662int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2663 const mbedtls_x509_crl *crl )
2664{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002665 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002666 mbedtls_x509_crt_frame const *frame;
Hanno Becker79ae5b62019-02-25 18:12:00 +00002667
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002668 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002669 if( ret != 0 )
2670 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2671
2672 ret = x509_serial_is_revoked( frame->serial.p,
2673 frame->serial.len,
2674 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002675 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002676 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002677}
2678
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002679/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002680 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002681 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002682 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002683static int x509_crt_verifycrl( unsigned char *crt_serial,
2684 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002685 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002686 mbedtls_x509_crl *crl_list,
2687 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002688{
Hanno Beckerbb266132019-02-25 18:12:46 +00002689 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002690 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +01002692 mbedtls_md_handle_t md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002693 mbedtls_x509_buf_raw ca_subject;
2694 mbedtls_pk_context *pk;
2695 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002696
Hanno Beckerbb266132019-02-25 18:12:46 +00002697 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002698 return( flags );
2699
Hanno Beckerbb266132019-02-25 18:12:46 +00002700 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002701 mbedtls_x509_crt_frame const *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002702 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002703 if( ret != 0 )
2704 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2705
2706 ca_subject = ca->subject_raw;
2707
2708 can_sign = 0;
2709 if( x509_crt_check_key_usage_frame( ca,
2710 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2711 {
2712 can_sign = 1;
2713 }
2714
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002715 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002716 }
2717
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002718 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002719 if( ret != 0 )
2720 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2721
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002722 while( crl_list != NULL )
2723 {
2724 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002725 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002726 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002727 {
2728 crl_list = crl_list->next;
2729 continue;
2730 }
2731
2732 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002733 * Check if the CA is configured to sign CRLs
2734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002736 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002739 break;
2740 }
2741#endif
2742
2743 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002744 * Check if CRL is correctly signed by the trusted CA
2745 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002746 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2747 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2748
2749 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2750 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002753 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002754 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002755 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002757 break;
2758 }
2759
Hanno Beckerbb266132019-02-25 18:12:46 +00002760 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002761 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002762
Hanno Beckerbb266132019-02-25 18:12:46 +00002763 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002765 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002768 break;
2769 }
2770
2771 /*
2772 * Check for validity of CRL (Do not drop out)
2773 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002774 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002776
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002777 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002778 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002779
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002780 /*
2781 * Check if certificate is revoked
2782 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002783 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2784 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002787 break;
2788 }
2789
2790 crl_list = crl_list->next;
2791 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002792
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002793 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002794 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002797
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002798/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002799 * Check the signature of a certificate by its parent
2800 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002801static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002802 mbedtls_x509_crt *parent,
2803 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002804{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002805 int ret;
2806 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002807
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002808 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002809 if( ret != 0 )
2810 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2811
2812 /* Skip expensive computation on obvious mismatch */
2813 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002814 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002815 ret = -1;
2816 goto exit;
2817 }
2818
2819#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2820 ((void) rs_ctx);
2821#else
2822 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2823 {
2824 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002825 sig_info->sig_md,
2826 sig_info->crt_hash, sig_info->crt_hash_len,
2827 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002828 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002829 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002830 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002831#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002832 {
2833 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2834 sig_info->sig_opts,
2835 pk,
2836 sig_info->sig_md,
2837 sig_info->crt_hash, sig_info->crt_hash_len,
2838 sig_info->sig.p, sig_info->sig.len );
2839 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002840
Hanno Beckere449e2d2019-02-25 14:45:31 +00002841exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002842 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002843 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002844}
2845
2846/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002847 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2848 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002849 *
2850 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002851 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002852static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2853 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002854 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002855{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002856 int need_ca_bit;
2857
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002858 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002859 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2860 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002861 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002862 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002863 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002864 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002865
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002866 /* Parent must have the basicConstraints CA bit set as a general rule */
2867 need_ca_bit = 1;
2868
2869 /* Exception: v1/v2 certificates that are locally trusted. */
2870 if( top && parent->version < 3 )
2871 need_ca_bit = 0;
2872
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002873 if( need_ca_bit && ! parent->ca_istrue )
2874 return( -1 );
2875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002877 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002878 x509_crt_check_key_usage_frame( parent,
2879 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002880 {
2881 return( -1 );
2882 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002883#endif
2884
2885 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002886}
2887
Manuel Pégourié-Gonnardf66657a2019-11-08 11:14:09 +01002888/* This value is different enough from 0 that it's hard for an active physical
2889 * attacker to reach it just by flipping a few bits. */
2890#define X509_SIGNATURE_IS_GOOD 0x7f5a5a5a
2891
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002892/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002893 * Find a suitable parent for child in candidates, or return NULL.
2894 *
2895 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002896 * 1. subject name matches child's issuer
2897 * 2. if necessary, the CA bit is set and key usage allows signing certs
2898 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002899 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002900 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002901 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002902 * If there's a suitable candidate which is also time-valid, return the first
2903 * such. Otherwise, return the first suitable candidate (or NULL if there is
2904 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002905 *
2906 * The rationale for this rule is that someone could have a list of trusted
2907 * roots with two versions on the same root with different validity periods.
2908 * (At least one user reported having such a list and wanted it to just work.)
2909 * The reason we don't just require time-validity is that generally there is
2910 * only one version, and if it's expired we want the flags to state that
2911 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002912 *
2913 * The rationale for rule 3 (signature for trusted roots) is that users might
2914 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002915 * way we select the correct one is by checking the signature (as we don't
2916 * rely on key identifier extensions). (This is one way users might choose to
2917 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002918 *
2919 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002920 * - [in] child: certificate for which we're looking for a parent
2921 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002922 * - [out] r_parent: parent found (or NULL)
Manuel Pégourié-Gonnardf66657a2019-11-08 11:14:09 +01002923 * - [out] r_signature_is_good: set to X509_SIGNATURE_IS_GOOD if
2924 * child signature by parent is valid, or to 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002925 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2926 * of the chain, 0 otherwise
2927 * - [in] path_cnt: number of intermediates seen so far
2928 * - [in] self_cnt: number of self-signed intermediates seen so far
2929 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002930 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002931 *
2932 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002933 * - 0 on success
2934 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002935 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002936static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002937 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002938 mbedtls_x509_crt *candidates,
2939 mbedtls_x509_crt **r_parent,
2940 int *r_signature_is_good,
2941 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002942 unsigned path_cnt,
2943 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002944 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002945{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002946 int ret;
Manuel Pégourié-Gonnard6bdc6802019-11-28 10:29:41 +01002947 volatile int ret_fi = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002948 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard6bdc6802019-11-28 10:29:41 +01002949 int signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002950
2951#if defined(MBEDTLS_HAVE_TIME_DATE)
2952 mbedtls_x509_crt *fallback_parent;
2953 int fallback_signature_is_good;
2954#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002955
2956#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002957 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002958 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2959 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002960 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002961 parent_crt = rs_ctx->parent;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002962#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002963 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002964 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002965#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002966
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002967 /* clear saved state */
2968 rs_ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002969#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002970 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002971 rs_ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002972#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002973
2974 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002975 goto check_signature;
2976 }
2977#endif
2978
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002979#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002980 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002981 fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01002982#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002983
Hanno Becker43bf9002019-02-25 14:46:49 +00002984 for( parent_crt = candidates; parent_crt != NULL;
2985 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002986 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002987 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002988
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002989#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2990check_signature:
2991#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002992
2993 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002994 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002995 mbedtls_x509_crt_frame const *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002996
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002997 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002998 if( ret != 0 )
2999 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00003000
Hanno Becker843b71a2019-06-25 09:39:21 +01003001#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker040c5642019-06-10 11:14:24 +01003002 if( !mbedtls_x509_time_is_past( &parent->valid_to ) &&
3003 !mbedtls_x509_time_is_future( &parent->valid_from ) )
Manuel Pégourié-Gonnardf1358ac2019-07-30 16:03:06 +02003004#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker43bf9002019-02-25 14:46:49 +00003005 {
3006 parent_valid = 1;
3007 }
3008
3009 /* basic parenting skills (name, CA bit, key usage) */
3010 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
3011 parent_match = 1;
3012
3013 /* +1 because the stored max_pathlen is 1 higher
3014 * than the actual value */
3015 if( !( parent->max_pathlen > 0 &&
3016 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
3017 {
3018 path_len_ok = 1;
3019 }
3020
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003021 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00003022 }
3023
3024 if( parent_match == 0 || path_len_ok == 0 )
3025 continue;
3026
3027 /* Signature */
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003028 ret_fi = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003029
3030#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003031 if( rs_ctx != NULL && ret_fi == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003032 {
3033 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00003034 rs_ctx->parent = parent_crt;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003035#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003036 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003037 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003038#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003039
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003040 return( ret_fi );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003041 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003042#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003043
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003044 if( ret_fi == 0 )
3045 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02003046 mbedtls_platform_random_delay();
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003047 if( ret_fi == 0 )
Manuel Pégourié-Gonnardf66657a2019-11-08 11:14:09 +01003048 signature_is_good = X509_SIGNATURE_IS_GOOD;
Manuel Pégourié-Gonnardd1e55df2019-11-08 11:02:56 +01003049 }
3050
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003051 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02003052 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02003053
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003054 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00003055 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003056 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003057#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003058 if( fallback_parent == NULL )
3059 {
Hanno Becker43bf9002019-02-25 14:46:49 +00003060 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003061 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003062 }
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003063#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003064
3065 continue;
3066 }
3067
Manuel Pégourié-Gonnard8abd0a02019-09-10 11:24:29 +02003068 *r_parent = parent_crt;
Andy Gross3fc6f9d2019-01-30 10:25:53 -06003069 *r_signature_is_good = signature_is_good;
3070
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003071 break;
3072 }
3073
Manuel Pégourié-Gonnard8abd0a02019-09-10 11:24:29 +02003074 if( parent_crt == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003075 {
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003076#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003077 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003078 *r_signature_is_good = fallback_signature_is_good;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003079#else /* MBEDTLS_HAVE_TIME_DATE */
3080 *r_parent = NULL;
3081#endif /* !MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003082 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02003083
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003084 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02003085}
3086
3087/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003088 * Find a parent in trusted CAs or the provided chain, or return NULL.
3089 *
3090 * Searches in trusted CAs first, and return the first suitable parent found
3091 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003092 *
3093 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003094 * - [in] child: certificate for which we're looking for a parent, followed
3095 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003096 * - [in] trust_ca: list of locally trusted certificates
3097 * - [out] parent: parent found (or NULL)
3098 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
3099 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
3100 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
3101 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003102 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003103 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01003104 *
3105 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02003106 * - 0 on success
3107 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003108 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003109static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00003110 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00003111 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003112 mbedtls_x509_crt *trust_ca,
3113 mbedtls_x509_crt **parent,
3114 int *parent_is_trusted,
3115 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003116 unsigned path_cnt,
3117 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003118 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003119{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003120 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003121 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003122
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003123 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003124
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003125#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003126 /* restore then clear saved state if we have some stored */
3127 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
3128 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003129 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003130 rs_ctx->parent_is_trusted = -1;
3131 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003132#endif
3133
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003134 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00003135 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003136
Hanno Becker5299cf82019-02-25 13:50:41 +00003137 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003138 parent, signature_is_good,
3139 *parent_is_trusted,
3140 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003141
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003142#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003143 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3144 {
3145 /* save state */
3146 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003147 return( ret );
3148 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003149#else
3150 (void) ret;
3151#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003152
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003153 /* stop here if found or already in second iteration */
3154 if( *parent != NULL || *parent_is_trusted == 0 )
3155 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003156
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003157 /* prepare second iteration */
3158 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003159 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003160
3161 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01003162 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003163 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02003164 *parent_is_trusted = 0;
3165 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003166 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003167
3168 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003169}
3170
3171/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003172 * Check if an end-entity certificate is locally trusted
3173 *
3174 * Currently we require such certificates to be self-signed (actually only
3175 * check for self-issued as self-signatures are not checked)
3176 */
3177static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003178 mbedtls_x509_crt_frame const *crt,
3179 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003180{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003181 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003182
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003183 /* look for an exact match with trusted cert */
3184 for( cur = trust_ca; cur != NULL; cur = cur->next )
3185 {
3186 if( crt->raw.len == cur->raw.len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +03003187 mbedtls_platform_memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003188 {
3189 return( 0 );
3190 }
3191 }
3192
3193 /* too bad */
3194 return( -1 );
3195}
3196
Hanno Becker8d6d3202019-08-16 17:18:15 +01003197#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3198
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003199/*
Hanno Beckeradc282a2019-08-16 17:14:25 +01003200 * Reset (init or clear) a verify_chain
3201 */
3202static void x509_crt_verify_chain_reset(
3203 mbedtls_x509_crt_verify_chain *ver_chain )
3204{
3205 size_t i;
3206
3207 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
3208 {
3209 ver_chain->items[i].crt = NULL;
3210 ver_chain->items[i].flags = (uint32_t) -1;
3211 }
3212
3213 ver_chain->len = 0;
3214}
3215
3216/*
3217 * Merge the flags for all certs in the chain, after calling callback
3218 */
3219static int x509_crt_verify_chain_get_flags(
3220 const mbedtls_x509_crt_verify_chain *ver_chain,
3221 uint32_t *flags,
3222 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3223 void *p_vrfy )
3224{
3225 int ret;
3226 unsigned i;
3227 uint32_t cur_flags;
3228 const mbedtls_x509_crt_verify_chain_item *cur;
3229
3230 for( i = ver_chain->len; i != 0; --i )
3231 {
3232 cur = &ver_chain->items[i-1];
3233 cur_flags = cur->flags;
3234
3235 if( NULL != f_vrfy )
3236 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
3237 return( ret );
3238
3239 *flags |= cur_flags;
3240 }
3241
3242 return( 0 );
3243}
3244
3245static void x509_crt_verify_chain_add_ee_flags(
3246 mbedtls_x509_crt_verify_chain *chain,
3247 uint32_t ee_flags )
3248{
3249 chain->items[0].flags |= ee_flags;
3250}
3251
3252static void x509_crt_verify_chain_add_crt(
3253 mbedtls_x509_crt_verify_chain *chain,
3254 mbedtls_x509_crt *crt )
3255{
3256 mbedtls_x509_crt_verify_chain_item *cur;
3257 cur = &chain->items[chain->len];
3258 cur->crt = crt;
3259 cur->flags = 0;
3260 chain->len++;
3261}
3262
3263static uint32_t* x509_crt_verify_chain_get_cur_flags(
3264 mbedtls_x509_crt_verify_chain *chain )
3265{
3266 return( &chain->items[chain->len - 1].flags );
3267}
3268
3269static unsigned x509_crt_verify_chain_len(
3270 mbedtls_x509_crt_verify_chain const *chain )
3271{
3272 return( chain->len );
3273}
3274
Hanno Becker14b0a682019-08-29 15:26:15 +01003275#else /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Hanno Becker8d6d3202019-08-16 17:18:15 +01003276
3277/*
3278 * Reset (init or clear) a verify_chain
3279 */
3280static void x509_crt_verify_chain_reset(
3281 mbedtls_x509_crt_verify_chain *ver_chain )
3282{
3283 ver_chain->len = 0;
3284 ver_chain->flags = 0;
3285}
3286
3287/*
3288 * Merge the flags for all certs in the chain, after calling callback
3289 */
3290static int x509_crt_verify_chain_get_flags(
3291 const mbedtls_x509_crt_verify_chain *ver_chain,
3292 uint32_t *flags,
3293 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3294 void *p_vrfy )
3295{
3296 ((void) f_vrfy);
3297 ((void) p_vrfy);
3298 *flags = ver_chain->flags;
3299 return( 0 );
3300}
3301
3302static void x509_crt_verify_chain_add_ee_flags(
3303 mbedtls_x509_crt_verify_chain *chain,
3304 uint32_t ee_flags )
3305{
3306 chain->flags |= ee_flags;
3307}
3308
3309static void x509_crt_verify_chain_add_crt(
3310 mbedtls_x509_crt_verify_chain *chain,
3311 mbedtls_x509_crt *crt )
3312{
3313 ((void) crt);
3314 chain->len++;
3315}
3316
3317static uint32_t* x509_crt_verify_chain_get_cur_flags(
3318 mbedtls_x509_crt_verify_chain *chain )
3319{
3320 return( &chain->flags );
3321}
3322
3323static unsigned x509_crt_verify_chain_len(
3324 mbedtls_x509_crt_verify_chain const *chain )
3325{
3326 return( chain->len );
3327}
3328
3329#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3330
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003331/*
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003332 * This is used in addition to the flag for a specific issue, to ensure that
3333 * it is not possible for an active physical attacker to entirely clear the
3334 * flags just by flipping a single bit. Take advantage of the fact that all
3335 * values defined in include/mbedtls/x509.h so far are 24-bit or less, so the
3336 * top byte is free.
3337 *
3338 * Currently this protection is not compatible with the vrfy callback (as it
3339 * can observ and modify flags freely), so it's only enabled when the callback
3340 * is disabled.
3341 */
3342#if defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3343#define X509_BADCERT_FI_EXTRA 0xff000000u
3344#else
3345#define X509_BADCERT_FI_EXTRA 0u
3346#endif
3347
3348/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003349 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003350 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003351 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3352 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003353 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003354 * such that every cert in the chain is a child of the next one,
3355 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003356 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003357 * Verify that chain and return it with flags for all issues found.
3358 *
3359 * Special cases:
3360 * - EE == Rj -> return a one-element list containing it
3361 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3362 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003363 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003364 * Tests for (aspects of) this function should include at least:
3365 * - trusted EE
3366 * - EE -> trusted root
Antonin Décimod5f47592019-01-23 15:24:37 +01003367 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003368 * - if relevant: EE untrusted
3369 * - if relevant: EE -> intermediate, untrusted
3370 * with the aspect under test checked at each relevant level (EE, int, root).
3371 * For some aspects longer chains are required, but usually length 2 is
3372 * enough (but length 1 is not in general).
3373 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003374 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003375 * - [in] crt: the cert list EE, C1, ..., Cn
3376 * - [in] trust_ca: the trusted list R1, ..., Rp
3377 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003378 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003379 * Only valid when return value is 0, may contain garbage otherwise!
3380 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003381 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003382 *
3383 * Return value:
3384 * - non-zero if the chain could not be fully built and examined
3385 * - 0 is the chain was successfully built and examined,
3386 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003387 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003388static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003389 mbedtls_x509_crt *crt,
3390 mbedtls_x509_crt *trust_ca,
3391 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003392 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003393 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003394 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003395{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003396 /* Don't initialize any of those variables here, so that the compiler can
3397 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003398 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003399 uint32_t *flags;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003400 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003401 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003402 int parent_is_trusted;
3403 int child_is_trusted;
3404 int signature_is_good;
Manuel Pégourié-Gonnard81c1fc42019-11-08 11:25:16 +01003405 volatile int signature_is_good_fi;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003406 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003407
3408#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3409 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003410 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003411 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003412 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003413 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003414 self_cnt = rs_ctx->self_cnt;
Hanno Beckeradc282a2019-08-16 17:14:25 +01003415 child_crt = rs_ctx->cur_crt;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003416
3417 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003418 goto find_parent;
3419 }
3420#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003421
Hanno Becker1e0677a2019-02-25 14:58:22 +00003422 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003423 self_cnt = 0;
3424 parent_is_trusted = 0;
3425 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003426
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003427 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003428#if defined(MBEDTLS_X509_CRL_PARSE_C)
3429 mbedtls_x509_buf_raw child_serial;
3430#endif /* MBEDTLS_X509_CRL_PARSE_C */
3431 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003432
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003433 /* Add certificate to the verification chain */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003434 x509_crt_verify_chain_add_crt( ver_chain, child_crt );
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003435
3436#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3437find_parent:
3438#endif
3439
Hanno Beckeradc282a2019-08-16 17:14:25 +01003440 flags = x509_crt_verify_chain_get_cur_flags( ver_chain );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003441
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003442 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003443 mbedtls_x509_crt_sig_info child_sig;
3444 {
Hanno Becker5f268b32019-05-20 16:26:34 +01003445 mbedtls_x509_crt_frame const *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003446
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003447 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003448 if( ret != 0 )
3449 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3450
Hanno Becker843b71a2019-06-25 09:39:21 +01003451#if !defined(MBEDTLS_X509_CRT_REMOVE_TIME)
Hanno Becker5299cf82019-02-25 13:50:41 +00003452 /* Check time-validity (all certificates) */
3453 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003454 *flags |= MBEDTLS_X509_BADCERT_EXPIRED | X509_BADCERT_FI_EXTRA;
Hanno Becker5299cf82019-02-25 13:50:41 +00003455 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003456 *flags |= MBEDTLS_X509_BADCERT_FUTURE | X509_BADCERT_FI_EXTRA;
Hanno Becker843b71a2019-06-25 09:39:21 +01003457#endif /* !MBEDTLS_X509_CRT_REMOVE_TIME */
Hanno Becker5299cf82019-02-25 13:50:41 +00003458
3459 /* Stop here for trusted roots (but not for trusted EE certs) */
3460 if( child_is_trusted )
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 self_issued = 0;
3467 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3468 &child->subject_raw,
3469 NULL, NULL ) == 0 )
3470 {
3471 self_issued = 1;
3472 }
3473
3474 /* Check signature algorithm: MD & PK algs */
3475 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003476 *flags |= MBEDTLS_X509_BADCERT_BAD_MD | X509_BADCERT_FI_EXTRA;
Hanno Becker5299cf82019-02-25 13:50:41 +00003477
3478 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003479 *flags |= MBEDTLS_X509_BADCERT_BAD_PK | X509_BADCERT_FI_EXTRA;
Hanno Becker5299cf82019-02-25 13:50:41 +00003480
3481 /* Special case: EE certs that are locally trusted */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003482 if( x509_crt_verify_chain_len( ver_chain ) == 1 && self_issued &&
Hanno Becker5299cf82019-02-25 13:50:41 +00003483 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3484 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003485 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003486 return( 0 );
3487 }
3488
3489#if defined(MBEDTLS_X509_CRL_PARSE_C)
3490 child_serial = child->serial;
3491#endif /* MBEDTLS_X509_CRL_PARSE_C */
3492
3493 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003494 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003495
Hanno Becker5299cf82019-02-25 13:50:41 +00003496 if( ret != 0 )
3497 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3498 }
3499
3500 /* Look for a parent in trusted CAs or up the chain */
3501 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003502 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003503 &parent_is_trusted, &signature_is_good,
Hanno Beckeradc282a2019-08-16 17:14:25 +01003504 x509_crt_verify_chain_len( ver_chain ) - 1,
3505 self_cnt, rs_ctx );
Hanno Becker5299cf82019-02-25 13:50:41 +00003506
3507 x509_crt_free_sig_info( &child_sig );
3508 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003509
3510#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003511 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3512 {
3513 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003514 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003515 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003516 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003517 rs_ctx->cur_crt = child_crt;
Hanno Becker5299cf82019-02-25 13:50:41 +00003518 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003519 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003520#else
3521 (void) ret;
3522#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003523
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003524 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003525 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003526 {
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003527 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED | X509_BADCERT_FI_EXTRA;
Hanno Becker5299cf82019-02-25 13:50:41 +00003528 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003529 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003530
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003531 /* Count intermediate self-issued (not necessarily self-signed) certs.
3532 * These can occur with some strategies for key rollover, see [SIRO],
3533 * and should be excluded from max_pathlen checks. */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003534 if( x509_crt_verify_chain_len( ver_chain ) != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003535 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003536
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003537 /* path_cnt is 0 for the first intermediate CA,
3538 * and if parent is trusted it's not an intermediate CA */
3539 if( ! parent_is_trusted &&
Hanno Beckeradc282a2019-08-16 17:14:25 +01003540 x509_crt_verify_chain_len( ver_chain ) >
3541 MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003542 {
3543 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003544 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003545 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003546
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003547 /* signature was checked while searching parent */
Manuel Pégourié-Gonnard81c1fc42019-11-08 11:25:16 +01003548 signature_is_good_fi = signature_is_good;
3549 if( signature_is_good_fi != X509_SIGNATURE_IS_GOOD )
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003550 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED | X509_BADCERT_FI_EXTRA;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003551
Arto Kinnunenac6d2262020-01-09 10:11:20 +02003552 mbedtls_platform_random_delay();
Manuel Pégourié-Gonnard18761922019-11-14 09:19:08 +01003553 if( signature_is_good_fi != X509_SIGNATURE_IS_GOOD )
3554 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED | X509_BADCERT_FI_EXTRA;
Manuel Pégourié-Gonnard81c1fc42019-11-08 11:25:16 +01003555
Hanno Becker58c35642019-02-25 18:13:46 +00003556 {
3557 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003558 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003559 if( ret != 0 )
3560 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3561
3562 /* check size of signing key */
3563 if( x509_profile_check_key( profile, parent_pk ) != 0 )
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003564 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY | X509_BADCERT_FI_EXTRA;
Hanno Becker58c35642019-02-25 18:13:46 +00003565
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003566 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003567 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003570 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003571 *flags |= x509_crt_verifycrl( child_serial.p,
3572 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003573 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003574#else
3575 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003576#endif
3577
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003578 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003579 child_crt = parent_crt;
3580 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003581 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003582 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003583 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003584}
3585
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003586#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003587/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003588 * Check for CN match
3589 */
Hanno Becker24926222019-02-21 13:10:55 +00003590static int x509_crt_check_cn( unsigned char const *buf,
3591 size_t buflen,
3592 const char *cn,
3593 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003594{
Hanno Becker24926222019-02-21 13:10:55 +00003595 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003596 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003597 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003598
3599 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003600 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003601 {
3602 return( 0 );
3603 }
3604
3605 return( -1 );
3606}
3607
Hanno Becker8b543b32019-02-21 11:50:44 +00003608/* Returns 1 on a match and 0 on a mismatch.
3609 * This is because this function is used as a callback for
3610 * mbedtls_x509_name_cmp_raw(), which continues the name
3611 * traversal as long as the callback returns 0. */
3612static int x509_crt_check_name( void *ctx,
3613 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003614 mbedtls_x509_buf *val,
3615 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003616{
3617 char const *cn = (char const*) ctx;
3618 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003619 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003620
3621 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003622 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003623 {
3624 return( 1 );
3625 }
3626
3627 return( 0 );
3628}
3629
Hanno Beckerda410822019-02-21 13:36:59 +00003630/* Returns 1 on a match and 0 on a mismatch.
3631 * This is because this function is used as a callback for
3632 * mbedtls_asn1_traverse_sequence_of(), which continues the
3633 * traversal as long as the callback returns 0. */
3634static int x509_crt_subject_alt_check_name( void *ctx,
3635 int tag,
3636 unsigned char *data,
3637 size_t data_len )
3638{
3639 char const *cn = (char const*) ctx;
3640 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003641 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003642
3643 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3644 return( 1 );
3645
3646 return( 0 );
3647}
3648
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003649/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003650 * Verify the requested CN - only call this if cn is not NULL!
3651 */
Hanno Becker082435c2019-02-25 18:14:40 +00003652static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3653 const char *cn,
3654 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003655{
Hanno Beckerda410822019-02-21 13:36:59 +00003656 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01003657 mbedtls_x509_crt_frame const *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003658
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003659 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003660 if( ret != 0 )
3661 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3662
3663 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003664 {
Hanno Becker90b94082019-02-21 21:13:21 +00003665 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003666 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003667 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003668 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003669
Hanno Becker90b94082019-02-21 21:13:21 +00003670 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3671 MBEDTLS_ASN1_TAG_CLASS_MASK,
3672 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3673 MBEDTLS_ASN1_TAG_VALUE_MASK,
3674 2 /* SubjectAlt DNS */,
3675 x509_crt_subject_alt_check_name,
Hanno Becker484caf02019-05-29 14:41:44 +01003676 (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003677 }
3678 else
3679 {
Hanno Becker082435c2019-02-25 18:14:40 +00003680 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3681 &frame->subject_raw,
Hanno Becker484caf02019-05-29 14:41:44 +01003682 x509_crt_check_name, (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003683 }
Hanno Beckerda410822019-02-21 13:36:59 +00003684
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003685 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003686
3687 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3688 * return 1 when finding a name component matching `cn`. */
3689 if( ret == 1 )
3690 return( 0 );
3691
3692 if( ret != 0 )
3693 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3694
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003695 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH | X509_BADCERT_FI_EXTRA;
Hanno Becker082435c2019-02-25 18:14:40 +00003696 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003697}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003698#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003699
3700/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003701 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3704 mbedtls_x509_crt *trust_ca,
3705 mbedtls_x509_crl *ca_crl,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003706#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3707 const char *cn,
3708#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003709 uint32_t *flags
3710#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3711 , int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *)
3712 , void *p_vrfy
3713#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3714 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003715{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003716 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003717 &mbedtls_x509_crt_profile_default,
3718#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3719 cn,
3720#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
3721 flags,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003722#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3723 f_vrfy, p_vrfy,
3724#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3725 NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003726}
3727
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003728/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003729 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003730 */
3731int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3732 mbedtls_x509_crt *trust_ca,
3733 mbedtls_x509_crl *ca_crl,
3734 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003735#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3736 const char *cn,
3737#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003738 uint32_t *flags
3739#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3740 , int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *)
3741 , void *p_vrfy
3742#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3743 )
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003744{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003745 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003746 profile,
3747#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3748 cn,
3749#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003750 flags,
3751#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
3752 f_vrfy, p_vrfy,
3753#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
3754 NULL ) );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003755}
3756
3757/*
3758 * Verify the certificate validity, with profile, restartable version
3759 *
3760 * This function:
3761 * - checks the requested CN (if any)
3762 * - checks the type and size of the EE cert's key,
3763 * as that isn't done as part of chain building/verification currently
3764 * - builds and verifies the chain
3765 * - then calls the callback and merges the flags
3766 */
3767int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3768 mbedtls_x509_crt *trust_ca,
3769 mbedtls_x509_crl *ca_crl,
3770 const mbedtls_x509_crt_profile *profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003771#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
3772 const char *cn,
3773#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
3774 uint32_t *flags,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003775#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003776 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3777 void *p_vrfy,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003778#endif /* !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003779 mbedtls_x509_crt_restart_ctx *rs_ctx )
3780{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003781 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003782 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003783 uint32_t ee_flags;
Manuel Pégourié-Gonnard9ca11fc2019-11-28 12:07:01 +01003784 volatile uint32_t flags_fi = (uint32_t) -1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003785
3786 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003787 ee_flags = 0;
3788 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003789
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003790 if( profile == NULL )
3791 {
3792 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3793 goto exit;
3794 }
3795
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003796#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003797 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003798 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003799 {
3800 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3801 if( ret != 0 )
3802 return( ret );
3803 }
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003804#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003805
Hanno Becker87233362019-02-25 18:15:33 +00003806 {
3807 mbedtls_pk_context *pk;
3808 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003809
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003810 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003811 if( ret != 0 )
3812 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003813
Hanno Becker87233362019-02-25 18:15:33 +00003814 /* Check the type and size of the key */
3815 pk_type = mbedtls_pk_get_type( pk );
3816
3817 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003818 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK | X509_BADCERT_FI_EXTRA;
Hanno Becker87233362019-02-25 18:15:33 +00003819
3820 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003821 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY | X509_BADCERT_FI_EXTRA;
Hanno Becker87233362019-02-25 18:15:33 +00003822
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003823 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003824 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003825
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003826 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003827 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003828 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003829
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003830 if( ret != 0 )
3831 goto exit;
3832
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003833 /* Merge end-entity flags */
Hanno Beckeradc282a2019-08-16 17:14:25 +01003834 x509_crt_verify_chain_add_ee_flags( &ver_chain, ee_flags );
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003835
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003836 /* Build final flags, calling callback on the way if any */
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003837#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Beckeradc282a2019-08-16 17:14:25 +01003838 ret = x509_crt_verify_chain_get_flags( &ver_chain, flags, f_vrfy, p_vrfy );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003839#else
Hanno Beckeradc282a2019-08-16 17:14:25 +01003840 ret = x509_crt_verify_chain_get_flags( &ver_chain, flags, NULL, NULL );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01003841#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003842
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003843exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003844#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3845 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3846 mbedtls_x509_crt_restart_free( rs_ctx );
3847#endif
3848
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003849 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3850 * the SSL module for authmode optional, but non-zero return from the
3851 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003852 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3853 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3854
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003855 if( ret != 0 )
3856 {
3857 *flags = (uint32_t) -1;
3858 return( ret );
3859 }
3860
Manuel Pégourié-Gonnard4c9b5562019-11-12 10:45:32 +01003861 flags_fi = *flags;
3862 if( flags_fi == 0 )
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003863 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02003864 mbedtls_platform_random_delay();
Manuel Pégourié-Gonnard4c9b5562019-11-12 10:45:32 +01003865 if( flags_fi == 0 )
3866 return( 0 );
Manuel Pégourié-Gonnardea7eab12019-11-12 10:31:12 +01003867 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003868
Manuel Pégourié-Gonnard4c9b5562019-11-12 10:45:32 +01003869 /* Preserve the API by removing internal extra bits - from now on the
3870 * fact that flags is non-zero is also redundantly encoded by the
3871 * non-zero return value from this function. */
3872 *flags &= ~ X509_BADCERT_FI_EXTRA;
3873 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003874}
3875
3876/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003877 * Initialize a certificate chain
3878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003880{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003882}
3883
3884/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003885 * Unallocate all certificate data
3886 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 mbedtls_x509_crt *cert_cur = crt;
3891 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003892
3893 if( crt == NULL )
3894 return;
3895
3896 do
3897 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003898 x509_crt_cache_free( cert_cur->cache );
3899 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003900
3901#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3905 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003906#endif
3907
Hanno Becker2bcc7642019-02-26 19:01:00 +00003908 mbedtls_x509_name_free( cert_cur->issuer.next );
3909 mbedtls_x509_name_free( cert_cur->subject.next );
3910 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003911#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker2bcc7642019-02-26 19:01:00 +00003912 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03003913#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
3914
Hanno Becker180f7bf2019-02-28 13:23:38 +00003915#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003916
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003917 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003918 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003919 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003921 }
3922
3923 cert_cur = cert_cur->next;
3924 }
3925 while( cert_cur != NULL );
3926
3927 cert_cur = crt;
3928 do
3929 {
3930 cert_prv = cert_cur;
3931 cert_cur = cert_cur->next;
3932
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003933 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003934 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003936 }
3937 while( cert_cur != NULL );
3938}
3939
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003940#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3941/*
3942 * Initialize a restart context
3943 */
3944void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3945{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003946 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003947
3948 ctx->parent = NULL;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003949#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003950 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003951 ctx->fallback_signature_is_good = 0;
Hanno Becker6f61b7b2019-06-10 11:12:33 +01003952#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003953
3954 ctx->parent_is_trusted = -1;
3955
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003956 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003957 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003958 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003959}
3960
3961/*
3962 * Free the components of a restart context
3963 */
3964void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3965{
3966 if( ctx == NULL )
3967 return;
3968
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003969 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003970 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003971}
3972#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3973
Teppo Järvelinffaba552019-09-03 12:33:16 +03003974int mbedtls_x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
3975 mbedtls_x509_crt_frame const **dst )
3976{
3977 int ret = 0;
3978#if defined(MBEDTLS_THREADING_C)
3979 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
3980 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
3981#endif /* MBEDTLS_THREADING_C */
3982
3983#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
3984 defined(MBEDTLS_THREADING_C)
3985 if( crt->cache->frame_readers == 0 )
3986#endif
3987 ret = mbedtls_x509_crt_cache_provide_frame( crt );
3988
3989#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
3990 defined(MBEDTLS_THREADING_C)
3991 if( crt->cache->frame_readers == MBEDTLS_X509_CACHE_FRAME_READERS_MAX )
3992 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
3993
3994 crt->cache->frame_readers++;
3995#endif
3996
3997#if defined(MBEDTLS_THREADING_C)
3998 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
3999 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4000#endif /* MBEDTLS_THREADING_C */
4001
4002 *dst = crt->cache->frame;
4003 return( ret );
4004}
4005
4006int mbedtls_x509_crt_frame_release( mbedtls_x509_crt const *crt )
4007{
4008#if defined(MBEDTLS_THREADING_C)
4009 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
4010 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4011#endif /* MBEDTLS_THREADING_C */
4012
4013#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
4014 defined(MBEDTLS_THREADING_C)
4015 if( crt->cache->frame_readers == 0 )
4016 return( MBEDTLS_ERR_X509_FATAL_ERROR );
4017
4018 crt->cache->frame_readers--;
4019#endif
4020
4021#if defined(MBEDTLS_THREADING_C)
4022 mbedtls_mutex_unlock( &crt->cache->frame_mutex );
4023#endif /* MBEDTLS_THREADING_C */
4024
4025#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
4026 (void) mbedtls_x509_crt_flush_cache_frame( crt );
4027#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
4028
4029#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
4030 !defined(MBEDTLS_THREADING_C)
4031 ((void) crt);
4032#endif
4033
4034 return( 0 );
4035}
4036
4037int mbedtls_x509_crt_pk_acquire( mbedtls_x509_crt const *crt,
4038 mbedtls_pk_context **dst )
4039{
4040 int ret = 0;
4041#if defined(MBEDTLS_THREADING_C)
4042 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
4043 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4044#endif /* MBEDTLS_THREADING_C */
4045
4046#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
4047 defined(MBEDTLS_THREADING_C)
4048 if( crt->cache->pk_readers == 0 )
4049#endif
4050 ret = mbedtls_x509_crt_cache_provide_pk( crt );
4051
4052#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
4053 defined(MBEDTLS_THREADING_C)
4054 if( crt->cache->pk_readers == MBEDTLS_X509_CACHE_PK_READERS_MAX )
4055 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4056
4057 crt->cache->pk_readers++;
4058#endif
4059
4060#if defined(MBEDTLS_THREADING_C)
4061 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
4062 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4063#endif /* MBEDTLS_THREADING_C */
4064
4065 *dst = crt->cache->pk;
4066 return( ret );
4067}
4068
4069int mbedtls_x509_crt_pk_release( mbedtls_x509_crt const *crt )
4070{
4071#if defined(MBEDTLS_THREADING_C)
4072 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
4073 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
4074#endif /* MBEDTLS_THREADING_C */
4075
4076#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
4077 defined(MBEDTLS_THREADING_C)
4078 if( crt->cache->pk_readers == 0 )
4079 return( MBEDTLS_ERR_X509_FATAL_ERROR );
4080
4081 crt->cache->pk_readers--;
4082#endif
4083
4084#if defined(MBEDTLS_THREADING_C)
4085 mbedtls_mutex_unlock( &crt->cache->pk_mutex );
4086#endif /* MBEDTLS_THREADING_C */
4087
4088#if defined(MBEDTLS_X509_ALWAYS_FLUSH)
4089 (void) mbedtls_x509_crt_flush_cache_pk( crt );
4090#endif /* MBEDTLS_X509_ALWAYS_FLUSH */
4091
4092#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
4093 !defined(MBEDTLS_THREADING_C)
4094 ((void) crt);
4095#endif
4096
4097 return( 0 );
4098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099#endif /* MBEDTLS_X509_CRT_PARSE_C */