blob: 4f33d211d5a0b9407155c0788c11097b1c2bf24d [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 {
196#if !defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker76428352019-03-05 15:29:23 +0000197 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100198#else
199 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
200 * allow nested uses of acquire. */
201 return( MBEDTLS_ERR_X509_FATAL_ERROR );
202#endif
203 }
Hanno Becker76428352019-03-05 15:29:23 +0000204
Hanno Becker337088a2019-02-25 14:53:14 +0000205 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
206 if( frame == NULL )
207 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000208 cache->frame = frame;
Hanno Becker337088a2019-02-25 14:53:14 +0000209
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000210#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
211 /* This would work with !MBEDTLS_X509_ON_DEMAND_PARSING, too,
212 * but is inefficient compared to copying the respective fields
213 * from the legacy mbedtls_x509_crt. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000214 return( x509_crt_parse_frame( crt->raw.p,
215 crt->raw.p + crt->raw.len,
216 frame ) );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000217#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
218 /* Make sure all extension related fields are properly initialized. */
219 frame->ca_istrue = 0;
220 frame->max_pathlen = 0;
221 frame->ext_types = 0;
222 frame->version = crt->version;
223 frame->sig_md = crt->sig_md;
224 frame->sig_pk = crt->sig_pk;
225 frame->valid_from = crt->valid_from;
226 frame->valid_to = crt->valid_to;
Hanno Becker38f0cb42019-03-04 15:13:45 +0000227 x509_buf_to_buf_raw( &frame->raw, &crt->raw );
228 x509_buf_to_buf_raw( &frame->tbs, &crt->tbs );
229 x509_buf_to_buf_raw( &frame->serial, &crt->serial );
230 x509_buf_to_buf_raw( &frame->pubkey_raw, &crt->pk_raw );
231 x509_buf_to_buf_raw( &frame->issuer_raw, &crt->issuer_raw );
232 x509_buf_to_buf_raw( &frame->subject_raw, &crt->subject_raw );
233 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
234 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
235 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
236 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000237
238 /* The legacy CRT structure doesn't explicitly contain
239 * the `AlgorithmIdentifier` bounds; however, those can
240 * be inferred from the surrounding (mandatory) `SerialNumber`
241 * and `Issuer` fields. */
242 frame->sig_alg.p = crt->serial.p + crt->serial.len;
243 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
244
245 return( x509_crt_frame_parse_ext( frame ) );
246#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000247}
Hanno Becker337088a2019-02-25 14:53:14 +0000248
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000249int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
250{
251 mbedtls_x509_crt_cache *cache = crt->cache;
252 mbedtls_pk_context *pk;
253
Hanno Becker76428352019-03-05 15:29:23 +0000254 if( cache->pk != NULL )
Hanno Beckerfc99a092019-06-28 14:45:26 +0100255 {
256#if !defined(MBEDTLS_X509_ALWAYS_FLUSH)
Hanno Becker76428352019-03-05 15:29:23 +0000257 return( 0 );
Hanno Beckerfc99a092019-06-28 14:45:26 +0100258#else
259 /* If MBEDTLS_X509_ALWAYS_FLUSH is set, we don't
260 * allow nested uses of acquire. */
261 return( MBEDTLS_ERR_X509_FATAL_ERROR );
262#endif
263 }
Hanno Becker76428352019-03-05 15:29:23 +0000264
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000265 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
266 if( pk == NULL )
267 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000268 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000269
270#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
271 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000272 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000273#else
274 {
275 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
276 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
277 pk_raw.p + pk_raw.len,
278 pk ) );
279 }
280#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000281}
282
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000283static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000284{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000285 memset( cache, 0, sizeof( *cache ) );
286#if defined(MBEDTLS_THREADING_C)
287 mbedtls_mutex_init( &cache->frame_mutex );
288 mbedtls_mutex_init( &cache->pk_mutex );
289#endif
290}
291
292static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
293{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000294#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000295 /* The cache holds a shallow copy of the PK context
296 * in the legacy struct, so don't free PK context. */
297 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000298#else
299 mbedtls_pk_free( cache->pk );
300 mbedtls_free( cache->pk );
301#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
302
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000303 cache->pk = NULL;
304}
305
306static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
307{
308 mbedtls_free( cache->frame );
309 cache->frame = NULL;
310}
311
312static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
313{
314 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000315 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000316
317#if defined(MBEDTLS_THREADING_C)
318 mbedtls_mutex_free( &cache->frame_mutex );
319 mbedtls_mutex_free( &cache->pk_mutex );
320#endif
321
322 x509_crt_cache_clear_frame( cache );
323 x509_crt_cache_clear_pk( cache );
324
325 memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000326}
327
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000328int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
329 mbedtls_x509_sequence **subj_alt )
330{
331 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100332 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000333 mbedtls_x509_sequence *seq;
334
335 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
336 if( ret != 0 )
337 return( ret );
338
339 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
340 if( seq == NULL )
341 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
342 else
343 ret = x509_crt_subject_alt_from_frame( frame, seq );
344
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000345 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000346
347 *subj_alt = seq;
348 return( ret );
349}
350
351int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
352 mbedtls_x509_sequence **ext_key_usage )
353{
354 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100355 mbedtls_x509_crt_frame const *frame;
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000356 mbedtls_x509_sequence *seq;
357
358 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
359 if( ret != 0 )
360 return( ret );
361
362 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
363 if( seq == NULL )
364 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
365 else
366 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
367
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000368 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000369
370 *ext_key_usage = seq;
371 return( ret );
372}
373
Hanno Becker63e69982019-02-26 18:50:49 +0000374int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
375 mbedtls_x509_name **subject )
376{
377 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100378 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000379 mbedtls_x509_name *name;
380
381 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
382 if( ret != 0 )
383 return( ret );
384
385 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
386 if( name == NULL )
387 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
388 else
389 ret = x509_crt_subject_from_frame( frame, name );
390
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000391 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000392
393 *subject = name;
394 return( ret );
395}
396
397int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
398 mbedtls_x509_name **issuer )
399{
400 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100401 mbedtls_x509_crt_frame const *frame;
Hanno Becker63e69982019-02-26 18:50:49 +0000402 mbedtls_x509_name *name;
403
404 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
405 if( ret != 0 )
406 return( ret );
407
408 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
409 if( name == NULL )
410 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
411 else
412 ret = x509_crt_issuer_from_frame( frame, name );
413
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000414 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000415
416 *issuer = name;
417 return( ret );
418}
419
Hanno Becker823efad2019-02-28 13:23:58 +0000420int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
421 mbedtls_x509_crt_frame *dst )
422{
423 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +0100424 mbedtls_x509_crt_frame const *frame;
Hanno Becker823efad2019-02-28 13:23:58 +0000425 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
426 if( ret != 0 )
427 return( ret );
428 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000429 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000430 return( 0 );
431}
432
433int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
434 mbedtls_pk_context *dst )
435{
436#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
437 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
438 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
439 pk_raw.p + pk_raw.len,
440 dst ) );
441#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
442 int ret;
443 mbedtls_pk_context *pk;
444 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
445 if( ret != 0 )
446 return( ret );
447
448 /* Move PK from CRT cache to destination pointer
449 * to avoid a copy. */
450 *dst = *pk;
451 mbedtls_free( crt->cache->pk );
452 crt->cache->pk = NULL;
453
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000454 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000455 return( 0 );
456#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
457}
458
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200459/*
460 * Item in a verification chain: cert and flags for it
461 */
462typedef struct {
463 mbedtls_x509_crt *crt;
464 uint32_t flags;
465} x509_crt_verify_chain_item;
466
467/*
468 * Max size of verification chain: end-entity + intermediates + trusted root
469 */
470#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200471
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200473 * Default profile
474 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200475const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
476{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200477#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200478 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200479 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200480#endif
481 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200482 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
483 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
484 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
485 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
486 0xFFFFFFF, /* Any PK alg */
487 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200488 2048,
489};
490
491/*
492 * Next-default profile
493 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200494const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
495{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200496 /* Hashes from SHA-256 and above */
497 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
498 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
499 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
500 0xFFFFFFF, /* Any PK alg */
501#if defined(MBEDTLS_ECP_C)
502 /* Curves at or above 128-bit security level */
503 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
504 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
505 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
506 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
507 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
508 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
509 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
510#else
511 0,
512#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200513 2048,
514};
515
516/*
517 * NSA Suite B Profile
518 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200519const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
520{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200521 /* Only SHA-256 and 384 */
522 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
523 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
524 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200525 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
526 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200527#if defined(MBEDTLS_ECP_C)
528 /* Only NIST P-256 and P-384 */
529 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
530 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
531#else
532 0,
533#endif
534 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200535};
536
537/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200538 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200539 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200540 */
541static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
542 mbedtls_md_type_t md_alg )
543{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200544 if( md_alg == MBEDTLS_MD_NONE )
545 return( -1 );
546
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200547 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
548 return( 0 );
549
550 return( -1 );
551}
552
553/*
554 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200555 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200556 */
557static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
558 mbedtls_pk_type_t pk_alg )
559{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200560 if( pk_alg == MBEDTLS_PK_NONE )
561 return( -1 );
562
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200563 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
564 return( 0 );
565
566 return( -1 );
567}
568
569/*
570 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200571 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200572 */
573static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200574 const mbedtls_pk_context *pk )
575{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200576 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200577
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200578#if defined(MBEDTLS_RSA_C)
579 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
580 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200581 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200582 return( 0 );
583
584 return( -1 );
585 }
586#endif
587
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200588#if defined(MBEDTLS_ECP_C)
589 if( pk_alg == MBEDTLS_PK_ECDSA ||
590 pk_alg == MBEDTLS_PK_ECKEY ||
591 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200592 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200593 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200594
Philippe Antoineb5b25432018-05-11 11:06:29 +0200595 if( gid == MBEDTLS_ECP_DP_NONE )
596 return( -1 );
597
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200598 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
599 return( 0 );
600
601 return( -1 );
602 }
603#endif
604
605 return( -1 );
606}
607
608/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000609 * Return 0 if name matches wildcard, -1 otherwise
610 */
Hanno Becker24926222019-02-21 13:10:55 +0000611static int x509_check_wildcard( char const *cn,
612 size_t cn_len,
613 unsigned char const *buf,
614 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000615{
616 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000617 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000618
619 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000620 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000621 return( -1 );
622
623 for( i = 0; i < cn_len; ++i )
624 {
625 if( cn[i] == '.' )
626 {
627 cn_idx = i;
628 break;
629 }
630 }
631
632 if( cn_idx == 0 )
633 return( -1 );
634
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000635 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
636 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000637 {
638 return( 0 );
639 }
640
641 return( -1 );
642}
643
644/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200645 * Reset (init or clear) a verify_chain
646 */
647static void x509_crt_verify_chain_reset(
648 mbedtls_x509_crt_verify_chain *ver_chain )
649{
650 size_t i;
651
652 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
653 {
654 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000655 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200656 }
657
658 ver_chain->len = 0;
659}
660
661/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
663 */
664static int x509_get_version( unsigned char **p,
665 const unsigned char *end,
666 int *ver )
667{
668 int ret;
669 size_t len;
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
672 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 {
676 *ver = 0;
677 return( 0 );
678 }
679
Hanno Becker2f472142019-02-12 11:52:10 +0000680 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681 }
682
683 end = *p + len;
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
686 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
688 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_X509_INVALID_VERSION +
690 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691
692 return( 0 );
693}
694
695/*
696 * Validity ::= SEQUENCE {
697 * notBefore Time,
698 * notAfter Time }
699 */
700static int x509_get_dates( unsigned char **p,
701 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_x509_time *from,
703 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704{
705 int ret;
706 size_t len;
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
709 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
710 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711
712 end = *p + len;
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715 return( ret );
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718 return( ret );
719
720 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_X509_INVALID_DATE +
722 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723
724 return( 0 );
725}
726
727/*
728 * X.509 v2/v3 unique identifier (not parsed)
729 */
730static int x509_get_uid( unsigned char **p,
731 const unsigned char *end,
Hanno Beckere9084122019-06-07 12:04:39 +0100732 mbedtls_x509_buf_raw *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733{
734 int ret;
735
736 if( *p == end )
737 return( 0 );
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
740 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743 return( 0 );
744
Hanno Becker2f472142019-02-12 11:52:10 +0000745 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 }
747
748 uid->p = *p;
749 *p += uid->len;
750
751 return( 0 );
752}
753
754static int x509_get_basic_constraints( unsigned char **p,
755 const unsigned char *end,
756 int *ca_istrue,
757 int *max_pathlen )
758{
759 int ret;
760 size_t len;
761
762 /*
763 * BasicConstraints ::= SEQUENCE {
764 * cA BOOLEAN DEFAULT FALSE,
765 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
766 */
767 *ca_istrue = 0; /* DEFAULT FALSE */
768 *max_pathlen = 0; /* endless */
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
771 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000772 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773
774 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200775 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
780 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781
782 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000783 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784
785 if( *ca_istrue != 0 )
786 *ca_istrue = 1;
787 }
788
789 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200790 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000793 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794
795 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000796 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797
798 (*max_pathlen)++;
799
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200800 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801}
802
803static int x509_get_ns_cert_type( unsigned char **p,
804 const unsigned char *end,
805 unsigned char *ns_cert_type)
806{
807 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000811 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812
813 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000814 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815
816 /* Get actual bitstring */
817 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200818 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819}
820
821static int x509_get_key_usage( unsigned char **p,
822 const unsigned char *end,
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100823 uint16_t *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824{
825 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200826 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000830 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831
832 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000833 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834
835 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200836 *key_usage = 0;
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100837 for( i = 0; i < bs.len && i < sizeof( *key_usage ); i++ )
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200838 {
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100839 *key_usage |= (uint16_t) bs.p[i] << ( 8*i );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200840 }
841
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200842 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843}
844
Hanno Becker529f25d2019-05-02 14:48:25 +0100845static int asn1_build_sequence_cb( void *ctx,
846 int tag,
847 unsigned char *data,
848 size_t data_len )
Hanno Becker15b73b42019-05-02 13:21:27 +0100849{
850 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
851 mbedtls_asn1_sequence *cur = *cur_ptr;
852
853 /* Allocate and assign next pointer */
854 if( cur->buf.p != NULL )
855 {
856 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
857 if( cur->next == NULL )
858 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
859 cur = cur->next;
860 }
861
862 cur->buf.tag = tag;
863 cur->buf.p = data;
864 cur->buf.len = data_len;
865
866 *cur_ptr = cur;
867 return( 0 );
868}
869
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870/*
Hanno Becker529f25d2019-05-02 14:48:25 +0100871 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
872 *
873 * KeyPurposeId ::= OBJECT IDENTIFIER
874 */
875static int x509_get_ext_key_usage( unsigned char **p,
876 const unsigned char *end,
877 mbedtls_x509_sequence *ext_key_usage)
878{
879 return( mbedtls_asn1_traverse_sequence_of( p, end,
880 0xFF, MBEDTLS_ASN1_OID,
881 0, 0,
882 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100883 (void *) &ext_key_usage ) );
Hanno Becker529f25d2019-05-02 14:48:25 +0100884}
885
886/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 * SubjectAltName ::= GeneralNames
888 *
889 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
890 *
891 * GeneralName ::= CHOICE {
892 * otherName [0] OtherName,
893 * rfc822Name [1] IA5String,
894 * dNSName [2] IA5String,
895 * x400Address [3] ORAddress,
896 * directoryName [4] Name,
897 * ediPartyName [5] EDIPartyName,
898 * uniformResourceIdentifier [6] IA5String,
899 * iPAddress [7] OCTET STRING,
900 * registeredID [8] OBJECT IDENTIFIER }
901 *
902 * OtherName ::= SEQUENCE {
903 * type-id OBJECT IDENTIFIER,
904 * value [0] EXPLICIT ANY DEFINED BY type-id }
905 *
906 * EDIPartyName ::= SEQUENCE {
907 * nameAssigner [0] DirectoryString OPTIONAL,
908 * partyName [1] DirectoryString }
909 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000910 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 */
Hanno Becker5984d302019-02-21 14:46:54 +0000912static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000916 return( mbedtls_asn1_traverse_sequence_of( &p, end,
917 MBEDTLS_ASN1_TAG_CLASS_MASK,
918 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
919 MBEDTLS_ASN1_TAG_VALUE_MASK,
920 2 /* SubjectAlt DNS */,
Hanno Becker529f25d2019-05-02 14:48:25 +0100921 asn1_build_sequence_cb,
Hanno Becker484caf02019-05-29 14:41:44 +0100922 (void *) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923}
924
925/*
926 * X.509 v3 extensions
927 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000929static int x509_crt_get_ext_cb( void *ctx,
930 int tag,
931 unsigned char *p,
932 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933{
934 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000935 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000937 unsigned char *end, *end_ext_octet;
938 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
939 int is_critical = 0; /* DEFAULT FALSE */
940 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000942 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000943
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000944 /*
945 * Extension ::= SEQUENCE {
946 * extnID OBJECT IDENTIFIER,
947 * critical BOOLEAN DEFAULT FALSE,
948 * extnValue OCTET STRING }
949 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000951 end = p + ext_len;
952
953 /* Get extension ID */
954 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
955 MBEDTLS_ASN1_OID ) ) != 0 )
956 goto err;
957
958 extn_oid.tag = MBEDTLS_ASN1_OID;
959 extn_oid.p = p;
960 p += extn_oid.len;
961
962 /* Get optional critical */
963 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
964 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
965 goto err;
966
967 /* Data should be octet string type */
968 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
969 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
970 goto err;
971
972 end_ext_octet = p + len;
973 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000975 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
976 goto err;
977 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000979 /*
980 * Detect supported extensions
981 */
982 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
983 if( ret != 0 )
984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000986 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000988 /* Data is marked as critical: fail */
989 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
990 goto err;
991 }
Hanno Beckerb36a2452019-05-29 14:43:17 +0100992#endif /* MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000993 return( 0 );
994 }
995
996 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +0000997 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000998 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
999
Hanno Becker21f55672019-02-15 15:27:59 +00001000 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001001 switch( ext_type )
1002 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001003 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001004 {
1005 int ca_istrue;
1006 int max_pathlen;
1007
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001009 ret = x509_get_basic_constraints( &p, end_ext_octet,
1010 &ca_istrue,
1011 &max_pathlen );
1012 if( ret != 0 )
1013 goto err;
1014
Hanno Becker21f55672019-02-15 15:27:59 +00001015 frame->ca_istrue = ca_istrue;
1016 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001018 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001022 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001023 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001024 if( ret != 0 )
1025 goto err;
1026 break;
1027
1028 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1029 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +00001030 frame->subject_alt_raw.p = p;
1031 frame->subject_alt_raw.len = end_ext_octet - p;
1032
1033 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1034 MBEDTLS_ASN1_TAG_CLASS_MASK,
1035 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
1036 MBEDTLS_ASN1_TAG_VALUE_MASK,
1037 2 /* SubjectAlt DNS */,
1038 NULL, NULL );
1039 if( ret != 0 )
1040 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 break;
1042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001045 frame->ext_key_usage_raw.p = p;
1046 frame->ext_key_usage_raw.len = end_ext_octet - p;
1047 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001048 {
Hanno Becker21f55672019-02-15 15:27:59 +00001049 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1050 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001051 }
Hanno Becker21f55672019-02-15 15:27:59 +00001052
1053 /* Check structural sanity of extension. */
1054 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1055 0xFF, MBEDTLS_ASN1_OID,
1056 0, 0, NULL, NULL );
1057 if( ret != 0 )
1058 goto err;
1059
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060 break;
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001064 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001065 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001066 if( ret != 0 )
1067 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 break;
1069
1070 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001071 /*
1072 * If this is a non-critical extension, which the oid layer
1073 * supports, but there isn't an X.509 parser for it,
1074 * skip the extension.
1075 */
1076#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1077 if( is_critical )
1078 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1079#endif
1080 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081 }
1082
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001084
1085err:
1086 return( ret );
1087}
1088
Hanno Becker21f55672019-02-15 15:27:59 +00001089static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001090{
1091 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001092 unsigned char *p = frame->v3_ext.p;
1093 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001094
Hanno Becker21f55672019-02-15 15:27:59 +00001095 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001096 return( 0 );
1097
Hanno Becker21f55672019-02-15 15:27:59 +00001098 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001099 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001100 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001101
1102 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1103 return( ret );
1104 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1105 return( ret );
1106
1107 if( ret != 0 )
1108 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1109
1110 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111}
1112
Hanno Becker21f55672019-02-15 15:27:59 +00001113static int x509_crt_parse_frame( unsigned char *start,
1114 unsigned char *end,
1115 mbedtls_x509_crt_frame *frame )
1116{
1117 int ret;
1118 unsigned char *p;
1119 size_t len;
1120
1121 mbedtls_x509_buf tmp;
1122 unsigned char *tbs_start;
1123
1124 mbedtls_x509_buf outer_sig_alg;
1125 size_t inner_sig_alg_len;
1126 unsigned char *inner_sig_alg_start;
1127
1128 memset( frame, 0, sizeof( *frame ) );
1129
1130 /*
1131 * Certificate ::= SEQUENCE {
1132 * tbsCertificate TBSCertificate,
1133 * signatureAlgorithm AlgorithmIdentifier,
1134 * signatureValue BIT STRING
1135 * }
1136 *
1137 */
1138 p = start;
1139
1140 frame->raw.p = p;
1141 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1142 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1143 {
1144 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1145 }
1146
1147 /* NOTE: We are currently not checking that the `Certificate`
1148 * structure spans the entire buffer. */
1149 end = p + len;
1150 frame->raw.len = end - frame->raw.p;
1151
1152 /*
1153 * TBSCertificate ::= SEQUENCE { ...
1154 */
1155 frame->tbs.p = p;
1156 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1157 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1158 {
1159 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1160 }
1161 tbs_start = p;
1162
1163 /* Breadth-first parsing: Jump over TBS for now. */
1164 p += len;
1165 frame->tbs.len = p - frame->tbs.p;
1166
1167 /*
1168 * AlgorithmIdentifier ::= SEQUENCE { ...
1169 */
1170 outer_sig_alg.p = p;
1171 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1172 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1173 {
1174 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1175 }
1176 p += len;
1177 outer_sig_alg.len = p - outer_sig_alg.p;
1178
1179 /*
1180 * signatureValue BIT STRING
1181 */
1182 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1183 if( ret != 0 )
1184 return( ret );
1185 frame->sig.p = tmp.p;
1186 frame->sig.len = tmp.len;
1187
1188 /* Check that we consumed the entire `Certificate` structure. */
1189 if( p != end )
1190 {
1191 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1192 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1193 }
1194
1195 /* Parse TBSCertificate structure
1196 *
1197 * TBSCertificate ::= SEQUENCE {
1198 * version [0] EXPLICIT Version DEFAULT v1,
1199 * serialNumber CertificateSerialNumber,
1200 * signature AlgorithmIdentifier,
1201 * issuer Name,
1202 * validity Validity,
1203 * subject Name,
1204 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1205 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1206 * -- If present, version MUST be v2 or v3
1207 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1208 * -- If present, version MUST be v2 or v3
1209 * extensions [3] EXPLICIT Extensions OPTIONAL
1210 * -- If present, version MUST be v3
1211 * }
1212 */
1213 end = frame->tbs.p + frame->tbs.len;
1214 p = tbs_start;
1215
1216 /*
1217 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1218 */
1219 {
1220 int version;
1221 ret = x509_get_version( &p, end, &version );
1222 if( ret != 0 )
1223 return( ret );
1224
1225 if( version < 0 || version > 2 )
1226 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1227
1228 frame->version = version + 1;
1229 }
1230
1231 /*
1232 * CertificateSerialNumber ::= INTEGER
1233 */
1234 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1235 if( ret != 0 )
1236 return( ret );
1237
1238 frame->serial.p = tmp.p;
1239 frame->serial.len = tmp.len;
1240
1241 /*
1242 * signature AlgorithmIdentifier
1243 */
1244 inner_sig_alg_start = p;
1245 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1246 &frame->sig_pk, NULL );
1247 if( ret != 0 )
1248 return( ret );
1249 inner_sig_alg_len = p - inner_sig_alg_start;
1250
1251 frame->sig_alg.p = inner_sig_alg_start;
1252 frame->sig_alg.len = inner_sig_alg_len;
1253
1254 /* Consistency check:
1255 * Inner and outer AlgorithmIdentifier structures must coincide:
1256 *
1257 * Quoting RFC 5280, Section 4.1.1.2:
1258 * This field MUST contain the same algorithm identifier as the
1259 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1260 */
1261 if( outer_sig_alg.len != inner_sig_alg_len ||
1262 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1263 {
1264 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1265 }
1266
1267 /*
1268 * issuer Name
1269 *
1270 * Name ::= CHOICE { -- only one possibility for now --
1271 * rdnSequence RDNSequence }
1272 *
1273 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1274 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001275 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001276
1277 ret = mbedtls_asn1_get_tag( &p, end, &len,
1278 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1279 if( ret != 0 )
1280 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001281 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001282 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001283
1284 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1285 &frame->issuer_raw,
1286 NULL, NULL );
1287 if( ret != 0 )
1288 return( ret );
1289
Hanno Becker21f55672019-02-15 15:27:59 +00001290 /*
1291 * Validity ::= SEQUENCE { ...
1292 */
1293 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1294 if( ret != 0 )
1295 return( ret );
1296
1297 /*
1298 * subject Name
1299 *
1300 * Name ::= CHOICE { -- only one possibility for now --
1301 * rdnSequence RDNSequence }
1302 *
1303 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1304 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001305 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001306
1307 ret = mbedtls_asn1_get_tag( &p, end, &len,
1308 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1309 if( ret != 0 )
1310 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001311 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001312 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001313
1314 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1315 &frame->subject_raw,
1316 NULL, NULL );
1317 if( ret != 0 )
1318 return( ret );
1319
Hanno Becker21f55672019-02-15 15:27:59 +00001320 /*
1321 * SubjectPublicKeyInfo
1322 */
1323 frame->pubkey_raw.p = p;
1324 ret = mbedtls_asn1_get_tag( &p, end, &len,
1325 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1326 if( ret != 0 )
1327 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1328 p += len;
1329 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1330
Hanno Becker97aa4362019-06-08 07:38:20 +01001331 if( frame->version != 1 )
Hanno Becker21f55672019-02-15 15:27:59 +00001332 {
Hanno Beckerfd64f142019-06-07 11:47:12 +01001333 /*
1334 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1335 * -- If present, version shall be v2 or v3
1336 */
Hanno Beckere9084122019-06-07 12:04:39 +01001337 ret = x509_get_uid( &p, end, &frame->issuer_id, 1 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001338 if( ret != 0 )
1339 return( ret );
1340
Hanno Beckerfd64f142019-06-07 11:47:12 +01001341 /*
1342 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1343 * -- If present, version shall be v2 or v3
1344 */
Hanno Beckere9084122019-06-07 12:04:39 +01001345 ret = x509_get_uid( &p, end, &frame->subject_id, 2 /* implicit tag */ );
Hanno Becker21f55672019-02-15 15:27:59 +00001346 if( ret != 0 )
1347 return( ret );
Hanno Becker21f55672019-02-15 15:27:59 +00001348 }
1349
1350 /*
1351 * extensions [3] EXPLICIT Extensions OPTIONAL
1352 * -- If present, version shall be v3
1353 */
1354#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1355 if( frame->version == 3 )
1356#endif
1357 {
1358 if( p != end )
1359 {
1360 ret = mbedtls_asn1_get_tag( &p, end, &len,
1361 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1362 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1363 if( len == 0 )
1364 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1365 if( ret != 0 )
1366 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1367
1368 frame->v3_ext.p = p;
1369 frame->v3_ext.len = len;
1370
1371 p += len;
1372 }
1373
1374 ret = x509_crt_frame_parse_ext( frame );
1375 if( ret != 0 )
1376 return( ret );
1377 }
1378
1379 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1380 if( p != end )
1381 {
1382 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1383 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1384 }
1385
1386 return( 0 );
1387}
1388
Hanno Becker12506232019-05-13 13:53:21 +01001389static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001390 mbedtls_x509_name *subject )
1391{
Hanno Becker1e11f212019-03-04 14:43:43 +00001392 return( mbedtls_x509_get_name( frame->subject_raw.p,
1393 frame->subject_raw.len,
1394 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001395}
1396
Hanno Becker12506232019-05-13 13:53:21 +01001397static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001398 mbedtls_x509_name *issuer )
1399{
Hanno Becker1e11f212019-03-04 14:43:43 +00001400 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1401 frame->issuer_raw.len,
1402 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001403}
1404
Hanno Becker12506232019-05-13 13:53:21 +01001405static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001406 mbedtls_x509_sequence *subject_alt )
1407{
1408 int ret;
1409 unsigned char *p = frame->subject_alt_raw.p;
1410 unsigned char *end = p + frame->subject_alt_raw.len;
1411
1412 memset( subject_alt, 0, sizeof( *subject_alt ) );
1413
1414 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1415 return( 0 );
1416
1417 ret = x509_get_subject_alt_name( p, end, subject_alt );
1418 if( ret != 0 )
1419 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1420 return( ret );
1421}
1422
Hanno Becker12506232019-05-13 13:53:21 +01001423static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame const *frame,
Hanno Becker21f55672019-02-15 15:27:59 +00001424 mbedtls_x509_sequence *ext_key_usage )
1425{
1426 int ret;
1427 unsigned char *p = frame->ext_key_usage_raw.p;
1428 unsigned char *end = p + frame->ext_key_usage_raw.len;
1429
1430 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1431
1432 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1433 return( 0 );
1434
1435 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1436 if( ret != 0 )
1437 {
1438 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1439 return( ret );
1440 }
1441
1442 return( 0 );
1443}
1444
Hanno Becker180f7bf2019-02-28 13:23:38 +00001445#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001446static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1447 mbedtls_pk_context *pk )
1448{
1449 unsigned char *p = frame->pubkey_raw.p;
1450 unsigned char *end = p + frame->pubkey_raw.len;
1451 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1452}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001453#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001454
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455/*
1456 * Parse and fill a single X.509 certificate in DER format
1457 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001458static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1459 const unsigned char *buf,
1460 size_t buflen,
1461 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462{
1463 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001464 mbedtls_x509_crt_frame *frame;
1465 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001466
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469
Hanno Becker21f55672019-02-15 15:27:59 +00001470 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471 {
Hanno Becker21f55672019-02-15 15:27:59 +00001472 crt->raw.p = (unsigned char*) buf;
1473 crt->raw.len = buflen;
1474 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001475 }
1476 else
1477 {
Hanno Becker7b8e11e2019-05-03 12:37:12 +01001478 /* Call mbedtls_calloc with buflen + 1 in order to avoid potential
1479 * return of NULL in case of length 0 certificates, which we want
1480 * to cleanly fail with MBEDTLS_ERR_X509_INVALID_FORMAT in the
1481 * core parsing routine, but not here. */
1482 crt->raw.p = mbedtls_calloc( 1, buflen + 1 );
Hanno Becker21f55672019-02-15 15:27:59 +00001483 if( crt->raw.p == NULL )
1484 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1485 crt->raw.len = buflen;
1486 memcpy( crt->raw.p, buf, buflen );
1487
1488 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001489 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001490
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001491 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1492 if( cache == NULL )
1493 {
1494 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1495 goto exit;
1496 }
1497 crt->cache = cache;
1498 x509_crt_cache_init( cache );
1499
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001500#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1501
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001502 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001503 if( ret != 0 )
1504 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505
Hanno Becker76428352019-03-05 15:29:23 +00001506 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001507
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001508#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001510 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1511 if( frame == NULL )
1512 {
1513 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1514 goto exit;
1515 }
1516 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001517
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001518 ret = x509_crt_parse_frame( crt->raw.p,
1519 crt->raw.p + crt->raw.len,
1520 frame );
1521 if( ret != 0 )
1522 goto exit;
1523
Hanno Becker21f55672019-02-15 15:27:59 +00001524 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1525 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001526 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1527 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1528 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1529 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
1530 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1531 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
1532 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1533 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1534 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001535 crt->valid_from = frame->valid_from;
1536 crt->valid_to = frame->valid_to;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001537 crt->version = frame->version;
1538 crt->ca_istrue = frame->ca_istrue;
1539 crt->max_pathlen = frame->max_pathlen;
1540 crt->ext_types = frame->ext_types;
1541 crt->key_usage = frame->key_usage;
1542 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543
1544 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001545 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548 {
Hanno Becker21f55672019-02-15 15:27:59 +00001549 /* sig_oid: Previously, needed for convenience in
1550 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001551 unsigned char *tmp = frame->sig_alg.p;
1552 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001553 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554
Hanno Becker21f55672019-02-15 15:27:59 +00001555 ret = mbedtls_x509_get_alg( &tmp, end,
1556 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557 if( ret != 0 )
1558 {
Hanno Becker21f55672019-02-15 15:27:59 +00001559 /* This should never happen, because we check
1560 * the sanity of the AlgorithmIdentifier structure
1561 * during frame parsing. */
1562 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1563 goto exit;
1564 }
1565 crt->sig_oid = sig_oid;
1566
1567 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001568 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001569 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1570 &crt->sig_md, &crt->sig_pk,
1571 &crt->sig_opts );
1572 if( ret != 0 )
1573 {
1574 /* Again, this should never happen. */
1575 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1576 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577 }
1578 }
1579
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001580 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001581 if( ret != 0 )
1582 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001584 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001585 if( ret != 0 )
1586 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001588 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001589 if( ret != 0 )
1590 goto exit;
1591
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001592 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001593 if( ret != 0 )
1594 goto exit;
1595
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001596 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1597 if( ret != 0 )
1598 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001599#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001600
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001601 /* Currently, we accept DER encoded CRTs with trailing garbage
1602 * and promise to not account for the garbage in the `raw` field.
1603 *
1604 * Note that this means that `crt->raw.len` is not necessarily the
1605 * full size of the heap buffer allocated at `crt->raw.p` in case
1606 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1607 * need the size, and the garbage data doesn't need zeroization. */
1608 crt->raw.len = frame->raw.len;
1609
1610 cache->pk_raw = frame->pubkey_raw;
1611
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001612 /* Free the frame before parsing the public key to
1613 * keep peak RAM usage low. This is slightly inefficient
1614 * because the frame will need to be parsed again on the
1615 * first usage of the CRT, but that seems acceptable.
1616 * As soon as the frame gets used multiple times, it
1617 * will be cached by default. */
1618 x509_crt_cache_clear_frame( crt->cache );
1619
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001620 /* The cache just references the PK structure from the legacy
1621 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001622 * the cache.
1623 *
1624 * We're not actually using the parsed PK context here;
1625 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001626 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001627 if( ret != 0 )
1628 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001629 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001630
1631exit:
1632 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634
Hanno Becker21f55672019-02-15 15:27:59 +00001635 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636}
1637
1638/*
1639 * Parse one X.509 certificate in DER format from a buffer and add them to a
1640 * chained list
1641 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001642static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1643 const unsigned char *buf,
1644 size_t buflen,
1645 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646{
1647 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649
1650 /*
1651 * Check for valid input
1652 */
1653 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655
Hanno Becker371e0e42019-02-25 18:08:59 +00001656 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657 {
1658 prev = crt;
1659 crt = crt->next;
1660 }
1661
1662 /*
1663 * Add new certificate on the end of the chain if needed.
1664 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001665 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001666 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001667 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668
1669 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001670 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671
1672 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675 }
1676
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001677 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 {
1679 if( prev )
1680 prev->next = NULL;
1681
1682 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684
1685 return( ret );
1686 }
1687
1688 return( 0 );
1689}
1690
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001691int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1692 const unsigned char *buf,
1693 size_t buflen )
1694{
1695 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1696}
1697
1698int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1699 const unsigned char *buf,
1700 size_t buflen )
1701{
1702 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1703}
1704
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001706 * Parse one or more PEM certificates from a buffer and add them to the chained
1707 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001708 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001709int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1710 const unsigned char *buf,
1711 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712{
Janos Follath98e28a72016-05-31 14:03:54 +01001713#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001714 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001716#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717
1718 /*
1719 * Check for valid input
1720 */
1721 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723
1724 /*
1725 * Determine buffer content. Buffer contains either one DER certificate or
1726 * one or more PEM certificates.
1727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001729 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001730 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001733 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1736 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001737#else
1738 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1739#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#if defined(MBEDTLS_PEM_PARSE_C)
1742 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743 {
1744 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001746
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001747 /* 1 rather than 0 since the terminating NULL byte is counted in */
1748 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749 {
1750 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001753 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755 "-----BEGIN CERTIFICATE-----",
1756 "-----END CERTIFICATE-----",
1757 buf, NULL, 0, &use_len );
1758
1759 if( ret == 0 )
1760 {
1761 /*
1762 * Was PEM encoded
1763 */
1764 buflen -= use_len;
1765 buf += use_len;
1766 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 {
1769 return( ret );
1770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001774
1775 /*
1776 * PEM header and footer were found
1777 */
1778 buflen -= use_len;
1779 buf += use_len;
1780
1781 if( first_error == 0 )
1782 first_error = ret;
1783
Paul Bakker5a5fa922014-09-26 14:53:04 +02001784 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785 continue;
1786 }
1787 else
1788 break;
1789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793
1794 if( ret != 0 )
1795 {
1796 /*
1797 * Quit parsing on a memory error
1798 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001799 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001800 return( ret );
1801
1802 if( first_error == 0 )
1803 first_error = ret;
1804
1805 total_failed++;
1806 continue;
1807 }
1808
1809 success = 1;
1810 }
1811 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001812
1813 if( success )
1814 return( total_failed );
1815 else if( first_error )
1816 return( first_error );
1817 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001819#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820}
1821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001823/*
1824 * Load one or more certificates and add them to the chained list
1825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827{
1828 int ret;
1829 size_t n;
1830 unsigned char *buf;
1831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833 return( ret );
1834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001836
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001837 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001839
1840 return( ret );
1841}
1842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001844{
1845 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001846#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001847 int w_ret;
1848 WCHAR szDir[MAX_PATH];
1849 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001850 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001851 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001852
Paul Bakker9af723c2014-05-01 13:03:14 +02001853 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001854 HANDLE hFind;
1855
1856 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001858
Paul Bakker9af723c2014-05-01 13:03:14 +02001859 memset( szDir, 0, sizeof(szDir) );
1860 memset( filename, 0, MAX_PATH );
1861 memcpy( filename, path, len );
1862 filename[len++] = '\\';
1863 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001864 filename[len++] = '*';
1865
Simon B3c6b18d2016-11-03 01:11:37 +00001866 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001867 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001868 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001870
1871 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001872 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001874
1875 len = MAX_PATH - len;
1876 do
1877 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001878 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001879
1880 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1881 continue;
1882
Paul Bakker9af723c2014-05-01 13:03:14 +02001883 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001884 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001885 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001886 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001887 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001888 {
1889 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1890 goto cleanup;
1891 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001894 if( w_ret < 0 )
1895 ret++;
1896 else
1897 ret += w_ret;
1898 }
1899 while( FindNextFileW( hFind, &file_data ) != 0 );
1900
Paul Bakker66d5d072014-06-17 16:39:18 +02001901 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001903
Ron Eldor36d90422017-01-09 15:09:16 +02001904cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001905 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001906#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001907 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001908 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001910 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001911 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001912 DIR *dir = opendir( path );
1913
Paul Bakker66d5d072014-06-17 16:39:18 +02001914 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001916
Ron Eldor63140682017-01-09 19:27:59 +02001917#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001918 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001919 {
1920 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001921 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001922 }
Ron Eldor63140682017-01-09 19:27:59 +02001923#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001924
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001925 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001926 {
Andres AGf9113192016-09-02 14:06:04 +01001927 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1928 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929
Andres AGf9113192016-09-02 14:06:04 +01001930 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931 {
Andres AGf9113192016-09-02 14:06:04 +01001932 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1933 goto cleanup;
1934 }
1935 else if( stat( entry_name, &sb ) == -1 )
1936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001938 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001939 }
1940
1941 if( !S_ISREG( sb.st_mode ) )
1942 continue;
1943
1944 // Ignore parse errors
1945 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001947 if( t_ret < 0 )
1948 ret++;
1949 else
1950 ret += t_ret;
1951 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001952
1953cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001954 closedir( dir );
1955
Ron Eldor63140682017-01-09 19:27:59 +02001956#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001957 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001959#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001960
Paul Bakkerbe089b02013-10-14 15:51:50 +02001961#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001962
1963 return( ret );
1964}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001966
Hanno Becker08d34122019-06-25 09:42:57 +01001967typedef struct mbedtls_x509_crt_sig_info
1968{
1969 mbedtls_md_type_t sig_md;
1970 mbedtls_pk_type_t sig_pk;
1971 void *sig_opts;
1972 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
1973 size_t crt_hash_len;
1974 mbedtls_x509_buf_raw sig;
1975 mbedtls_x509_buf_raw issuer_raw;
1976} mbedtls_x509_crt_sig_info;
1977
1978static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
1979{
1980#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
1981 mbedtls_free( info->sig_opts );
1982#else
1983 ((void) info);
1984#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
1985}
1986
1987static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
1988 mbedtls_x509_crt_sig_info *info )
1989{
1990 const mbedtls_md_info_t *md_info;
1991
1992 md_info = mbedtls_md_info_from_type( frame->sig_md );
1993 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
1994 info->crt_hash ) != 0 )
1995 {
1996 /* Note: this can't happen except after an internal error */
1997 return( -1 );
1998 }
1999
2000 info->crt_hash_len = mbedtls_md_get_size( md_info );
2001
2002 /* Make sure that this function leaves the target structure
2003 * ready to be freed, regardless of success of failure. */
2004 info->sig_opts = NULL;
2005
2006#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2007 {
2008 int ret;
2009 unsigned char *alg_start = frame->sig_alg.p;
2010 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2011
2012 /* Get signature options -- currently only
2013 * necessary for RSASSA-PSS. */
2014 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2015 &info->sig_pk, &info->sig_opts );
2016 if( ret != 0 )
2017 {
2018 /* Note: this can't happen except after an internal error */
2019 return( -1 );
2020 }
2021 }
2022#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2023 info->sig_md = frame->sig_md;
2024 info->sig_pk = frame->sig_pk;
2025#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2026
2027 info->issuer_raw = frame->issuer_raw;
2028 info->sig = frame->sig;
2029 return( 0 );
2030}
2031
Hanno Becker02a21932019-06-10 15:08:43 +01002032#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002033static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002035{
2036 size_t i;
2037 size_t n = *size;
2038 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002040 const char *sep = "";
2041 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002042
2043 while( cur != NULL )
2044 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002045 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002046 {
2047 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002048 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002049 }
2050
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002051 n -= cur->buf.len + sep_len;
2052 for( i = 0; i < sep_len; i++ )
2053 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002054 for( i = 0; i < cur->buf.len; i++ )
2055 *p++ = cur->buf.p[i];
2056
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002057 sep = ", ";
2058 sep_len = 2;
2059
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002060 cur = cur->next;
2061 }
2062
2063 *p = '\0';
2064
2065 *size = n;
2066 *buf = p;
2067
2068 return( 0 );
2069}
2070
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002071#define PRINT_ITEM(i) \
2072 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002074 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002075 sep = ", "; \
2076 }
2077
2078#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002079 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002080 PRINT_ITEM( name );
2081
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002082static int x509_info_cert_type( char **buf, size_t *size,
2083 unsigned char ns_cert_type )
2084{
2085 int ret;
2086 size_t n = *size;
2087 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002088 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002091 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
2093 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
2094 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002095 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2096 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2097 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002098
2099 *size = n;
2100 *buf = p;
2101
2102 return( 0 );
2103}
2104
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002105#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002106 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002107 PRINT_ITEM( name );
2108
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002109static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002110 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002111{
2112 int ret;
2113 size_t n = *size;
2114 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002115 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2118 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002119 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2120 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2121 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2123 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002124 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2125 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002126
2127 *size = n;
2128 *buf = p;
2129
2130 return( 0 );
2131}
2132
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002133static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002135{
2136 int ret;
2137 const char *desc;
2138 size_t n = *size;
2139 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002141 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002142
2143 while( cur != NULL )
2144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002146 desc = "???";
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002149 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002150
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002151 sep = ", ";
2152
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002153 cur = cur->next;
2154 }
2155
2156 *size = n;
2157 *buf = p;
2158
2159 return( 0 );
2160}
2161
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002162/*
2163 * Return an informational string about the certificate.
2164 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002165#define BEFORE_COLON 18
2166#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002168 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002169{
2170 int ret;
2171 size_t n;
2172 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002173 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002174 mbedtls_x509_crt_frame frame;
2175 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002176
Hanno Becker5226c532019-02-27 17:38:40 +00002177 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2178 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002179 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002180
2181 p = buf;
2182 n = size;
2183
Hanno Becker4f869ed2019-02-24 16:47:57 +00002184 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002185 mbedtls_pk_init( &pk );
2186
2187 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002188 {
2189 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002190 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Janos Follath98e28a72016-05-31 14:03:54 +01002191
2192 return( (int) ( size - n ) );
2193 }
2194
Hanno Becker5226c532019-02-27 17:38:40 +00002195 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002196 if( ret != 0 )
2197 {
2198 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2199 goto cleanup;
2200 }
2201
Hanno Becker5226c532019-02-27 17:38:40 +00002202 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002203 if( ret != 0 )
2204 {
2205 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2206 goto cleanup;
2207 }
2208
Hanno Becker5226c532019-02-27 17:38:40 +00002209 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002210 if( ret != 0 )
2211 {
2212 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2213 goto cleanup;
2214 }
2215
Hanno Becker5226c532019-02-27 17:38:40 +00002216 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002217 if( ret != 0 )
2218 {
2219 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2220 goto cleanup;
2221 }
2222
Hanno Becker5226c532019-02-27 17:38:40 +00002223 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002224 if( ret != 0 )
2225 {
2226 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2227 goto cleanup;
2228 }
2229
Hanno Becker5226c532019-02-27 17:38:40 +00002230 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2231 if( ret != 0 )
2232 {
2233 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2234 goto cleanup;
2235 }
2236
2237 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002238 if( ret != 0 )
2239 {
2240 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2241 goto cleanup;
2242 }
2243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002245 prefix, frame.version );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002246 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002247
Hanno Becker4f869ed2019-02-24 16:47:57 +00002248 {
2249 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002250 serial.p = frame.serial.p;
2251 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002252 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2253 prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002254 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002255 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002256 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002257 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002260 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002261 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002262 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002265 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002266 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002267 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002270 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002271 frame.valid_from.year, frame.valid_from.mon,
2272 frame.valid_from.day, frame.valid_from.hour,
2273 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002274 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002277 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002278 frame.valid_to.year, frame.valid_to.mon,
2279 frame.valid_to.day, frame.valid_to.hour,
2280 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002281 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002284 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002285
Hanno Becker83cd8672019-02-21 17:13:46 +00002286 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2287 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002288 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002289
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002290 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002292 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002293 {
2294 return( ret );
2295 }
2296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002298 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002299 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002300
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002301 /*
2302 * Optional extensions
2303 */
2304
Hanno Becker5226c532019-02-27 17:38:40 +00002305 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002308 frame.ca_istrue ? "true" : "false" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002309 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002310
Hanno Becker5226c532019-02-27 17:38:40 +00002311 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002312 {
Hanno Becker5226c532019-02-27 17:38:40 +00002313 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002314 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002315 }
2316 }
2317
Hanno Becker5226c532019-02-27 17:38:40 +00002318 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002321 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002322
2323 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002324 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002325 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002326 }
2327
Hanno Becker5226c532019-02-27 17:38:40 +00002328 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002331 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002332
Hanno Becker5226c532019-02-27 17:38:40 +00002333 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002334 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002335 }
2336
Hanno Becker5226c532019-02-27 17:38:40 +00002337 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002340 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002341
Hanno Becker5226c532019-02-27 17:38:40 +00002342 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002343 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002344 }
2345
Hanno Becker5226c532019-02-27 17:38:40 +00002346 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002349 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002350
2351 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002352 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002353 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002354 }
2355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002357 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002358
Hanno Becker4f869ed2019-02-24 16:47:57 +00002359 ret = (int) ( size - n );
2360
2361cleanup:
2362
Hanno Becker4f869ed2019-02-24 16:47:57 +00002363 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002364 mbedtls_pk_free( &pk );
2365 mbedtls_x509_name_free( issuer );
2366 mbedtls_x509_name_free( subject );
2367 mbedtls_x509_sequence_free( ext_key_usage );
2368 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002369
2370 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371}
2372
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002373struct x509_crt_verify_string {
2374 int code;
2375 const char *string;
2376};
2377
2378static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002379 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002380 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2381 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2382 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2383 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2384 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002385 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2386 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2387 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002388 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002389 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2390 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2391 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2392 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002393 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2394 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2395 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2396 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2397 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2398 { 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 +01002399 { 0, NULL }
2400};
2401
2402int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002403 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002404{
2405 int ret;
2406 const struct x509_crt_verify_string *cur;
2407 char *p = buf;
2408 size_t n = size;
2409
2410 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2411 {
2412 if( ( flags & cur->code ) == 0 )
2413 continue;
2414
2415 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002416 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002417 flags ^= cur->code;
2418 }
2419
2420 if( flags != 0 )
2421 {
2422 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2423 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002424 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002425 }
2426
2427 return( (int) ( size - n ) );
2428}
Hanno Becker02a21932019-06-10 15:08:43 +01002429#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002432static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2433 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002434{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002435 unsigned int usage_must, usage_may;
2436 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2437 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2438
2439 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2440 return( 0 );
2441
2442 usage_must = usage & ~may_mask;
2443
2444 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2445 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2446
2447 usage_may = usage & may_mask;
2448
2449 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002451
2452 return( 0 );
2453}
Hanno Becker45eedf12019-02-25 13:55:33 +00002454
2455int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2456 unsigned int usage )
2457{
2458 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002459 mbedtls_x509_crt_frame const *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002460 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002461 if( ret != 0 )
2462 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2463
2464 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002465 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002466
2467 return( ret );
2468}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002469#endif
2470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002472typedef struct
2473{
2474 const char *oid;
2475 size_t oid_len;
2476} x509_crt_check_ext_key_usage_cb_ctx_t;
2477
2478static int x509_crt_check_ext_key_usage_cb( void *ctx,
2479 int tag,
2480 unsigned char *data,
2481 size_t data_len )
2482{
2483 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2484 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2485 ((void) tag);
2486
2487 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2488 data, data_len ) == 0 )
2489 {
2490 return( 1 );
2491 }
2492
2493 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2494 data_len ) == 0 )
2495 {
2496 return( 1 );
2497 }
2498
2499 return( 0 );
2500}
2501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002503 const char *usage_oid,
2504 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002505{
Hanno Beckere1956af2019-02-21 14:28:12 +00002506 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002507 mbedtls_x509_crt_frame const *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002508 unsigned ext_types;
2509 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002510 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002511
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002512 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002513 if( ret != 0 )
2514 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2515
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002516 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002517 ext_types = frame->ext_types;
2518 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2519 {
2520 p = frame->ext_key_usage_raw.p;
2521 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002522
Hanno Beckere9718b42019-02-25 18:11:42 +00002523 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2524 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2525 x509_crt_check_ext_key_usage_cb,
2526 &cb_ctx );
2527 if( ret == 1 )
2528 ret = 0;
2529 else
2530 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2531 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002532
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002533 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002534 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002539/*
2540 * Return 1 if the certificate is revoked, or 0 otherwise.
2541 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002542static int x509_serial_is_revoked( unsigned char const *serial,
2543 size_t serial_len,
2544 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002547
2548 while( cur != NULL && cur->serial.len != 0 )
2549 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002550 if( serial_len == cur->serial.len &&
2551 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002552 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002553 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002554 return( 1 );
2555 }
2556
2557 cur = cur->next;
2558 }
2559
2560 return( 0 );
2561}
2562
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002563int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2564 const mbedtls_x509_crl *crl )
2565{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002566 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01002567 mbedtls_x509_crt_frame const *frame;
Hanno Becker79ae5b62019-02-25 18:12:00 +00002568
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002569 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002570 if( ret != 0 )
2571 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2572
2573 ret = x509_serial_is_revoked( frame->serial.p,
2574 frame->serial.len,
2575 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002576 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002577 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002578}
2579
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002580/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002581 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002582 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002583 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002584static int x509_crt_verifycrl( unsigned char *crt_serial,
2585 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002586 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002587 mbedtls_x509_crl *crl_list,
2588 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002589{
Hanno Beckerbb266132019-02-25 18:12:46 +00002590 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002591 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2593 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002594 mbedtls_x509_buf_raw ca_subject;
2595 mbedtls_pk_context *pk;
2596 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002597
Hanno Beckerbb266132019-02-25 18:12:46 +00002598 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002599 return( flags );
2600
Hanno Beckerbb266132019-02-25 18:12:46 +00002601 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002602 mbedtls_x509_crt_frame const *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002603 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002604 if( ret != 0 )
2605 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2606
2607 ca_subject = ca->subject_raw;
2608
2609 can_sign = 0;
2610 if( x509_crt_check_key_usage_frame( ca,
2611 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2612 {
2613 can_sign = 1;
2614 }
2615
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002616 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002617 }
2618
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002619 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002620 if( ret != 0 )
2621 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2622
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002623 while( crl_list != NULL )
2624 {
2625 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002626 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002627 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002628 {
2629 crl_list = crl_list->next;
2630 continue;
2631 }
2632
2633 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002634 * Check if the CA is configured to sign CRLs
2635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002637 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002640 break;
2641 }
2642#endif
2643
2644 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002645 * Check if CRL is correctly signed by the trusted CA
2646 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002647 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2648 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2649
2650 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2651 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002654 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002655 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002656 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002658 break;
2659 }
2660
Hanno Beckerbb266132019-02-25 18:12:46 +00002661 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002662 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002663
Hanno Beckerbb266132019-02-25 18:12:46 +00002664 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002666 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002669 break;
2670 }
2671
2672 /*
2673 * Check for validity of CRL (Do not drop out)
2674 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002675 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002677
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002678 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002679 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002680
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002681 /*
2682 * Check if certificate is revoked
2683 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002684 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2685 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002688 break;
2689 }
2690
2691 crl_list = crl_list->next;
2692 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002693
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002694 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002695 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002698
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002699/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002700 * Check the signature of a certificate by its parent
2701 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002702static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002703 mbedtls_x509_crt *parent,
2704 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002705{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002706 int ret;
2707 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002708
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002709 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002710 if( ret != 0 )
2711 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2712
2713 /* Skip expensive computation on obvious mismatch */
2714 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002715 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002716 ret = -1;
2717 goto exit;
2718 }
2719
2720#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2721 ((void) rs_ctx);
2722#else
2723 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2724 {
2725 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002726 sig_info->sig_md,
2727 sig_info->crt_hash, sig_info->crt_hash_len,
2728 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002729 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002730 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002731 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002732#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002733 {
2734 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2735 sig_info->sig_opts,
2736 pk,
2737 sig_info->sig_md,
2738 sig_info->crt_hash, sig_info->crt_hash_len,
2739 sig_info->sig.p, sig_info->sig.len );
2740 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002741
Hanno Beckere449e2d2019-02-25 14:45:31 +00002742exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002743 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002744 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002745}
2746
2747/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002748 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2749 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002750 *
2751 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002752 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002753static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2754 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002755 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002756{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002757 int need_ca_bit;
2758
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002759 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002760 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2761 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002762 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002763 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002764 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002765 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002766
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002767 /* Parent must have the basicConstraints CA bit set as a general rule */
2768 need_ca_bit = 1;
2769
2770 /* Exception: v1/v2 certificates that are locally trusted. */
2771 if( top && parent->version < 3 )
2772 need_ca_bit = 0;
2773
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002774 if( need_ca_bit && ! parent->ca_istrue )
2775 return( -1 );
2776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002778 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002779 x509_crt_check_key_usage_frame( parent,
2780 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002781 {
2782 return( -1 );
2783 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002784#endif
2785
2786 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002787}
2788
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002789/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002790 * Find a suitable parent for child in candidates, or return NULL.
2791 *
2792 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002793 * 1. subject name matches child's issuer
2794 * 2. if necessary, the CA bit is set and key usage allows signing certs
2795 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002796 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002797 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002798 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002799 * If there's a suitable candidate which is also time-valid, return the first
2800 * such. Otherwise, return the first suitable candidate (or NULL if there is
2801 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002802 *
2803 * The rationale for this rule is that someone could have a list of trusted
2804 * roots with two versions on the same root with different validity periods.
2805 * (At least one user reported having such a list and wanted it to just work.)
2806 * The reason we don't just require time-validity is that generally there is
2807 * only one version, and if it's expired we want the flags to state that
2808 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002809 *
2810 * The rationale for rule 3 (signature for trusted roots) is that users might
2811 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002812 * way we select the correct one is by checking the signature (as we don't
2813 * rely on key identifier extensions). (This is one way users might choose to
2814 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002815 *
2816 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002817 * - [in] child: certificate for which we're looking for a parent
2818 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002819 * - [out] r_parent: parent found (or NULL)
2820 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002821 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2822 * of the chain, 0 otherwise
2823 * - [in] path_cnt: number of intermediates seen so far
2824 * - [in] self_cnt: number of self-signed intermediates seen so far
2825 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002826 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002827 *
2828 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002829 * - 0 on success
2830 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002831 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002832static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002833 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002834 mbedtls_x509_crt *candidates,
2835 mbedtls_x509_crt **r_parent,
2836 int *r_signature_is_good,
2837 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002838 unsigned path_cnt,
2839 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002840 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002841{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002842 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002843 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002844 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002845
2846#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002847 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002848 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2849 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002850 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002851 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002852 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002853 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002854
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002855 /* clear saved state */
2856 rs_ctx->parent = NULL;
2857 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002858 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002859
2860 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002861 goto check_signature;
2862 }
2863#endif
2864
2865 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002866 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002867
Hanno Becker43bf9002019-02-25 14:46:49 +00002868 for( parent_crt = candidates; parent_crt != NULL;
2869 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002870 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002871 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002872
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002873#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2874check_signature:
2875#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002876
2877 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002878 {
Hanno Becker5f268b32019-05-20 16:26:34 +01002879 mbedtls_x509_crt_frame const *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002880
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002881 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002882 if( ret != 0 )
2883 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002884
Hanno Becker040c5642019-06-10 11:14:24 +01002885 if( !mbedtls_x509_time_is_past( &parent->valid_to ) &&
2886 !mbedtls_x509_time_is_future( &parent->valid_from ) )
Hanno Becker43bf9002019-02-25 14:46:49 +00002887 {
2888 parent_valid = 1;
2889 }
2890
2891 /* basic parenting skills (name, CA bit, key usage) */
2892 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2893 parent_match = 1;
2894
2895 /* +1 because the stored max_pathlen is 1 higher
2896 * than the actual value */
2897 if( !( parent->max_pathlen > 0 &&
2898 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2899 {
2900 path_len_ok = 1;
2901 }
2902
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002903 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002904 }
2905
2906 if( parent_match == 0 || path_len_ok == 0 )
2907 continue;
2908
2909 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002910 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002911
2912#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002913 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2914 {
2915 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002916 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002917 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002918 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002919
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002920 return( ret );
2921 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002922#else
2923 (void) ret;
2924#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002925
2926 signature_is_good = ret == 0;
2927 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002928 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002929
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002930 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002931 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002932 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002933 if( fallback_parent == NULL )
2934 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002935 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002936 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002937 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002938
2939 continue;
2940 }
2941
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002942 break;
2943 }
2944
Hanno Becker43bf9002019-02-25 14:46:49 +00002945 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002946 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002947 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002948 *r_signature_is_good = signature_is_good;
2949 }
2950 else
2951 {
2952 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002953 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002954 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002955
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002956 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002957}
2958
2959/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002960 * Find a parent in trusted CAs or the provided chain, or return NULL.
2961 *
2962 * Searches in trusted CAs first, and return the first suitable parent found
2963 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002964 *
2965 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002966 * - [in] child: certificate for which we're looking for a parent, followed
2967 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002968 * - [in] trust_ca: list of locally trusted certificates
2969 * - [out] parent: parent found (or NULL)
2970 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2971 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2972 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2973 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002974 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002975 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002976 *
2977 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002978 * - 0 on success
2979 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002980 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002981static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002982 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002983 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002984 mbedtls_x509_crt *trust_ca,
2985 mbedtls_x509_crt **parent,
2986 int *parent_is_trusted,
2987 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002988 unsigned path_cnt,
2989 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002990 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002991{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002992 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002993 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002994
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002995 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002996
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002997#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002998 /* restore then clear saved state if we have some stored */
2999 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
3000 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003001 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003002 rs_ctx->parent_is_trusted = -1;
3003 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003004#endif
3005
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003006 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00003007 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003008
Hanno Becker5299cf82019-02-25 13:50:41 +00003009 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003010 parent, signature_is_good,
3011 *parent_is_trusted,
3012 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003013
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003014#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003015 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3016 {
3017 /* save state */
3018 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003019 return( ret );
3020 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003021#else
3022 (void) ret;
3023#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003024
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003025 /* stop here if found or already in second iteration */
3026 if( *parent != NULL || *parent_is_trusted == 0 )
3027 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003028
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003029 /* prepare second iteration */
3030 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003031 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003032
3033 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01003034 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003035 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02003036 *parent_is_trusted = 0;
3037 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003038 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003039
3040 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003041}
3042
3043/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003044 * Check if an end-entity certificate is locally trusted
3045 *
3046 * Currently we require such certificates to be self-signed (actually only
3047 * check for self-issued as self-signatures are not checked)
3048 */
3049static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003050 mbedtls_x509_crt_frame const *crt,
3051 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003052{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003053 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003054
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003055 /* look for an exact match with trusted cert */
3056 for( cur = trust_ca; cur != NULL; cur = cur->next )
3057 {
3058 if( crt->raw.len == cur->raw.len &&
3059 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3060 {
3061 return( 0 );
3062 }
3063 }
3064
3065 /* too bad */
3066 return( -1 );
3067}
3068
3069/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003070 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003071 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003072 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3073 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003074 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003075 * such that every cert in the chain is a child of the next one,
3076 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003077 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003078 * Verify that chain and return it with flags for all issues found.
3079 *
3080 * Special cases:
3081 * - EE == Rj -> return a one-element list containing it
3082 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3083 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003084 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003085 * Tests for (aspects of) this function should include at least:
3086 * - trusted EE
3087 * - EE -> trusted root
3088 * - EE -> intermedate CA -> trusted root
3089 * - if relevant: EE untrusted
3090 * - if relevant: EE -> intermediate, untrusted
3091 * with the aspect under test checked at each relevant level (EE, int, root).
3092 * For some aspects longer chains are required, but usually length 2 is
3093 * enough (but length 1 is not in general).
3094 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003095 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003096 * - [in] crt: the cert list EE, C1, ..., Cn
3097 * - [in] trust_ca: the trusted list R1, ..., Rp
3098 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003099 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003100 * Only valid when return value is 0, may contain garbage otherwise!
3101 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003102 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003103 *
3104 * Return value:
3105 * - non-zero if the chain could not be fully built and examined
3106 * - 0 is the chain was successfully built and examined,
3107 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003108 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003109static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003110 mbedtls_x509_crt *crt,
3111 mbedtls_x509_crt *trust_ca,
3112 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003113 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003114 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003115 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003116{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003117 /* Don't initialize any of those variables here, so that the compiler can
3118 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003119 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003120 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003121 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003122 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003123 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003124 int parent_is_trusted;
3125 int child_is_trusted;
3126 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003127 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003128
3129#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3130 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003131 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003132 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003133 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003134 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003135 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003136
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003137 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003138 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003139 child_crt = cur->crt;
3140
3141 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003142 goto find_parent;
3143 }
3144#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003145
Hanno Becker1e0677a2019-02-25 14:58:22 +00003146 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003147 self_cnt = 0;
3148 parent_is_trusted = 0;
3149 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003150
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003151 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003152#if defined(MBEDTLS_X509_CRL_PARSE_C)
3153 mbedtls_x509_buf_raw child_serial;
3154#endif /* MBEDTLS_X509_CRL_PARSE_C */
3155 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003156
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003157 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003158 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003159 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003160 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003161 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003162
3163#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3164find_parent:
3165#endif
3166
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003167 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003168
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003169 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003170 mbedtls_x509_crt_sig_info child_sig;
3171 {
Hanno Becker5f268b32019-05-20 16:26:34 +01003172 mbedtls_x509_crt_frame const *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003173
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003174 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003175 if( ret != 0 )
3176 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3177
3178 /* Check time-validity (all certificates) */
3179 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3180 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3181 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3182 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
3183
3184 /* Stop here for trusted roots (but not for trusted EE certs) */
3185 if( child_is_trusted )
3186 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003187 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003188 return( 0 );
3189 }
3190
3191 self_issued = 0;
3192 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3193 &child->subject_raw,
3194 NULL, NULL ) == 0 )
3195 {
3196 self_issued = 1;
3197 }
3198
3199 /* Check signature algorithm: MD & PK algs */
3200 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3201 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3202
3203 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3204 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3205
3206 /* Special case: EE certs that are locally trusted */
3207 if( ver_chain->len == 1 && self_issued &&
3208 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3209 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003210 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003211 return( 0 );
3212 }
3213
3214#if defined(MBEDTLS_X509_CRL_PARSE_C)
3215 child_serial = child->serial;
3216#endif /* MBEDTLS_X509_CRL_PARSE_C */
3217
3218 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003219 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003220
Hanno Becker5299cf82019-02-25 13:50:41 +00003221 if( ret != 0 )
3222 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3223 }
3224
3225 /* Look for a parent in trusted CAs or up the chain */
3226 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003227 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003228 &parent_is_trusted, &signature_is_good,
3229 ver_chain->len - 1, self_cnt, rs_ctx );
3230
3231 x509_crt_free_sig_info( &child_sig );
3232 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003233
3234#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003235 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3236 {
3237 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003238 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003239 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003240 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003241 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003242 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003243#else
3244 (void) ret;
3245#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003246
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003247 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003248 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003249 {
3250 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003251 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003252 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003253
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003254 /* Count intermediate self-issued (not necessarily self-signed) certs.
3255 * These can occur with some strategies for key rollover, see [SIRO],
3256 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003257 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003258 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003259
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003260 /* path_cnt is 0 for the first intermediate CA,
3261 * and if parent is trusted it's not an intermediate CA */
3262 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003263 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003264 {
3265 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003266 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003267 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003268
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003269 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003270 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003271 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3272
Hanno Becker58c35642019-02-25 18:13:46 +00003273 {
3274 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003275 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003276 if( ret != 0 )
3277 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3278
3279 /* check size of signing key */
3280 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3281 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3282
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003283 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003284 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003287 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003288 *flags |= x509_crt_verifycrl( child_serial.p,
3289 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003290 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003291#else
3292 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003293#endif
3294
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003295 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003296 child_crt = parent_crt;
3297 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003298 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003299 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003300 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003301}
3302
3303/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003304 * Check for CN match
3305 */
Hanno Becker24926222019-02-21 13:10:55 +00003306static int x509_crt_check_cn( unsigned char const *buf,
3307 size_t buflen,
3308 const char *cn,
3309 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003310{
Hanno Becker24926222019-02-21 13:10:55 +00003311 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003312 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003313 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003314
3315 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003316 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003317 {
3318 return( 0 );
3319 }
3320
3321 return( -1 );
3322}
3323
Hanno Becker8b543b32019-02-21 11:50:44 +00003324/* Returns 1 on a match and 0 on a mismatch.
3325 * This is because this function is used as a callback for
3326 * mbedtls_x509_name_cmp_raw(), which continues the name
3327 * traversal as long as the callback returns 0. */
3328static int x509_crt_check_name( void *ctx,
3329 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003330 mbedtls_x509_buf *val,
3331 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003332{
3333 char const *cn = (char const*) ctx;
3334 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003335 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003336
3337 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003338 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003339 {
3340 return( 1 );
3341 }
3342
3343 return( 0 );
3344}
3345
Hanno Beckerda410822019-02-21 13:36:59 +00003346/* Returns 1 on a match and 0 on a mismatch.
3347 * This is because this function is used as a callback for
3348 * mbedtls_asn1_traverse_sequence_of(), which continues the
3349 * traversal as long as the callback returns 0. */
3350static int x509_crt_subject_alt_check_name( void *ctx,
3351 int tag,
3352 unsigned char *data,
3353 size_t data_len )
3354{
3355 char const *cn = (char const*) ctx;
3356 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003357 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003358
3359 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3360 return( 1 );
3361
3362 return( 0 );
3363}
3364
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003365/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003366 * Verify the requested CN - only call this if cn is not NULL!
3367 */
Hanno Becker082435c2019-02-25 18:14:40 +00003368static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3369 const char *cn,
3370 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003371{
Hanno Beckerda410822019-02-21 13:36:59 +00003372 int ret;
Hanno Becker5f268b32019-05-20 16:26:34 +01003373 mbedtls_x509_crt_frame const *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003374
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003375 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003376 if( ret != 0 )
3377 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3378
3379 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003380 {
Hanno Becker90b94082019-02-21 21:13:21 +00003381 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003382 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003383 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003384 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003385
Hanno Becker90b94082019-02-21 21:13:21 +00003386 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3387 MBEDTLS_ASN1_TAG_CLASS_MASK,
3388 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3389 MBEDTLS_ASN1_TAG_VALUE_MASK,
3390 2 /* SubjectAlt DNS */,
3391 x509_crt_subject_alt_check_name,
Hanno Becker484caf02019-05-29 14:41:44 +01003392 (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003393 }
3394 else
3395 {
Hanno Becker082435c2019-02-25 18:14:40 +00003396 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3397 &frame->subject_raw,
Hanno Becker484caf02019-05-29 14:41:44 +01003398 x509_crt_check_name, (void *) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003399 }
Hanno Beckerda410822019-02-21 13:36:59 +00003400
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003401 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003402
3403 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3404 * return 1 when finding a name component matching `cn`. */
3405 if( ret == 1 )
3406 return( 0 );
3407
3408 if( ret != 0 )
3409 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3410
3411 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3412 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003413}
3414
3415/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003416 * Merge the flags for all certs in the chain, after calling callback
3417 */
3418static int x509_crt_merge_flags_with_cb(
3419 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003420 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003421 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3422 void *p_vrfy )
3423{
3424 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003425 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003426 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003427 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003428
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003429 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003430 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003431 cur = &ver_chain->items[i-1];
3432 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003433
3434 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003435 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003436 return( ret );
3437
3438 *flags |= cur_flags;
3439 }
3440
3441 return( 0 );
3442}
3443
3444/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003445 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3448 mbedtls_x509_crt *trust_ca,
3449 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003450 const char *cn, uint32_t *flags,
3451 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003452 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003453{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003454 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3455 &mbedtls_x509_crt_profile_default, cn, flags,
3456 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003457}
3458
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003459/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003460 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003461 */
3462int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3463 mbedtls_x509_crt *trust_ca,
3464 mbedtls_x509_crl *ca_crl,
3465 const mbedtls_x509_crt_profile *profile,
3466 const char *cn, uint32_t *flags,
3467 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3468 void *p_vrfy )
3469{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003470 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3471 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3472}
3473
3474/*
3475 * Verify the certificate validity, with profile, restartable version
3476 *
3477 * This function:
3478 * - checks the requested CN (if any)
3479 * - checks the type and size of the EE cert's key,
3480 * as that isn't done as part of chain building/verification currently
3481 * - builds and verifies the chain
3482 * - then calls the callback and merges the flags
3483 */
3484int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3485 mbedtls_x509_crt *trust_ca,
3486 mbedtls_x509_crl *ca_crl,
3487 const mbedtls_x509_crt_profile *profile,
3488 const char *cn, uint32_t *flags,
3489 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3490 void *p_vrfy,
3491 mbedtls_x509_crt_restart_ctx *rs_ctx )
3492{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003493 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003494 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003495 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003496
3497 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003498 ee_flags = 0;
3499 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003500
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003501 if( profile == NULL )
3502 {
3503 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3504 goto exit;
3505 }
3506
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003507 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003508 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003509 {
3510 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3511 if( ret != 0 )
3512 return( ret );
3513 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003514
Hanno Becker87233362019-02-25 18:15:33 +00003515 {
3516 mbedtls_pk_context *pk;
3517 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003518
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003519 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003520 if( ret != 0 )
3521 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003522
Hanno Becker87233362019-02-25 18:15:33 +00003523 /* Check the type and size of the key */
3524 pk_type = mbedtls_pk_get_type( pk );
3525
3526 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3527 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3528
3529 if( x509_profile_check_key( profile, pk ) != 0 )
3530 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3531
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003532 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003533 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003534
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003535 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003536 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003537 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003538
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003539 if( ret != 0 )
3540 goto exit;
3541
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003542 /* Merge end-entity flags */
3543 ver_chain.items[0].flags |= ee_flags;
3544
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003545 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003546 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003547
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003548exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003549#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3550 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3551 mbedtls_x509_crt_restart_free( rs_ctx );
3552#endif
3553
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003554 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3555 * the SSL module for authmode optional, but non-zero return from the
3556 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003557 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3558 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3559
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003560 if( ret != 0 )
3561 {
3562 *flags = (uint32_t) -1;
3563 return( ret );
3564 }
3565
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003566 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003568
3569 return( 0 );
3570}
3571
3572/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003573 * Initialize a certificate chain
3574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003578}
3579
3580/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003581 * Unallocate all certificate data
3582 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 mbedtls_x509_crt *cert_cur = crt;
3587 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003588
3589 if( crt == NULL )
3590 return;
3591
3592 do
3593 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003594 x509_crt_cache_free( cert_cur->cache );
3595 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003596
3597#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3601 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003602#endif
3603
Hanno Becker2bcc7642019-02-26 19:01:00 +00003604 mbedtls_x509_name_free( cert_cur->issuer.next );
3605 mbedtls_x509_name_free( cert_cur->subject.next );
3606 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3607 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003608#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003609
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003610 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003611 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003612 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003614 }
3615
3616 cert_cur = cert_cur->next;
3617 }
3618 while( cert_cur != NULL );
3619
3620 cert_cur = crt;
3621 do
3622 {
3623 cert_prv = cert_cur;
3624 cert_cur = cert_cur->next;
3625
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003626 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003627 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003629 }
3630 while( cert_cur != NULL );
3631}
3632
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003633#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3634/*
3635 * Initialize a restart context
3636 */
3637void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3638{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003639 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003640
3641 ctx->parent = NULL;
3642 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003643 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003644
3645 ctx->parent_is_trusted = -1;
3646
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003647 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003648 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003649 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003650}
3651
3652/*
3653 * Free the components of a restart context
3654 */
3655void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3656{
3657 if( ctx == NULL )
3658 return;
3659
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003660 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003661 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003662}
3663#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665#endif /* MBEDTLS_X509_CRT_PARSE_C */