blob: 9004be4f0b3c62207bca307d9a50121b9805e4a9 [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
Hanno Becker76428352019-03-05 15:29:23 +0000168 if( cache->frame != NULL )
169 return( 0 );
170
Hanno Becker337088a2019-02-25 14:53:14 +0000171 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
172 if( frame == NULL )
173 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000174 cache->frame = frame;
Hanno Becker337088a2019-02-25 14:53:14 +0000175
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000176#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
177 /* This would work with !MBEDTLS_X509_ON_DEMAND_PARSING, too,
178 * but is inefficient compared to copying the respective fields
179 * from the legacy mbedtls_x509_crt. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000180 return( x509_crt_parse_frame( crt->raw.p,
181 crt->raw.p + crt->raw.len,
182 frame ) );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000183#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
184 /* Make sure all extension related fields are properly initialized. */
185 frame->ca_istrue = 0;
186 frame->max_pathlen = 0;
187 frame->ext_types = 0;
188 frame->version = crt->version;
189 frame->sig_md = crt->sig_md;
190 frame->sig_pk = crt->sig_pk;
191 frame->valid_from = crt->valid_from;
192 frame->valid_to = crt->valid_to;
Hanno Becker38f0cb42019-03-04 15:13:45 +0000193 x509_buf_to_buf_raw( &frame->raw, &crt->raw );
194 x509_buf_to_buf_raw( &frame->tbs, &crt->tbs );
195 x509_buf_to_buf_raw( &frame->serial, &crt->serial );
196 x509_buf_to_buf_raw( &frame->pubkey_raw, &crt->pk_raw );
197 x509_buf_to_buf_raw( &frame->issuer_raw, &crt->issuer_raw );
198 x509_buf_to_buf_raw( &frame->subject_raw, &crt->subject_raw );
199 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
200 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
201 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
202 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000203
204 /* The legacy CRT structure doesn't explicitly contain
205 * the `AlgorithmIdentifier` bounds; however, those can
206 * be inferred from the surrounding (mandatory) `SerialNumber`
207 * and `Issuer` fields. */
208 frame->sig_alg.p = crt->serial.p + crt->serial.len;
209 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
210
211 return( x509_crt_frame_parse_ext( frame ) );
212#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000213}
Hanno Becker337088a2019-02-25 14:53:14 +0000214
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000215int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
216{
217 mbedtls_x509_crt_cache *cache = crt->cache;
218 mbedtls_pk_context *pk;
219
Hanno Becker76428352019-03-05 15:29:23 +0000220 if( cache->pk != NULL )
221 return( 0 );
222
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000223 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
224 if( pk == NULL )
225 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000226 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000227
228#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
229 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000230 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000231#else
232 {
233 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
234 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
235 pk_raw.p + pk_raw.len,
236 pk ) );
237 }
238#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000239}
240
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000241static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000242{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000243 memset( cache, 0, sizeof( *cache ) );
244#if defined(MBEDTLS_THREADING_C)
245 mbedtls_mutex_init( &cache->frame_mutex );
246 mbedtls_mutex_init( &cache->pk_mutex );
247#endif
248}
249
250static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
251{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000252#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000253 /* The cache holds a shallow copy of the PK context
254 * in the legacy struct, so don't free PK context. */
255 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000256#else
257 mbedtls_pk_free( cache->pk );
258 mbedtls_free( cache->pk );
259#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
260
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000261 cache->pk = NULL;
262}
263
264static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
265{
266 mbedtls_free( cache->frame );
267 cache->frame = NULL;
268}
269
270static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
271{
272 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000273 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000274
275#if defined(MBEDTLS_THREADING_C)
276 mbedtls_mutex_free( &cache->frame_mutex );
277 mbedtls_mutex_free( &cache->pk_mutex );
278#endif
279
280 x509_crt_cache_clear_frame( cache );
281 x509_crt_cache_clear_pk( cache );
282
283 memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000284}
285
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000286int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
287 mbedtls_x509_sequence **subj_alt )
288{
289 int ret;
290 mbedtls_x509_crt_frame *frame;
291 mbedtls_x509_sequence *seq;
292
293 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
294 if( ret != 0 )
295 return( ret );
296
297 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
298 if( seq == NULL )
299 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
300 else
301 ret = x509_crt_subject_alt_from_frame( frame, seq );
302
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000303 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000304
305 *subj_alt = seq;
306 return( ret );
307}
308
309int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
310 mbedtls_x509_sequence **ext_key_usage )
311{
312 int ret;
313 mbedtls_x509_crt_frame *frame;
314 mbedtls_x509_sequence *seq;
315
316 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
317 if( ret != 0 )
318 return( ret );
319
320 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
321 if( seq == NULL )
322 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
323 else
324 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
325
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000326 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000327
328 *ext_key_usage = seq;
329 return( ret );
330}
331
Hanno Becker63e69982019-02-26 18:50:49 +0000332int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
333 mbedtls_x509_name **subject )
334{
335 int ret;
336 mbedtls_x509_crt_frame *frame;
337 mbedtls_x509_name *name;
338
339 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
340 if( ret != 0 )
341 return( ret );
342
343 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
344 if( name == NULL )
345 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
346 else
347 ret = x509_crt_subject_from_frame( frame, name );
348
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000349 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000350
351 *subject = name;
352 return( ret );
353}
354
355int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
356 mbedtls_x509_name **issuer )
357{
358 int ret;
359 mbedtls_x509_crt_frame *frame;
360 mbedtls_x509_name *name;
361
362 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
363 if( ret != 0 )
364 return( ret );
365
366 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
367 if( name == NULL )
368 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
369 else
370 ret = x509_crt_issuer_from_frame( frame, name );
371
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000372 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000373
374 *issuer = name;
375 return( ret );
376}
377
Hanno Becker823efad2019-02-28 13:23:58 +0000378int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
379 mbedtls_x509_crt_frame *dst )
380{
381 int ret;
382 mbedtls_x509_crt_frame *frame;
383 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
384 if( ret != 0 )
385 return( ret );
386 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000387 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000388 return( 0 );
389}
390
391int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
392 mbedtls_pk_context *dst )
393{
394#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
395 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
396 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
397 pk_raw.p + pk_raw.len,
398 dst ) );
399#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
400 int ret;
401 mbedtls_pk_context *pk;
402 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
403 if( ret != 0 )
404 return( ret );
405
406 /* Move PK from CRT cache to destination pointer
407 * to avoid a copy. */
408 *dst = *pk;
409 mbedtls_free( crt->cache->pk );
410 crt->cache->pk = NULL;
411
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000412 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000413 return( 0 );
414#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
415}
416
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200417/*
418 * Item in a verification chain: cert and flags for it
419 */
420typedef struct {
421 mbedtls_x509_crt *crt;
422 uint32_t flags;
423} x509_crt_verify_chain_item;
424
425/*
426 * Max size of verification chain: end-entity + intermediates + trusted root
427 */
428#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200429
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200431 * Default profile
432 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200433const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
434{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200435#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200436 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200437 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200438#endif
439 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200440 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
441 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
442 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
443 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
444 0xFFFFFFF, /* Any PK alg */
445 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200446 2048,
447};
448
449/*
450 * Next-default profile
451 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200452const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
453{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200454 /* Hashes from SHA-256 and above */
455 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
456 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
457 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
458 0xFFFFFFF, /* Any PK alg */
459#if defined(MBEDTLS_ECP_C)
460 /* Curves at or above 128-bit security level */
461 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
462 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
463 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
464 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
465 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
466 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
467 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
468#else
469 0,
470#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200471 2048,
472};
473
474/*
475 * NSA Suite B Profile
476 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200477const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
478{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200479 /* Only SHA-256 and 384 */
480 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
481 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
482 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200483 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
484 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200485#if defined(MBEDTLS_ECP_C)
486 /* Only NIST P-256 and P-384 */
487 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
488 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
489#else
490 0,
491#endif
492 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200493};
494
495/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200496 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200497 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200498 */
499static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
500 mbedtls_md_type_t md_alg )
501{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200502 if( md_alg == MBEDTLS_MD_NONE )
503 return( -1 );
504
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200505 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
506 return( 0 );
507
508 return( -1 );
509}
510
511/*
512 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200513 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200514 */
515static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
516 mbedtls_pk_type_t pk_alg )
517{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200518 if( pk_alg == MBEDTLS_PK_NONE )
519 return( -1 );
520
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200521 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
522 return( 0 );
523
524 return( -1 );
525}
526
527/*
528 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200529 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200530 */
531static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200532 const mbedtls_pk_context *pk )
533{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200534 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200535
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200536#if defined(MBEDTLS_RSA_C)
537 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
538 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200539 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200540 return( 0 );
541
542 return( -1 );
543 }
544#endif
545
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200546#if defined(MBEDTLS_ECP_C)
547 if( pk_alg == MBEDTLS_PK_ECDSA ||
548 pk_alg == MBEDTLS_PK_ECKEY ||
549 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200550 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200551 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200552
Philippe Antoineb5b25432018-05-11 11:06:29 +0200553 if( gid == MBEDTLS_ECP_DP_NONE )
554 return( -1 );
555
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200556 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
557 return( 0 );
558
559 return( -1 );
560 }
561#endif
562
563 return( -1 );
564}
565
566/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000567 * Return 0 if name matches wildcard, -1 otherwise
568 */
Hanno Becker24926222019-02-21 13:10:55 +0000569static int x509_check_wildcard( char const *cn,
570 size_t cn_len,
571 unsigned char const *buf,
572 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000573{
574 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000575 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000576
577 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000578 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000579 return( -1 );
580
581 for( i = 0; i < cn_len; ++i )
582 {
583 if( cn[i] == '.' )
584 {
585 cn_idx = i;
586 break;
587 }
588 }
589
590 if( cn_idx == 0 )
591 return( -1 );
592
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000593 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
594 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000595 {
596 return( 0 );
597 }
598
599 return( -1 );
600}
601
602/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200603 * Reset (init or clear) a verify_chain
604 */
605static void x509_crt_verify_chain_reset(
606 mbedtls_x509_crt_verify_chain *ver_chain )
607{
608 size_t i;
609
610 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
611 {
612 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000613 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200614 }
615
616 ver_chain->len = 0;
617}
618
619/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
621 */
622static int x509_get_version( unsigned char **p,
623 const unsigned char *end,
624 int *ver )
625{
626 int ret;
627 size_t len;
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
630 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633 {
634 *ver = 0;
635 return( 0 );
636 }
637
Hanno Becker2f472142019-02-12 11:52:10 +0000638 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 }
640
641 end = *p + len;
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
644 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645
646 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_X509_INVALID_VERSION +
648 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649
650 return( 0 );
651}
652
653/*
654 * Validity ::= SEQUENCE {
655 * notBefore Time,
656 * notAfter Time }
657 */
658static int x509_get_dates( unsigned char **p,
659 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_x509_time *from,
661 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662{
663 int ret;
664 size_t len;
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
667 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
668 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200669
670 end = *p + len;
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 return( ret );
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 return( ret );
677
678 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 return( MBEDTLS_ERR_X509_INVALID_DATE +
680 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682 return( 0 );
683}
684
685/*
686 * X.509 v2/v3 unique identifier (not parsed)
687 */
688static int x509_get_uid( unsigned char **p,
689 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691{
692 int ret;
693
694 if( *p == end )
695 return( 0 );
696
697 uid->tag = **p;
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
700 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 return( 0 );
704
Hanno Becker2f472142019-02-12 11:52:10 +0000705 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 }
707
708 uid->p = *p;
709 *p += uid->len;
710
711 return( 0 );
712}
713
714static int x509_get_basic_constraints( unsigned char **p,
715 const unsigned char *end,
716 int *ca_istrue,
717 int *max_pathlen )
718{
719 int ret;
720 size_t len;
721
722 /*
723 * BasicConstraints ::= SEQUENCE {
724 * cA BOOLEAN DEFAULT FALSE,
725 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
726 */
727 *ca_istrue = 0; /* DEFAULT FALSE */
728 *max_pathlen = 0; /* endless */
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
731 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000732 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733
734 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200735 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
740 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741
742 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000743 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744
745 if( *ca_istrue != 0 )
746 *ca_istrue = 1;
747 }
748
749 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200750 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000753 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754
755 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000756 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757
758 (*max_pathlen)++;
759
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200760 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761}
762
763static int x509_get_ns_cert_type( unsigned char **p,
764 const unsigned char *end,
765 unsigned char *ns_cert_type)
766{
767 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000771 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772
773 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000774 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775
776 /* Get actual bitstring */
777 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200778 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779}
780
781static int x509_get_key_usage( unsigned char **p,
782 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100783 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784{
785 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200786 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000790 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791
792 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000793 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794
795 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200796 *key_usage = 0;
797 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
798 {
799 *key_usage |= (unsigned int) bs.p[i] << (8*i);
800 }
801
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200802 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803}
804
805/*
806 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
807 *
808 * KeyPurposeId ::= OBJECT IDENTIFIER
809 */
810static int x509_get_ext_key_usage( unsigned char **p,
811 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000814 return( mbedtls_asn1_get_sequence_of( p, end, ext_key_usage,
815 MBEDTLS_ASN1_OID ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816}
817
818/*
819 * SubjectAltName ::= GeneralNames
820 *
821 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
822 *
823 * GeneralName ::= CHOICE {
824 * otherName [0] OtherName,
825 * rfc822Name [1] IA5String,
826 * dNSName [2] IA5String,
827 * x400Address [3] ORAddress,
828 * directoryName [4] Name,
829 * ediPartyName [5] EDIPartyName,
830 * uniformResourceIdentifier [6] IA5String,
831 * iPAddress [7] OCTET STRING,
832 * registeredID [8] OBJECT IDENTIFIER }
833 *
834 * OtherName ::= SEQUENCE {
835 * type-id OBJECT IDENTIFIER,
836 * value [0] EXPLICIT ANY DEFINED BY type-id }
837 *
838 * EDIPartyName ::= SEQUENCE {
839 * nameAssigner [0] DirectoryString OPTIONAL,
840 * partyName [1] DirectoryString }
841 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000842 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843 */
Hanno Beckerad462192019-02-21 13:32:31 +0000844static int x509_get_subject_alt_name_cb( void *ctx,
845 int tag,
846 unsigned char *data,
847 size_t data_len )
848{
849 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
850 mbedtls_asn1_sequence *cur = *cur_ptr;
851
Hanno Beckerad462192019-02-21 13:32:31 +0000852 /* Allocate and assign next pointer */
853 if( cur->buf.p != NULL )
854 {
855 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
856 if( cur->next == NULL )
Hanno Becker90b94082019-02-21 21:13:21 +0000857 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Hanno Beckerad462192019-02-21 13:32:31 +0000858 cur = cur->next;
859 }
860
861 cur->buf.tag = tag;
862 cur->buf.p = data;
863 cur->buf.len = data_len;
864
865 *cur_ptr = cur;
866 return( 0 );
867}
868
Hanno Becker5984d302019-02-21 14:46:54 +0000869static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000873 return( mbedtls_asn1_traverse_sequence_of( &p, end,
874 MBEDTLS_ASN1_TAG_CLASS_MASK,
875 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
876 MBEDTLS_ASN1_TAG_VALUE_MASK,
877 2 /* SubjectAlt DNS */,
878 x509_get_subject_alt_name_cb,
879 (void*) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880}
881
882/*
883 * X.509 v3 extensions
884 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000886static int x509_crt_get_ext_cb( void *ctx,
887 int tag,
888 unsigned char *p,
889 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890{
891 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000892 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000894 unsigned char *end, *end_ext_octet;
895 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
896 int is_critical = 0; /* DEFAULT FALSE */
897 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000899 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000900
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000901 /*
902 * Extension ::= SEQUENCE {
903 * extnID OBJECT IDENTIFIER,
904 * critical BOOLEAN DEFAULT FALSE,
905 * extnValue OCTET STRING }
906 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000908 end = p + ext_len;
909
910 /* Get extension ID */
911 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
912 MBEDTLS_ASN1_OID ) ) != 0 )
913 goto err;
914
915 extn_oid.tag = MBEDTLS_ASN1_OID;
916 extn_oid.p = p;
917 p += extn_oid.len;
918
919 /* Get optional critical */
920 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
921 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
922 goto err;
923
924 /* Data should be octet string type */
925 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
926 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
927 goto err;
928
929 end_ext_octet = p + len;
930 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000932 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
933 goto err;
934 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000936 /*
937 * Detect supported extensions
938 */
939 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
940 if( ret != 0 )
941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000943 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000945 /* Data is marked as critical: fail */
946 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
947 goto err;
948 }
949#endif
950 return( 0 );
951 }
952
953 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +0000954 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000955 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
956
Hanno Becker21f55672019-02-15 15:27:59 +0000957 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000958 switch( ext_type )
959 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100960 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000961 {
962 int ca_istrue;
963 int max_pathlen;
964
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000966 ret = x509_get_basic_constraints( &p, end_ext_octet,
967 &ca_istrue,
968 &max_pathlen );
969 if( ret != 0 )
970 goto err;
971
Hanno Becker21f55672019-02-15 15:27:59 +0000972 frame->ca_istrue = ca_istrue;
973 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000975 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000979 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +0000980 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000981 if( ret != 0 )
982 goto err;
983 break;
984
985 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
986 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +0000987 frame->subject_alt_raw.p = p;
988 frame->subject_alt_raw.len = end_ext_octet - p;
989
990 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
991 MBEDTLS_ASN1_TAG_CLASS_MASK,
992 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
993 MBEDTLS_ASN1_TAG_VALUE_MASK,
994 2 /* SubjectAlt DNS */,
995 NULL, NULL );
996 if( ret != 0 )
997 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 break;
999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001002 frame->ext_key_usage_raw.p = p;
1003 frame->ext_key_usage_raw.len = end_ext_octet - p;
1004 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001005 {
Hanno Becker21f55672019-02-15 15:27:59 +00001006 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1007 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001008 }
Hanno Becker21f55672019-02-15 15:27:59 +00001009
1010 /* Check structural sanity of extension. */
1011 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1012 0xFF, MBEDTLS_ASN1_OID,
1013 0, 0, NULL, NULL );
1014 if( ret != 0 )
1015 goto err;
1016
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 break;
1018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001021 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001022 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001023 if( ret != 0 )
1024 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 break;
1026
1027 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001028 /*
1029 * If this is a non-critical extension, which the oid layer
1030 * supports, but there isn't an X.509 parser for it,
1031 * skip the extension.
1032 */
1033#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1034 if( is_critical )
1035 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1036#endif
1037 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 }
1039
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001041
1042err:
1043 return( ret );
1044}
1045
Hanno Becker21f55672019-02-15 15:27:59 +00001046static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001047{
1048 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001049 unsigned char *p = frame->v3_ext.p;
1050 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001051
Hanno Becker21f55672019-02-15 15:27:59 +00001052 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001053 return( 0 );
1054
Hanno Becker21f55672019-02-15 15:27:59 +00001055 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001056 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001057 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001058
1059 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1060 return( ret );
1061 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1062 return( ret );
1063
1064 if( ret != 0 )
1065 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1066
1067 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068}
1069
Hanno Becker21f55672019-02-15 15:27:59 +00001070static int x509_crt_parse_frame( unsigned char *start,
1071 unsigned char *end,
1072 mbedtls_x509_crt_frame *frame )
1073{
1074 int ret;
1075 unsigned char *p;
1076 size_t len;
1077
1078 mbedtls_x509_buf tmp;
1079 unsigned char *tbs_start;
1080
1081 mbedtls_x509_buf outer_sig_alg;
1082 size_t inner_sig_alg_len;
1083 unsigned char *inner_sig_alg_start;
1084
1085 memset( frame, 0, sizeof( *frame ) );
1086
1087 /*
1088 * Certificate ::= SEQUENCE {
1089 * tbsCertificate TBSCertificate,
1090 * signatureAlgorithm AlgorithmIdentifier,
1091 * signatureValue BIT STRING
1092 * }
1093 *
1094 */
1095 p = start;
1096
1097 frame->raw.p = p;
1098 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1099 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1100 {
1101 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1102 }
1103
1104 /* NOTE: We are currently not checking that the `Certificate`
1105 * structure spans the entire buffer. */
1106 end = p + len;
1107 frame->raw.len = end - frame->raw.p;
1108
1109 /*
1110 * TBSCertificate ::= SEQUENCE { ...
1111 */
1112 frame->tbs.p = p;
1113 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1114 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1115 {
1116 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1117 }
1118 tbs_start = p;
1119
1120 /* Breadth-first parsing: Jump over TBS for now. */
1121 p += len;
1122 frame->tbs.len = p - frame->tbs.p;
1123
1124 /*
1125 * AlgorithmIdentifier ::= SEQUENCE { ...
1126 */
1127 outer_sig_alg.p = p;
1128 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1129 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1130 {
1131 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1132 }
1133 p += len;
1134 outer_sig_alg.len = p - outer_sig_alg.p;
1135
1136 /*
1137 * signatureValue BIT STRING
1138 */
1139 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1140 if( ret != 0 )
1141 return( ret );
1142 frame->sig.p = tmp.p;
1143 frame->sig.len = tmp.len;
1144
1145 /* Check that we consumed the entire `Certificate` structure. */
1146 if( p != end )
1147 {
1148 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1149 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1150 }
1151
1152 /* Parse TBSCertificate structure
1153 *
1154 * TBSCertificate ::= SEQUENCE {
1155 * version [0] EXPLICIT Version DEFAULT v1,
1156 * serialNumber CertificateSerialNumber,
1157 * signature AlgorithmIdentifier,
1158 * issuer Name,
1159 * validity Validity,
1160 * subject Name,
1161 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1162 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1163 * -- If present, version MUST be v2 or v3
1164 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1165 * -- If present, version MUST be v2 or v3
1166 * extensions [3] EXPLICIT Extensions OPTIONAL
1167 * -- If present, version MUST be v3
1168 * }
1169 */
1170 end = frame->tbs.p + frame->tbs.len;
1171 p = tbs_start;
1172
1173 /*
1174 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1175 */
1176 {
1177 int version;
1178 ret = x509_get_version( &p, end, &version );
1179 if( ret != 0 )
1180 return( ret );
1181
1182 if( version < 0 || version > 2 )
1183 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1184
1185 frame->version = version + 1;
1186 }
1187
1188 /*
1189 * CertificateSerialNumber ::= INTEGER
1190 */
1191 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1192 if( ret != 0 )
1193 return( ret );
1194
1195 frame->serial.p = tmp.p;
1196 frame->serial.len = tmp.len;
1197
1198 /*
1199 * signature AlgorithmIdentifier
1200 */
1201 inner_sig_alg_start = p;
1202 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1203 &frame->sig_pk, NULL );
1204 if( ret != 0 )
1205 return( ret );
1206 inner_sig_alg_len = p - inner_sig_alg_start;
1207
1208 frame->sig_alg.p = inner_sig_alg_start;
1209 frame->sig_alg.len = inner_sig_alg_len;
1210
1211 /* Consistency check:
1212 * Inner and outer AlgorithmIdentifier structures must coincide:
1213 *
1214 * Quoting RFC 5280, Section 4.1.1.2:
1215 * This field MUST contain the same algorithm identifier as the
1216 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1217 */
1218 if( outer_sig_alg.len != inner_sig_alg_len ||
1219 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1220 {
1221 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1222 }
1223
1224 /*
1225 * issuer Name
1226 *
1227 * Name ::= CHOICE { -- only one possibility for now --
1228 * rdnSequence RDNSequence }
1229 *
1230 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1231 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001232 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001233
1234 ret = mbedtls_asn1_get_tag( &p, end, &len,
1235 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1236 if( ret != 0 )
1237 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001238 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001239 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001240
1241 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1242 &frame->issuer_raw,
1243 NULL, NULL );
1244 if( ret != 0 )
1245 return( ret );
1246
Hanno Becker21f55672019-02-15 15:27:59 +00001247 /*
1248 * Validity ::= SEQUENCE { ...
1249 */
1250 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1251 if( ret != 0 )
1252 return( ret );
1253
1254 /*
1255 * subject Name
1256 *
1257 * Name ::= CHOICE { -- only one possibility for now --
1258 * rdnSequence RDNSequence }
1259 *
1260 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1261 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001262 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001263
1264 ret = mbedtls_asn1_get_tag( &p, end, &len,
1265 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1266 if( ret != 0 )
1267 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001268 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001269 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001270
1271 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1272 &frame->subject_raw,
1273 NULL, NULL );
1274 if( ret != 0 )
1275 return( ret );
1276
Hanno Becker21f55672019-02-15 15:27:59 +00001277 /*
1278 * SubjectPublicKeyInfo
1279 */
1280 frame->pubkey_raw.p = p;
1281 ret = mbedtls_asn1_get_tag( &p, end, &len,
1282 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1283 if( ret != 0 )
1284 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1285 p += len;
1286 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1287
1288 /*
1289 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1290 * -- If present, version shall be v2 or v3
1291 */
1292 if( frame->version == 2 || frame->version == 3 )
1293 {
1294 ret = x509_get_uid( &p, end, &tmp, 1 /* implicit tag */ );
1295 if( ret != 0 )
1296 return( ret );
1297
1298 frame->issuer_id.p = tmp.p;
1299 frame->issuer_id.len = tmp.len;
1300 }
1301
1302 /*
1303 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1304 * -- If present, version shall be v2 or v3
1305 */
1306 if( frame->version == 2 || frame->version == 3 )
1307 {
1308 ret = x509_get_uid( &p, end, &tmp, 2 /* implicit tag */ );
1309 if( ret != 0 )
1310 return( ret );
1311
1312 frame->subject_id.p = tmp.p;
1313 frame->subject_id.len = tmp.len;
1314 }
1315
1316 /*
1317 * extensions [3] EXPLICIT Extensions OPTIONAL
1318 * -- If present, version shall be v3
1319 */
1320#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1321 if( frame->version == 3 )
1322#endif
1323 {
1324 if( p != end )
1325 {
1326 ret = mbedtls_asn1_get_tag( &p, end, &len,
1327 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1328 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1329 if( len == 0 )
1330 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1331 if( ret != 0 )
1332 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1333
1334 frame->v3_ext.p = p;
1335 frame->v3_ext.len = len;
1336
1337 p += len;
1338 }
1339
1340 ret = x509_crt_frame_parse_ext( frame );
1341 if( ret != 0 )
1342 return( ret );
1343 }
1344
1345 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1346 if( p != end )
1347 {
1348 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1349 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1350 }
1351
1352 return( 0 );
1353}
1354
1355static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
1356 mbedtls_x509_name *subject )
1357{
Hanno Becker1e11f212019-03-04 14:43:43 +00001358 return( mbedtls_x509_get_name( frame->subject_raw.p,
1359 frame->subject_raw.len,
1360 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001361}
1362
1363static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
1364 mbedtls_x509_name *issuer )
1365{
Hanno Becker1e11f212019-03-04 14:43:43 +00001366 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1367 frame->issuer_raw.len,
1368 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001369}
1370
1371static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
1372 mbedtls_x509_sequence *subject_alt )
1373{
1374 int ret;
1375 unsigned char *p = frame->subject_alt_raw.p;
1376 unsigned char *end = p + frame->subject_alt_raw.len;
1377
1378 memset( subject_alt, 0, sizeof( *subject_alt ) );
1379
1380 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1381 return( 0 );
1382
1383 ret = x509_get_subject_alt_name( p, end, subject_alt );
1384 if( ret != 0 )
1385 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1386 return( ret );
1387}
1388
1389static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
1390 mbedtls_x509_sequence *ext_key_usage )
1391{
1392 int ret;
1393 unsigned char *p = frame->ext_key_usage_raw.p;
1394 unsigned char *end = p + frame->ext_key_usage_raw.len;
1395
1396 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1397
1398 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1399 return( 0 );
1400
1401 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1402 if( ret != 0 )
1403 {
1404 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1405 return( ret );
1406 }
1407
1408 return( 0 );
1409}
1410
Hanno Becker180f7bf2019-02-28 13:23:38 +00001411#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001412static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1413 mbedtls_pk_context *pk )
1414{
1415 unsigned char *p = frame->pubkey_raw.p;
1416 unsigned char *end = p + frame->pubkey_raw.len;
1417 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1418}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001419#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001420
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421/*
1422 * Parse and fill a single X.509 certificate in DER format
1423 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001424static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1425 const unsigned char *buf,
1426 size_t buflen,
1427 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428{
1429 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001430 mbedtls_x509_crt_frame *frame;
1431 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001432
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435
Hanno Becker21f55672019-02-15 15:27:59 +00001436 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437 {
Hanno Becker21f55672019-02-15 15:27:59 +00001438 crt->raw.p = (unsigned char*) buf;
1439 crt->raw.len = buflen;
1440 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001441 }
1442 else
1443 {
Hanno Becker21f55672019-02-15 15:27:59 +00001444 crt->raw.p = mbedtls_calloc( 1, buflen );
1445 if( crt->raw.p == NULL )
1446 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1447 crt->raw.len = buflen;
1448 memcpy( crt->raw.p, buf, buflen );
1449
1450 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001451 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001452
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001453 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1454 if( cache == NULL )
1455 {
1456 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1457 goto exit;
1458 }
1459 crt->cache = cache;
1460 x509_crt_cache_init( cache );
1461
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001462#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1463
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001464 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001465 if( ret != 0 )
1466 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467
Hanno Becker76428352019-03-05 15:29:23 +00001468 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001469
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001470#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001472 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1473 if( frame == NULL )
1474 {
1475 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1476 goto exit;
1477 }
1478 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001479
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001480 ret = x509_crt_parse_frame( crt->raw.p,
1481 crt->raw.p + crt->raw.len,
1482 frame );
1483 if( ret != 0 )
1484 goto exit;
1485
Hanno Becker21f55672019-02-15 15:27:59 +00001486 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1487 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001488 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1489 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1490 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1491 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
1492 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1493 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
1494 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1495 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1496 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001497 crt->valid_from = frame->valid_from;
1498 crt->valid_to = frame->valid_to;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001499 crt->version = frame->version;
1500 crt->ca_istrue = frame->ca_istrue;
1501 crt->max_pathlen = frame->max_pathlen;
1502 crt->ext_types = frame->ext_types;
1503 crt->key_usage = frame->key_usage;
1504 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505
1506 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001507 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 {
Hanno Becker21f55672019-02-15 15:27:59 +00001511 /* sig_oid: Previously, needed for convenience in
1512 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001513 unsigned char *tmp = frame->sig_alg.p;
1514 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001515 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516
Hanno Becker21f55672019-02-15 15:27:59 +00001517 ret = mbedtls_x509_get_alg( &tmp, end,
1518 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519 if( ret != 0 )
1520 {
Hanno Becker21f55672019-02-15 15:27:59 +00001521 /* This should never happen, because we check
1522 * the sanity of the AlgorithmIdentifier structure
1523 * during frame parsing. */
1524 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1525 goto exit;
1526 }
1527 crt->sig_oid = sig_oid;
1528
1529 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001530 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001531 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1532 &crt->sig_md, &crt->sig_pk,
1533 &crt->sig_opts );
1534 if( ret != 0 )
1535 {
1536 /* Again, this should never happen. */
1537 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1538 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539 }
1540 }
1541
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001542 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001543 if( ret != 0 )
1544 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001546 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001547 if( ret != 0 )
1548 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001550 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001551 if( ret != 0 )
1552 goto exit;
1553
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001554 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001555 if( ret != 0 )
1556 goto exit;
1557
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001558 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1559 if( ret != 0 )
1560 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001561#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001562
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001563 /* Currently, we accept DER encoded CRTs with trailing garbage
1564 * and promise to not account for the garbage in the `raw` field.
1565 *
1566 * Note that this means that `crt->raw.len` is not necessarily the
1567 * full size of the heap buffer allocated at `crt->raw.p` in case
1568 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1569 * need the size, and the garbage data doesn't need zeroization. */
1570 crt->raw.len = frame->raw.len;
1571
1572 cache->pk_raw = frame->pubkey_raw;
1573
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001574 /* Free the frame before parsing the public key to
1575 * keep peak RAM usage low. This is slightly inefficient
1576 * because the frame will need to be parsed again on the
1577 * first usage of the CRT, but that seems acceptable.
1578 * As soon as the frame gets used multiple times, it
1579 * will be cached by default. */
1580 x509_crt_cache_clear_frame( crt->cache );
1581
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001582 /* The cache just references the PK structure from the legacy
1583 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001584 * the cache.
1585 *
1586 * We're not actually using the parsed PK context here;
1587 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001588 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001589 if( ret != 0 )
1590 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001591 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001592
1593exit:
1594 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596
Hanno Becker21f55672019-02-15 15:27:59 +00001597 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598}
1599
1600/*
1601 * Parse one X.509 certificate in DER format from a buffer and add them to a
1602 * chained list
1603 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001604static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1605 const unsigned char *buf,
1606 size_t buflen,
1607 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001608{
1609 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
1612 /*
1613 * Check for valid input
1614 */
1615 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617
Hanno Becker371e0e42019-02-25 18:08:59 +00001618 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619 {
1620 prev = crt;
1621 crt = crt->next;
1622 }
1623
1624 /*
1625 * Add new certificate on the end of the chain if needed.
1626 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001627 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001629 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630
1631 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001632 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633
1634 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637 }
1638
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001639 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640 {
1641 if( prev )
1642 prev->next = NULL;
1643
1644 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646
1647 return( ret );
1648 }
1649
1650 return( 0 );
1651}
1652
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001653int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1654 const unsigned char *buf,
1655 size_t buflen )
1656{
1657 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1658}
1659
1660int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1661 const unsigned char *buf,
1662 size_t buflen )
1663{
1664 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1665}
1666
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001668 * Parse one or more PEM certificates from a buffer and add them to the chained
1669 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001671int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1672 const unsigned char *buf,
1673 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674{
Janos Follath98e28a72016-05-31 14:03:54 +01001675#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001676 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001678#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679
1680 /*
1681 * Check for valid input
1682 */
1683 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685
1686 /*
1687 * Determine buffer content. Buffer contains either one DER certificate or
1688 * one or more PEM certificates.
1689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001691 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001692 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001695 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1698 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001699#else
1700 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1701#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#if defined(MBEDTLS_PEM_PARSE_C)
1704 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705 {
1706 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001708
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001709 /* 1 rather than 0 since the terminating NULL byte is counted in */
1710 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711 {
1712 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001715 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717 "-----BEGIN CERTIFICATE-----",
1718 "-----END CERTIFICATE-----",
1719 buf, NULL, 0, &use_len );
1720
1721 if( ret == 0 )
1722 {
1723 /*
1724 * Was PEM encoded
1725 */
1726 buflen -= use_len;
1727 buf += use_len;
1728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730 {
1731 return( ret );
1732 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736
1737 /*
1738 * PEM header and footer were found
1739 */
1740 buflen -= use_len;
1741 buf += use_len;
1742
1743 if( first_error == 0 )
1744 first_error = ret;
1745
Paul Bakker5a5fa922014-09-26 14:53:04 +02001746 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747 continue;
1748 }
1749 else
1750 break;
1751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755
1756 if( ret != 0 )
1757 {
1758 /*
1759 * Quit parsing on a memory error
1760 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001761 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001762 return( ret );
1763
1764 if( first_error == 0 )
1765 first_error = ret;
1766
1767 total_failed++;
1768 continue;
1769 }
1770
1771 success = 1;
1772 }
1773 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001774
1775 if( success )
1776 return( total_failed );
1777 else if( first_error )
1778 return( first_error );
1779 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001781#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782}
1783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785/*
1786 * Load one or more certificates and add them to the chained list
1787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789{
1790 int ret;
1791 size_t n;
1792 unsigned char *buf;
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795 return( ret );
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001798
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001799 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801
1802 return( ret );
1803}
1804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806{
1807 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001808#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001809 int w_ret;
1810 WCHAR szDir[MAX_PATH];
1811 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001812 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001813 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814
Paul Bakker9af723c2014-05-01 13:03:14 +02001815 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816 HANDLE hFind;
1817
1818 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820
Paul Bakker9af723c2014-05-01 13:03:14 +02001821 memset( szDir, 0, sizeof(szDir) );
1822 memset( filename, 0, MAX_PATH );
1823 memcpy( filename, path, len );
1824 filename[len++] = '\\';
1825 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826 filename[len++] = '*';
1827
Simon B3c6b18d2016-11-03 01:11:37 +00001828 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001829 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001830 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001832
1833 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001834 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001836
1837 len = MAX_PATH - len;
1838 do
1839 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001840 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001841
1842 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1843 continue;
1844
Paul Bakker9af723c2014-05-01 13:03:14 +02001845 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001846 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001847 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001848 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001849 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001850 {
1851 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1852 goto cleanup;
1853 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001856 if( w_ret < 0 )
1857 ret++;
1858 else
1859 ret += w_ret;
1860 }
1861 while( FindNextFileW( hFind, &file_data ) != 0 );
1862
Paul Bakker66d5d072014-06-17 16:39:18 +02001863 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001865
Ron Eldor36d90422017-01-09 15:09:16 +02001866cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001867 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001868#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001869 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001870 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001871 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001872 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001873 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001874 DIR *dir = opendir( path );
1875
Paul Bakker66d5d072014-06-17 16:39:18 +02001876 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001878
Ron Eldor63140682017-01-09 19:27:59 +02001879#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001880 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001881 {
1882 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001883 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001884 }
Ron Eldor63140682017-01-09 19:27:59 +02001885#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001886
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001887 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001888 {
Andres AGf9113192016-09-02 14:06:04 +01001889 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1890 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001891
Andres AGf9113192016-09-02 14:06:04 +01001892 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001893 {
Andres AGf9113192016-09-02 14:06:04 +01001894 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1895 goto cleanup;
1896 }
1897 else if( stat( entry_name, &sb ) == -1 )
1898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001900 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001901 }
1902
1903 if( !S_ISREG( sb.st_mode ) )
1904 continue;
1905
1906 // Ignore parse errors
1907 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909 if( t_ret < 0 )
1910 ret++;
1911 else
1912 ret += t_ret;
1913 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001914
1915cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001916 closedir( dir );
1917
Ron Eldor63140682017-01-09 19:27:59 +02001918#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001919 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001921#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001922
Paul Bakkerbe089b02013-10-14 15:51:50 +02001923#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001924
1925 return( ret );
1926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928
Hanno Becker02a21932019-06-10 15:08:43 +01001929#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001930static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001932{
1933 size_t i;
1934 size_t n = *size;
1935 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001937 const char *sep = "";
1938 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001939
1940 while( cur != NULL )
1941 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001942 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001943 {
1944 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001945 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001946 }
1947
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001948 n -= cur->buf.len + sep_len;
1949 for( i = 0; i < sep_len; i++ )
1950 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001951 for( i = 0; i < cur->buf.len; i++ )
1952 *p++ = cur->buf.p[i];
1953
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001954 sep = ", ";
1955 sep_len = 2;
1956
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001957 cur = cur->next;
1958 }
1959
1960 *p = '\0';
1961
1962 *size = n;
1963 *buf = p;
1964
1965 return( 0 );
1966}
1967
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001968#define PRINT_ITEM(i) \
1969 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001971 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001972 sep = ", "; \
1973 }
1974
1975#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001976 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001977 PRINT_ITEM( name );
1978
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001979static int x509_info_cert_type( char **buf, size_t *size,
1980 unsigned char ns_cert_type )
1981{
1982 int ret;
1983 size_t n = *size;
1984 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001985 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001988 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1990 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1991 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001992 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1993 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1994 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001995
1996 *size = n;
1997 *buf = p;
1998
1999 return( 0 );
2000}
2001
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002002#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002003 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002004 PRINT_ITEM( name );
2005
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002006static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002007 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002008{
2009 int ret;
2010 size_t n = *size;
2011 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002012 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2015 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002016 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2017 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2018 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2020 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002021 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2022 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002023
2024 *size = n;
2025 *buf = p;
2026
2027 return( 0 );
2028}
2029
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002030static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002032{
2033 int ret;
2034 const char *desc;
2035 size_t n = *size;
2036 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002038 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002039
2040 while( cur != NULL )
2041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002043 desc = "???";
2044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002046 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002047
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002048 sep = ", ";
2049
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002050 cur = cur->next;
2051 }
2052
2053 *size = n;
2054 *buf = p;
2055
2056 return( 0 );
2057}
2058
Hanno Becker4f869ed2019-02-24 16:47:57 +00002059typedef struct mbedtls_x509_crt_sig_info
2060{
2061 mbedtls_md_type_t sig_md;
2062 mbedtls_pk_type_t sig_pk;
2063 void *sig_opts;
2064 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
2065 size_t crt_hash_len;
2066 mbedtls_x509_buf_raw sig;
2067 mbedtls_x509_buf_raw issuer_raw;
2068} mbedtls_x509_crt_sig_info;
2069
2070static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2071{
2072#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2073 mbedtls_free( info->sig_opts );
2074#else
2075 ((void) info);
2076#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2077}
2078
2079static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2080 mbedtls_x509_crt_sig_info *info )
2081{
2082 const mbedtls_md_info_t *md_info;
2083
2084 md_info = mbedtls_md_info_from_type( frame->sig_md );
2085 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2086 info->crt_hash ) != 0 )
2087 {
2088 /* Note: this can't happen except after an internal error */
2089 return( -1 );
2090 }
2091
2092 info->crt_hash_len = mbedtls_md_get_size( md_info );
2093
2094 /* Make sure that this function leaves the target structure
2095 * ready to be freed, regardless of success of failure. */
2096 info->sig_opts = NULL;
2097
2098#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2099 {
2100 int ret;
2101 unsigned char *alg_start = frame->sig_alg.p;
2102 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2103
2104 /* Get signature options -- currently only
2105 * necessary for RSASSA-PSS. */
2106 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2107 &info->sig_pk, &info->sig_opts );
2108 if( ret != 0 )
2109 {
2110 /* Note: this can't happen except after an internal error */
2111 return( -1 );
2112 }
2113 }
2114#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2115 info->sig_md = frame->sig_md;
2116 info->sig_pk = frame->sig_pk;
2117#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2118
2119 info->issuer_raw = frame->issuer_raw;
2120 info->sig = frame->sig;
2121 return( 0 );
2122}
2123
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124/*
2125 * Return an informational string about the certificate.
2126 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002127#define BEFORE_COLON 18
2128#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002130 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131{
2132 int ret;
2133 size_t n;
2134 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002135 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002136 mbedtls_x509_crt_frame frame;
2137 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002138
Hanno Becker5226c532019-02-27 17:38:40 +00002139 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2140 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002141 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002142
2143 p = buf;
2144 n = size;
2145
Hanno Becker4f869ed2019-02-24 16:47:57 +00002146 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002147 mbedtls_pk_init( &pk );
2148
2149 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002150 {
2151 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002152 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Janos Follath98e28a72016-05-31 14:03:54 +01002153
2154 return( (int) ( size - n ) );
2155 }
2156
Hanno Becker5226c532019-02-27 17:38:40 +00002157 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002158 if( ret != 0 )
2159 {
2160 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2161 goto cleanup;
2162 }
2163
Hanno Becker5226c532019-02-27 17:38:40 +00002164 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002165 if( ret != 0 )
2166 {
2167 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2168 goto cleanup;
2169 }
2170
Hanno Becker5226c532019-02-27 17:38:40 +00002171 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002172 if( ret != 0 )
2173 {
2174 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2175 goto cleanup;
2176 }
2177
Hanno Becker5226c532019-02-27 17:38:40 +00002178 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002179 if( ret != 0 )
2180 {
2181 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2182 goto cleanup;
2183 }
2184
Hanno Becker5226c532019-02-27 17:38:40 +00002185 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002186 if( ret != 0 )
2187 {
2188 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2189 goto cleanup;
2190 }
2191
Hanno Becker5226c532019-02-27 17:38:40 +00002192 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2193 if( ret != 0 )
2194 {
2195 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2196 goto cleanup;
2197 }
2198
2199 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002200 if( ret != 0 )
2201 {
2202 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2203 goto cleanup;
2204 }
2205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002207 prefix, frame.version );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002208 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002209
Hanno Becker4f869ed2019-02-24 16:47:57 +00002210 {
2211 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002212 serial.p = frame.serial.p;
2213 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002214 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2215 prefix );
2216 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
2217 ret = mbedtls_x509_serial_gets( p, n, &serial );
2218 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
2219 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002222 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Hanno Becker5226c532019-02-27 17:38:40 +00002223 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002224 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002227 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Hanno Becker5226c532019-02-27 17:38:40 +00002228 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002229 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002232 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002233 frame.valid_from.year, frame.valid_from.mon,
2234 frame.valid_from.day, frame.valid_from.hour,
2235 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002236 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002239 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002240 frame.valid_to.year, frame.valid_to.mon,
2241 frame.valid_to.day, frame.valid_to.hour,
2242 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002243 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002246 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002247
Hanno Becker83cd8672019-02-21 17:13:46 +00002248 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2249 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002250 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002251
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002252 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002254 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002255 {
2256 return( ret );
2257 }
2258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002260 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002261 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002262
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002263 /*
2264 * Optional extensions
2265 */
2266
Hanno Becker5226c532019-02-27 17:38:40 +00002267 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002270 frame.ca_istrue ? "true" : "false" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002271 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002272
Hanno Becker5226c532019-02-27 17:38:40 +00002273 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002274 {
Hanno Becker5226c532019-02-27 17:38:40 +00002275 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002276 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002277 }
2278 }
2279
Hanno Becker5226c532019-02-27 17:38:40 +00002280 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002283 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002284
2285 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002286 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002287 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002288 }
2289
Hanno Becker5226c532019-02-27 17:38:40 +00002290 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002293 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002294
Hanno Becker5226c532019-02-27 17:38:40 +00002295 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002296 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002297 }
2298
Hanno Becker5226c532019-02-27 17:38:40 +00002299 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002302 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002303
Hanno Becker5226c532019-02-27 17:38:40 +00002304 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002305 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002306 }
2307
Hanno Becker5226c532019-02-27 17:38:40 +00002308 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002311 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002312
2313 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002314 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002315 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002316 }
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002319 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002320
Hanno Becker4f869ed2019-02-24 16:47:57 +00002321 ret = (int) ( size - n );
2322
2323cleanup:
2324
Hanno Becker4f869ed2019-02-24 16:47:57 +00002325 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002326 mbedtls_pk_free( &pk );
2327 mbedtls_x509_name_free( issuer );
2328 mbedtls_x509_name_free( subject );
2329 mbedtls_x509_sequence_free( ext_key_usage );
2330 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002331
2332 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333}
2334
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002335struct x509_crt_verify_string {
2336 int code;
2337 const char *string;
2338};
2339
2340static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002341 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002342 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2343 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2344 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2345 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2346 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002347 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2348 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2349 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002350 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002351 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2352 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2353 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2354 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002355 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2356 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2357 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2358 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2359 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2360 { 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 +01002361 { 0, NULL }
2362};
2363
2364int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002365 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002366{
2367 int ret;
2368 const struct x509_crt_verify_string *cur;
2369 char *p = buf;
2370 size_t n = size;
2371
2372 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2373 {
2374 if( ( flags & cur->code ) == 0 )
2375 continue;
2376
2377 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002378 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002379 flags ^= cur->code;
2380 }
2381
2382 if( flags != 0 )
2383 {
2384 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2385 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002386 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002387 }
2388
2389 return( (int) ( size - n ) );
2390}
Hanno Becker02a21932019-06-10 15:08:43 +01002391#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002394static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2395 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002396{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002397 unsigned int usage_must, usage_may;
2398 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2399 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2400
2401 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2402 return( 0 );
2403
2404 usage_must = usage & ~may_mask;
2405
2406 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2407 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2408
2409 usage_may = usage & may_mask;
2410
2411 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002413
2414 return( 0 );
2415}
Hanno Becker45eedf12019-02-25 13:55:33 +00002416
2417int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2418 unsigned int usage )
2419{
2420 int ret;
2421 mbedtls_x509_crt_frame *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002422 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002423 if( ret != 0 )
2424 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2425
2426 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002427 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002428
2429 return( ret );
2430}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002431#endif
2432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002434typedef struct
2435{
2436 const char *oid;
2437 size_t oid_len;
2438} x509_crt_check_ext_key_usage_cb_ctx_t;
2439
2440static int x509_crt_check_ext_key_usage_cb( void *ctx,
2441 int tag,
2442 unsigned char *data,
2443 size_t data_len )
2444{
2445 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2446 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2447 ((void) tag);
2448
2449 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2450 data, data_len ) == 0 )
2451 {
2452 return( 1 );
2453 }
2454
2455 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2456 data_len ) == 0 )
2457 {
2458 return( 1 );
2459 }
2460
2461 return( 0 );
2462}
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002465 const char *usage_oid,
2466 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002467{
Hanno Beckere1956af2019-02-21 14:28:12 +00002468 int ret;
Hanno Beckere9718b42019-02-25 18:11:42 +00002469 mbedtls_x509_crt_frame *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002470 unsigned ext_types;
2471 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002472 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002473
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002474 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002475 if( ret != 0 )
2476 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2477
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002478 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002479 ext_types = frame->ext_types;
2480 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2481 {
2482 p = frame->ext_key_usage_raw.p;
2483 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002484
Hanno Beckere9718b42019-02-25 18:11:42 +00002485 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2486 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2487 x509_crt_check_ext_key_usage_cb,
2488 &cb_ctx );
2489 if( ret == 1 )
2490 ret = 0;
2491 else
2492 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2493 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002494
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002495 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002496 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002501/*
2502 * Return 1 if the certificate is revoked, or 0 otherwise.
2503 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002504static int x509_serial_is_revoked( unsigned char const *serial,
2505 size_t serial_len,
2506 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002509
2510 while( cur != NULL && cur->serial.len != 0 )
2511 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002512 if( serial_len == cur->serial.len &&
2513 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002514 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002515 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002516 return( 1 );
2517 }
2518
2519 cur = cur->next;
2520 }
2521
2522 return( 0 );
2523}
2524
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002525int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2526 const mbedtls_x509_crl *crl )
2527{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002528 int ret;
2529 mbedtls_x509_crt_frame *frame;
2530
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002531 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002532 if( ret != 0 )
2533 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2534
2535 ret = x509_serial_is_revoked( frame->serial.p,
2536 frame->serial.len,
2537 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002538 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002539 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002540}
2541
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002542/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002543 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002544 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002545 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002546static int x509_crt_verifycrl( unsigned char *crt_serial,
2547 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002548 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002549 mbedtls_x509_crl *crl_list,
2550 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002551{
Hanno Beckerbb266132019-02-25 18:12:46 +00002552 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002553 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2555 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002556 mbedtls_x509_buf_raw ca_subject;
2557 mbedtls_pk_context *pk;
2558 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002559
Hanno Beckerbb266132019-02-25 18:12:46 +00002560 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002561 return( flags );
2562
Hanno Beckerbb266132019-02-25 18:12:46 +00002563 {
2564 mbedtls_x509_crt_frame *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002565 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002566 if( ret != 0 )
2567 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2568
2569 ca_subject = ca->subject_raw;
2570
2571 can_sign = 0;
2572 if( x509_crt_check_key_usage_frame( ca,
2573 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2574 {
2575 can_sign = 1;
2576 }
2577
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002578 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002579 }
2580
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002581 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002582 if( ret != 0 )
2583 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2584
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002585 while( crl_list != NULL )
2586 {
2587 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002588 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002589 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002590 {
2591 crl_list = crl_list->next;
2592 continue;
2593 }
2594
2595 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002596 * Check if the CA is configured to sign CRLs
2597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002599 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002602 break;
2603 }
2604#endif
2605
2606 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002607 * Check if CRL is correctly signed by the trusted CA
2608 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002609 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2610 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2611
2612 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2613 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002616 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002617 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002618 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002620 break;
2621 }
2622
Hanno Beckerbb266132019-02-25 18:12:46 +00002623 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002624 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002625
Hanno Beckerbb266132019-02-25 18:12:46 +00002626 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002628 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002631 break;
2632 }
2633
2634 /*
2635 * Check for validity of CRL (Do not drop out)
2636 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002637 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002639
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002640 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002641 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002642
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002643 /*
2644 * Check if certificate is revoked
2645 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002646 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2647 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002650 break;
2651 }
2652
2653 crl_list = crl_list->next;
2654 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002655
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002656 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002657 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002660
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002661/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002662 * Check the signature of a certificate by its parent
2663 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002664static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002665 mbedtls_x509_crt *parent,
2666 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002667{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002668 int ret;
2669 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002670
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002671 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002672 if( ret != 0 )
2673 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2674
2675 /* Skip expensive computation on obvious mismatch */
2676 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002677 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002678 ret = -1;
2679 goto exit;
2680 }
2681
2682#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2683 ((void) rs_ctx);
2684#else
2685 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2686 {
2687 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002688 sig_info->sig_md,
2689 sig_info->crt_hash, sig_info->crt_hash_len,
2690 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002691 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002692 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002693 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002694#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002695 {
2696 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2697 sig_info->sig_opts,
2698 pk,
2699 sig_info->sig_md,
2700 sig_info->crt_hash, sig_info->crt_hash_len,
2701 sig_info->sig.p, sig_info->sig.len );
2702 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002703
Hanno Beckere449e2d2019-02-25 14:45:31 +00002704exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002705 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002706 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002707}
2708
2709/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002710 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2711 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002712 *
2713 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002714 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002715static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2716 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002717 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002718{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002719 int need_ca_bit;
2720
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002721 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002722 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2723 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002724 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002725 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002726 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002727 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002728
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002729 /* Parent must have the basicConstraints CA bit set as a general rule */
2730 need_ca_bit = 1;
2731
2732 /* Exception: v1/v2 certificates that are locally trusted. */
2733 if( top && parent->version < 3 )
2734 need_ca_bit = 0;
2735
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002736 if( need_ca_bit && ! parent->ca_istrue )
2737 return( -1 );
2738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002740 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002741 x509_crt_check_key_usage_frame( parent,
2742 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002743 {
2744 return( -1 );
2745 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002746#endif
2747
2748 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002749}
2750
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002751/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002752 * Find a suitable parent for child in candidates, or return NULL.
2753 *
2754 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002755 * 1. subject name matches child's issuer
2756 * 2. if necessary, the CA bit is set and key usage allows signing certs
2757 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002758 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002759 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002760 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002761 * If there's a suitable candidate which is also time-valid, return the first
2762 * such. Otherwise, return the first suitable candidate (or NULL if there is
2763 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002764 *
2765 * The rationale for this rule is that someone could have a list of trusted
2766 * roots with two versions on the same root with different validity periods.
2767 * (At least one user reported having such a list and wanted it to just work.)
2768 * The reason we don't just require time-validity is that generally there is
2769 * only one version, and if it's expired we want the flags to state that
2770 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002771 *
2772 * The rationale for rule 3 (signature for trusted roots) is that users might
2773 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002774 * way we select the correct one is by checking the signature (as we don't
2775 * rely on key identifier extensions). (This is one way users might choose to
2776 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002777 *
2778 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002779 * - [in] child: certificate for which we're looking for a parent
2780 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002781 * - [out] r_parent: parent found (or NULL)
2782 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002783 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2784 * of the chain, 0 otherwise
2785 * - [in] path_cnt: number of intermediates seen so far
2786 * - [in] self_cnt: number of self-signed intermediates seen so far
2787 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002788 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002789 *
2790 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002791 * - 0 on success
2792 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002793 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002794static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002795 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002796 mbedtls_x509_crt *candidates,
2797 mbedtls_x509_crt **r_parent,
2798 int *r_signature_is_good,
2799 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002800 unsigned path_cnt,
2801 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002802 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002803{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002804 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002805 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002806 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002807
2808#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002809 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002810 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2811 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002812 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002813 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002814 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002815 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002816
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002817 /* clear saved state */
2818 rs_ctx->parent = NULL;
2819 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002820 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002821
2822 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002823 goto check_signature;
2824 }
2825#endif
2826
2827 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002828 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002829
Hanno Becker43bf9002019-02-25 14:46:49 +00002830 for( parent_crt = candidates; parent_crt != NULL;
2831 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002832 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002833 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002834
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002835#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2836check_signature:
2837#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002838
2839 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002840 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002841 mbedtls_x509_crt_frame *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002842
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002843 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002844 if( ret != 0 )
2845 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002846
Hanno Becker43bf9002019-02-25 14:46:49 +00002847 if( mbedtls_x509_time_is_past( &parent->valid_from ) &&
2848 mbedtls_x509_time_is_future( &parent->valid_to ) )
2849 {
2850 parent_valid = 1;
2851 }
2852
2853 /* basic parenting skills (name, CA bit, key usage) */
2854 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2855 parent_match = 1;
2856
2857 /* +1 because the stored max_pathlen is 1 higher
2858 * than the actual value */
2859 if( !( parent->max_pathlen > 0 &&
2860 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2861 {
2862 path_len_ok = 1;
2863 }
2864
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002865 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002866 }
2867
2868 if( parent_match == 0 || path_len_ok == 0 )
2869 continue;
2870
2871 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002872 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002873
2874#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002875 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2876 {
2877 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002878 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002879 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002880 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002881
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002882 return( ret );
2883 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002884#else
2885 (void) ret;
2886#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002887
2888 signature_is_good = ret == 0;
2889 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002890 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002891
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002892 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002893 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002894 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002895 if( fallback_parent == NULL )
2896 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002897 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002898 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002899 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002900
2901 continue;
2902 }
2903
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002904 break;
2905 }
2906
Hanno Becker43bf9002019-02-25 14:46:49 +00002907 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002908 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002909 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002910 *r_signature_is_good = signature_is_good;
2911 }
2912 else
2913 {
2914 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002915 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002916 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002917
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002918 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002919}
2920
2921/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002922 * Find a parent in trusted CAs or the provided chain, or return NULL.
2923 *
2924 * Searches in trusted CAs first, and return the first suitable parent found
2925 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002926 *
2927 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002928 * - [in] child: certificate for which we're looking for a parent, followed
2929 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002930 * - [in] trust_ca: list of locally trusted certificates
2931 * - [out] parent: parent found (or NULL)
2932 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2933 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2934 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2935 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002936 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002937 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002938 *
2939 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002940 * - 0 on success
2941 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002942 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002943static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002944 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002945 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002946 mbedtls_x509_crt *trust_ca,
2947 mbedtls_x509_crt **parent,
2948 int *parent_is_trusted,
2949 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002950 unsigned path_cnt,
2951 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002952 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002953{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002954 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002955 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002956
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002957 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002958
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002959#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002960 /* restore then clear saved state if we have some stored */
2961 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2962 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002963 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002964 rs_ctx->parent_is_trusted = -1;
2965 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002966#endif
2967
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002968 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00002969 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002970
Hanno Becker5299cf82019-02-25 13:50:41 +00002971 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002972 parent, signature_is_good,
2973 *parent_is_trusted,
2974 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002975
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002976#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002977 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2978 {
2979 /* save state */
2980 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002981 return( ret );
2982 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002983#else
2984 (void) ret;
2985#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002986
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002987 /* stop here if found or already in second iteration */
2988 if( *parent != NULL || *parent_is_trusted == 0 )
2989 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002990
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002991 /* prepare second iteration */
2992 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002993 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002994
2995 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01002996 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002997 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002998 *parent_is_trusted = 0;
2999 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003000 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003001
3002 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003003}
3004
3005/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003006 * Check if an end-entity certificate is locally trusted
3007 *
3008 * Currently we require such certificates to be self-signed (actually only
3009 * check for self-issued as self-signatures are not checked)
3010 */
3011static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003012 mbedtls_x509_crt_frame const *crt,
3013 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003014{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003015 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003016
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003017 /* look for an exact match with trusted cert */
3018 for( cur = trust_ca; cur != NULL; cur = cur->next )
3019 {
3020 if( crt->raw.len == cur->raw.len &&
3021 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3022 {
3023 return( 0 );
3024 }
3025 }
3026
3027 /* too bad */
3028 return( -1 );
3029}
3030
3031/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003032 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003033 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003034 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3035 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003036 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003037 * such that every cert in the chain is a child of the next one,
3038 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003039 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003040 * Verify that chain and return it with flags for all issues found.
3041 *
3042 * Special cases:
3043 * - EE == Rj -> return a one-element list containing it
3044 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3045 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003046 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003047 * Tests for (aspects of) this function should include at least:
3048 * - trusted EE
3049 * - EE -> trusted root
3050 * - EE -> intermedate CA -> trusted root
3051 * - if relevant: EE untrusted
3052 * - if relevant: EE -> intermediate, untrusted
3053 * with the aspect under test checked at each relevant level (EE, int, root).
3054 * For some aspects longer chains are required, but usually length 2 is
3055 * enough (but length 1 is not in general).
3056 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003057 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003058 * - [in] crt: the cert list EE, C1, ..., Cn
3059 * - [in] trust_ca: the trusted list R1, ..., Rp
3060 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003061 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003062 * Only valid when return value is 0, may contain garbage otherwise!
3063 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003064 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003065 *
3066 * Return value:
3067 * - non-zero if the chain could not be fully built and examined
3068 * - 0 is the chain was successfully built and examined,
3069 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003070 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003071static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003072 mbedtls_x509_crt *crt,
3073 mbedtls_x509_crt *trust_ca,
3074 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003075 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003076 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003077 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003078{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003079 /* Don't initialize any of those variables here, so that the compiler can
3080 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003081 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003082 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003083 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003084 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003085 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003086 int parent_is_trusted;
3087 int child_is_trusted;
3088 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003089 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003090
3091#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3092 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003093 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003094 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003095 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003096 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003097 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003098
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003099 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003100 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003101 child_crt = cur->crt;
3102
3103 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003104 goto find_parent;
3105 }
3106#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003107
Hanno Becker1e0677a2019-02-25 14:58:22 +00003108 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003109 self_cnt = 0;
3110 parent_is_trusted = 0;
3111 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003112
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003113 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003114#if defined(MBEDTLS_X509_CRL_PARSE_C)
3115 mbedtls_x509_buf_raw child_serial;
3116#endif /* MBEDTLS_X509_CRL_PARSE_C */
3117 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003118
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003119 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003120 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003121 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003122 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003123 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003124
3125#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3126find_parent:
3127#endif
3128
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003129 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003130
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003131 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003132 mbedtls_x509_crt_sig_info child_sig;
3133 {
3134 mbedtls_x509_crt_frame *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003135
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003136 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003137 if( ret != 0 )
3138 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3139
3140 /* Check time-validity (all certificates) */
3141 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3142 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3143 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3144 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
3145
3146 /* Stop here for trusted roots (but not for trusted EE certs) */
3147 if( child_is_trusted )
3148 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003149 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003150 return( 0 );
3151 }
3152
3153 self_issued = 0;
3154 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3155 &child->subject_raw,
3156 NULL, NULL ) == 0 )
3157 {
3158 self_issued = 1;
3159 }
3160
3161 /* Check signature algorithm: MD & PK algs */
3162 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3163 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3164
3165 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3166 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3167
3168 /* Special case: EE certs that are locally trusted */
3169 if( ver_chain->len == 1 && self_issued &&
3170 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3171 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003172 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003173 return( 0 );
3174 }
3175
3176#if defined(MBEDTLS_X509_CRL_PARSE_C)
3177 child_serial = child->serial;
3178#endif /* MBEDTLS_X509_CRL_PARSE_C */
3179
3180 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003181 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003182
Hanno Becker5299cf82019-02-25 13:50:41 +00003183 if( ret != 0 )
3184 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3185 }
3186
3187 /* Look for a parent in trusted CAs or up the chain */
3188 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003189 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003190 &parent_is_trusted, &signature_is_good,
3191 ver_chain->len - 1, self_cnt, rs_ctx );
3192
3193 x509_crt_free_sig_info( &child_sig );
3194 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003195
3196#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003197 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3198 {
3199 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003200 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003201 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003202 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003203 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003204 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003205#else
3206 (void) ret;
3207#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003208
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003209 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003210 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003211 {
3212 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003213 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003214 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003215
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003216 /* Count intermediate self-issued (not necessarily self-signed) certs.
3217 * These can occur with some strategies for key rollover, see [SIRO],
3218 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003219 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003220 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003221
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003222 /* path_cnt is 0 for the first intermediate CA,
3223 * and if parent is trusted it's not an intermediate CA */
3224 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003225 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003226 {
3227 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003228 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003229 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003230
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003231 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003232 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003233 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3234
Hanno Becker58c35642019-02-25 18:13:46 +00003235 {
3236 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003237 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003238 if( ret != 0 )
3239 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3240
3241 /* check size of signing key */
3242 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3243 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3244
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003245 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003246 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003249 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003250 *flags |= x509_crt_verifycrl( child_serial.p,
3251 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003252 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003253#else
3254 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003255#endif
3256
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003257 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003258 child_crt = parent_crt;
3259 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003260 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003261 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003262 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003263}
3264
3265/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003266 * Check for CN match
3267 */
Hanno Becker24926222019-02-21 13:10:55 +00003268static int x509_crt_check_cn( unsigned char const *buf,
3269 size_t buflen,
3270 const char *cn,
3271 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003272{
Hanno Becker24926222019-02-21 13:10:55 +00003273 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003274 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003275 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003276
3277 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003278 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003279 {
3280 return( 0 );
3281 }
3282
3283 return( -1 );
3284}
3285
Hanno Becker8b543b32019-02-21 11:50:44 +00003286/* Returns 1 on a match and 0 on a mismatch.
3287 * This is because this function is used as a callback for
3288 * mbedtls_x509_name_cmp_raw(), which continues the name
3289 * traversal as long as the callback returns 0. */
3290static int x509_crt_check_name( void *ctx,
3291 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003292 mbedtls_x509_buf *val,
3293 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003294{
3295 char const *cn = (char const*) ctx;
3296 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003297 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003298
3299 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003300 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003301 {
3302 return( 1 );
3303 }
3304
3305 return( 0 );
3306}
3307
Hanno Beckerda410822019-02-21 13:36:59 +00003308/* Returns 1 on a match and 0 on a mismatch.
3309 * This is because this function is used as a callback for
3310 * mbedtls_asn1_traverse_sequence_of(), which continues the
3311 * traversal as long as the callback returns 0. */
3312static int x509_crt_subject_alt_check_name( void *ctx,
3313 int tag,
3314 unsigned char *data,
3315 size_t data_len )
3316{
3317 char const *cn = (char const*) ctx;
3318 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003319 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003320
3321 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3322 return( 1 );
3323
3324 return( 0 );
3325}
3326
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003327/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003328 * Verify the requested CN - only call this if cn is not NULL!
3329 */
Hanno Becker082435c2019-02-25 18:14:40 +00003330static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3331 const char *cn,
3332 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003333{
Hanno Beckerda410822019-02-21 13:36:59 +00003334 int ret;
Hanno Becker082435c2019-02-25 18:14:40 +00003335 mbedtls_x509_crt_frame *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003336
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003337 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003338 if( ret != 0 )
3339 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3340
3341 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003342 {
Hanno Becker90b94082019-02-21 21:13:21 +00003343 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003344 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003345 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003346 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003347
Hanno Becker90b94082019-02-21 21:13:21 +00003348 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3349 MBEDTLS_ASN1_TAG_CLASS_MASK,
3350 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3351 MBEDTLS_ASN1_TAG_VALUE_MASK,
3352 2 /* SubjectAlt DNS */,
3353 x509_crt_subject_alt_check_name,
3354 (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003355 }
3356 else
3357 {
Hanno Becker082435c2019-02-25 18:14:40 +00003358 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3359 &frame->subject_raw,
Hanno Becker8b543b32019-02-21 11:50:44 +00003360 x509_crt_check_name, (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003361 }
Hanno Beckerda410822019-02-21 13:36:59 +00003362
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003363 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003364
3365 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3366 * return 1 when finding a name component matching `cn`. */
3367 if( ret == 1 )
3368 return( 0 );
3369
3370 if( ret != 0 )
3371 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3372
3373 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3374 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003375}
3376
3377/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003378 * Merge the flags for all certs in the chain, after calling callback
3379 */
3380static int x509_crt_merge_flags_with_cb(
3381 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003382 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003383 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3384 void *p_vrfy )
3385{
3386 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003387 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003388 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003389 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003390
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003391 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003392 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003393 cur = &ver_chain->items[i-1];
3394 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003395
3396 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003397 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003398 return( ret );
3399
3400 *flags |= cur_flags;
3401 }
3402
3403 return( 0 );
3404}
3405
3406/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003407 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3410 mbedtls_x509_crt *trust_ca,
3411 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003412 const char *cn, uint32_t *flags,
3413 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003414 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003415{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003416 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3417 &mbedtls_x509_crt_profile_default, cn, flags,
3418 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003419}
3420
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003421/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003422 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003423 */
3424int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3425 mbedtls_x509_crt *trust_ca,
3426 mbedtls_x509_crl *ca_crl,
3427 const mbedtls_x509_crt_profile *profile,
3428 const char *cn, uint32_t *flags,
3429 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3430 void *p_vrfy )
3431{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003432 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3433 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3434}
3435
3436/*
3437 * Verify the certificate validity, with profile, restartable version
3438 *
3439 * This function:
3440 * - checks the requested CN (if any)
3441 * - checks the type and size of the EE cert's key,
3442 * as that isn't done as part of chain building/verification currently
3443 * - builds and verifies the chain
3444 * - then calls the callback and merges the flags
3445 */
3446int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3447 mbedtls_x509_crt *trust_ca,
3448 mbedtls_x509_crl *ca_crl,
3449 const mbedtls_x509_crt_profile *profile,
3450 const char *cn, uint32_t *flags,
3451 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3452 void *p_vrfy,
3453 mbedtls_x509_crt_restart_ctx *rs_ctx )
3454{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003455 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003456 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003457 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003458
3459 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003460 ee_flags = 0;
3461 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003462
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003463 if( profile == NULL )
3464 {
3465 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3466 goto exit;
3467 }
3468
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003469 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003470 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003471 {
3472 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3473 if( ret != 0 )
3474 return( ret );
3475 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003476
Hanno Becker87233362019-02-25 18:15:33 +00003477 {
3478 mbedtls_pk_context *pk;
3479 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003480
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003481 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003482 if( ret != 0 )
3483 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003484
Hanno Becker87233362019-02-25 18:15:33 +00003485 /* Check the type and size of the key */
3486 pk_type = mbedtls_pk_get_type( pk );
3487
3488 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3489 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3490
3491 if( x509_profile_check_key( profile, pk ) != 0 )
3492 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3493
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003494 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003495 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003496
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003497 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003498 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003499 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003500
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003501 if( ret != 0 )
3502 goto exit;
3503
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003504 /* Merge end-entity flags */
3505 ver_chain.items[0].flags |= ee_flags;
3506
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003507 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003508 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003509
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003510exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003511#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3512 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3513 mbedtls_x509_crt_restart_free( rs_ctx );
3514#endif
3515
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003516 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3517 * the SSL module for authmode optional, but non-zero return from the
3518 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003519 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3520 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3521
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003522 if( ret != 0 )
3523 {
3524 *flags = (uint32_t) -1;
3525 return( ret );
3526 }
3527
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003528 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003530
3531 return( 0 );
3532}
3533
3534/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003535 * Initialize a certificate chain
3536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003540}
3541
3542/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003543 * Unallocate all certificate data
3544 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 mbedtls_x509_crt *cert_cur = crt;
3549 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003550
3551 if( crt == NULL )
3552 return;
3553
3554 do
3555 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003556 x509_crt_cache_free( cert_cur->cache );
3557 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003558
3559#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3563 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003564#endif
3565
Hanno Becker2bcc7642019-02-26 19:01:00 +00003566 mbedtls_x509_name_free( cert_cur->issuer.next );
3567 mbedtls_x509_name_free( cert_cur->subject.next );
3568 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3569 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003570#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003571
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003572 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003573 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003574 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003576 }
3577
3578 cert_cur = cert_cur->next;
3579 }
3580 while( cert_cur != NULL );
3581
3582 cert_cur = crt;
3583 do
3584 {
3585 cert_prv = cert_cur;
3586 cert_cur = cert_cur->next;
3587
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003588 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003589 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003591 }
3592 while( cert_cur != NULL );
3593}
3594
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003595#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3596/*
3597 * Initialize a restart context
3598 */
3599void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3600{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003601 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003602
3603 ctx->parent = NULL;
3604 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003605 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003606
3607 ctx->parent_is_trusted = -1;
3608
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003609 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003610 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003611 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003612}
3613
3614/*
3615 * Free the components of a restart context
3616 */
3617void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3618{
3619 if( ctx == NULL )
3620 return;
3621
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003622 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003623 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003624}
3625#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627#endif /* MBEDTLS_X509_CRT_PARSE_C */