blob: 91b29b6145e7754def14b964bd5e640ebe7a5222 [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"
43#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050044#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000045
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
77#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Hanno Becker38f0cb42019-03-04 15:13:45 +000081#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
82static void x509_buf_to_buf_raw( mbedtls_x509_buf_raw *dst,
83 mbedtls_x509_buf const *src )
84{
85 dst->p = src->p;
86 dst->len = src->len;
87}
88
89static void x509_buf_raw_to_buf( mbedtls_x509_buf *dst,
90 mbedtls_x509_buf_raw const *src )
91{
92 dst->p = src->p;
93 dst->len = src->len;
94}
95#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
96
Hanno Becker21f55672019-02-15 15:27:59 +000097static int x509_crt_parse_frame( unsigned char *start,
98 unsigned char *end,
99 mbedtls_x509_crt_frame *frame );
100static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
101 mbedtls_x509_name *subject );
102static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
103 mbedtls_x509_name *issuer );
104static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
105 mbedtls_x509_sequence *subject_alt );
106static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
107 mbedtls_x509_sequence *ext_key_usage );
108
Hanno Beckerbc685192019-03-05 15:35:31 +0000109int mbedtls_x509_crt_flush_cache_pk( mbedtls_x509_crt const *crt )
110{
111#if defined(MBEDTLS_THREADING_C)
112 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
113 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
114#endif
115
116#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
117 /* The cache holds a shallow copy of the PK context
118 * in the legacy struct, so don't free PK context. */
119 mbedtls_free( crt->cache->pk );
120#else
121 mbedtls_pk_free( crt->cache->pk );
122 mbedtls_free( crt->cache->pk );
123#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
124 crt->cache->pk = NULL;
125
126#if defined(MBEDTLS_THREADING_C)
127 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
128 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
129#endif
130 return( 0 );
131}
132
133int mbedtls_x509_crt_flush_cache_frame( mbedtls_x509_crt const *crt )
134{
135#if defined(MBEDTLS_THREADING_C)
136 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
137 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
138#endif
139
140 mbedtls_free( crt->cache->frame );
141 crt->cache->frame = NULL;
142
143#if defined(MBEDTLS_THREADING_C)
144 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
145 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
146#endif
147 return( 0 );
148}
149
150int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt )
151{
152 int ret;
153 ret = mbedtls_x509_crt_flush_cache_frame( crt );
154 if( ret != 0 )
155 return( ret );
156 ret = mbedtls_x509_crt_flush_cache_pk( crt );
157 if( ret != 0 )
158 return( ret );
159 return( 0 );
160}
161
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000162static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000163int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt )
Hanno Becker337088a2019-02-25 14:53:14 +0000164{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000165 mbedtls_x509_crt_cache *cache = crt->cache;
Hanno Becker337088a2019-02-25 14:53:14 +0000166 mbedtls_x509_crt_frame *frame;
167
168 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
169 if( frame == NULL )
170 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000171 cache->frame = frame;
Hanno Becker337088a2019-02-25 14:53:14 +0000172
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000173#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
174 /* This would work with !MBEDTLS_X509_ON_DEMAND_PARSING, too,
175 * but is inefficient compared to copying the respective fields
176 * from the legacy mbedtls_x509_crt. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000177 return( x509_crt_parse_frame( crt->raw.p,
178 crt->raw.p + crt->raw.len,
179 frame ) );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000180#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
181 /* Make sure all extension related fields are properly initialized. */
182 frame->ca_istrue = 0;
183 frame->max_pathlen = 0;
184 frame->ext_types = 0;
185 frame->version = crt->version;
186 frame->sig_md = crt->sig_md;
187 frame->sig_pk = crt->sig_pk;
188 frame->valid_from = crt->valid_from;
189 frame->valid_to = crt->valid_to;
Hanno Becker38f0cb42019-03-04 15:13:45 +0000190 x509_buf_to_buf_raw( &frame->raw, &crt->raw );
191 x509_buf_to_buf_raw( &frame->tbs, &crt->tbs );
192 x509_buf_to_buf_raw( &frame->serial, &crt->serial );
193 x509_buf_to_buf_raw( &frame->pubkey_raw, &crt->pk_raw );
194 x509_buf_to_buf_raw( &frame->issuer_raw, &crt->issuer_raw );
195 x509_buf_to_buf_raw( &frame->subject_raw, &crt->subject_raw );
196 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
197 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
198 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
199 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000200
201 /* The legacy CRT structure doesn't explicitly contain
202 * the `AlgorithmIdentifier` bounds; however, those can
203 * be inferred from the surrounding (mandatory) `SerialNumber`
204 * and `Issuer` fields. */
205 frame->sig_alg.p = crt->serial.p + crt->serial.len;
206 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
207
208 return( x509_crt_frame_parse_ext( frame ) );
209#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000210}
Hanno Becker337088a2019-02-25 14:53:14 +0000211
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000212int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
213{
214 mbedtls_x509_crt_cache *cache = crt->cache;
215 mbedtls_pk_context *pk;
216
217 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
218 if( pk == NULL )
219 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000220 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000221
222#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
223 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000224 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000225#else
226 {
227 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
228 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
229 pk_raw.p + pk_raw.len,
230 pk ) );
231 }
232#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000233}
234
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000235static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000236{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000237 memset( cache, 0, sizeof( *cache ) );
238#if defined(MBEDTLS_THREADING_C)
239 mbedtls_mutex_init( &cache->frame_mutex );
240 mbedtls_mutex_init( &cache->pk_mutex );
241#endif
242}
243
244static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
245{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000246#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000247 /* The cache holds a shallow copy of the PK context
248 * in the legacy struct, so don't free PK context. */
249 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000250#else
251 mbedtls_pk_free( cache->pk );
252 mbedtls_free( cache->pk );
253#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
254
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000255 cache->pk = NULL;
256}
257
258static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
259{
260 mbedtls_free( cache->frame );
261 cache->frame = NULL;
262}
263
264static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
265{
266 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000267 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000268
269#if defined(MBEDTLS_THREADING_C)
270 mbedtls_mutex_free( &cache->frame_mutex );
271 mbedtls_mutex_free( &cache->pk_mutex );
272#endif
273
274 x509_crt_cache_clear_frame( cache );
275 x509_crt_cache_clear_pk( cache );
276
277 memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000278}
279
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000280int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
281 mbedtls_x509_sequence **subj_alt )
282{
283 int ret;
284 mbedtls_x509_crt_frame *frame;
285 mbedtls_x509_sequence *seq;
286
287 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
288 if( ret != 0 )
289 return( ret );
290
291 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
292 if( seq == NULL )
293 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
294 else
295 ret = x509_crt_subject_alt_from_frame( frame, seq );
296
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000297 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000298
299 *subj_alt = seq;
300 return( ret );
301}
302
303int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
304 mbedtls_x509_sequence **ext_key_usage )
305{
306 int ret;
307 mbedtls_x509_crt_frame *frame;
308 mbedtls_x509_sequence *seq;
309
310 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
311 if( ret != 0 )
312 return( ret );
313
314 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
315 if( seq == NULL )
316 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
317 else
318 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
319
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000320 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000321
322 *ext_key_usage = seq;
323 return( ret );
324}
325
Hanno Becker63e69982019-02-26 18:50:49 +0000326int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
327 mbedtls_x509_name **subject )
328{
329 int ret;
330 mbedtls_x509_crt_frame *frame;
331 mbedtls_x509_name *name;
332
333 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
334 if( ret != 0 )
335 return( ret );
336
337 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
338 if( name == NULL )
339 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
340 else
341 ret = x509_crt_subject_from_frame( frame, name );
342
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000343 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000344
345 *subject = name;
346 return( ret );
347}
348
349int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
350 mbedtls_x509_name **issuer )
351{
352 int ret;
353 mbedtls_x509_crt_frame *frame;
354 mbedtls_x509_name *name;
355
356 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
357 if( ret != 0 )
358 return( ret );
359
360 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
361 if( name == NULL )
362 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
363 else
364 ret = x509_crt_issuer_from_frame( frame, name );
365
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000366 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000367
368 *issuer = name;
369 return( ret );
370}
371
Hanno Becker823efad2019-02-28 13:23:58 +0000372int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
373 mbedtls_x509_crt_frame *dst )
374{
375 int ret;
376 mbedtls_x509_crt_frame *frame;
377 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
378 if( ret != 0 )
379 return( ret );
380 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000381 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000382 return( 0 );
383}
384
385int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
386 mbedtls_pk_context *dst )
387{
388#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
389 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
390 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
391 pk_raw.p + pk_raw.len,
392 dst ) );
393#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
394 int ret;
395 mbedtls_pk_context *pk;
396 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
397 if( ret != 0 )
398 return( ret );
399
400 /* Move PK from CRT cache to destination pointer
401 * to avoid a copy. */
402 *dst = *pk;
403 mbedtls_free( crt->cache->pk );
404 crt->cache->pk = NULL;
405
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000406 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000407 return( 0 );
408#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
409}
410
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200411/*
412 * Item in a verification chain: cert and flags for it
413 */
414typedef struct {
415 mbedtls_x509_crt *crt;
416 uint32_t flags;
417} x509_crt_verify_chain_item;
418
419/*
420 * Max size of verification chain: end-entity + intermediates + trusted root
421 */
422#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200423
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200425 * Default profile
426 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200427const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
428{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200429#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200430 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200431 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200432#endif
433 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200434 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
435 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
436 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
437 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
438 0xFFFFFFF, /* Any PK alg */
439 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200440 2048,
441};
442
443/*
444 * Next-default profile
445 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200446const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
447{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200448 /* Hashes from SHA-256 and above */
449 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
450 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
451 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
452 0xFFFFFFF, /* Any PK alg */
453#if defined(MBEDTLS_ECP_C)
454 /* Curves at or above 128-bit security level */
455 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
456 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
457 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
458 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
459 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
460 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
461 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
462#else
463 0,
464#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200465 2048,
466};
467
468/*
469 * NSA Suite B Profile
470 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200471const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
472{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200473 /* Only SHA-256 and 384 */
474 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
475 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
476 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200477 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
478 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200479#if defined(MBEDTLS_ECP_C)
480 /* Only NIST P-256 and P-384 */
481 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
482 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
483#else
484 0,
485#endif
486 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200487};
488
489/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200490 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200491 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200492 */
493static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
494 mbedtls_md_type_t md_alg )
495{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200496 if( md_alg == MBEDTLS_MD_NONE )
497 return( -1 );
498
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200499 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
500 return( 0 );
501
502 return( -1 );
503}
504
505/*
506 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200507 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200508 */
509static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
510 mbedtls_pk_type_t pk_alg )
511{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200512 if( pk_alg == MBEDTLS_PK_NONE )
513 return( -1 );
514
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200515 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
516 return( 0 );
517
518 return( -1 );
519}
520
521/*
522 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200523 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200524 */
525static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200526 const mbedtls_pk_context *pk )
527{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200528 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200529
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200530#if defined(MBEDTLS_RSA_C)
531 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
532 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200533 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200534 return( 0 );
535
536 return( -1 );
537 }
538#endif
539
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200540#if defined(MBEDTLS_ECP_C)
541 if( pk_alg == MBEDTLS_PK_ECDSA ||
542 pk_alg == MBEDTLS_PK_ECKEY ||
543 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200544 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200545 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200546
Philippe Antoineb5b25432018-05-11 11:06:29 +0200547 if( gid == MBEDTLS_ECP_DP_NONE )
548 return( -1 );
549
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200550 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
551 return( 0 );
552
553 return( -1 );
554 }
555#endif
556
557 return( -1 );
558}
559
560/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000561 * Return 0 if name matches wildcard, -1 otherwise
562 */
Hanno Becker24926222019-02-21 13:10:55 +0000563static int x509_check_wildcard( char const *cn,
564 size_t cn_len,
565 unsigned char const *buf,
566 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000567{
568 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000569 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000570
571 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000572 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000573 return( -1 );
574
575 for( i = 0; i < cn_len; ++i )
576 {
577 if( cn[i] == '.' )
578 {
579 cn_idx = i;
580 break;
581 }
582 }
583
584 if( cn_idx == 0 )
585 return( -1 );
586
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000587 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
588 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000589 {
590 return( 0 );
591 }
592
593 return( -1 );
594}
595
596/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200597 * Reset (init or clear) a verify_chain
598 */
599static void x509_crt_verify_chain_reset(
600 mbedtls_x509_crt_verify_chain *ver_chain )
601{
602 size_t i;
603
604 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
605 {
606 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000607 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200608 }
609
610 ver_chain->len = 0;
611}
612
613/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
615 */
616static int x509_get_version( unsigned char **p,
617 const unsigned char *end,
618 int *ver )
619{
620 int ret;
621 size_t len;
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
624 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 {
628 *ver = 0;
629 return( 0 );
630 }
631
Hanno Becker2f472142019-02-12 11:52:10 +0000632 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633 }
634
635 end = *p + len;
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
638 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639
640 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( MBEDTLS_ERR_X509_INVALID_VERSION +
642 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643
644 return( 0 );
645}
646
647/*
648 * Validity ::= SEQUENCE {
649 * notBefore Time,
650 * notAfter Time }
651 */
652static int x509_get_dates( unsigned char **p,
653 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_x509_time *from,
655 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656{
657 int ret;
658 size_t len;
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
661 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
662 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663
664 end = *p + len;
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667 return( ret );
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 return( ret );
671
672 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_X509_INVALID_DATE +
674 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675
676 return( 0 );
677}
678
679/*
680 * X.509 v2/v3 unique identifier (not parsed)
681 */
682static int x509_get_uid( unsigned char **p,
683 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685{
686 int ret;
687
688 if( *p == end )
689 return( 0 );
690
691 uid->tag = **p;
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
694 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697 return( 0 );
698
Hanno Becker2f472142019-02-12 11:52:10 +0000699 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 }
701
702 uid->p = *p;
703 *p += uid->len;
704
705 return( 0 );
706}
707
708static int x509_get_basic_constraints( unsigned char **p,
709 const unsigned char *end,
710 int *ca_istrue,
711 int *max_pathlen )
712{
713 int ret;
714 size_t len;
715
716 /*
717 * BasicConstraints ::= SEQUENCE {
718 * cA BOOLEAN DEFAULT FALSE,
719 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
720 */
721 *ca_istrue = 0; /* DEFAULT FALSE */
722 *max_pathlen = 0; /* endless */
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
725 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000726 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727
728 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200729 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
734 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735
736 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000737 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738
739 if( *ca_istrue != 0 )
740 *ca_istrue = 1;
741 }
742
743 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200744 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000747 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748
749 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000750 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751
752 (*max_pathlen)++;
753
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200754 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755}
756
757static int x509_get_ns_cert_type( unsigned char **p,
758 const unsigned char *end,
759 unsigned char *ns_cert_type)
760{
761 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000765 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766
767 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000768 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769
770 /* Get actual bitstring */
771 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200772 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773}
774
775static int x509_get_key_usage( unsigned char **p,
776 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100777 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778{
779 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200780 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000784 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785
786 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000787 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788
789 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200790 *key_usage = 0;
791 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
792 {
793 *key_usage |= (unsigned int) bs.p[i] << (8*i);
794 }
795
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200796 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797}
798
799/*
800 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
801 *
802 * KeyPurposeId ::= OBJECT IDENTIFIER
803 */
804static int x509_get_ext_key_usage( unsigned char **p,
805 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000808 return( mbedtls_asn1_get_sequence_of( p, end, ext_key_usage,
809 MBEDTLS_ASN1_OID ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810}
811
812/*
813 * SubjectAltName ::= GeneralNames
814 *
815 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
816 *
817 * GeneralName ::= CHOICE {
818 * otherName [0] OtherName,
819 * rfc822Name [1] IA5String,
820 * dNSName [2] IA5String,
821 * x400Address [3] ORAddress,
822 * directoryName [4] Name,
823 * ediPartyName [5] EDIPartyName,
824 * uniformResourceIdentifier [6] IA5String,
825 * iPAddress [7] OCTET STRING,
826 * registeredID [8] OBJECT IDENTIFIER }
827 *
828 * OtherName ::= SEQUENCE {
829 * type-id OBJECT IDENTIFIER,
830 * value [0] EXPLICIT ANY DEFINED BY type-id }
831 *
832 * EDIPartyName ::= SEQUENCE {
833 * nameAssigner [0] DirectoryString OPTIONAL,
834 * partyName [1] DirectoryString }
835 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000836 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837 */
Hanno Beckerad462192019-02-21 13:32:31 +0000838static int x509_get_subject_alt_name_cb( void *ctx,
839 int tag,
840 unsigned char *data,
841 size_t data_len )
842{
843 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
844 mbedtls_asn1_sequence *cur = *cur_ptr;
845
Hanno Beckerad462192019-02-21 13:32:31 +0000846 /* Allocate and assign next pointer */
847 if( cur->buf.p != NULL )
848 {
849 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
850 if( cur->next == NULL )
Hanno Becker90b94082019-02-21 21:13:21 +0000851 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Hanno Beckerad462192019-02-21 13:32:31 +0000852 cur = cur->next;
853 }
854
855 cur->buf.tag = tag;
856 cur->buf.p = data;
857 cur->buf.len = data_len;
858
859 *cur_ptr = cur;
860 return( 0 );
861}
862
Hanno Becker5984d302019-02-21 14:46:54 +0000863static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000867 return( mbedtls_asn1_traverse_sequence_of( &p, end,
868 MBEDTLS_ASN1_TAG_CLASS_MASK,
869 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
870 MBEDTLS_ASN1_TAG_VALUE_MASK,
871 2 /* SubjectAlt DNS */,
872 x509_get_subject_alt_name_cb,
873 (void*) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874}
875
876/*
877 * X.509 v3 extensions
878 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000880static int x509_crt_get_ext_cb( void *ctx,
881 int tag,
882 unsigned char *p,
883 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884{
885 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000886 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000888 unsigned char *end, *end_ext_octet;
889 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
890 int is_critical = 0; /* DEFAULT FALSE */
891 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000893 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000894
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000895 /*
896 * Extension ::= SEQUENCE {
897 * extnID OBJECT IDENTIFIER,
898 * critical BOOLEAN DEFAULT FALSE,
899 * extnValue OCTET STRING }
900 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000902 end = p + ext_len;
903
904 /* Get extension ID */
905 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
906 MBEDTLS_ASN1_OID ) ) != 0 )
907 goto err;
908
909 extn_oid.tag = MBEDTLS_ASN1_OID;
910 extn_oid.p = p;
911 p += extn_oid.len;
912
913 /* Get optional critical */
914 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
915 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
916 goto err;
917
918 /* Data should be octet string type */
919 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
920 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
921 goto err;
922
923 end_ext_octet = p + len;
924 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000926 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
927 goto err;
928 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000930 /*
931 * Detect supported extensions
932 */
933 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
934 if( ret != 0 )
935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000937 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000939 /* Data is marked as critical: fail */
940 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
941 goto err;
942 }
943#endif
944 return( 0 );
945 }
946
947 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +0000948 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000949 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
950
Hanno Becker21f55672019-02-15 15:27:59 +0000951 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000952 switch( ext_type )
953 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100954 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000955 {
956 int ca_istrue;
957 int max_pathlen;
958
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000960 ret = x509_get_basic_constraints( &p, end_ext_octet,
961 &ca_istrue,
962 &max_pathlen );
963 if( ret != 0 )
964 goto err;
965
Hanno Becker21f55672019-02-15 15:27:59 +0000966 frame->ca_istrue = ca_istrue;
967 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000969 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000973 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +0000974 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000975 if( ret != 0 )
976 goto err;
977 break;
978
979 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
980 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +0000981 frame->subject_alt_raw.p = p;
982 frame->subject_alt_raw.len = end_ext_octet - p;
983
984 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
985 MBEDTLS_ASN1_TAG_CLASS_MASK,
986 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
987 MBEDTLS_ASN1_TAG_VALUE_MASK,
988 2 /* SubjectAlt DNS */,
989 NULL, NULL );
990 if( ret != 0 )
991 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 break;
993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +0000996 frame->ext_key_usage_raw.p = p;
997 frame->ext_key_usage_raw.len = end_ext_octet - p;
998 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +0000999 {
Hanno Becker21f55672019-02-15 15:27:59 +00001000 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1001 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001002 }
Hanno Becker21f55672019-02-15 15:27:59 +00001003
1004 /* Check structural sanity of extension. */
1005 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1006 0xFF, MBEDTLS_ASN1_OID,
1007 0, 0, NULL, NULL );
1008 if( ret != 0 )
1009 goto err;
1010
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 break;
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001015 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001016 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001017 if( ret != 0 )
1018 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 break;
1020
1021 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001022 /*
1023 * If this is a non-critical extension, which the oid layer
1024 * supports, but there isn't an X.509 parser for it,
1025 * skip the extension.
1026 */
1027#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1028 if( is_critical )
1029 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1030#endif
1031 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 }
1033
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001035
1036err:
1037 return( ret );
1038}
1039
Hanno Becker21f55672019-02-15 15:27:59 +00001040static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001041{
1042 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001043 unsigned char *p = frame->v3_ext.p;
1044 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001045
Hanno Becker21f55672019-02-15 15:27:59 +00001046 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001047 return( 0 );
1048
Hanno Becker21f55672019-02-15 15:27:59 +00001049 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001050 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001051 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001052
1053 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1054 return( ret );
1055 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1056 return( ret );
1057
1058 if( ret != 0 )
1059 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1060
1061 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062}
1063
Hanno Becker21f55672019-02-15 15:27:59 +00001064static int x509_crt_parse_frame( unsigned char *start,
1065 unsigned char *end,
1066 mbedtls_x509_crt_frame *frame )
1067{
1068 int ret;
1069 unsigned char *p;
1070 size_t len;
1071
1072 mbedtls_x509_buf tmp;
1073 unsigned char *tbs_start;
1074
1075 mbedtls_x509_buf outer_sig_alg;
1076 size_t inner_sig_alg_len;
1077 unsigned char *inner_sig_alg_start;
1078
1079 memset( frame, 0, sizeof( *frame ) );
1080
1081 /*
1082 * Certificate ::= SEQUENCE {
1083 * tbsCertificate TBSCertificate,
1084 * signatureAlgorithm AlgorithmIdentifier,
1085 * signatureValue BIT STRING
1086 * }
1087 *
1088 */
1089 p = start;
1090
1091 frame->raw.p = p;
1092 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1093 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1094 {
1095 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1096 }
1097
1098 /* NOTE: We are currently not checking that the `Certificate`
1099 * structure spans the entire buffer. */
1100 end = p + len;
1101 frame->raw.len = end - frame->raw.p;
1102
1103 /*
1104 * TBSCertificate ::= SEQUENCE { ...
1105 */
1106 frame->tbs.p = p;
1107 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1108 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1109 {
1110 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1111 }
1112 tbs_start = p;
1113
1114 /* Breadth-first parsing: Jump over TBS for now. */
1115 p += len;
1116 frame->tbs.len = p - frame->tbs.p;
1117
1118 /*
1119 * AlgorithmIdentifier ::= SEQUENCE { ...
1120 */
1121 outer_sig_alg.p = p;
1122 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1123 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1124 {
1125 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1126 }
1127 p += len;
1128 outer_sig_alg.len = p - outer_sig_alg.p;
1129
1130 /*
1131 * signatureValue BIT STRING
1132 */
1133 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1134 if( ret != 0 )
1135 return( ret );
1136 frame->sig.p = tmp.p;
1137 frame->sig.len = tmp.len;
1138
1139 /* Check that we consumed the entire `Certificate` structure. */
1140 if( p != end )
1141 {
1142 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1143 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1144 }
1145
1146 /* Parse TBSCertificate structure
1147 *
1148 * TBSCertificate ::= SEQUENCE {
1149 * version [0] EXPLICIT Version DEFAULT v1,
1150 * serialNumber CertificateSerialNumber,
1151 * signature AlgorithmIdentifier,
1152 * issuer Name,
1153 * validity Validity,
1154 * subject Name,
1155 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1156 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1157 * -- If present, version MUST be v2 or v3
1158 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1159 * -- If present, version MUST be v2 or v3
1160 * extensions [3] EXPLICIT Extensions OPTIONAL
1161 * -- If present, version MUST be v3
1162 * }
1163 */
1164 end = frame->tbs.p + frame->tbs.len;
1165 p = tbs_start;
1166
1167 /*
1168 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1169 */
1170 {
1171 int version;
1172 ret = x509_get_version( &p, end, &version );
1173 if( ret != 0 )
1174 return( ret );
1175
1176 if( version < 0 || version > 2 )
1177 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1178
1179 frame->version = version + 1;
1180 }
1181
1182 /*
1183 * CertificateSerialNumber ::= INTEGER
1184 */
1185 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1186 if( ret != 0 )
1187 return( ret );
1188
1189 frame->serial.p = tmp.p;
1190 frame->serial.len = tmp.len;
1191
1192 /*
1193 * signature AlgorithmIdentifier
1194 */
1195 inner_sig_alg_start = p;
1196 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1197 &frame->sig_pk, NULL );
1198 if( ret != 0 )
1199 return( ret );
1200 inner_sig_alg_len = p - inner_sig_alg_start;
1201
1202 frame->sig_alg.p = inner_sig_alg_start;
1203 frame->sig_alg.len = inner_sig_alg_len;
1204
1205 /* Consistency check:
1206 * Inner and outer AlgorithmIdentifier structures must coincide:
1207 *
1208 * Quoting RFC 5280, Section 4.1.1.2:
1209 * This field MUST contain the same algorithm identifier as the
1210 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1211 */
1212 if( outer_sig_alg.len != inner_sig_alg_len ||
1213 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1214 {
1215 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1216 }
1217
1218 /*
1219 * issuer Name
1220 *
1221 * Name ::= CHOICE { -- only one possibility for now --
1222 * rdnSequence RDNSequence }
1223 *
1224 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1225 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001226 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001227
1228 ret = mbedtls_asn1_get_tag( &p, end, &len,
1229 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1230 if( ret != 0 )
1231 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001232 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001233 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001234
1235 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1236 &frame->issuer_raw,
1237 NULL, NULL );
1238 if( ret != 0 )
1239 return( ret );
1240
Hanno Becker21f55672019-02-15 15:27:59 +00001241 /*
1242 * Validity ::= SEQUENCE { ...
1243 */
1244 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1245 if( ret != 0 )
1246 return( ret );
1247
1248 /*
1249 * subject Name
1250 *
1251 * Name ::= CHOICE { -- only one possibility for now --
1252 * rdnSequence RDNSequence }
1253 *
1254 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1255 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001256 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001257
1258 ret = mbedtls_asn1_get_tag( &p, end, &len,
1259 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1260 if( ret != 0 )
1261 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001262 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001263 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001264
1265 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1266 &frame->subject_raw,
1267 NULL, NULL );
1268 if( ret != 0 )
1269 return( ret );
1270
Hanno Becker21f55672019-02-15 15:27:59 +00001271 /*
1272 * SubjectPublicKeyInfo
1273 */
1274 frame->pubkey_raw.p = p;
1275 ret = mbedtls_asn1_get_tag( &p, end, &len,
1276 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1277 if( ret != 0 )
1278 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1279 p += len;
1280 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1281
1282 /*
1283 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1284 * -- If present, version shall be v2 or v3
1285 */
1286 if( frame->version == 2 || frame->version == 3 )
1287 {
1288 ret = x509_get_uid( &p, end, &tmp, 1 /* implicit tag */ );
1289 if( ret != 0 )
1290 return( ret );
1291
1292 frame->issuer_id.p = tmp.p;
1293 frame->issuer_id.len = tmp.len;
1294 }
1295
1296 /*
1297 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1298 * -- If present, version shall be v2 or v3
1299 */
1300 if( frame->version == 2 || frame->version == 3 )
1301 {
1302 ret = x509_get_uid( &p, end, &tmp, 2 /* implicit tag */ );
1303 if( ret != 0 )
1304 return( ret );
1305
1306 frame->subject_id.p = tmp.p;
1307 frame->subject_id.len = tmp.len;
1308 }
1309
1310 /*
1311 * extensions [3] EXPLICIT Extensions OPTIONAL
1312 * -- If present, version shall be v3
1313 */
1314#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1315 if( frame->version == 3 )
1316#endif
1317 {
1318 if( p != end )
1319 {
1320 ret = mbedtls_asn1_get_tag( &p, end, &len,
1321 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1322 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1323 if( len == 0 )
1324 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1325 if( ret != 0 )
1326 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1327
1328 frame->v3_ext.p = p;
1329 frame->v3_ext.len = len;
1330
1331 p += len;
1332 }
1333
1334 ret = x509_crt_frame_parse_ext( frame );
1335 if( ret != 0 )
1336 return( ret );
1337 }
1338
1339 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1340 if( p != end )
1341 {
1342 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1343 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1344 }
1345
1346 return( 0 );
1347}
1348
1349static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
1350 mbedtls_x509_name *subject )
1351{
Hanno Becker1e11f212019-03-04 14:43:43 +00001352 return( mbedtls_x509_get_name( frame->subject_raw.p,
1353 frame->subject_raw.len,
1354 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001355}
1356
1357static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
1358 mbedtls_x509_name *issuer )
1359{
Hanno Becker1e11f212019-03-04 14:43:43 +00001360 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1361 frame->issuer_raw.len,
1362 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001363}
1364
1365static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
1366 mbedtls_x509_sequence *subject_alt )
1367{
1368 int ret;
1369 unsigned char *p = frame->subject_alt_raw.p;
1370 unsigned char *end = p + frame->subject_alt_raw.len;
1371
1372 memset( subject_alt, 0, sizeof( *subject_alt ) );
1373
1374 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1375 return( 0 );
1376
1377 ret = x509_get_subject_alt_name( p, end, subject_alt );
1378 if( ret != 0 )
1379 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1380 return( ret );
1381}
1382
1383static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
1384 mbedtls_x509_sequence *ext_key_usage )
1385{
1386 int ret;
1387 unsigned char *p = frame->ext_key_usage_raw.p;
1388 unsigned char *end = p + frame->ext_key_usage_raw.len;
1389
1390 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1391
1392 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1393 return( 0 );
1394
1395 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1396 if( ret != 0 )
1397 {
1398 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1399 return( ret );
1400 }
1401
1402 return( 0 );
1403}
1404
Hanno Becker180f7bf2019-02-28 13:23:38 +00001405#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001406static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1407 mbedtls_pk_context *pk )
1408{
1409 unsigned char *p = frame->pubkey_raw.p;
1410 unsigned char *end = p + frame->pubkey_raw.len;
1411 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1412}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001413#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001414
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415/*
1416 * Parse and fill a single X.509 certificate in DER format
1417 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001418static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1419 const unsigned char *buf,
1420 size_t buflen,
1421 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422{
1423 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001424 mbedtls_x509_crt_frame *frame;
1425 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001426
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429
Hanno Becker21f55672019-02-15 15:27:59 +00001430 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431 {
Hanno Becker21f55672019-02-15 15:27:59 +00001432 crt->raw.p = (unsigned char*) buf;
1433 crt->raw.len = buflen;
1434 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001435 }
1436 else
1437 {
Hanno Becker21f55672019-02-15 15:27:59 +00001438 crt->raw.p = mbedtls_calloc( 1, buflen );
1439 if( crt->raw.p == NULL )
1440 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1441 crt->raw.len = buflen;
1442 memcpy( crt->raw.p, buf, buflen );
1443
1444 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001445 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001446
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001447 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1448 if( cache == NULL )
1449 {
1450 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1451 goto exit;
1452 }
1453 crt->cache = cache;
1454 x509_crt_cache_init( cache );
1455
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001456#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1457
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001458 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001459 if( ret != 0 )
1460 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001462 frame = mbedtls_x509_crt_cache_get_frame( crt->cache );
1463
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001464#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001466 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1467 if( frame == NULL )
1468 {
1469 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1470 goto exit;
1471 }
1472 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001473
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001474 ret = x509_crt_parse_frame( crt->raw.p,
1475 crt->raw.p + crt->raw.len,
1476 frame );
1477 if( ret != 0 )
1478 goto exit;
1479
Hanno Becker21f55672019-02-15 15:27:59 +00001480 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1481 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001482 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1483 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1484 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1485 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
1486 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1487 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
1488 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1489 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1490 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001491 crt->valid_from = frame->valid_from;
1492 crt->valid_to = frame->valid_to;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001493 crt->version = frame->version;
1494 crt->ca_istrue = frame->ca_istrue;
1495 crt->max_pathlen = frame->max_pathlen;
1496 crt->ext_types = frame->ext_types;
1497 crt->key_usage = frame->key_usage;
1498 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499
1500 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001501 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504 {
Hanno Becker21f55672019-02-15 15:27:59 +00001505 /* sig_oid: Previously, needed for convenience in
1506 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001507 unsigned char *tmp = frame->sig_alg.p;
1508 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001509 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510
Hanno Becker21f55672019-02-15 15:27:59 +00001511 ret = mbedtls_x509_get_alg( &tmp, end,
1512 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001513 if( ret != 0 )
1514 {
Hanno Becker21f55672019-02-15 15:27:59 +00001515 /* This should never happen, because we check
1516 * the sanity of the AlgorithmIdentifier structure
1517 * during frame parsing. */
1518 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1519 goto exit;
1520 }
1521 crt->sig_oid = sig_oid;
1522
1523 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001524 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001525 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1526 &crt->sig_md, &crt->sig_pk,
1527 &crt->sig_opts );
1528 if( ret != 0 )
1529 {
1530 /* Again, this should never happen. */
1531 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1532 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533 }
1534 }
1535
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001536 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001537 if( ret != 0 )
1538 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001540 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001541 if( ret != 0 )
1542 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001544 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001545 if( ret != 0 )
1546 goto exit;
1547
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001548 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001549 if( ret != 0 )
1550 goto exit;
1551
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001552 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1553 if( ret != 0 )
1554 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001555#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001556
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001557 /* Currently, we accept DER encoded CRTs with trailing garbage
1558 * and promise to not account for the garbage in the `raw` field.
1559 *
1560 * Note that this means that `crt->raw.len` is not necessarily the
1561 * full size of the heap buffer allocated at `crt->raw.p` in case
1562 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1563 * need the size, and the garbage data doesn't need zeroization. */
1564 crt->raw.len = frame->raw.len;
1565
1566 cache->pk_raw = frame->pubkey_raw;
1567
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001568 /* Free the frame before parsing the public key to
1569 * keep peak RAM usage low. This is slightly inefficient
1570 * because the frame will need to be parsed again on the
1571 * first usage of the CRT, but that seems acceptable.
1572 * As soon as the frame gets used multiple times, it
1573 * will be cached by default. */
1574 x509_crt_cache_clear_frame( crt->cache );
1575
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001576 /* The cache just references the PK structure from the legacy
1577 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001578 * the cache.
1579 *
1580 * We're not actually using the parsed PK context here;
1581 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001582 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001583 if( ret != 0 )
1584 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001585 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001586
1587exit:
1588 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Hanno Becker21f55672019-02-15 15:27:59 +00001591 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592}
1593
1594/*
1595 * Parse one X.509 certificate in DER format from a buffer and add them to a
1596 * chained list
1597 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001598static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1599 const unsigned char *buf,
1600 size_t buflen,
1601 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602{
1603 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605
1606 /*
1607 * Check for valid input
1608 */
1609 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
Hanno Becker371e0e42019-02-25 18:08:59 +00001612 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613 {
1614 prev = crt;
1615 crt = crt->next;
1616 }
1617
1618 /*
1619 * Add new certificate on the end of the chain if needed.
1620 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001621 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001623 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624
1625 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001626 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627
1628 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 }
1632
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001633 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634 {
1635 if( prev )
1636 prev->next = NULL;
1637
1638 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640
1641 return( ret );
1642 }
1643
1644 return( 0 );
1645}
1646
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001647int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1648 const unsigned char *buf,
1649 size_t buflen )
1650{
1651 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1652}
1653
1654int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1655 const unsigned char *buf,
1656 size_t buflen )
1657{
1658 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1659}
1660
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001662 * Parse one or more PEM certificates from a buffer and add them to the chained
1663 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001665int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1666 const unsigned char *buf,
1667 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668{
Janos Follath98e28a72016-05-31 14:03:54 +01001669#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001670 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001672#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673
1674 /*
1675 * Check for valid input
1676 */
1677 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679
1680 /*
1681 * Determine buffer content. Buffer contains either one DER certificate or
1682 * one or more PEM certificates.
1683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001685 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001686 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001689 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1692 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001693#else
1694 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1695#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#if defined(MBEDTLS_PEM_PARSE_C)
1698 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001699 {
1700 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001703 /* 1 rather than 0 since the terminating NULL byte is counted in */
1704 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705 {
1706 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001708
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001709 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711 "-----BEGIN CERTIFICATE-----",
1712 "-----END CERTIFICATE-----",
1713 buf, NULL, 0, &use_len );
1714
1715 if( ret == 0 )
1716 {
1717 /*
1718 * Was PEM encoded
1719 */
1720 buflen -= use_len;
1721 buf += use_len;
1722 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001724 {
1725 return( ret );
1726 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730
1731 /*
1732 * PEM header and footer were found
1733 */
1734 buflen -= use_len;
1735 buf += use_len;
1736
1737 if( first_error == 0 )
1738 first_error = ret;
1739
Paul Bakker5a5fa922014-09-26 14:53:04 +02001740 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741 continue;
1742 }
1743 else
1744 break;
1745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749
1750 if( ret != 0 )
1751 {
1752 /*
1753 * Quit parsing on a memory error
1754 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001755 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756 return( ret );
1757
1758 if( first_error == 0 )
1759 first_error = ret;
1760
1761 total_failed++;
1762 continue;
1763 }
1764
1765 success = 1;
1766 }
1767 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768
1769 if( success )
1770 return( total_failed );
1771 else if( first_error )
1772 return( first_error );
1773 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001775#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776}
1777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779/*
1780 * Load one or more certificates and add them to the chained list
1781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001783{
1784 int ret;
1785 size_t n;
1786 unsigned char *buf;
1787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789 return( ret );
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001792
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001793 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795
1796 return( ret );
1797}
1798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001800{
1801 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001802#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803 int w_ret;
1804 WCHAR szDir[MAX_PATH];
1805 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001806 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001807 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808
Paul Bakker9af723c2014-05-01 13:03:14 +02001809 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810 HANDLE hFind;
1811
1812 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814
Paul Bakker9af723c2014-05-01 13:03:14 +02001815 memset( szDir, 0, sizeof(szDir) );
1816 memset( filename, 0, MAX_PATH );
1817 memcpy( filename, path, len );
1818 filename[len++] = '\\';
1819 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820 filename[len++] = '*';
1821
Simon B3c6b18d2016-11-03 01:11:37 +00001822 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001823 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001824 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826
1827 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001828 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001830
1831 len = MAX_PATH - len;
1832 do
1833 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001834 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001835
1836 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1837 continue;
1838
Paul Bakker9af723c2014-05-01 13:03:14 +02001839 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001840 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001841 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001842 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001843 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001844 {
1845 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1846 goto cleanup;
1847 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001850 if( w_ret < 0 )
1851 ret++;
1852 else
1853 ret += w_ret;
1854 }
1855 while( FindNextFileW( hFind, &file_data ) != 0 );
1856
Paul Bakker66d5d072014-06-17 16:39:18 +02001857 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001859
Ron Eldor36d90422017-01-09 15:09:16 +02001860cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001861 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001862#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001863 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001864 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001865 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001866 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001867 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001868 DIR *dir = opendir( path );
1869
Paul Bakker66d5d072014-06-17 16:39:18 +02001870 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001872
Ron Eldor63140682017-01-09 19:27:59 +02001873#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001874 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001875 {
1876 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001877 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001878 }
Ron Eldor63140682017-01-09 19:27:59 +02001879#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001880
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001881 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001882 {
Andres AGf9113192016-09-02 14:06:04 +01001883 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1884 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001885
Andres AGf9113192016-09-02 14:06:04 +01001886 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001887 {
Andres AGf9113192016-09-02 14:06:04 +01001888 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1889 goto cleanup;
1890 }
1891 else if( stat( entry_name, &sb ) == -1 )
1892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001894 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001895 }
1896
1897 if( !S_ISREG( sb.st_mode ) )
1898 continue;
1899
1900 // Ignore parse errors
1901 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001903 if( t_ret < 0 )
1904 ret++;
1905 else
1906 ret += t_ret;
1907 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001908
1909cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001910 closedir( dir );
1911
Ron Eldor63140682017-01-09 19:27:59 +02001912#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001913 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001915#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001916
Paul Bakkerbe089b02013-10-14 15:51:50 +02001917#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918
1919 return( ret );
1920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001922
Hanno Becker02a21932019-06-10 15:08:43 +01001923#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001924static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001926{
1927 size_t i;
1928 size_t n = *size;
1929 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001931 const char *sep = "";
1932 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001933
1934 while( cur != NULL )
1935 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001936 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001937 {
1938 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001939 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001940 }
1941
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001942 n -= cur->buf.len + sep_len;
1943 for( i = 0; i < sep_len; i++ )
1944 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001945 for( i = 0; i < cur->buf.len; i++ )
1946 *p++ = cur->buf.p[i];
1947
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001948 sep = ", ";
1949 sep_len = 2;
1950
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001951 cur = cur->next;
1952 }
1953
1954 *p = '\0';
1955
1956 *size = n;
1957 *buf = p;
1958
1959 return( 0 );
1960}
1961
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001962#define PRINT_ITEM(i) \
1963 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001965 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001966 sep = ", "; \
1967 }
1968
1969#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001970 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001971 PRINT_ITEM( name );
1972
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001973static int x509_info_cert_type( char **buf, size_t *size,
1974 unsigned char ns_cert_type )
1975{
1976 int ret;
1977 size_t n = *size;
1978 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001979 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001982 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1984 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1985 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001986 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1987 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1988 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001989
1990 *size = n;
1991 *buf = p;
1992
1993 return( 0 );
1994}
1995
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001996#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001997 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001998 PRINT_ITEM( name );
1999
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002000static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002001 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002002{
2003 int ret;
2004 size_t n = *size;
2005 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002006 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2009 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002010 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2011 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2012 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2014 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002015 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2016 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002017
2018 *size = n;
2019 *buf = p;
2020
2021 return( 0 );
2022}
2023
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002024static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002026{
2027 int ret;
2028 const char *desc;
2029 size_t n = *size;
2030 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002032 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002033
2034 while( cur != NULL )
2035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002037 desc = "???";
2038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002040 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002041
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002042 sep = ", ";
2043
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002044 cur = cur->next;
2045 }
2046
2047 *size = n;
2048 *buf = p;
2049
2050 return( 0 );
2051}
2052
Hanno Becker4f869ed2019-02-24 16:47:57 +00002053typedef struct mbedtls_x509_crt_sig_info
2054{
2055 mbedtls_md_type_t sig_md;
2056 mbedtls_pk_type_t sig_pk;
2057 void *sig_opts;
2058 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
2059 size_t crt_hash_len;
2060 mbedtls_x509_buf_raw sig;
2061 mbedtls_x509_buf_raw issuer_raw;
2062} mbedtls_x509_crt_sig_info;
2063
2064static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2065{
2066#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2067 mbedtls_free( info->sig_opts );
2068#else
2069 ((void) info);
2070#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2071}
2072
2073static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2074 mbedtls_x509_crt_sig_info *info )
2075{
2076 const mbedtls_md_info_t *md_info;
2077
2078 md_info = mbedtls_md_info_from_type( frame->sig_md );
2079 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2080 info->crt_hash ) != 0 )
2081 {
2082 /* Note: this can't happen except after an internal error */
2083 return( -1 );
2084 }
2085
2086 info->crt_hash_len = mbedtls_md_get_size( md_info );
2087
2088 /* Make sure that this function leaves the target structure
2089 * ready to be freed, regardless of success of failure. */
2090 info->sig_opts = NULL;
2091
2092#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2093 {
2094 int ret;
2095 unsigned char *alg_start = frame->sig_alg.p;
2096 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2097
2098 /* Get signature options -- currently only
2099 * necessary for RSASSA-PSS. */
2100 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2101 &info->sig_pk, &info->sig_opts );
2102 if( ret != 0 )
2103 {
2104 /* Note: this can't happen except after an internal error */
2105 return( -1 );
2106 }
2107 }
2108#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2109 info->sig_md = frame->sig_md;
2110 info->sig_pk = frame->sig_pk;
2111#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2112
2113 info->issuer_raw = frame->issuer_raw;
2114 info->sig = frame->sig;
2115 return( 0 );
2116}
2117
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118/*
2119 * Return an informational string about the certificate.
2120 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002121#define BEFORE_COLON 18
2122#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002124 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125{
2126 int ret;
2127 size_t n;
2128 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002130 mbedtls_x509_crt_frame frame;
2131 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002132
Hanno Becker5226c532019-02-27 17:38:40 +00002133 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2134 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002135 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136
2137 p = buf;
2138 n = size;
2139
Hanno Becker4f869ed2019-02-24 16:47:57 +00002140 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002141 mbedtls_pk_init( &pk );
2142
2143 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002144 {
2145 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002146 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Janos Follath98e28a72016-05-31 14:03:54 +01002147
2148 return( (int) ( size - n ) );
2149 }
2150
Hanno Becker5226c532019-02-27 17:38:40 +00002151 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002152 if( ret != 0 )
2153 {
2154 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2155 goto cleanup;
2156 }
2157
Hanno Becker5226c532019-02-27 17:38:40 +00002158 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002159 if( ret != 0 )
2160 {
2161 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2162 goto cleanup;
2163 }
2164
Hanno Becker5226c532019-02-27 17:38:40 +00002165 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002166 if( ret != 0 )
2167 {
2168 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2169 goto cleanup;
2170 }
2171
Hanno Becker5226c532019-02-27 17:38:40 +00002172 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002173 if( ret != 0 )
2174 {
2175 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2176 goto cleanup;
2177 }
2178
Hanno Becker5226c532019-02-27 17:38:40 +00002179 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002180 if( ret != 0 )
2181 {
2182 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2183 goto cleanup;
2184 }
2185
Hanno Becker5226c532019-02-27 17:38:40 +00002186 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2187 if( ret != 0 )
2188 {
2189 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2190 goto cleanup;
2191 }
2192
2193 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002194 if( ret != 0 )
2195 {
2196 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2197 goto cleanup;
2198 }
2199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002201 prefix, frame.version );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002202 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002203
Hanno Becker4f869ed2019-02-24 16:47:57 +00002204 {
2205 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002206 serial.p = frame.serial.p;
2207 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002208 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2209 prefix );
2210 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
2211 ret = mbedtls_x509_serial_gets( p, n, &serial );
2212 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
2213 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002216 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Hanno Becker5226c532019-02-27 17:38:40 +00002217 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002218 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002221 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Hanno Becker5226c532019-02-27 17:38:40 +00002222 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002223 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002226 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002227 frame.valid_from.year, frame.valid_from.mon,
2228 frame.valid_from.day, frame.valid_from.hour,
2229 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002230 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002233 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002234 frame.valid_to.year, frame.valid_to.mon,
2235 frame.valid_to.day, frame.valid_to.hour,
2236 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002237 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002240 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002241
Hanno Becker83cd8672019-02-21 17:13:46 +00002242 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2243 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002244 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002245
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002246 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002248 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002249 {
2250 return( ret );
2251 }
2252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002254 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002255 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002256
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002257 /*
2258 * Optional extensions
2259 */
2260
Hanno Becker5226c532019-02-27 17:38:40 +00002261 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002264 frame.ca_istrue ? "true" : "false" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002265 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002266
Hanno Becker5226c532019-02-27 17:38:40 +00002267 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002268 {
Hanno Becker5226c532019-02-27 17:38:40 +00002269 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002270 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002271 }
2272 }
2273
Hanno Becker5226c532019-02-27 17:38:40 +00002274 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002277 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002278
2279 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002280 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002281 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002282 }
2283
Hanno Becker5226c532019-02-27 17:38:40 +00002284 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002287 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002288
Hanno Becker5226c532019-02-27 17:38:40 +00002289 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002290 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002291 }
2292
Hanno Becker5226c532019-02-27 17:38:40 +00002293 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002296 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002297
Hanno Becker5226c532019-02-27 17:38:40 +00002298 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002299 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002300 }
2301
Hanno Becker5226c532019-02-27 17:38:40 +00002302 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002305 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002306
2307 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002308 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002309 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002310 }
2311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002313 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002314
Hanno Becker4f869ed2019-02-24 16:47:57 +00002315 ret = (int) ( size - n );
2316
2317cleanup:
2318
Hanno Becker4f869ed2019-02-24 16:47:57 +00002319 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002320 mbedtls_pk_free( &pk );
2321 mbedtls_x509_name_free( issuer );
2322 mbedtls_x509_name_free( subject );
2323 mbedtls_x509_sequence_free( ext_key_usage );
2324 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002325
2326 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327}
2328
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002329struct x509_crt_verify_string {
2330 int code;
2331 const char *string;
2332};
2333
2334static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002335 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002336 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2337 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2338 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2339 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2340 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002341 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2342 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2343 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002344 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002345 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2346 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2347 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2348 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002349 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2350 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2351 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2352 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2353 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2354 { 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 +01002355 { 0, NULL }
2356};
2357
2358int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002359 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002360{
2361 int ret;
2362 const struct x509_crt_verify_string *cur;
2363 char *p = buf;
2364 size_t n = size;
2365
2366 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2367 {
2368 if( ( flags & cur->code ) == 0 )
2369 continue;
2370
2371 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002372 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002373 flags ^= cur->code;
2374 }
2375
2376 if( flags != 0 )
2377 {
2378 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2379 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002380 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002381 }
2382
2383 return( (int) ( size - n ) );
2384}
Hanno Becker02a21932019-06-10 15:08:43 +01002385#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002388static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2389 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002390{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002391 unsigned int usage_must, usage_may;
2392 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2393 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2394
2395 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2396 return( 0 );
2397
2398 usage_must = usage & ~may_mask;
2399
2400 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2401 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2402
2403 usage_may = usage & may_mask;
2404
2405 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002407
2408 return( 0 );
2409}
Hanno Becker45eedf12019-02-25 13:55:33 +00002410
2411int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2412 unsigned int usage )
2413{
2414 int ret;
2415 mbedtls_x509_crt_frame *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002416 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002417 if( ret != 0 )
2418 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2419
2420 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002421 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002422
2423 return( ret );
2424}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002425#endif
2426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002428typedef struct
2429{
2430 const char *oid;
2431 size_t oid_len;
2432} x509_crt_check_ext_key_usage_cb_ctx_t;
2433
2434static int x509_crt_check_ext_key_usage_cb( void *ctx,
2435 int tag,
2436 unsigned char *data,
2437 size_t data_len )
2438{
2439 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2440 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2441 ((void) tag);
2442
2443 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2444 data, data_len ) == 0 )
2445 {
2446 return( 1 );
2447 }
2448
2449 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2450 data_len ) == 0 )
2451 {
2452 return( 1 );
2453 }
2454
2455 return( 0 );
2456}
2457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002459 const char *usage_oid,
2460 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002461{
Hanno Beckere1956af2019-02-21 14:28:12 +00002462 int ret;
Hanno Beckere9718b42019-02-25 18:11:42 +00002463 mbedtls_x509_crt_frame *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002464 unsigned ext_types;
2465 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002466 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002467
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002468 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002469 if( ret != 0 )
2470 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2471
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002472 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002473 ext_types = frame->ext_types;
2474 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2475 {
2476 p = frame->ext_key_usage_raw.p;
2477 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002478
Hanno Beckere9718b42019-02-25 18:11:42 +00002479 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2480 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2481 x509_crt_check_ext_key_usage_cb,
2482 &cb_ctx );
2483 if( ret == 1 )
2484 ret = 0;
2485 else
2486 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2487 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002488
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002489 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002490 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002495/*
2496 * Return 1 if the certificate is revoked, or 0 otherwise.
2497 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002498static int x509_serial_is_revoked( unsigned char const *serial,
2499 size_t serial_len,
2500 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002503
2504 while( cur != NULL && cur->serial.len != 0 )
2505 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002506 if( serial_len == cur->serial.len &&
2507 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002508 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002509 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002510 return( 1 );
2511 }
2512
2513 cur = cur->next;
2514 }
2515
2516 return( 0 );
2517}
2518
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002519int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2520 const mbedtls_x509_crl *crl )
2521{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002522 int ret;
2523 mbedtls_x509_crt_frame *frame;
2524
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002525 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002526 if( ret != 0 )
2527 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2528
2529 ret = x509_serial_is_revoked( frame->serial.p,
2530 frame->serial.len,
2531 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002532 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002533 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002534}
2535
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002536/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002537 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002538 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002539 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002540static int x509_crt_verifycrl( unsigned char *crt_serial,
2541 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002542 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002543 mbedtls_x509_crl *crl_list,
2544 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002545{
Hanno Beckerbb266132019-02-25 18:12:46 +00002546 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002547 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2549 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002550 mbedtls_x509_buf_raw ca_subject;
2551 mbedtls_pk_context *pk;
2552 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002553
Hanno Beckerbb266132019-02-25 18:12:46 +00002554 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002555 return( flags );
2556
Hanno Beckerbb266132019-02-25 18:12:46 +00002557 {
2558 mbedtls_x509_crt_frame *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002559 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002560 if( ret != 0 )
2561 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2562
2563 ca_subject = ca->subject_raw;
2564
2565 can_sign = 0;
2566 if( x509_crt_check_key_usage_frame( ca,
2567 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2568 {
2569 can_sign = 1;
2570 }
2571
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002572 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002573 }
2574
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002575 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002576 if( ret != 0 )
2577 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2578
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002579 while( crl_list != NULL )
2580 {
2581 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002582 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002583 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002584 {
2585 crl_list = crl_list->next;
2586 continue;
2587 }
2588
2589 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002590 * Check if the CA is configured to sign CRLs
2591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002593 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002596 break;
2597 }
2598#endif
2599
2600 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002601 * Check if CRL is correctly signed by the trusted CA
2602 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002603 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2604 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2605
2606 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2607 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002610 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002611 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002612 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002614 break;
2615 }
2616
Hanno Beckerbb266132019-02-25 18:12:46 +00002617 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002618 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002619
Hanno Beckerbb266132019-02-25 18:12:46 +00002620 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002622 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002625 break;
2626 }
2627
2628 /*
2629 * Check for validity of CRL (Do not drop out)
2630 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002631 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002633
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002634 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002635 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002636
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002637 /*
2638 * Check if certificate is revoked
2639 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002640 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2641 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002644 break;
2645 }
2646
2647 crl_list = crl_list->next;
2648 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002649
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002650 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002651 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002654
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002655/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002656 * Check the signature of a certificate by its parent
2657 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002658static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002659 mbedtls_x509_crt *parent,
2660 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002661{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002662 int ret;
2663 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002664
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002665 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002666 if( ret != 0 )
2667 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2668
2669 /* Skip expensive computation on obvious mismatch */
2670 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002671 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002672 ret = -1;
2673 goto exit;
2674 }
2675
2676#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2677 ((void) rs_ctx);
2678#else
2679 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2680 {
2681 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002682 sig_info->sig_md,
2683 sig_info->crt_hash, sig_info->crt_hash_len,
2684 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002685 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002686 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002687 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002688#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002689 {
2690 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2691 sig_info->sig_opts,
2692 pk,
2693 sig_info->sig_md,
2694 sig_info->crt_hash, sig_info->crt_hash_len,
2695 sig_info->sig.p, sig_info->sig.len );
2696 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002697
Hanno Beckere449e2d2019-02-25 14:45:31 +00002698exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002699 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002700 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002701}
2702
2703/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002704 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2705 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002706 *
2707 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002708 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002709static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2710 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002711 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002712{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002713 int need_ca_bit;
2714
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002715 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002716 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2717 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002718 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002719 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002720 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002721 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002722
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002723 /* Parent must have the basicConstraints CA bit set as a general rule */
2724 need_ca_bit = 1;
2725
2726 /* Exception: v1/v2 certificates that are locally trusted. */
2727 if( top && parent->version < 3 )
2728 need_ca_bit = 0;
2729
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002730 if( need_ca_bit && ! parent->ca_istrue )
2731 return( -1 );
2732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002734 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002735 x509_crt_check_key_usage_frame( parent,
2736 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002737 {
2738 return( -1 );
2739 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002740#endif
2741
2742 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002743}
2744
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002745/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002746 * Find a suitable parent for child in candidates, or return NULL.
2747 *
2748 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002749 * 1. subject name matches child's issuer
2750 * 2. if necessary, the CA bit is set and key usage allows signing certs
2751 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002752 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002753 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002754 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002755 * If there's a suitable candidate which is also time-valid, return the first
2756 * such. Otherwise, return the first suitable candidate (or NULL if there is
2757 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002758 *
2759 * The rationale for this rule is that someone could have a list of trusted
2760 * roots with two versions on the same root with different validity periods.
2761 * (At least one user reported having such a list and wanted it to just work.)
2762 * The reason we don't just require time-validity is that generally there is
2763 * only one version, and if it's expired we want the flags to state that
2764 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002765 *
2766 * The rationale for rule 3 (signature for trusted roots) is that users might
2767 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002768 * way we select the correct one is by checking the signature (as we don't
2769 * rely on key identifier extensions). (This is one way users might choose to
2770 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002771 *
2772 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002773 * - [in] child: certificate for which we're looking for a parent
2774 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002775 * - [out] r_parent: parent found (or NULL)
2776 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002777 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2778 * of the chain, 0 otherwise
2779 * - [in] path_cnt: number of intermediates seen so far
2780 * - [in] self_cnt: number of self-signed intermediates seen so far
2781 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002782 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002783 *
2784 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002785 * - 0 on success
2786 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002787 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002788static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002789 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002790 mbedtls_x509_crt *candidates,
2791 mbedtls_x509_crt **r_parent,
2792 int *r_signature_is_good,
2793 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002794 unsigned path_cnt,
2795 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002796 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002797{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002798 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002799 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002800 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002801
2802#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002803 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002804 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2805 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002806 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002807 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002808 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002809 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002810
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002811 /* clear saved state */
2812 rs_ctx->parent = NULL;
2813 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002814 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002815
2816 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002817 goto check_signature;
2818 }
2819#endif
2820
2821 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002822 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002823
Hanno Becker43bf9002019-02-25 14:46:49 +00002824 for( parent_crt = candidates; parent_crt != NULL;
2825 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002826 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002827 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002828
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002829#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2830check_signature:
2831#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002832
2833 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002834 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002835 mbedtls_x509_crt_frame *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002836
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002837 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002838 if( ret != 0 )
2839 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002840
Hanno Becker43bf9002019-02-25 14:46:49 +00002841 if( mbedtls_x509_time_is_past( &parent->valid_from ) &&
2842 mbedtls_x509_time_is_future( &parent->valid_to ) )
2843 {
2844 parent_valid = 1;
2845 }
2846
2847 /* basic parenting skills (name, CA bit, key usage) */
2848 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2849 parent_match = 1;
2850
2851 /* +1 because the stored max_pathlen is 1 higher
2852 * than the actual value */
2853 if( !( parent->max_pathlen > 0 &&
2854 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2855 {
2856 path_len_ok = 1;
2857 }
2858
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002859 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002860 }
2861
2862 if( parent_match == 0 || path_len_ok == 0 )
2863 continue;
2864
2865 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002866 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002867
2868#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002869 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2870 {
2871 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002872 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002873 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002874 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002875
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002876 return( ret );
2877 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002878#else
2879 (void) ret;
2880#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002881
2882 signature_is_good = ret == 0;
2883 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002884 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002885
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002886 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002887 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002888 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002889 if( fallback_parent == NULL )
2890 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002891 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002892 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002893 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002894
2895 continue;
2896 }
2897
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002898 break;
2899 }
2900
Hanno Becker43bf9002019-02-25 14:46:49 +00002901 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002902 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002903 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002904 *r_signature_is_good = signature_is_good;
2905 }
2906 else
2907 {
2908 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002909 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002910 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002911
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002912 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002913}
2914
2915/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002916 * Find a parent in trusted CAs or the provided chain, or return NULL.
2917 *
2918 * Searches in trusted CAs first, and return the first suitable parent found
2919 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002920 *
2921 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002922 * - [in] child: certificate for which we're looking for a parent, followed
2923 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002924 * - [in] trust_ca: list of locally trusted certificates
2925 * - [out] parent: parent found (or NULL)
2926 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2927 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2928 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2929 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002930 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002931 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002932 *
2933 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002934 * - 0 on success
2935 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002936 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002937static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002938 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002939 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002940 mbedtls_x509_crt *trust_ca,
2941 mbedtls_x509_crt **parent,
2942 int *parent_is_trusted,
2943 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002944 unsigned path_cnt,
2945 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002946 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002947{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002948 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002949 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002950
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002951 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002952
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002953#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002954 /* restore then clear saved state if we have some stored */
2955 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2956 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002957 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002958 rs_ctx->parent_is_trusted = -1;
2959 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002960#endif
2961
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002962 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00002963 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002964
Hanno Becker5299cf82019-02-25 13:50:41 +00002965 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002966 parent, signature_is_good,
2967 *parent_is_trusted,
2968 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002969
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002970#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002971 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2972 {
2973 /* save state */
2974 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002975 return( ret );
2976 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002977#else
2978 (void) ret;
2979#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002980
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002981 /* stop here if found or already in second iteration */
2982 if( *parent != NULL || *parent_is_trusted == 0 )
2983 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002984
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002985 /* prepare second iteration */
2986 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002987 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002988
2989 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01002990 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002991 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002992 *parent_is_trusted = 0;
2993 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002994 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002995
2996 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002997}
2998
2999/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003000 * Check if an end-entity certificate is locally trusted
3001 *
3002 * Currently we require such certificates to be self-signed (actually only
3003 * check for self-issued as self-signatures are not checked)
3004 */
3005static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003006 mbedtls_x509_crt_frame const *crt,
3007 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003008{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003009 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003010
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003011 /* look for an exact match with trusted cert */
3012 for( cur = trust_ca; cur != NULL; cur = cur->next )
3013 {
3014 if( crt->raw.len == cur->raw.len &&
3015 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3016 {
3017 return( 0 );
3018 }
3019 }
3020
3021 /* too bad */
3022 return( -1 );
3023}
3024
3025/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003026 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003027 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003028 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3029 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003030 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003031 * such that every cert in the chain is a child of the next one,
3032 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003033 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003034 * Verify that chain and return it with flags for all issues found.
3035 *
3036 * Special cases:
3037 * - EE == Rj -> return a one-element list containing it
3038 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3039 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003040 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003041 * Tests for (aspects of) this function should include at least:
3042 * - trusted EE
3043 * - EE -> trusted root
3044 * - EE -> intermedate CA -> trusted root
3045 * - if relevant: EE untrusted
3046 * - if relevant: EE -> intermediate, untrusted
3047 * with the aspect under test checked at each relevant level (EE, int, root).
3048 * For some aspects longer chains are required, but usually length 2 is
3049 * enough (but length 1 is not in general).
3050 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003051 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003052 * - [in] crt: the cert list EE, C1, ..., Cn
3053 * - [in] trust_ca: the trusted list R1, ..., Rp
3054 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003055 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003056 * Only valid when return value is 0, may contain garbage otherwise!
3057 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003058 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003059 *
3060 * Return value:
3061 * - non-zero if the chain could not be fully built and examined
3062 * - 0 is the chain was successfully built and examined,
3063 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003064 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003065static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003066 mbedtls_x509_crt *crt,
3067 mbedtls_x509_crt *trust_ca,
3068 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003069 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003070 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003071 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003072{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003073 /* Don't initialize any of those variables here, so that the compiler can
3074 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003075 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003076 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003077 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003078 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003079 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003080 int parent_is_trusted;
3081 int child_is_trusted;
3082 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003083 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003084
3085#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3086 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003087 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003088 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003089 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003090 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003091 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003092
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003093 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003094 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003095 child_crt = cur->crt;
3096
3097 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003098 goto find_parent;
3099 }
3100#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003101
Hanno Becker1e0677a2019-02-25 14:58:22 +00003102 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003103 self_cnt = 0;
3104 parent_is_trusted = 0;
3105 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003106
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003107 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003108#if defined(MBEDTLS_X509_CRL_PARSE_C)
3109 mbedtls_x509_buf_raw child_serial;
3110#endif /* MBEDTLS_X509_CRL_PARSE_C */
3111 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003112
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003113 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003114 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003115 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003116 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003117 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003118
3119#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3120find_parent:
3121#endif
3122
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003123 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003124
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003125 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003126 mbedtls_x509_crt_sig_info child_sig;
3127 {
3128 mbedtls_x509_crt_frame *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003129
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003130 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003131 if( ret != 0 )
3132 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3133
3134 /* Check time-validity (all certificates) */
3135 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3136 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3137 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3138 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
3139
3140 /* Stop here for trusted roots (but not for trusted EE certs) */
3141 if( child_is_trusted )
3142 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003143 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003144 return( 0 );
3145 }
3146
3147 self_issued = 0;
3148 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3149 &child->subject_raw,
3150 NULL, NULL ) == 0 )
3151 {
3152 self_issued = 1;
3153 }
3154
3155 /* Check signature algorithm: MD & PK algs */
3156 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3157 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3158
3159 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3160 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3161
3162 /* Special case: EE certs that are locally trusted */
3163 if( ver_chain->len == 1 && self_issued &&
3164 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3165 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003166 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003167 return( 0 );
3168 }
3169
3170#if defined(MBEDTLS_X509_CRL_PARSE_C)
3171 child_serial = child->serial;
3172#endif /* MBEDTLS_X509_CRL_PARSE_C */
3173
3174 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003175 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003176
Hanno Becker5299cf82019-02-25 13:50:41 +00003177 if( ret != 0 )
3178 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3179 }
3180
3181 /* Look for a parent in trusted CAs or up the chain */
3182 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003183 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003184 &parent_is_trusted, &signature_is_good,
3185 ver_chain->len - 1, self_cnt, rs_ctx );
3186
3187 x509_crt_free_sig_info( &child_sig );
3188 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003189
3190#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003191 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3192 {
3193 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003194 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003195 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003196 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003197 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003198 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003199#else
3200 (void) ret;
3201#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003202
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003203 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003204 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003205 {
3206 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003207 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003208 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003209
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003210 /* Count intermediate self-issued (not necessarily self-signed) certs.
3211 * These can occur with some strategies for key rollover, see [SIRO],
3212 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003213 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003214 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003215
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003216 /* path_cnt is 0 for the first intermediate CA,
3217 * and if parent is trusted it's not an intermediate CA */
3218 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003219 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003220 {
3221 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003222 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003223 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003224
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003225 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003226 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003227 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3228
Hanno Becker58c35642019-02-25 18:13:46 +00003229 {
3230 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003231 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003232 if( ret != 0 )
3233 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3234
3235 /* check size of signing key */
3236 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3237 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3238
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003239 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003240 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003243 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003244 *flags |= x509_crt_verifycrl( child_serial.p,
3245 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003246 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003247#else
3248 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003249#endif
3250
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003251 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003252 child_crt = parent_crt;
3253 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003254 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003255 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003256 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003257}
3258
3259/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003260 * Check for CN match
3261 */
Hanno Becker24926222019-02-21 13:10:55 +00003262static int x509_crt_check_cn( unsigned char const *buf,
3263 size_t buflen,
3264 const char *cn,
3265 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003266{
Hanno Becker24926222019-02-21 13:10:55 +00003267 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003268 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003269 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003270
3271 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003272 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003273 {
3274 return( 0 );
3275 }
3276
3277 return( -1 );
3278}
3279
Hanno Becker8b543b32019-02-21 11:50:44 +00003280/* Returns 1 on a match and 0 on a mismatch.
3281 * This is because this function is used as a callback for
3282 * mbedtls_x509_name_cmp_raw(), which continues the name
3283 * traversal as long as the callback returns 0. */
3284static int x509_crt_check_name( void *ctx,
3285 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003286 mbedtls_x509_buf *val,
3287 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003288{
3289 char const *cn = (char const*) ctx;
3290 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003291 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003292
3293 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003294 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003295 {
3296 return( 1 );
3297 }
3298
3299 return( 0 );
3300}
3301
Hanno Beckerda410822019-02-21 13:36:59 +00003302/* Returns 1 on a match and 0 on a mismatch.
3303 * This is because this function is used as a callback for
3304 * mbedtls_asn1_traverse_sequence_of(), which continues the
3305 * traversal as long as the callback returns 0. */
3306static int x509_crt_subject_alt_check_name( void *ctx,
3307 int tag,
3308 unsigned char *data,
3309 size_t data_len )
3310{
3311 char const *cn = (char const*) ctx;
3312 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003313 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003314
3315 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3316 return( 1 );
3317
3318 return( 0 );
3319}
3320
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003321/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003322 * Verify the requested CN - only call this if cn is not NULL!
3323 */
Hanno Becker082435c2019-02-25 18:14:40 +00003324static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3325 const char *cn,
3326 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003327{
Hanno Beckerda410822019-02-21 13:36:59 +00003328 int ret;
Hanno Becker082435c2019-02-25 18:14:40 +00003329 mbedtls_x509_crt_frame *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003330
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003331 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003332 if( ret != 0 )
3333 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3334
3335 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003336 {
Hanno Becker90b94082019-02-21 21:13:21 +00003337 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003338 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003339 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003340 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003341
Hanno Becker90b94082019-02-21 21:13:21 +00003342 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3343 MBEDTLS_ASN1_TAG_CLASS_MASK,
3344 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3345 MBEDTLS_ASN1_TAG_VALUE_MASK,
3346 2 /* SubjectAlt DNS */,
3347 x509_crt_subject_alt_check_name,
3348 (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003349 }
3350 else
3351 {
Hanno Becker082435c2019-02-25 18:14:40 +00003352 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3353 &frame->subject_raw,
Hanno Becker8b543b32019-02-21 11:50:44 +00003354 x509_crt_check_name, (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003355 }
Hanno Beckerda410822019-02-21 13:36:59 +00003356
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003357 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003358
3359 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3360 * return 1 when finding a name component matching `cn`. */
3361 if( ret == 1 )
3362 return( 0 );
3363
3364 if( ret != 0 )
3365 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3366
3367 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3368 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003369}
3370
3371/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003372 * Merge the flags for all certs in the chain, after calling callback
3373 */
3374static int x509_crt_merge_flags_with_cb(
3375 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003376 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003377 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3378 void *p_vrfy )
3379{
3380 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003381 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003382 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003383 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003384
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003385 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003386 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003387 cur = &ver_chain->items[i-1];
3388 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003389
3390 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003391 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003392 return( ret );
3393
3394 *flags |= cur_flags;
3395 }
3396
3397 return( 0 );
3398}
3399
3400/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003401 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3404 mbedtls_x509_crt *trust_ca,
3405 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003406 const char *cn, uint32_t *flags,
3407 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003408 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003409{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003410 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3411 &mbedtls_x509_crt_profile_default, cn, flags,
3412 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003413}
3414
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003415/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003416 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003417 */
3418int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3419 mbedtls_x509_crt *trust_ca,
3420 mbedtls_x509_crl *ca_crl,
3421 const mbedtls_x509_crt_profile *profile,
3422 const char *cn, uint32_t *flags,
3423 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3424 void *p_vrfy )
3425{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003426 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3427 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3428}
3429
3430/*
3431 * Verify the certificate validity, with profile, restartable version
3432 *
3433 * This function:
3434 * - checks the requested CN (if any)
3435 * - checks the type and size of the EE cert's key,
3436 * as that isn't done as part of chain building/verification currently
3437 * - builds and verifies the chain
3438 * - then calls the callback and merges the flags
3439 */
3440int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3441 mbedtls_x509_crt *trust_ca,
3442 mbedtls_x509_crl *ca_crl,
3443 const mbedtls_x509_crt_profile *profile,
3444 const char *cn, uint32_t *flags,
3445 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3446 void *p_vrfy,
3447 mbedtls_x509_crt_restart_ctx *rs_ctx )
3448{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003449 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003450 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003451 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003452
3453 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003454 ee_flags = 0;
3455 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003456
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003457 if( profile == NULL )
3458 {
3459 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3460 goto exit;
3461 }
3462
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003463 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003464 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003465 {
3466 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3467 if( ret != 0 )
3468 return( ret );
3469 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003470
Hanno Becker87233362019-02-25 18:15:33 +00003471 {
3472 mbedtls_pk_context *pk;
3473 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003474
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003475 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003476 if( ret != 0 )
3477 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003478
Hanno Becker87233362019-02-25 18:15:33 +00003479 /* Check the type and size of the key */
3480 pk_type = mbedtls_pk_get_type( pk );
3481
3482 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3483 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3484
3485 if( x509_profile_check_key( profile, pk ) != 0 )
3486 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3487
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003488 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003489 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003490
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003491 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003492 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003493 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003494
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003495 if( ret != 0 )
3496 goto exit;
3497
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003498 /* Merge end-entity flags */
3499 ver_chain.items[0].flags |= ee_flags;
3500
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003501 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003502 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003503
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003504exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003505#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3506 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3507 mbedtls_x509_crt_restart_free( rs_ctx );
3508#endif
3509
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003510 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3511 * the SSL module for authmode optional, but non-zero return from the
3512 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003513 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3514 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3515
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003516 if( ret != 0 )
3517 {
3518 *flags = (uint32_t) -1;
3519 return( ret );
3520 }
3521
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003522 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003524
3525 return( 0 );
3526}
3527
3528/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003529 * Initialize a certificate chain
3530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003534}
3535
3536/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003537 * Unallocate all certificate data
3538 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 mbedtls_x509_crt *cert_cur = crt;
3543 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003544
3545 if( crt == NULL )
3546 return;
3547
3548 do
3549 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003550 x509_crt_cache_free( cert_cur->cache );
3551 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003552
3553#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3557 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003558#endif
3559
Hanno Becker2bcc7642019-02-26 19:01:00 +00003560 mbedtls_x509_name_free( cert_cur->issuer.next );
3561 mbedtls_x509_name_free( cert_cur->subject.next );
3562 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3563 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003564#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003565
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003566 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003567 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003568 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003570 }
3571
3572 cert_cur = cert_cur->next;
3573 }
3574 while( cert_cur != NULL );
3575
3576 cert_cur = crt;
3577 do
3578 {
3579 cert_prv = cert_cur;
3580 cert_cur = cert_cur->next;
3581
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003582 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003583 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003585 }
3586 while( cert_cur != NULL );
3587}
3588
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003589#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3590/*
3591 * Initialize a restart context
3592 */
3593void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3594{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003595 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003596
3597 ctx->parent = NULL;
3598 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003599 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003600
3601 ctx->parent_is_trusted = -1;
3602
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003603 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003604 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003605 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003606}
3607
3608/*
3609 * Free the components of a restart context
3610 */
3611void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3612{
3613 if( ctx == NULL )
3614 return;
3615
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003616 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003617 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003618}
3619#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621#endif /* MBEDTLS_X509_CRT_PARSE_C */