blob: 59898f724b121e7072c41720e43b5de5b3eb86cd [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020030 *
31 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/x509_crt.h"
Hanno Beckerf6bc8882019-05-02 13:05:58 +010043#include "mbedtls/x509_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000046
Rich Evans00ab4702015-02-06 13:43:58 +000047#include <string.h>
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#else
Simon Butcherd2642582018-10-03 15:11:19 +010056#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010065#endif
66
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <windows.h>
69#else
70#include <time.h>
71#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000074#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020075#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#include <sys/types.h>
77#include <sys/stat.h>
78#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000079#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#endif
81
Hanno Becker38f0cb42019-03-04 15:13:45 +000082#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
83static void x509_buf_to_buf_raw( mbedtls_x509_buf_raw *dst,
84 mbedtls_x509_buf const *src )
85{
86 dst->p = src->p;
87 dst->len = src->len;
88}
89
90static void x509_buf_raw_to_buf( mbedtls_x509_buf *dst,
91 mbedtls_x509_buf_raw const *src )
92{
93 dst->p = src->p;
94 dst->len = src->len;
95}
96#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
97
Hanno Becker21f55672019-02-15 15:27:59 +000098static int x509_crt_parse_frame( unsigned char *start,
99 unsigned char *end,
100 mbedtls_x509_crt_frame *frame );
Hanno Becker12506232019-05-13 13:53:21 +0100101static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000102 mbedtls_x509_name *subject );
Hanno Becker12506232019-05-13 13:53:21 +0100103static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000104 mbedtls_x509_name *issuer );
Hanno Becker12506232019-05-13 13:53:21 +0100105static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000106 mbedtls_x509_sequence *subject_alt );
Hanno Becker12506232019-05-13 13:53:21 +0100107static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +0000108 mbedtls_x509_sequence *ext_key_usage );
109
Hanno Beckerbc685192019-03-05 15:35:31 +0000110int mbedtls_x509_crt_flush_cache_pk( mbedtls_x509_crt const *crt )
111{
112#if defined(MBEDTLS_THREADING_C)
113 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
114 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100115#endif
Hanno Beckerfc99a092019-06-28 14:45:26 +0100116
117#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
118 defined(MBEDTLS_THREADING_C)
119 /* Can only free the PK context if nobody is using it.
120 * If MBEDTLS_X509_ALWAYS_FLUSH is set, nested uses
121 * of xxx_acquire() are prohibited, and no reference
122 * counting is needed. Also, notice that the code-path
123 * below is safe if the cache isn't filled. */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100124 if( crt->cache->pk_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100125#endif /* !MBEDTLS_X509_ALWAYS_FLUSH ||
126 MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100127 {
Hanno Beckerbc685192019-03-05 15:35:31 +0000128#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100129 /* The cache holds a shallow copy of the PK context
130 * in the legacy struct, so don't free PK context. */
131 mbedtls_free( crt->cache->pk );
Hanno Beckerbc685192019-03-05 15:35:31 +0000132#else
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100133 mbedtls_pk_free( crt->cache->pk );
134 mbedtls_free( crt->cache->pk );
Hanno Beckerbc685192019-03-05 15:35:31 +0000135#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100136 crt->cache->pk = NULL;
137 }
Hanno Beckerbc685192019-03-05 15:35:31 +0000138
139#if defined(MBEDTLS_THREADING_C)
140 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
141 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
142#endif
143 return( 0 );
144}
145
146int mbedtls_x509_crt_flush_cache_frame( mbedtls_x509_crt const *crt )
147{
148#if defined(MBEDTLS_THREADING_C)
149 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
150 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Hanno Beckera4bfaa82019-06-28 10:34:23 +0100151#endif
Hanno Beckerbc685192019-03-05 15:35:31 +0000152
Hanno Beckerfc99a092019-06-28 14:45:26 +0100153#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
154 defined(MBEDTLS_THREADING_C)
155 /* Can only free the PK context if nobody is using it.
156 * If MBEDTLS_X509_ALWAYS_FLUSH is set, nested uses
157 * of xxx_acquire() are prohibited, and no reference
158 * counting is needed. Also, notice that the code-path
159 * below is safe if the cache isn't filled. */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100160 if( crt->cache->frame_readers == 0 )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100161#endif /* !MBEDTLS_X509_ALWAYS_FLUSH ||
162 MBEDTLS_THREADING_C */
Hanno Becker2ba9fbd2019-05-28 16:11:43 +0100163 {
164 mbedtls_free( crt->cache->frame );
165 crt->cache->frame = NULL;
166 }
Hanno Beckerbc685192019-03-05 15:35:31 +0000167
168#if defined(MBEDTLS_THREADING_C)
169 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
170 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
171#endif
172 return( 0 );
173}
174
175int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt )
176{
177 int ret;
178 ret = mbedtls_x509_crt_flush_cache_frame( crt );
179 if( ret != 0 )
180 return( ret );
181 ret = mbedtls_x509_crt_flush_cache_pk( crt );
182 if( ret != 0 )
183 return( ret );
184 return( 0 );
185}
186
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000187static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame );
Hanno Beckered058882019-06-28 10:46:43 +0100188
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000189int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt )
Hanno Becker337088a2019-02-25 14:53:14 +0000190{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000191 mbedtls_x509_crt_cache *cache = crt->cache;
Hanno Becker337088a2019-02-25 14:53:14 +0000192 mbedtls_x509_crt_frame *frame;
193
Hanno Becker76428352019-03-05 15:29:23 +0000194 if( cache->frame != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100195 {
Hanno Becker410322f2019-07-02 13:37:12 +0100196#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
197 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000198 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100199#else
200 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
201 * allow nested uses of acquire. */
202 return( MBEDTLS_ERR_X509_FATAL_ERROR );
203#endif
204 }
Hanno Becker76428352019-03-05 15:29:23 +0000205
Hanno Becker337088a2019-02-25 14:53:14 +0000206 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
207 if( frame == NULL )
208 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000209 cache->frame = frame;
Hanno Becker337088a2019-02-25 14:53:14 +0000210
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000211#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
212 /* This would work with !MBEDTLS_X509_ON_DEMAND_PARSING, too,
213 * but is inefficient compared to copying the respective fields
214 * from the legacy mbedtls_x509_crt. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000215 return( x509_crt_parse_frame( crt->raw.p,
216 crt->raw.p + crt->raw.len,
217 frame ) );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000218#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
219 /* Make sure all extension related fields are properly initialized. */
220 frame->ca_istrue = 0;
221 frame->max_pathlen = 0;
222 frame->ext_types = 0;
223 frame->version = crt->version;
224 frame->sig_md = crt->sig_md;
225 frame->sig_pk = crt->sig_pk;
226 frame->valid_from = crt->valid_from;
227 frame->valid_to = crt->valid_to;
Hanno Becker38f0cb42019-03-04 15:13:45 +0000228 x509_buf_to_buf_raw( &frame->raw, &crt->raw );
229 x509_buf_to_buf_raw( &frame->tbs, &crt->tbs );
230 x509_buf_to_buf_raw( &frame->serial, &crt->serial );
231 x509_buf_to_buf_raw( &frame->pubkey_raw, &crt->pk_raw );
232 x509_buf_to_buf_raw( &frame->issuer_raw, &crt->issuer_raw );
233 x509_buf_to_buf_raw( &frame->subject_raw, &crt->subject_raw );
234 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
235 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
236 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
237 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000238
239 /* The legacy CRT structure doesn't explicitly contain
240 * the `AlgorithmIdentifier` bounds; however, those can
241 * be inferred from the surrounding (mandatory) `SerialNumber`
242 * and `Issuer` fields. */
243 frame->sig_alg.p = crt->serial.p + crt->serial.len;
244 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
245
246 return( x509_crt_frame_parse_ext( frame ) );
247#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000248}
Hanno Becker337088a2019-02-25 14:53:14 +0000249
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000250int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
251{
252 mbedtls_x509_crt_cache *cache = crt->cache;
253 mbedtls_pk_context *pk;
254
Hanno Becker76428352019-03-05 15:29:23 +0000255 if( cache->pk != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100256 {
Hanno Becker410322f2019-07-02 13:37:12 +0100257#if !defined(MBEDTLS_X509_ALWAYS_FLUSH) || \
258 defined(MBEDTLS_THREADING_C)
Hanno Becker76428352019-03-05 15:29:23 +0000259 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100260#else
261 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
262 * allow nested uses of acquire. */
263 return( MBEDTLS_ERR_X509_FATAL_ERROR );
264#endif
265 }
Hanno Becker76428352019-03-05 15:29:23 +0000266
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000267 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
268 if( pk == NULL )
269 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000270 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000271
272#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
273 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000274 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000275#else
276 {
277 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
278 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
279 pk_raw.p + pk_raw.len,
280 pk ) );
281 }
282#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000283}
284
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000285static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000286{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000287 memset( cache, 0, sizeof( *cache ) );
288#if defined(MBEDTLS_THREADING_C)
289 mbedtls_mutex_init( &cache->frame_mutex );
290 mbedtls_mutex_init( &cache->pk_mutex );
291#endif
292}
293
294static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
295{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000296#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000297 /* The cache holds a shallow copy of the PK context
298 * in the legacy struct, so don't free PK context. */
299 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000300#else
301 mbedtls_pk_free( cache->pk );
302 mbedtls_free( cache->pk );
303#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
304
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000305 cache->pk = NULL;
306}
307
308static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
309{
310 mbedtls_free( cache->frame );
311 cache->frame = NULL;
312}
313
314static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
315{
316 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000317 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000318
319#if defined(MBEDTLS_THREADING_C)
320 mbedtls_mutex_free( &cache->frame_mutex );
321 mbedtls_mutex_free( &cache->pk_mutex );
322#endif
323
324 x509_crt_cache_clear_frame( cache );
325 x509_crt_cache_clear_pk( cache );
326
327 memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000328}
329
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000330int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
331 mbedtls_x509_sequence **subj_alt )
332{
333 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100334 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000335 mbedtls_x509_sequence *seq;
336
337 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
338 if( ret != 0 )
339 return( ret );
340
341 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
342 if( seq == NULL )
343 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
344 else
345 ret = x509_crt_subject_alt_from_frame( frame, seq );
346
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000347 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000348
349 *subj_alt = seq;
350 return( ret );
351}
352
353int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
354 mbedtls_x509_sequence **ext_key_usage )
355{
356 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100357 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000358 mbedtls_x509_sequence *seq;
359
360 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
361 if( ret != 0 )
362 return( ret );
363
364 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
365 if( seq == NULL )
366 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
367 else
368 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
369
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000370 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000371
372 *ext_key_usage = seq;
373 return( ret );
374}
375
Hanno Becker63e69982019-02-26 18:50:49 +0000376int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
377 mbedtls_x509_name **subject )
378{
379 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100380 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000381 mbedtls_x509_name *name;
382
383 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
384 if( ret != 0 )
385 return( ret );
386
387 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
388 if( name == NULL )
389 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
390 else
391 ret = x509_crt_subject_from_frame( frame, name );
392
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000393 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000394
395 *subject = name;
396 return( ret );
397}
398
399int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
400 mbedtls_x509_name **issuer )
401{
402 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100403 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000404 mbedtls_x509_name *name;
405
406 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
407 if( ret != 0 )
408 return( ret );
409
410 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
411 if( name == NULL )
412 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
413 else
414 ret = x509_crt_issuer_from_frame( frame, name );
415
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000416 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000417
418 *issuer = name;
419 return( ret );
420}
421
Hanno Becker823efad2019-02-28 13:23:58 +0000422int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
423 mbedtls_x509_crt_frame *dst )
424{
425 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100426 mbedtls_x509_crt_frame const *frame;
Hanno Becker823efad2019-02-28 13:23:58 +0000427 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
428 if( ret != 0 )
429 return( ret );
430 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000431 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000432 return( 0 );
433}
434
435int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
436 mbedtls_pk_context *dst )
437{
438#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
439 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
440 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
441 pk_raw.p + pk_raw.len,
442 dst ) );
443#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
444 int ret;
445 mbedtls_pk_context *pk;
446 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
447 if( ret != 0 )
448 return( ret );
449
450 /* Move PK from CRT cache to destination pointer
451 * to avoid a copy. */
452 *dst = *pk;
453 mbedtls_free( crt->cache->pk );
454 crt->cache->pk = NULL;
455
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000456 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000457 return( 0 );
458#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
459}
460
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200461/*
462 * Item in a verification chain: cert and flags for it
463 */
464typedef struct {
465 mbedtls_x509_crt *crt;
466 uint32_t flags;
467} x509_crt_verify_chain_item;
468
469/*
470 * Max size of verification chain: end-entity + intermediates + trusted root
471 */
472#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200473
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200475 * Default profile
476 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200477const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
478{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200479#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200480 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200481 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200482#endif
483 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200484 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
485 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
486 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
487 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
488 0xFFFFFFF, /* Any PK alg */
489 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200490 2048,
491};
492
493/*
494 * Next-default profile
495 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200496const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
497{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200498 /* Hashes from SHA-256 and above */
499 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
500 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
501 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
502 0xFFFFFFF, /* Any PK alg */
503#if defined(MBEDTLS_ECP_C)
504 /* Curves at or above 128-bit security level */
505 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
506 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
507 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
508 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
509 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
510 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
511 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
512#else
513 0,
514#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200515 2048,
516};
517
518/*
519 * NSA Suite B Profile
520 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200521const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
522{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200523 /* Only SHA-256 and 384 */
524 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
525 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
526 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200527 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
528 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200529#if defined(MBEDTLS_ECP_C)
530 /* Only NIST P-256 and P-384 */
531 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
532 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
533#else
534 0,
535#endif
536 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200537};
538
539/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200540 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200541 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200542 */
543static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
544 mbedtls_md_type_t md_alg )
545{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200546 if( md_alg == MBEDTLS_MD_NONE )
547 return( -1 );
548
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200549 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
550 return( 0 );
551
552 return( -1 );
553}
554
555/*
556 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200557 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200558 */
559static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
560 mbedtls_pk_type_t pk_alg )
561{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200562 if( pk_alg == MBEDTLS_PK_NONE )
563 return( -1 );
564
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200565 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
566 return( 0 );
567
568 return( -1 );
569}
570
571/*
572 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200573 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200574 */
575static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200576 const mbedtls_pk_context *pk )
577{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200578 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200579
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200580#if defined(MBEDTLS_RSA_C)
581 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
582 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200583 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200584 return( 0 );
585
586 return( -1 );
587 }
588#endif
589
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200590#if defined(MBEDTLS_ECP_C)
591 if( pk_alg == MBEDTLS_PK_ECDSA ||
592 pk_alg == MBEDTLS_PK_ECKEY ||
593 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200594 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200595 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200596
Philippe Antoineb5b25432018-05-11 11:06:29 +0200597 if( gid == MBEDTLS_ECP_DP_NONE )
598 return( -1 );
599
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200600 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
601 return( 0 );
602
603 return( -1 );
604 }
605#endif
606
607 return( -1 );
608}
609
610/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000611 * Return 0 if name matches wildcard, -1 otherwise
612 */
Hanno Becker24926222019-02-21 13:10:55 +0000613static int x509_check_wildcard( char const *cn,
614 size_t cn_len,
615 unsigned char const *buf,
616 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000617{
618 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000619 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000620
621 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000622 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000623 return( -1 );
624
625 for( i = 0; i < cn_len; ++i )
626 {
627 if( cn[i] == '.' )
628 {
629 cn_idx = i;
630 break;
631 }
632 }
633
634 if( cn_idx == 0 )
635 return( -1 );
636
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000637 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
638 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000639 {
640 return( 0 );
641 }
642
643 return( -1 );
644}
645
646/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200647 * Reset (init or clear) a verify_chain
648 */
649static void x509_crt_verify_chain_reset(
650 mbedtls_x509_crt_verify_chain *ver_chain )
651{
652 size_t i;
653
654 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
655 {
656 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000657 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200658 }
659
660 ver_chain->len = 0;
661}
662
663/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
665 */
666static int x509_get_version( unsigned char **p,
667 const unsigned char *end,
668 int *ver )
669{
670 int ret;
671 size_t len;
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
674 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 {
678 *ver = 0;
679 return( 0 );
680 }
681
Hanno Becker2f472142019-02-12 11:52:10 +0000682 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 }
684
685 end = *p + len;
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
688 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689
690 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_X509_INVALID_VERSION +
692 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
694 return( 0 );
695}
696
697/*
698 * Validity ::= SEQUENCE {
699 * notBefore Time,
700 * notAfter Time }
701 */
702static int x509_get_dates( unsigned char **p,
703 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_x509_time *from,
705 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706{
707 int ret;
708 size_t len;
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
711 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
712 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713
714 end = *p + len;
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717 return( ret );
718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720 return( ret );
721
722 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 return( MBEDTLS_ERR_X509_INVALID_DATE +
724 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725
726 return( 0 );
727}
728
729/*
730 * X.509 v2/v3 unique identifier (not parsed)
731 */
732static int x509_get_uid( unsigned char **p,
733 const unsigned char *end,
Hanno Beckere9084122019-06-07 12:04:39 +0100734 mbedtls_x509_buf_raw *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735{
736 int ret;
737
738 if( *p == end )
739 return( 0 );
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
742 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 return( 0 );
746
Hanno Becker2f472142019-02-12 11:52:10 +0000747 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 }
749
750 uid->p = *p;
751 *p += uid->len;
752
753 return( 0 );
754}
755
756static int x509_get_basic_constraints( unsigned char **p,
757 const unsigned char *end,
758 int *ca_istrue,
759 int *max_pathlen )
760{
761 int ret;
762 size_t len;
763
764 /*
765 * BasicConstraints ::= SEQUENCE {
766 * cA BOOLEAN DEFAULT FALSE,
767 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
768 */
769 *ca_istrue = 0; /* DEFAULT FALSE */
770 *max_pathlen = 0; /* endless */
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
773 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000774 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775
776 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200777 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
782 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783
784 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000785 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786
787 if( *ca_istrue != 0 )
788 *ca_istrue = 1;
789 }
790
791 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200792 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000795 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796
797 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000798 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799
800 (*max_pathlen)++;
801
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200802 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803}
804
805static int x509_get_ns_cert_type( unsigned char **p,
806 const unsigned char *end,
807 unsigned char *ns_cert_type)
808{
809 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000813 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814
815 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000816 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817
818 /* Get actual bitstring */
819 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200820 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821}
822
823static int x509_get_key_usage( unsigned char **p,
824 const unsigned char *end,
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100825 uint16_t *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826{
827 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200828 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000832 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833
834 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000835 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
837 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200838 *key_usage = 0;
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100839 for( i = 0; i < bs.len && i < sizeof( *key_usage ); i++ )
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200840 {
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100841 *key_usage |= (uint16_t) bs.p[i] << ( 8*i );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200842 }
843
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200844 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845}
846
Hanno Becker529f25d2019-05-02 14:48:25 +0100847static int asn1_build_sequence_cb( void *ctx,
848 int tag,
849 unsigned char *data,
850 size_t data_len )
Hanno Becker15b73b42019-05-02 13:21:27 +0100851{
852 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
853 mbedtls_asn1_sequence *cur = *cur_ptr;
854
855 /* Allocate and assign next pointer */
856 if( cur->buf.p != NULL )
857 {
858 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
859 if( cur->next == NULL )
860 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
861 cur = cur->next;
862 }
863
864 cur->buf.tag = tag;
865 cur->buf.p = data;
866 cur->buf.len = data_len;
867
868 *cur_ptr = cur;
869 return( 0 );
870}
871
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872/*
Hanno Becker529f25d2019-05-02 14:48:25 +0100873 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
874 *
875 * KeyPurposeId ::= OBJECT IDENTIFIER
876 */
877static int x509_get_ext_key_usage( unsigned char **p,
878 const unsigned char *end,
879 mbedtls_x509_sequence *ext_key_usage)
880{
881 return( mbedtls_asn1_traverse_sequence_of( p, end,
882 0xFF, MBEDTLS_ASN1_OID,
883 0, 0,
884 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100885 (void *) &ext_key_usage ) );
Hanno Becker529f25d2019-05-02 14:48:25 +0100886}
887
888/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 * SubjectAltName ::= GeneralNames
890 *
891 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
892 *
893 * GeneralName ::= CHOICE {
894 * otherName [0] OtherName,
895 * rfc822Name [1] IA5String,
896 * dNSName [2] IA5String,
897 * x400Address [3] ORAddress,
898 * directoryName [4] Name,
899 * ediPartyName [5] EDIPartyName,
900 * uniformResourceIdentifier [6] IA5String,
901 * iPAddress [7] OCTET STRING,
902 * registeredID [8] OBJECT IDENTIFIER }
903 *
904 * OtherName ::= SEQUENCE {
905 * type-id OBJECT IDENTIFIER,
906 * value [0] EXPLICIT ANY DEFINED BY type-id }
907 *
908 * EDIPartyName ::= SEQUENCE {
909 * nameAssigner [0] DirectoryString OPTIONAL,
910 * partyName [1] DirectoryString }
911 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000912 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 */
Hanno Becker5984d302019-02-21 14:46:54 +0000914static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000918 return( mbedtls_asn1_traverse_sequence_of( &p, end,
919 MBEDTLS_ASN1_TAG_CLASS_MASK,
920 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
921 MBEDTLS_ASN1_TAG_VALUE_MASK,
922 2 /* SubjectAlt DNS */,
Hanno Becker529f25d2019-05-02 14:48:25 +0100923 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100924 (void *) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925}
926
927/*
928 * X.509 v3 extensions
929 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000931static int x509_crt_get_ext_cb( void *ctx,
932 int tag,
933 unsigned char *p,
934 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935{
936 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000937 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000939 unsigned char *end, *end_ext_octet;
940 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
941 int is_critical = 0; /* DEFAULT FALSE */
942 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000944 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000945
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000946 /*
947 * Extension ::= SEQUENCE {
948 * extnID OBJECT IDENTIFIER,
949 * critical BOOLEAN DEFAULT FALSE,
950 * extnValue OCTET STRING }
951 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000953 end = p + ext_len;
954
955 /* Get extension ID */
956 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
957 MBEDTLS_ASN1_OID ) ) != 0 )
958 goto err;
959
960 extn_oid.tag = MBEDTLS_ASN1_OID;
961 extn_oid.p = p;
962 p += extn_oid.len;
963
964 /* Get optional critical */
965 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
966 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
967 goto err;
968
969 /* Data should be octet string type */
970 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
971 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
972 goto err;
973
974 end_ext_octet = p + len;
975 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000977 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
978 goto err;
979 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000981 /*
982 * Detect supported extensions
983 */
984 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
985 if( ret != 0 )
986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000988 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000990 /* Data is marked as critical: fail */
991 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
992 goto err;
993 }
Hanno Beckerb36a2452019-05-29 14:43:17 +0100994#endif /* MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000995 return( 0 );
996 }
997
998 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +0000999 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001000 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
1001
Hanno Becker21f55672019-02-15 15:27:59 +00001002 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001003 switch( ext_type )
1004 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001005 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001006 {
1007 int ca_istrue;
1008 int max_pathlen;
1009
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001011 ret = x509_get_basic_constraints( &p, end_ext_octet,
1012 &ca_istrue,
1013 &max_pathlen );
1014 if( ret != 0 )
1015 goto err;
1016
Hanno Becker21f55672019-02-15 15:27:59 +00001017 frame->ca_istrue = ca_istrue;
1018 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001020 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001024 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001025 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001026 if( ret != 0 )
1027 goto err;
1028 break;
1029
1030 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1031 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +00001032 frame->subject_alt_raw.p = p;
1033 frame->subject_alt_raw.len = end_ext_octet - p;
1034
1035 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1036 MBEDTLS_ASN1_TAG_CLASS_MASK,
1037 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
1038 MBEDTLS_ASN1_TAG_VALUE_MASK,
1039 2 /* SubjectAlt DNS */,
1040 NULL, NULL );
1041 if( ret != 0 )
1042 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 break;
1044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001047 frame->ext_key_usage_raw.p = p;
1048 frame->ext_key_usage_raw.len = end_ext_octet - p;
1049 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001050 {
Hanno Becker21f55672019-02-15 15:27:59 +00001051 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1052 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001053 }
Hanno Becker21f55672019-02-15 15:27:59 +00001054
1055 /* Check structural sanity of extension. */
1056 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1057 0xFF, MBEDTLS_ASN1_OID,
1058 0, 0, NULL, NULL );
1059 if( ret != 0 )
1060 goto err;
1061
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062 break;
1063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001066 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001067 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001068 if( ret != 0 )
1069 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070 break;
1071
1072 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001073 /*
1074 * If this is a non-critical extension, which the oid layer
1075 * supports, but there isn't an X.509 parser for it,
1076 * skip the extension.
1077 */
1078#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1079 if( is_critical )
1080 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1081#endif
1082 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083 }
1084
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001086
1087err:
1088 return( ret );
1089}
1090
Hanno Becker21f55672019-02-15 15:27:59 +00001091static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001092{
1093 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001094 unsigned char *p = frame->v3_ext.p;
1095 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001096
Hanno Becker21f55672019-02-15 15:27:59 +00001097 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001098 return( 0 );
1099
Hanno Becker21f55672019-02-15 15:27:59 +00001100 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001101 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001102 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001103
1104 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1105 return( ret );
1106 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1107 return( ret );
1108
1109 if( ret != 0 )
1110 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1111
1112 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113}
1114
Hanno Becker21f55672019-02-15 15:27:59 +00001115static int x509_crt_parse_frame( unsigned char *start,
1116 unsigned char *end,
1117 mbedtls_x509_crt_frame *frame )
1118{
1119 int ret;
1120 unsigned char *p;
1121 size_t len;
1122
1123 mbedtls_x509_buf tmp;
1124 unsigned char *tbs_start;
1125
1126 mbedtls_x509_buf outer_sig_alg;
1127 size_t inner_sig_alg_len;
1128 unsigned char *inner_sig_alg_start;
1129
1130 memset( frame, 0, sizeof( *frame ) );
1131
1132 /*
1133 * Certificate ::= SEQUENCE {
1134 * tbsCertificate TBSCertificate,
1135 * signatureAlgorithm AlgorithmIdentifier,
1136 * signatureValue BIT STRING
1137 * }
1138 *
1139 */
1140 p = start;
1141
1142 frame->raw.p = p;
1143 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1144 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1145 {
1146 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1147 }
1148
1149 /* NOTE: We are currently not checking that the `Certificate`
1150 * structure spans the entire buffer. */
1151 end = p + len;
1152 frame->raw.len = end - frame->raw.p;
1153
1154 /*
1155 * TBSCertificate ::= SEQUENCE { ...
1156 */
1157 frame->tbs.p = p;
1158 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1159 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1160 {
1161 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1162 }
1163 tbs_start = p;
1164
1165 /* Breadth-first parsing: Jump over TBS for now. */
1166 p += len;
1167 frame->tbs.len = p - frame->tbs.p;
1168
1169 /*
1170 * AlgorithmIdentifier ::= SEQUENCE { ...
1171 */
1172 outer_sig_alg.p = p;
1173 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1174 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1175 {
1176 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1177 }
1178 p += len;
1179 outer_sig_alg.len = p - outer_sig_alg.p;
1180
1181 /*
1182 * signatureValue BIT STRING
1183 */
1184 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1185 if( ret != 0 )
1186 return( ret );
1187 frame->sig.p = tmp.p;
1188 frame->sig.len = tmp.len;
1189
1190 /* Check that we consumed the entire `Certificate` structure. */
1191 if( p != end )
1192 {
1193 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1194 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1195 }
1196
1197 /* Parse TBSCertificate structure
1198 *
1199 * TBSCertificate ::= SEQUENCE {
1200 * version [0] EXPLICIT Version DEFAULT v1,
1201 * serialNumber CertificateSerialNumber,
1202 * signature AlgorithmIdentifier,
1203 * issuer Name,
1204 * validity Validity,
1205 * subject Name,
1206 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1207 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1208 * -- If present, version MUST be v2 or v3
1209 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1210 * -- If present, version MUST be v2 or v3
1211 * extensions [3] EXPLICIT Extensions OPTIONAL
1212 * -- If present, version MUST be v3
1213 * }
1214 */
1215 end = frame->tbs.p + frame->tbs.len;
1216 p = tbs_start;
1217
1218 /*
1219 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1220 */
1221 {
1222 int version;
1223 ret = x509_get_version( &p, end, &version );
1224 if( ret != 0 )
1225 return( ret );
1226
1227 if( version < 0 || version > 2 )
1228 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1229
1230 frame->version = version + 1;
1231 }
1232
1233 /*
1234 * CertificateSerialNumber ::= INTEGER
1235 */
1236 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1237 if( ret != 0 )
1238 return( ret );
1239
1240 frame->serial.p = tmp.p;
1241 frame->serial.len = tmp.len;
1242
1243 /*
1244 * signature AlgorithmIdentifier
1245 */
1246 inner_sig_alg_start = p;
1247 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1248 &frame->sig_pk, NULL );
1249 if( ret != 0 )
1250 return( ret );
1251 inner_sig_alg_len = p - inner_sig_alg_start;
1252
1253 frame->sig_alg.p = inner_sig_alg_start;
1254 frame->sig_alg.len = inner_sig_alg_len;
1255
1256 /* Consistency check:
1257 * Inner and outer AlgorithmIdentifier structures must coincide:
1258 *
1259 * Quoting RFC 5280, Section 4.1.1.2:
1260 * This field MUST contain the same algorithm identifier as the
1261 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1262 */
1263 if( outer_sig_alg.len != inner_sig_alg_len ||
1264 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1265 {
1266 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1267 }
1268
1269 /*
1270 * issuer Name
1271 *
1272 * Name ::= CHOICE { -- only one possibility for now --
1273 * rdnSequence RDNSequence }
1274 *
1275 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1276 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001277 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001278
1279 ret = mbedtls_asn1_get_tag( &p, end, &len,
1280 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1281 if( ret != 0 )
1282 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001283 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001284 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001285
1286 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1287 &frame->issuer_raw,
1288 NULL, NULL );
1289 if( ret != 0 )
1290 return( ret );
1291
Hanno Becker21f55672019-02-15 15:27:59 +00001292 /*
1293 * Validity ::= SEQUENCE { ...
1294 */
1295 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1296 if( ret != 0 )
1297 return( ret );
1298
1299 /*
1300 * subject Name
1301 *
1302 * Name ::= CHOICE { -- only one possibility for now --
1303 * rdnSequence RDNSequence }
1304 *
1305 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1306 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001307 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001308
1309 ret = mbedtls_asn1_get_tag( &p, end, &len,
1310 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1311 if( ret != 0 )
1312 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001313 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001314 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001315
1316 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1317 &frame->subject_raw,
1318 NULL, NULL );
1319 if( ret != 0 )
1320 return( ret );
1321
Hanno Becker21f55672019-02-15 15:27:59 +00001322 /*
1323 * SubjectPublicKeyInfo
1324 */
1325 frame->pubkey_raw.p = p;
1326 ret = mbedtls_asn1_get_tag( &p, end, &len,
1327 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1328 if( ret != 0 )
1329 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1330 p += len;
1331 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1332
Hanno Becker97aa4362019-06-08 07:38:20 +01001333 if( frame->version != 1 )
Hanno Becker21f55672019-02-15 15:27:59 +00001334 {
Hanno Beckerfd64f142019-06-07 11:47:12 +01001335 /*
1336 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1337 * -- If present, version shall be v2 or v3
1338 */
Hanno Beckere9084122019-06-07 12:04:39 +01001339 ret = x509_get_uid( &p, end, &frame->issuer_id, 1 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001340 if( ret != 0 )
1341 return( ret );
1342
Hanno Beckerfd64f142019-06-07 11:47:12 +01001343 /*
1344 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1345 * -- If present, version shall be v2 or v3
1346 */
Hanno Beckere9084122019-06-07 12:04:39 +01001347 ret = x509_get_uid( &p, end, &frame->subject_id, 2 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001348 if( ret != 0 )
1349 return( ret );
Hanno Becker21f55672019-02-15 15:27:59 +00001350 }
1351
1352 /*
1353 * extensions [3] EXPLICIT Extensions OPTIONAL
1354 * -- If present, version shall be v3
1355 */
1356#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1357 if( frame->version == 3 )
1358#endif
1359 {
1360 if( p != end )
1361 {
1362 ret = mbedtls_asn1_get_tag( &p, end, &len,
1363 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1364 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1365 if( len == 0 )
1366 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1367 if( ret != 0 )
1368 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1369
1370 frame->v3_ext.p = p;
1371 frame->v3_ext.len = len;
1372
1373 p += len;
1374 }
1375
1376 ret = x509_crt_frame_parse_ext( frame );
1377 if( ret != 0 )
1378 return( ret );
1379 }
1380
1381 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1382 if( p != end )
1383 {
1384 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1385 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1386 }
1387
1388 return( 0 );
1389}
1390
Hanno Becker12506232019-05-13 13:53:21 +01001391static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001392 mbedtls_x509_name *subject )
1393{
Hanno Becker1e11f212019-03-04 14:43:43 +00001394 return( mbedtls_x509_get_name( frame->subject_raw.p,
1395 frame->subject_raw.len,
1396 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001397}
1398
Hanno Becker12506232019-05-13 13:53:21 +01001399static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001400 mbedtls_x509_name *issuer )
1401{
Hanno Becker1e11f212019-03-04 14:43:43 +00001402 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1403 frame->issuer_raw.len,
1404 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001405}
1406
Hanno Becker12506232019-05-13 13:53:21 +01001407static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001408 mbedtls_x509_sequence *subject_alt )
1409{
1410 int ret;
1411 unsigned char *p = frame->subject_alt_raw.p;
1412 unsigned char *end = p + frame->subject_alt_raw.len;
1413
1414 memset( subject_alt, 0, sizeof( *subject_alt ) );
1415
1416 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1417 return( 0 );
1418
1419 ret = x509_get_subject_alt_name( p, end, subject_alt );
1420 if( ret != 0 )
1421 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1422 return( ret );
1423}
1424
Hanno Becker12506232019-05-13 13:53:21 +01001425static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001426 mbedtls_x509_sequence *ext_key_usage )
1427{
1428 int ret;
1429 unsigned char *p = frame->ext_key_usage_raw.p;
1430 unsigned char *end = p + frame->ext_key_usage_raw.len;
1431
1432 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1433
1434 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1435 return( 0 );
1436
1437 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1438 if( ret != 0 )
1439 {
1440 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1441 return( ret );
1442 }
1443
1444 return( 0 );
1445}
1446
Hanno Becker180f7bf2019-02-28 13:23:38 +00001447#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001448static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1449 mbedtls_pk_context *pk )
1450{
1451 unsigned char *p = frame->pubkey_raw.p;
1452 unsigned char *end = p + frame->pubkey_raw.len;
1453 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1454}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001455#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001456
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457/*
1458 * Parse and fill a single X.509 certificate in DER format
1459 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001460static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1461 const unsigned char *buf,
1462 size_t buflen,
1463 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464{
1465 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001466 mbedtls_x509_crt_frame *frame;
1467 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001468
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471
Hanno Becker21f55672019-02-15 15:27:59 +00001472 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001473 {
Hanno Becker21f55672019-02-15 15:27:59 +00001474 crt->raw.p = (unsigned char*) buf;
1475 crt->raw.len = buflen;
1476 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001477 }
1478 else
1479 {
Hanno Becker7b8e11e2019-05-03 12:37:12 +01001480 /* Call mbedtls_calloc with buflen + 1 in order to avoid potential
1481 * return of NULL in case of length 0 certificates, which we want
1482 * to cleanly fail with MBEDTLS_ERR_X509_INVALID_FORMAT in the
1483 * core parsing routine, but not here. */
1484 crt->raw.p = mbedtls_calloc( 1, buflen + 1 );
Hanno Becker21f55672019-02-15 15:27:59 +00001485 if( crt->raw.p == NULL )
1486 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1487 crt->raw.len = buflen;
1488 memcpy( crt->raw.p, buf, buflen );
1489
1490 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001491 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001492
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001493 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1494 if( cache == NULL )
1495 {
1496 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1497 goto exit;
1498 }
1499 crt->cache = cache;
1500 x509_crt_cache_init( cache );
1501
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001502#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1503
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001504 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001505 if( ret != 0 )
1506 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507
Hanno Becker76428352019-03-05 15:29:23 +00001508 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001509
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001510#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001511
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001512 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1513 if( frame == NULL )
1514 {
1515 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1516 goto exit;
1517 }
1518 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001519
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001520 ret = x509_crt_parse_frame( crt->raw.p,
1521 crt->raw.p + crt->raw.len,
1522 frame );
1523 if( ret != 0 )
1524 goto exit;
1525
Hanno Becker21f55672019-02-15 15:27:59 +00001526 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1527 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001528 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1529 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1530 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1531 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
1532 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1533 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
1534 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1535 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1536 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001537 crt->valid_from = frame->valid_from;
1538 crt->valid_to = frame->valid_to;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001539 crt->version = frame->version;
1540 crt->ca_istrue = frame->ca_istrue;
1541 crt->max_pathlen = frame->max_pathlen;
1542 crt->ext_types = frame->ext_types;
1543 crt->key_usage = frame->key_usage;
1544 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545
1546 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001547 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550 {
Hanno Becker21f55672019-02-15 15:27:59 +00001551 /* sig_oid: Previously, needed for convenience in
1552 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001553 unsigned char *tmp = frame->sig_alg.p;
1554 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001555 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556
Hanno Becker21f55672019-02-15 15:27:59 +00001557 ret = mbedtls_x509_get_alg( &tmp, end,
1558 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559 if( ret != 0 )
1560 {
Hanno Becker21f55672019-02-15 15:27:59 +00001561 /* This should never happen, because we check
1562 * the sanity of the AlgorithmIdentifier structure
1563 * during frame parsing. */
1564 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1565 goto exit;
1566 }
1567 crt->sig_oid = sig_oid;
1568
1569 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001570 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001571 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1572 &crt->sig_md, &crt->sig_pk,
1573 &crt->sig_opts );
1574 if( ret != 0 )
1575 {
1576 /* Again, this should never happen. */
1577 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1578 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579 }
1580 }
1581
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001582 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001583 if( ret != 0 )
1584 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001586 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001587 if( ret != 0 )
1588 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001590 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001591 if( ret != 0 )
1592 goto exit;
1593
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001594 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001595 if( ret != 0 )
1596 goto exit;
1597
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001598 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1599 if( ret != 0 )
1600 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001601#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001602
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001603 /* Currently, we accept DER encoded CRTs with trailing garbage
1604 * and promise to not account for the garbage in the `raw` field.
1605 *
1606 * Note that this means that `crt->raw.len` is not necessarily the
1607 * full size of the heap buffer allocated at `crt->raw.p` in case
1608 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1609 * need the size, and the garbage data doesn't need zeroization. */
1610 crt->raw.len = frame->raw.len;
1611
1612 cache->pk_raw = frame->pubkey_raw;
1613
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001614 /* Free the frame before parsing the public key to
1615 * keep peak RAM usage low. This is slightly inefficient
1616 * because the frame will need to be parsed again on the
1617 * first usage of the CRT, but that seems acceptable.
1618 * As soon as the frame gets used multiple times, it
1619 * will be cached by default. */
1620 x509_crt_cache_clear_frame( crt->cache );
1621
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001622 /* The cache just references the PK structure from the legacy
1623 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001624 * the cache.
1625 *
1626 * We're not actually using the parsed PK context here;
1627 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001628 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001629 if( ret != 0 )
1630 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001631 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001632
1633exit:
1634 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636
Hanno Becker21f55672019-02-15 15:27:59 +00001637 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638}
1639
1640/*
1641 * Parse one X.509 certificate in DER format from a buffer and add them to a
1642 * chained list
1643 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001644static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1645 const unsigned char *buf,
1646 size_t buflen,
1647 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648{
1649 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651
1652 /*
1653 * Check for valid input
1654 */
1655 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657
Hanno Becker371e0e42019-02-25 18:08:59 +00001658 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 {
1660 prev = crt;
1661 crt = crt->next;
1662 }
1663
1664 /*
1665 * Add new certificate on the end of the chain if needed.
1666 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001667 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001669 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670
1671 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001672 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673
1674 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677 }
1678
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001679 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680 {
1681 if( prev )
1682 prev->next = NULL;
1683
1684 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686
1687 return( ret );
1688 }
1689
1690 return( 0 );
1691}
1692
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001693int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1694 const unsigned char *buf,
1695 size_t buflen )
1696{
1697 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1698}
1699
1700int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1701 const unsigned char *buf,
1702 size_t buflen )
1703{
1704 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1705}
1706
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001708 * Parse one or more PEM certificates from a buffer and add them to the chained
1709 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001710 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001711int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1712 const unsigned char *buf,
1713 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714{
Janos Follath98e28a72016-05-31 14:03:54 +01001715#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001716 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001718#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719
1720 /*
1721 * Check for valid input
1722 */
1723 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001725
1726 /*
1727 * Determine buffer content. Buffer contains either one DER certificate or
1728 * one or more PEM certificates.
1729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001731 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001732 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001735 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1738 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001739#else
1740 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1741#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#if defined(MBEDTLS_PEM_PARSE_C)
1744 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745 {
1746 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001748
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001749 /* 1 rather than 0 since the terminating NULL byte is counted in */
1750 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751 {
1752 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001755 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757 "-----BEGIN CERTIFICATE-----",
1758 "-----END CERTIFICATE-----",
1759 buf, NULL, 0, &use_len );
1760
1761 if( ret == 0 )
1762 {
1763 /*
1764 * Was PEM encoded
1765 */
1766 buflen -= use_len;
1767 buf += use_len;
1768 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770 {
1771 return( ret );
1772 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776
1777 /*
1778 * PEM header and footer were found
1779 */
1780 buflen -= use_len;
1781 buf += use_len;
1782
1783 if( first_error == 0 )
1784 first_error = ret;
1785
Paul Bakker5a5fa922014-09-26 14:53:04 +02001786 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001787 continue;
1788 }
1789 else
1790 break;
1791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795
1796 if( ret != 0 )
1797 {
1798 /*
1799 * Quit parsing on a memory error
1800 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001801 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802 return( ret );
1803
1804 if( first_error == 0 )
1805 first_error = ret;
1806
1807 total_failed++;
1808 continue;
1809 }
1810
1811 success = 1;
1812 }
1813 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814
1815 if( success )
1816 return( total_failed );
1817 else if( first_error )
1818 return( first_error );
1819 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001821#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001822}
1823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825/*
1826 * Load one or more certificates and add them to the chained list
1827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829{
1830 int ret;
1831 size_t n;
1832 unsigned char *buf;
1833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001835 return( ret );
1836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001838
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001839 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001841
1842 return( ret );
1843}
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001846{
1847 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001848#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849 int w_ret;
1850 WCHAR szDir[MAX_PATH];
1851 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001852 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001853 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001854
Paul Bakker9af723c2014-05-01 13:03:14 +02001855 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001856 HANDLE hFind;
1857
1858 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001860
Paul Bakker9af723c2014-05-01 13:03:14 +02001861 memset( szDir, 0, sizeof(szDir) );
1862 memset( filename, 0, MAX_PATH );
1863 memcpy( filename, path, len );
1864 filename[len++] = '\\';
1865 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001866 filename[len++] = '*';
1867
Simon B3c6b18d2016-11-03 01:11:37 +00001868 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001869 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001870 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001872
1873 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001874 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001876
1877 len = MAX_PATH - len;
1878 do
1879 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001880 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001881
1882 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1883 continue;
1884
Paul Bakker9af723c2014-05-01 13:03:14 +02001885 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001886 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001887 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001888 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001889 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001890 {
1891 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1892 goto cleanup;
1893 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001896 if( w_ret < 0 )
1897 ret++;
1898 else
1899 ret += w_ret;
1900 }
1901 while( FindNextFileW( hFind, &file_data ) != 0 );
1902
Paul Bakker66d5d072014-06-17 16:39:18 +02001903 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001905
Ron Eldor36d90422017-01-09 15:09:16 +02001906cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001907 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001908#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001909 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001910 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001911 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001912 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001913 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001914 DIR *dir = opendir( path );
1915
Paul Bakker66d5d072014-06-17 16:39:18 +02001916 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918
Ron Eldor63140682017-01-09 19:27:59 +02001919#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001920 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001921 {
1922 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001923 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001924 }
Ron Eldor63140682017-01-09 19:27:59 +02001925#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001926
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001927 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928 {
Andres AGf9113192016-09-02 14:06:04 +01001929 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1930 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931
Andres AGf9113192016-09-02 14:06:04 +01001932 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001933 {
Andres AGf9113192016-09-02 14:06:04 +01001934 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1935 goto cleanup;
1936 }
1937 else if( stat( entry_name, &sb ) == -1 )
1938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001940 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001941 }
1942
1943 if( !S_ISREG( sb.st_mode ) )
1944 continue;
1945
1946 // Ignore parse errors
1947 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001949 if( t_ret < 0 )
1950 ret++;
1951 else
1952 ret += t_ret;
1953 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001954
1955cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001956 closedir( dir );
1957
Ron Eldor63140682017-01-09 19:27:59 +02001958#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001959 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001961#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001962
Paul Bakkerbe089b02013-10-14 15:51:50 +02001963#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001964
1965 return( ret );
1966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001968
Hanno Becker08d34122019-06-25 09:42:57 +01001969typedef struct mbedtls_x509_crt_sig_info
1970{
1971 mbedtls_md_type_t sig_md;
1972 mbedtls_pk_type_t sig_pk;
1973 void *sig_opts;
1974 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
1975 size_t crt_hash_len;
1976 mbedtls_x509_buf_raw sig;
1977 mbedtls_x509_buf_raw issuer_raw;
1978} mbedtls_x509_crt_sig_info;
1979
1980static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
1981{
1982#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
1983 mbedtls_free( info->sig_opts );
1984#else
1985 ((void) info);
1986#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
1987}
1988
1989static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
1990 mbedtls_x509_crt_sig_info *info )
1991{
1992 const mbedtls_md_info_t *md_info;
1993
1994 md_info = mbedtls_md_info_from_type( frame->sig_md );
1995 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
1996 info->crt_hash ) != 0 )
1997 {
1998 /* Note: this can't happen except after an internal error */
1999 return( -1 );
2000 }
2001
2002 info->crt_hash_len = mbedtls_md_get_size( md_info );
2003
2004 /* Make sure that this function leaves the target structure
2005 * ready to be freed, regardless of success of failure. */
2006 info->sig_opts = NULL;
2007
2008#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2009 {
2010 int ret;
2011 unsigned char *alg_start = frame->sig_alg.p;
2012 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2013
2014 /* Get signature options -- currently only
2015 * necessary for RSASSA-PSS. */
2016 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2017 &info->sig_pk, &info->sig_opts );
2018 if( ret != 0 )
2019 {
2020 /* Note: this can't happen except after an internal error */
2021 return( -1 );
2022 }
2023 }
2024#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2025 info->sig_md = frame->sig_md;
2026 info->sig_pk = frame->sig_pk;
2027#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2028
2029 info->issuer_raw = frame->issuer_raw;
2030 info->sig = frame->sig;
2031 return( 0 );
2032}
2033
Hanno Becker02a21932019-06-10 15:08:43 +01002034#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002035static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002037{
2038 size_t i;
2039 size_t n = *size;
2040 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002042 const char *sep = "";
2043 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002044
2045 while( cur != NULL )
2046 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002047 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002048 {
2049 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002050 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002051 }
2052
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002053 n -= cur->buf.len + sep_len;
2054 for( i = 0; i < sep_len; i++ )
2055 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002056 for( i = 0; i < cur->buf.len; i++ )
2057 *p++ = cur->buf.p[i];
2058
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002059 sep = ", ";
2060 sep_len = 2;
2061
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002062 cur = cur->next;
2063 }
2064
2065 *p = '\0';
2066
2067 *size = n;
2068 *buf = p;
2069
2070 return( 0 );
2071}
2072
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002073#define PRINT_ITEM(i) \
2074 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002076 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002077 sep = ", "; \
2078 }
2079
2080#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002081 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002082 PRINT_ITEM( name );
2083
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002084static int x509_info_cert_type( char **buf, size_t *size,
2085 unsigned char ns_cert_type )
2086{
2087 int ret;
2088 size_t n = *size;
2089 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002090 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002093 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2095 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2096 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002097 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2098 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2099 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002100
2101 *size = n;
2102 *buf = p;
2103
2104 return( 0 );
2105}
2106
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002107#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002108 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002109 PRINT_ITEM( name );
2110
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002111static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002112 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002113{
2114 int ret;
2115 size_t n = *size;
2116 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002117 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2120 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002121 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2122 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2123 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2125 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002126 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2127 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002128
2129 *size = n;
2130 *buf = p;
2131
2132 return( 0 );
2133}
2134
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002135static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002137{
2138 int ret;
2139 const char *desc;
2140 size_t n = *size;
2141 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002143 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002144
2145 while( cur != NULL )
2146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002148 desc = "???";
2149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002151 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002152
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002153 sep = ", ";
2154
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002155 cur = cur->next;
2156 }
2157
2158 *size = n;
2159 *buf = p;
2160
2161 return( 0 );
2162}
2163
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002164/*
2165 * Return an informational string about the certificate.
2166 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002167#define BEFORE_COLON 18
2168#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002170 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002171{
2172 int ret;
2173 size_t n;
2174 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002175 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002176 mbedtls_x509_crt_frame frame;
2177 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002178
Hanno Becker5226c532019-02-27 17:38:40 +00002179 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2180 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002181 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002182
2183 p = buf;
2184 n = size;
2185
Hanno Becker4f869ed2019-02-24 16:47:57 +00002186 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002187 mbedtls_pk_init( &pk );
2188
2189 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002190 {
2191 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002192 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Janos Follath98e28a72016-05-31 14:03:54 +01002193
2194 return( (int) ( size - n ) );
2195 }
2196
Hanno Becker5226c532019-02-27 17:38:40 +00002197 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002198 if( ret != 0 )
2199 {
2200 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2201 goto cleanup;
2202 }
2203
Hanno Becker5226c532019-02-27 17:38:40 +00002204 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002205 if( ret != 0 )
2206 {
2207 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2208 goto cleanup;
2209 }
2210
Hanno Becker5226c532019-02-27 17:38:40 +00002211 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002212 if( ret != 0 )
2213 {
2214 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2215 goto cleanup;
2216 }
2217
Hanno Becker5226c532019-02-27 17:38:40 +00002218 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002219 if( ret != 0 )
2220 {
2221 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2222 goto cleanup;
2223 }
2224
Hanno Becker5226c532019-02-27 17:38:40 +00002225 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002226 if( ret != 0 )
2227 {
2228 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2229 goto cleanup;
2230 }
2231
Hanno Becker5226c532019-02-27 17:38:40 +00002232 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2233 if( ret != 0 )
2234 {
2235 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2236 goto cleanup;
2237 }
2238
2239 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002240 if( ret != 0 )
2241 {
2242 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2243 goto cleanup;
2244 }
2245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002247 prefix, frame.version );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002248 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002249
Hanno Becker4f869ed2019-02-24 16:47:57 +00002250 {
2251 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002252 serial.p = frame.serial.p;
2253 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002254 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2255 prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002256 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002257 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002258 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002259 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002262 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002263 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002264 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002267 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002268 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002269 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002272 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002273 frame.valid_from.year, frame.valid_from.mon,
2274 frame.valid_from.day, frame.valid_from.hour,
2275 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002276 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002279 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002280 frame.valid_to.year, frame.valid_to.mon,
2281 frame.valid_to.day, frame.valid_to.hour,
2282 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002283 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002286 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002287
Hanno Becker83cd8672019-02-21 17:13:46 +00002288 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2289 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002290 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002291
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002292 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002294 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002295 {
2296 return( ret );
2297 }
2298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002300 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002301 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002302
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002303 /*
2304 * Optional extensions
2305 */
2306
Hanno Becker5226c532019-02-27 17:38:40 +00002307 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002310 frame.ca_istrue ? "true" : "false" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002311 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002312
Hanno Becker5226c532019-02-27 17:38:40 +00002313 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002314 {
Hanno Becker5226c532019-02-27 17:38:40 +00002315 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002316 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002317 }
2318 }
2319
Hanno Becker5226c532019-02-27 17:38:40 +00002320 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002323 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002324
2325 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002326 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002327 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002328 }
2329
Hanno Becker5226c532019-02-27 17:38:40 +00002330 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002333 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002334
Hanno Becker5226c532019-02-27 17:38:40 +00002335 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002336 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002337 }
2338
Hanno Becker5226c532019-02-27 17:38:40 +00002339 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002342 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002343
Hanno Becker5226c532019-02-27 17:38:40 +00002344 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002345 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002346 }
2347
Hanno Becker5226c532019-02-27 17:38:40 +00002348 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002351 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002352
2353 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002354 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002355 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002356 }
2357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002359 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002360
Hanno Becker4f869ed2019-02-24 16:47:57 +00002361 ret = (int) ( size - n );
2362
2363cleanup:
2364
Hanno Becker4f869ed2019-02-24 16:47:57 +00002365 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002366 mbedtls_pk_free( &pk );
2367 mbedtls_x509_name_free( issuer );
2368 mbedtls_x509_name_free( subject );
2369 mbedtls_x509_sequence_free( ext_key_usage );
2370 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002371
2372 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002373}
2374
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002375struct x509_crt_verify_string {
2376 int code;
2377 const char *string;
2378};
2379
2380static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002381 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002382 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2383 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2384 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2385 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2386 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002387 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2388 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2389 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002390 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002391 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2392 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2393 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2394 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002395 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2396 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2397 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2398 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2399 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2400 { 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 +01002401 { 0, NULL }
2402};
2403
2404int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002405 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002406{
2407 int ret;
2408 const struct x509_crt_verify_string *cur;
2409 char *p = buf;
2410 size_t n = size;
2411
2412 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2413 {
2414 if( ( flags & cur->code ) == 0 )
2415 continue;
2416
2417 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002418 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002419 flags ^= cur->code;
2420 }
2421
2422 if( flags != 0 )
2423 {
2424 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2425 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002426 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002427 }
2428
2429 return( (int) ( size - n ) );
2430}
Hanno Becker02a21932019-06-10 15:08:43 +01002431#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002434static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2435 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002436{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002437 unsigned int usage_must, usage_may;
2438 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2439 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2440
2441 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2442 return( 0 );
2443
2444 usage_must = usage & ~may_mask;
2445
2446 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2447 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2448
2449 usage_may = usage & may_mask;
2450
2451 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002453
2454 return( 0 );
2455}
Hanno Becker45eedf12019-02-25 13:55:33 +00002456
2457int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2458 unsigned int usage )
2459{
2460 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002461 mbedtls_x509_crt_frame const *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002462 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002463 if( ret != 0 )
2464 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2465
2466 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002467 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002468
2469 return( ret );
2470}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002471#endif
2472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002474typedef struct
2475{
2476 const char *oid;
2477 size_t oid_len;
2478} x509_crt_check_ext_key_usage_cb_ctx_t;
2479
2480static int x509_crt_check_ext_key_usage_cb( void *ctx,
2481 int tag,
2482 unsigned char *data,
2483 size_t data_len )
2484{
2485 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2486 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2487 ((void) tag);
2488
2489 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2490 data, data_len ) == 0 )
2491 {
2492 return( 1 );
2493 }
2494
2495 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2496 data_len ) == 0 )
2497 {
2498 return( 1 );
2499 }
2500
2501 return( 0 );
2502}
2503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002505 const char *usage_oid,
2506 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002507{
Hanno Beckere1956af2019-02-21 14:28:12 +00002508 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002509 mbedtls_x509_crt_frame const *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002510 unsigned ext_types;
2511 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002512 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002513
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002514 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002515 if( ret != 0 )
2516 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2517
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002518 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002519 ext_types = frame->ext_types;
2520 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2521 {
2522 p = frame->ext_key_usage_raw.p;
2523 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002524
Hanno Beckere9718b42019-02-25 18:11:42 +00002525 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2526 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2527 x509_crt_check_ext_key_usage_cb,
2528 &cb_ctx );
2529 if( ret == 1 )
2530 ret = 0;
2531 else
2532 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2533 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002534
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002535 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002536 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002541/*
2542 * Return 1 if the certificate is revoked, or 0 otherwise.
2543 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002544static int x509_serial_is_revoked( unsigned char const *serial,
2545 size_t serial_len,
2546 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002549
2550 while( cur != NULL && cur->serial.len != 0 )
2551 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002552 if( serial_len == cur->serial.len &&
2553 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002554 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002555 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002556 return( 1 );
2557 }
2558
2559 cur = cur->next;
2560 }
2561
2562 return( 0 );
2563}
2564
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002565int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2566 const mbedtls_x509_crl *crl )
2567{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002568 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002569 mbedtls_x509_crt_frame const *frame;
Hanno Becker79ae5b62019-02-25 18:12:00 +00002570
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002571 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002572 if( ret != 0 )
2573 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2574
2575 ret = x509_serial_is_revoked( frame->serial.p,
2576 frame->serial.len,
2577 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002578 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002579 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002580}
2581
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002582/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002583 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002584 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002585 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002586static int x509_crt_verifycrl( unsigned char *crt_serial,
2587 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002588 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002589 mbedtls_x509_crl *crl_list,
2590 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002591{
Hanno Beckerbb266132019-02-25 18:12:46 +00002592 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002593 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2595 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002596 mbedtls_x509_buf_raw ca_subject;
2597 mbedtls_pk_context *pk;
2598 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002599
Hanno Beckerbb266132019-02-25 18:12:46 +00002600 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002601 return( flags );
2602
Hanno Beckerbb266132019-02-25 18:12:46 +00002603 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002604 mbedtls_x509_crt_frame const *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002605 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002606 if( ret != 0 )
2607 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2608
2609 ca_subject = ca->subject_raw;
2610
2611 can_sign = 0;
2612 if( x509_crt_check_key_usage_frame( ca,
2613 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2614 {
2615 can_sign = 1;
2616 }
2617
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002618 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002619 }
2620
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002621 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002622 if( ret != 0 )
2623 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2624
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002625 while( crl_list != NULL )
2626 {
2627 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002628 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002629 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002630 {
2631 crl_list = crl_list->next;
2632 continue;
2633 }
2634
2635 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002636 * Check if the CA is configured to sign CRLs
2637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002639 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002642 break;
2643 }
2644#endif
2645
2646 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002647 * Check if CRL is correctly signed by the trusted CA
2648 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002649 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2650 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2651
2652 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2653 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002656 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002657 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002658 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002660 break;
2661 }
2662
Hanno Beckerbb266132019-02-25 18:12:46 +00002663 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002664 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002665
Hanno Beckerbb266132019-02-25 18:12:46 +00002666 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002668 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002671 break;
2672 }
2673
2674 /*
2675 * Check for validity of CRL (Do not drop out)
2676 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002677 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002679
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002680 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002681 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002682
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002683 /*
2684 * Check if certificate is revoked
2685 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002686 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2687 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002690 break;
2691 }
2692
2693 crl_list = crl_list->next;
2694 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002695
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002696 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002697 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002700
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002701/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002702 * Check the signature of a certificate by its parent
2703 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002704static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002705 mbedtls_x509_crt *parent,
2706 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002707{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002708 int ret;
2709 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002710
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002711 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002712 if( ret != 0 )
2713 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2714
2715 /* Skip expensive computation on obvious mismatch */
2716 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002717 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002718 ret = -1;
2719 goto exit;
2720 }
2721
2722#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2723 ((void) rs_ctx);
2724#else
2725 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2726 {
2727 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002728 sig_info->sig_md,
2729 sig_info->crt_hash, sig_info->crt_hash_len,
2730 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002731 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002732 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002733 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002734#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002735 {
2736 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2737 sig_info->sig_opts,
2738 pk,
2739 sig_info->sig_md,
2740 sig_info->crt_hash, sig_info->crt_hash_len,
2741 sig_info->sig.p, sig_info->sig.len );
2742 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002743
Hanno Beckere449e2d2019-02-25 14:45:31 +00002744exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002745 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002746 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002747}
2748
2749/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002750 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2751 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002752 *
2753 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002754 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002755static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2756 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002757 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002758{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002759 int need_ca_bit;
2760
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002761 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002762 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2763 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002764 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002765 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002766 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002767 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002768
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002769 /* Parent must have the basicConstraints CA bit set as a general rule */
2770 need_ca_bit = 1;
2771
2772 /* Exception: v1/v2 certificates that are locally trusted. */
2773 if( top && parent->version < 3 )
2774 need_ca_bit = 0;
2775
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002776 if( need_ca_bit && ! parent->ca_istrue )
2777 return( -1 );
2778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002780 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002781 x509_crt_check_key_usage_frame( parent,
2782 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002783 {
2784 return( -1 );
2785 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002786#endif
2787
2788 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002789}
2790
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002791/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002792 * Find a suitable parent for child in candidates, or return NULL.
2793 *
2794 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002795 * 1. subject name matches child's issuer
2796 * 2. if necessary, the CA bit is set and key usage allows signing certs
2797 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002798 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002799 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002800 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002801 * If there's a suitable candidate which is also time-valid, return the first
2802 * such. Otherwise, return the first suitable candidate (or NULL if there is
2803 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002804 *
2805 * The rationale for this rule is that someone could have a list of trusted
2806 * roots with two versions on the same root with different validity periods.
2807 * (At least one user reported having such a list and wanted it to just work.)
2808 * The reason we don't just require time-validity is that generally there is
2809 * only one version, and if it's expired we want the flags to state that
2810 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002811 *
2812 * The rationale for rule 3 (signature for trusted roots) is that users might
2813 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002814 * way we select the correct one is by checking the signature (as we don't
2815 * rely on key identifier extensions). (This is one way users might choose to
2816 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002817 *
2818 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002819 * - [in] child: certificate for which we're looking for a parent
2820 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002821 * - [out] r_parent: parent found (or NULL)
2822 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002823 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2824 * of the chain, 0 otherwise
2825 * - [in] path_cnt: number of intermediates seen so far
2826 * - [in] self_cnt: number of self-signed intermediates seen so far
2827 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002828 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002829 *
2830 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002831 * - 0 on success
2832 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002833 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002834static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002835 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002836 mbedtls_x509_crt *candidates,
2837 mbedtls_x509_crt **r_parent,
2838 int *r_signature_is_good,
2839 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002840 unsigned path_cnt,
2841 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002842 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002843{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002844 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002845 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002846 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002847
2848#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002849 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002850 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2851 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002852 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002853 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002854 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002855 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002856
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002857 /* clear saved state */
2858 rs_ctx->parent = NULL;
2859 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002860 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002861
2862 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002863 goto check_signature;
2864 }
2865#endif
2866
2867 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002868 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002869
Hanno Becker43bf9002019-02-25 14:46:49 +00002870 for( parent_crt = candidates; parent_crt != NULL;
2871 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002872 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002873 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002874
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002875#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2876check_signature:
2877#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002878
2879 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002880 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002881 mbedtls_x509_crt_frame const *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002882
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002883 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002884 if( ret != 0 )
2885 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002886
Hanno Becker040c5642019-06-10 11:14:24 +01002887 if( !mbedtls_x509_time_is_past( &parent->valid_to ) &&
2888 !mbedtls_x509_time_is_future( &parent->valid_from ) )
Hanno Becker43bf9002019-02-25 14:46:49 +00002889 {
2890 parent_valid = 1;
2891 }
2892
2893 /* basic parenting skills (name, CA bit, key usage) */
2894 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2895 parent_match = 1;
2896
2897 /* +1 because the stored max_pathlen is 1 higher
2898 * than the actual value */
2899 if( !( parent->max_pathlen > 0 &&
2900 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2901 {
2902 path_len_ok = 1;
2903 }
2904
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002905 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002906 }
2907
2908 if( parent_match == 0 || path_len_ok == 0 )
2909 continue;
2910
2911 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002912 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002913
2914#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002915 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2916 {
2917 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002918 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002919 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002920 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002921
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002922 return( ret );
2923 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002924#else
2925 (void) ret;
2926#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002927
2928 signature_is_good = ret == 0;
2929 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002930 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002931
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002932 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002933 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002934 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002935 if( fallback_parent == NULL )
2936 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002937 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002938 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002939 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002940
2941 continue;
2942 }
2943
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002944 break;
2945 }
2946
Hanno Becker43bf9002019-02-25 14:46:49 +00002947 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002948 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002949 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002950 *r_signature_is_good = signature_is_good;
2951 }
2952 else
2953 {
2954 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002955 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002956 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002957
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002958 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002959}
2960
2961/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002962 * Find a parent in trusted CAs or the provided chain, or return NULL.
2963 *
2964 * Searches in trusted CAs first, and return the first suitable parent found
2965 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002966 *
2967 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002968 * - [in] child: certificate for which we're looking for a parent, followed
2969 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002970 * - [in] trust_ca: list of locally trusted certificates
2971 * - [out] parent: parent found (or NULL)
2972 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2973 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2974 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2975 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002976 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002977 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002978 *
2979 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002980 * - 0 on success
2981 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002982 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002983static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002984 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002985 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002986 mbedtls_x509_crt *trust_ca,
2987 mbedtls_x509_crt **parent,
2988 int *parent_is_trusted,
2989 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002990 unsigned path_cnt,
2991 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002992 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002993{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002994 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002995 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002996
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002997 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002998
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002999#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003000 /* restore then clear saved state if we have some stored */
3001 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
3002 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003003 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003004 rs_ctx->parent_is_trusted = -1;
3005 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003006#endif
3007
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003008 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00003009 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003010
Hanno Becker5299cf82019-02-25 13:50:41 +00003011 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003012 parent, signature_is_good,
3013 *parent_is_trusted,
3014 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003015
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003016#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003017 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3018 {
3019 /* save state */
3020 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003021 return( ret );
3022 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003023#else
3024 (void) ret;
3025#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003026
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003027 /* stop here if found or already in second iteration */
3028 if( *parent != NULL || *parent_is_trusted == 0 )
3029 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003030
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003031 /* prepare second iteration */
3032 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003033 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003034
3035 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01003036 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003037 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02003038 *parent_is_trusted = 0;
3039 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003040 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003041
3042 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003043}
3044
3045/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003046 * Check if an end-entity certificate is locally trusted
3047 *
3048 * Currently we require such certificates to be self-signed (actually only
3049 * check for self-issued as self-signatures are not checked)
3050 */
3051static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003052 mbedtls_x509_crt_frame const *crt,
3053 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003054{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003055 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003056
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003057 /* look for an exact match with trusted cert */
3058 for( cur = trust_ca; cur != NULL; cur = cur->next )
3059 {
3060 if( crt->raw.len == cur->raw.len &&
3061 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3062 {
3063 return( 0 );
3064 }
3065 }
3066
3067 /* too bad */
3068 return( -1 );
3069}
3070
3071/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003072 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003073 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003074 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3075 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003076 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003077 * such that every cert in the chain is a child of the next one,
3078 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003079 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003080 * Verify that chain and return it with flags for all issues found.
3081 *
3082 * Special cases:
3083 * - EE == Rj -> return a one-element list containing it
3084 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3085 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003086 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003087 * Tests for (aspects of) this function should include at least:
3088 * - trusted EE
3089 * - EE -> trusted root
3090 * - EE -> intermedate CA -> trusted root
3091 * - if relevant: EE untrusted
3092 * - if relevant: EE -> intermediate, untrusted
3093 * with the aspect under test checked at each relevant level (EE, int, root).
3094 * For some aspects longer chains are required, but usually length 2 is
3095 * enough (but length 1 is not in general).
3096 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003097 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003098 * - [in] crt: the cert list EE, C1, ..., Cn
3099 * - [in] trust_ca: the trusted list R1, ..., Rp
3100 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003101 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003102 * Only valid when return value is 0, may contain garbage otherwise!
3103 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003104 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003105 *
3106 * Return value:
3107 * - non-zero if the chain could not be fully built and examined
3108 * - 0 is the chain was successfully built and examined,
3109 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003110 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003111static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003112 mbedtls_x509_crt *crt,
3113 mbedtls_x509_crt *trust_ca,
3114 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003115 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003116 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003117 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003118{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003119 /* Don't initialize any of those variables here, so that the compiler can
3120 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003121 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003122 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003123 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003124 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003125 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003126 int parent_is_trusted;
3127 int child_is_trusted;
3128 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003129 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003130
3131#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3132 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003133 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003134 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003135 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003136 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003137 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003138
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003139 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003140 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003141 child_crt = cur->crt;
3142
3143 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003144 goto find_parent;
3145 }
3146#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003147
Hanno Becker1e0677a2019-02-25 14:58:22 +00003148 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003149 self_cnt = 0;
3150 parent_is_trusted = 0;
3151 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003152
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003153 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003154#if defined(MBEDTLS_X509_CRL_PARSE_C)
3155 mbedtls_x509_buf_raw child_serial;
3156#endif /* MBEDTLS_X509_CRL_PARSE_C */
3157 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003158
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003159 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003160 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003161 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003162 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003163 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003164
3165#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3166find_parent:
3167#endif
3168
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003169 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003170
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003171 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003172 mbedtls_x509_crt_sig_info child_sig;
3173 {
Hanno Becker5f268b32019-05-20 16:26:34 +01003174 mbedtls_x509_crt_frame const *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003175
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003176 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003177 if( ret != 0 )
3178 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3179
3180 /* Check time-validity (all certificates) */
3181 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3182 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3183 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3184 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
3185
3186 /* Stop here for trusted roots (but not for trusted EE certs) */
3187 if( child_is_trusted )
3188 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003189 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003190 return( 0 );
3191 }
3192
3193 self_issued = 0;
3194 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3195 &child->subject_raw,
3196 NULL, NULL ) == 0 )
3197 {
3198 self_issued = 1;
3199 }
3200
3201 /* Check signature algorithm: MD & PK algs */
3202 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3203 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3204
3205 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3206 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3207
3208 /* Special case: EE certs that are locally trusted */
3209 if( ver_chain->len == 1 && self_issued &&
3210 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3211 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003212 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003213 return( 0 );
3214 }
3215
3216#if defined(MBEDTLS_X509_CRL_PARSE_C)
3217 child_serial = child->serial;
3218#endif /* MBEDTLS_X509_CRL_PARSE_C */
3219
3220 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003221 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003222
Hanno Becker5299cf82019-02-25 13:50:41 +00003223 if( ret != 0 )
3224 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3225 }
3226
3227 /* Look for a parent in trusted CAs or up the chain */
3228 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003229 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003230 &parent_is_trusted, &signature_is_good,
3231 ver_chain->len - 1, self_cnt, rs_ctx );
3232
3233 x509_crt_free_sig_info( &child_sig );
3234 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003235
3236#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003237 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3238 {
3239 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003240 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003241 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003242 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003243 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003244 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003245#else
3246 (void) ret;
3247#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003248
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003249 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003250 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003251 {
3252 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003253 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003254 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003255
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003256 /* Count intermediate self-issued (not necessarily self-signed) certs.
3257 * These can occur with some strategies for key rollover, see [SIRO],
3258 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003259 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003260 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003261
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003262 /* path_cnt is 0 for the first intermediate CA,
3263 * and if parent is trusted it's not an intermediate CA */
3264 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003265 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003266 {
3267 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003268 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003269 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003270
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003271 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003272 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003273 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3274
Hanno Becker58c35642019-02-25 18:13:46 +00003275 {
3276 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003277 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003278 if( ret != 0 )
3279 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3280
3281 /* check size of signing key */
3282 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3283 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3284
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003285 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003286 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003289 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003290 *flags |= x509_crt_verifycrl( child_serial.p,
3291 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003292 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003293#else
3294 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003295#endif
3296
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003297 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003298 child_crt = parent_crt;
3299 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003300 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003301 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003302 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003303}
3304
3305/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003306 * Check for CN match
3307 */
Hanno Becker24926222019-02-21 13:10:55 +00003308static int x509_crt_check_cn( unsigned char const *buf,
3309 size_t buflen,
3310 const char *cn,
3311 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003312{
Hanno Becker24926222019-02-21 13:10:55 +00003313 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003314 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003315 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003316
3317 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003318 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003319 {
3320 return( 0 );
3321 }
3322
3323 return( -1 );
3324}
3325
Hanno Becker8b543b32019-02-21 11:50:44 +00003326/* Returns 1 on a match and 0 on a mismatch.
3327 * This is because this function is used as a callback for
3328 * mbedtls_x509_name_cmp_raw(), which continues the name
3329 * traversal as long as the callback returns 0. */
3330static int x509_crt_check_name( void *ctx,
3331 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003332 mbedtls_x509_buf *val,
3333 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003334{
3335 char const *cn = (char const*) ctx;
3336 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003337 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003338
3339 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003340 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003341 {
3342 return( 1 );
3343 }
3344
3345 return( 0 );
3346}
3347
Hanno Beckerda410822019-02-21 13:36:59 +00003348/* Returns 1 on a match and 0 on a mismatch.
3349 * This is because this function is used as a callback for
3350 * mbedtls_asn1_traverse_sequence_of(), which continues the
3351 * traversal as long as the callback returns 0. */
3352static int x509_crt_subject_alt_check_name( void *ctx,
3353 int tag,
3354 unsigned char *data,
3355 size_t data_len )
3356{
3357 char const *cn = (char const*) ctx;
3358 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003359 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003360
3361 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3362 return( 1 );
3363
3364 return( 0 );
3365}
3366
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003367/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003368 * Verify the requested CN - only call this if cn is not NULL!
3369 */
Hanno Becker082435c2019-02-25 18:14:40 +00003370static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3371 const char *cn,
3372 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003373{
Hanno Beckerda410822019-02-21 13:36:59 +00003374 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01003375 mbedtls_x509_crt_frame const *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003376
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003377 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003378 if( ret != 0 )
3379 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3380
3381 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003382 {
Hanno Becker90b94082019-02-21 21:13:21 +00003383 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003384 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003385 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003386 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003387
Hanno Becker90b94082019-02-21 21:13:21 +00003388 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3389 MBEDTLS_ASN1_TAG_CLASS_MASK,
3390 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3391 MBEDTLS_ASN1_TAG_VALUE_MASK,
3392 2 /* SubjectAlt DNS */,
3393 x509_crt_subject_alt_check_name,
Hanno Becker484caf02019-05-29 14:41:44 +01003394 (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003395 }
3396 else
3397 {
Hanno Becker082435c2019-02-25 18:14:40 +00003398 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3399 &frame->subject_raw,
Hanno Becker484caf02019-05-29 14:41:44 +01003400 x509_crt_check_name, (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003401 }
Hanno Beckerda410822019-02-21 13:36:59 +00003402
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003403 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003404
3405 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3406 * return 1 when finding a name component matching `cn`. */
3407 if( ret == 1 )
3408 return( 0 );
3409
3410 if( ret != 0 )
3411 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3412
3413 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3414 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003415}
3416
3417/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003418 * Merge the flags for all certs in the chain, after calling callback
3419 */
3420static int x509_crt_merge_flags_with_cb(
3421 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003422 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003423 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3424 void *p_vrfy )
3425{
3426 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003427 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003428 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003429 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003430
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003431 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003432 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003433 cur = &ver_chain->items[i-1];
3434 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003435
3436 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003437 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003438 return( ret );
3439
3440 *flags |= cur_flags;
3441 }
3442
3443 return( 0 );
3444}
3445
3446/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003447 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3450 mbedtls_x509_crt *trust_ca,
3451 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003452 const char *cn, uint32_t *flags,
3453 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003454 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003455{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003456 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3457 &mbedtls_x509_crt_profile_default, cn, flags,
3458 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003459}
3460
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003461/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003462 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003463 */
3464int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3465 mbedtls_x509_crt *trust_ca,
3466 mbedtls_x509_crl *ca_crl,
3467 const mbedtls_x509_crt_profile *profile,
3468 const char *cn, uint32_t *flags,
3469 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3470 void *p_vrfy )
3471{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003472 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3473 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3474}
3475
3476/*
3477 * Verify the certificate validity, with profile, restartable version
3478 *
3479 * This function:
3480 * - checks the requested CN (if any)
3481 * - checks the type and size of the EE cert's key,
3482 * as that isn't done as part of chain building/verification currently
3483 * - builds and verifies the chain
3484 * - then calls the callback and merges the flags
3485 */
3486int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3487 mbedtls_x509_crt *trust_ca,
3488 mbedtls_x509_crl *ca_crl,
3489 const mbedtls_x509_crt_profile *profile,
3490 const char *cn, uint32_t *flags,
3491 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3492 void *p_vrfy,
3493 mbedtls_x509_crt_restart_ctx *rs_ctx )
3494{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003495 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003496 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003497 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003498
3499 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003500 ee_flags = 0;
3501 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003502
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003503 if( profile == NULL )
3504 {
3505 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3506 goto exit;
3507 }
3508
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003509 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003510 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003511 {
3512 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3513 if( ret != 0 )
3514 return( ret );
3515 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003516
Hanno Becker87233362019-02-25 18:15:33 +00003517 {
3518 mbedtls_pk_context *pk;
3519 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003520
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003521 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003522 if( ret != 0 )
3523 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003524
Hanno Becker87233362019-02-25 18:15:33 +00003525 /* Check the type and size of the key */
3526 pk_type = mbedtls_pk_get_type( pk );
3527
3528 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3529 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3530
3531 if( x509_profile_check_key( profile, pk ) != 0 )
3532 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3533
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003534 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003535 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003536
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003537 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003538 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003539 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003540
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003541 if( ret != 0 )
3542 goto exit;
3543
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003544 /* Merge end-entity flags */
3545 ver_chain.items[0].flags |= ee_flags;
3546
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003547 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003548 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003549
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003550exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003551#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3552 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3553 mbedtls_x509_crt_restart_free( rs_ctx );
3554#endif
3555
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003556 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3557 * the SSL module for authmode optional, but non-zero return from the
3558 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003559 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3560 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3561
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003562 if( ret != 0 )
3563 {
3564 *flags = (uint32_t) -1;
3565 return( ret );
3566 }
3567
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003568 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003570
3571 return( 0 );
3572}
3573
3574/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003575 * Initialize a certificate chain
3576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003580}
3581
3582/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003583 * Unallocate all certificate data
3584 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003587{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 mbedtls_x509_crt *cert_cur = crt;
3589 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003590
3591 if( crt == NULL )
3592 return;
3593
3594 do
3595 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003596 x509_crt_cache_free( cert_cur->cache );
3597 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003598
3599#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3603 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003604#endif
3605
Hanno Becker2bcc7642019-02-26 19:01:00 +00003606 mbedtls_x509_name_free( cert_cur->issuer.next );
3607 mbedtls_x509_name_free( cert_cur->subject.next );
3608 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3609 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003610#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003611
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003612 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003613 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003614 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003616 }
3617
3618 cert_cur = cert_cur->next;
3619 }
3620 while( cert_cur != NULL );
3621
3622 cert_cur = crt;
3623 do
3624 {
3625 cert_prv = cert_cur;
3626 cert_cur = cert_cur->next;
3627
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003628 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003629 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003631 }
3632 while( cert_cur != NULL );
3633}
3634
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003635#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3636/*
3637 * Initialize a restart context
3638 */
3639void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3640{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003641 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003642
3643 ctx->parent = NULL;
3644 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003645 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003646
3647 ctx->parent_is_trusted = -1;
3648
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003649 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003650 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003651 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003652}
3653
3654/*
3655 * Free the components of a restart context
3656 */
3657void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3658{
3659 if( ctx == NULL )
3660 return;
3661
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003662 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003663 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003664}
3665#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667#endif /* MBEDTLS_X509_CRT_PARSE_C */