blob: 0f72e2f08400c960b498aa7ad7e3861a94286b37 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020030 *
31 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/x509_crt.h"
Hanno Beckerf6bc8882019-05-02 13:05:58 +010043#include "mbedtls/x509_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000046
Rich Evans00ab4702015-02-06 13:43:58 +000047#include <string.h>
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#else
Simon Butcherd2642582018-10-03 15:11:19 +010056#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010065#endif
66
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <windows.h>
69#else
70#include <time.h>
71#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000074#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020075#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#include <sys/types.h>
77#include <sys/stat.h>
78#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000079#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#endif
81
Hanno Becker38f0cb42019-03-04 15:13:45 +000082#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
83static void x509_buf_to_buf_raw( mbedtls_x509_buf_raw *dst,
84 mbedtls_x509_buf const *src )
85{
86 dst->p = src->p;
87 dst->len = src->len;
88}
89
90static void x509_buf_raw_to_buf( mbedtls_x509_buf *dst,
91 mbedtls_x509_buf_raw const *src )
92{
93 dst->p = src->p;
94 dst->len = src->len;
95}
96#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
97
Hanno Becker21f55672019-02-15 15:27:59 +000098static int x509_crt_parse_frame( unsigned char *start,
99 unsigned char *end,
100 mbedtls_x509_crt_frame *frame );
101static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
102 mbedtls_x509_name *subject );
103static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
104 mbedtls_x509_name *issuer );
105static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
106 mbedtls_x509_sequence *subject_alt );
107static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
108 mbedtls_x509_sequence *ext_key_usage );
109
Hanno Beckerbc685192019-03-05 15:35:31 +0000110int mbedtls_x509_crt_flush_cache_pk( mbedtls_x509_crt const *crt )
111{
112#if defined(MBEDTLS_THREADING_C)
113 if( mbedtls_mutex_lock( &crt->cache->pk_mutex ) != 0 )
114 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
115#endif
116
117#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
118 /* The cache holds a shallow copy of the PK context
119 * in the legacy struct, so don't free PK context. */
120 mbedtls_free( crt->cache->pk );
121#else
122 mbedtls_pk_free( crt->cache->pk );
123 mbedtls_free( crt->cache->pk );
124#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
125 crt->cache->pk = NULL;
126
127#if defined(MBEDTLS_THREADING_C)
128 if( mbedtls_mutex_unlock( &crt->cache->pk_mutex ) != 0 )
129 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
130#endif
131 return( 0 );
132}
133
134int mbedtls_x509_crt_flush_cache_frame( mbedtls_x509_crt const *crt )
135{
136#if defined(MBEDTLS_THREADING_C)
137 if( mbedtls_mutex_lock( &crt->cache->frame_mutex ) != 0 )
138 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
139#endif
140
141 mbedtls_free( crt->cache->frame );
142 crt->cache->frame = NULL;
143
144#if defined(MBEDTLS_THREADING_C)
145 if( mbedtls_mutex_unlock( &crt->cache->frame_mutex ) != 0 )
146 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
147#endif
148 return( 0 );
149}
150
151int mbedtls_x509_crt_flush_cache( mbedtls_x509_crt const *crt )
152{
153 int ret;
154 ret = mbedtls_x509_crt_flush_cache_frame( crt );
155 if( ret != 0 )
156 return( ret );
157 ret = mbedtls_x509_crt_flush_cache_pk( crt );
158 if( ret != 0 )
159 return( ret );
160 return( 0 );
161}
162
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000163static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000164int mbedtls_x509_crt_cache_provide_frame( mbedtls_x509_crt const *crt )
Hanno Becker337088a2019-02-25 14:53:14 +0000165{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000166 mbedtls_x509_crt_cache *cache = crt->cache;
Hanno Becker337088a2019-02-25 14:53:14 +0000167 mbedtls_x509_crt_frame *frame;
168
Hanno Becker76428352019-03-05 15:29:23 +0000169 if( cache->frame != NULL )
170 return( 0 );
171
Hanno Becker337088a2019-02-25 14:53:14 +0000172 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
173 if( frame == NULL )
174 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000175 cache->frame = frame;
Hanno Becker337088a2019-02-25 14:53:14 +0000176
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000177#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
178 /* This would work with !MBEDTLS_X509_ON_DEMAND_PARSING, too,
179 * but is inefficient compared to copying the respective fields
180 * from the legacy mbedtls_x509_crt. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000181 return( x509_crt_parse_frame( crt->raw.p,
182 crt->raw.p + crt->raw.len,
183 frame ) );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000184#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
185 /* Make sure all extension related fields are properly initialized. */
186 frame->ca_istrue = 0;
187 frame->max_pathlen = 0;
188 frame->ext_types = 0;
189 frame->version = crt->version;
190 frame->sig_md = crt->sig_md;
191 frame->sig_pk = crt->sig_pk;
192 frame->valid_from = crt->valid_from;
193 frame->valid_to = crt->valid_to;
Hanno Becker38f0cb42019-03-04 15:13:45 +0000194 x509_buf_to_buf_raw( &frame->raw, &crt->raw );
195 x509_buf_to_buf_raw( &frame->tbs, &crt->tbs );
196 x509_buf_to_buf_raw( &frame->serial, &crt->serial );
197 x509_buf_to_buf_raw( &frame->pubkey_raw, &crt->pk_raw );
198 x509_buf_to_buf_raw( &frame->issuer_raw, &crt->issuer_raw );
199 x509_buf_to_buf_raw( &frame->subject_raw, &crt->subject_raw );
200 x509_buf_to_buf_raw( &frame->subject_id, &crt->subject_id );
201 x509_buf_to_buf_raw( &frame->issuer_id, &crt->issuer_id );
202 x509_buf_to_buf_raw( &frame->sig, &crt->sig );
203 x509_buf_to_buf_raw( &frame->v3_ext, &crt->v3_ext );
Hanno Beckerea32d8b2019-03-04 11:52:23 +0000204
205 /* The legacy CRT structure doesn't explicitly contain
206 * the `AlgorithmIdentifier` bounds; however, those can
207 * be inferred from the surrounding (mandatory) `SerialNumber`
208 * and `Issuer` fields. */
209 frame->sig_alg.p = crt->serial.p + crt->serial.len;
210 frame->sig_alg.len = crt->issuer_raw.p - frame->sig_alg.p;
211
212 return( x509_crt_frame_parse_ext( frame ) );
213#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000214}
Hanno Becker337088a2019-02-25 14:53:14 +0000215
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000216int mbedtls_x509_crt_cache_provide_pk( mbedtls_x509_crt const *crt )
217{
218 mbedtls_x509_crt_cache *cache = crt->cache;
219 mbedtls_pk_context *pk;
220
Hanno Becker76428352019-03-05 15:29:23 +0000221 if( cache->pk != NULL )
222 return( 0 );
223
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000224 pk = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) );
225 if( pk == NULL )
226 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000227 cache->pk = pk;
Hanno Becker180f7bf2019-02-28 13:23:38 +0000228
229#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
230 *pk = crt->pk;
Hanno Becker337088a2019-02-25 14:53:14 +0000231 return( 0 );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000232#else
233 {
234 mbedtls_x509_buf_raw pk_raw = cache->pk_raw;
235 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
236 pk_raw.p + pk_raw.len,
237 pk ) );
238 }
239#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker337088a2019-02-25 14:53:14 +0000240}
241
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000242static void x509_crt_cache_init( mbedtls_x509_crt_cache *cache )
Hanno Becker337088a2019-02-25 14:53:14 +0000243{
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000244 memset( cache, 0, sizeof( *cache ) );
245#if defined(MBEDTLS_THREADING_C)
246 mbedtls_mutex_init( &cache->frame_mutex );
247 mbedtls_mutex_init( &cache->pk_mutex );
248#endif
249}
250
251static void x509_crt_cache_clear_pk( mbedtls_x509_crt_cache *cache )
252{
Hanno Becker180f7bf2019-02-28 13:23:38 +0000253#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000254 /* The cache holds a shallow copy of the PK context
255 * in the legacy struct, so don't free PK context. */
256 mbedtls_free( cache->pk );
Hanno Becker180f7bf2019-02-28 13:23:38 +0000257#else
258 mbedtls_pk_free( cache->pk );
259 mbedtls_free( cache->pk );
260#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
261
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000262 cache->pk = NULL;
263}
264
265static void x509_crt_cache_clear_frame( mbedtls_x509_crt_cache *cache )
266{
267 mbedtls_free( cache->frame );
268 cache->frame = NULL;
269}
270
271static void x509_crt_cache_free( mbedtls_x509_crt_cache *cache )
272{
273 if( cache == NULL )
Hanno Becker337088a2019-02-25 14:53:14 +0000274 return;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +0000275
276#if defined(MBEDTLS_THREADING_C)
277 mbedtls_mutex_free( &cache->frame_mutex );
278 mbedtls_mutex_free( &cache->pk_mutex );
279#endif
280
281 x509_crt_cache_clear_frame( cache );
282 x509_crt_cache_clear_pk( cache );
283
284 memset( cache, 0, sizeof( *cache ) );
Hanno Becker337088a2019-02-25 14:53:14 +0000285}
286
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000287int mbedtls_x509_crt_get_subject_alt_names( mbedtls_x509_crt const *crt,
288 mbedtls_x509_sequence **subj_alt )
289{
290 int ret;
291 mbedtls_x509_crt_frame *frame;
292 mbedtls_x509_sequence *seq;
293
294 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
295 if( ret != 0 )
296 return( ret );
297
298 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
299 if( seq == NULL )
300 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
301 else
302 ret = x509_crt_subject_alt_from_frame( frame, seq );
303
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000304 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000305
306 *subj_alt = seq;
307 return( ret );
308}
309
310int mbedtls_x509_crt_get_ext_key_usage( mbedtls_x509_crt const *crt,
311 mbedtls_x509_sequence **ext_key_usage )
312{
313 int ret;
314 mbedtls_x509_crt_frame *frame;
315 mbedtls_x509_sequence *seq;
316
317 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
318 if( ret != 0 )
319 return( ret );
320
321 seq = mbedtls_calloc( 1, sizeof( mbedtls_x509_sequence ) );
322 if( seq == NULL )
323 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
324 else
325 ret = x509_crt_ext_key_usage_from_frame( frame, seq );
326
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000327 mbedtls_x509_crt_frame_release( crt );
Hanno Beckerab6c8ea2019-02-27 17:33:14 +0000328
329 *ext_key_usage = seq;
330 return( ret );
331}
332
Hanno Becker63e69982019-02-26 18:50:49 +0000333int mbedtls_x509_crt_get_subject( mbedtls_x509_crt const *crt,
334 mbedtls_x509_name **subject )
335{
336 int ret;
337 mbedtls_x509_crt_frame *frame;
338 mbedtls_x509_name *name;
339
340 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
341 if( ret != 0 )
342 return( ret );
343
344 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
345 if( name == NULL )
346 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
347 else
348 ret = x509_crt_subject_from_frame( frame, name );
349
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000350 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000351
352 *subject = name;
353 return( ret );
354}
355
356int mbedtls_x509_crt_get_issuer( mbedtls_x509_crt const *crt,
357 mbedtls_x509_name **issuer )
358{
359 int ret;
360 mbedtls_x509_crt_frame *frame;
361 mbedtls_x509_name *name;
362
363 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
364 if( ret != 0 )
365 return( ret );
366
367 name = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
368 if( name == NULL )
369 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
370 else
371 ret = x509_crt_issuer_from_frame( frame, name );
372
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000373 mbedtls_x509_crt_frame_release( crt );
Hanno Becker63e69982019-02-26 18:50:49 +0000374
375 *issuer = name;
376 return( ret );
377}
378
Hanno Becker823efad2019-02-28 13:23:58 +0000379int mbedtls_x509_crt_get_frame( mbedtls_x509_crt const *crt,
380 mbedtls_x509_crt_frame *dst )
381{
382 int ret;
383 mbedtls_x509_crt_frame *frame;
384 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
385 if( ret != 0 )
386 return( ret );
387 *dst = *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000388 mbedtls_x509_crt_frame_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000389 return( 0 );
390}
391
392int mbedtls_x509_crt_get_pk( mbedtls_x509_crt const *crt,
393 mbedtls_pk_context *dst )
394{
395#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
396 mbedtls_x509_buf_raw pk_raw = crt->cache->pk_raw;
397 return( mbedtls_pk_parse_subpubkey( &pk_raw.p,
398 pk_raw.p + pk_raw.len,
399 dst ) );
400#else /* !MBEDTLS_X509_ON_DEMAND_PARSING */
401 int ret;
402 mbedtls_pk_context *pk;
403 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
404 if( ret != 0 )
405 return( ret );
406
407 /* Move PK from CRT cache to destination pointer
408 * to avoid a copy. */
409 *dst = *pk;
410 mbedtls_free( crt->cache->pk );
411 crt->cache->pk = NULL;
412
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +0000413 mbedtls_x509_crt_pk_release( crt );
Hanno Becker823efad2019-02-28 13:23:58 +0000414 return( 0 );
415#endif /* MBEDTLS_X509_ON_DEMAND_PARSING */
416}
417
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200418/*
419 * Item in a verification chain: cert and flags for it
420 */
421typedef struct {
422 mbedtls_x509_crt *crt;
423 uint32_t flags;
424} x509_crt_verify_chain_item;
425
426/*
427 * Max size of verification chain: end-entity + intermediates + trusted root
428 */
429#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200430
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200432 * Default profile
433 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200434const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
435{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200436#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200437 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200438 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200439#endif
440 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200441 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
442 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
443 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
444 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
445 0xFFFFFFF, /* Any PK alg */
446 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200447 2048,
448};
449
450/*
451 * Next-default profile
452 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200453const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
454{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200455 /* Hashes from SHA-256 and above */
456 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
457 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
458 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
459 0xFFFFFFF, /* Any PK alg */
460#if defined(MBEDTLS_ECP_C)
461 /* Curves at or above 128-bit security level */
462 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
463 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
464 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
465 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
466 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
467 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
468 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
469#else
470 0,
471#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200472 2048,
473};
474
475/*
476 * NSA Suite B Profile
477 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200478const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
479{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200480 /* Only SHA-256 and 384 */
481 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
482 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
483 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200484 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
485 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200486#if defined(MBEDTLS_ECP_C)
487 /* Only NIST P-256 and P-384 */
488 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
489 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
490#else
491 0,
492#endif
493 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200494};
495
496/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200497 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200498 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200499 */
500static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
501 mbedtls_md_type_t md_alg )
502{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200503 if( md_alg == MBEDTLS_MD_NONE )
504 return( -1 );
505
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200506 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
507 return( 0 );
508
509 return( -1 );
510}
511
512/*
513 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200514 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200515 */
516static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
517 mbedtls_pk_type_t pk_alg )
518{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200519 if( pk_alg == MBEDTLS_PK_NONE )
520 return( -1 );
521
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200522 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
523 return( 0 );
524
525 return( -1 );
526}
527
528/*
529 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200530 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200531 */
532static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200533 const mbedtls_pk_context *pk )
534{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200535 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200536
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200537#if defined(MBEDTLS_RSA_C)
538 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
539 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200540 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200541 return( 0 );
542
543 return( -1 );
544 }
545#endif
546
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200547#if defined(MBEDTLS_ECP_C)
548 if( pk_alg == MBEDTLS_PK_ECDSA ||
549 pk_alg == MBEDTLS_PK_ECKEY ||
550 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200551 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200552 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200553
Philippe Antoineb5b25432018-05-11 11:06:29 +0200554 if( gid == MBEDTLS_ECP_DP_NONE )
555 return( -1 );
556
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200557 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
558 return( 0 );
559
560 return( -1 );
561 }
562#endif
563
564 return( -1 );
565}
566
567/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000568 * Return 0 if name matches wildcard, -1 otherwise
569 */
Hanno Becker24926222019-02-21 13:10:55 +0000570static int x509_check_wildcard( char const *cn,
571 size_t cn_len,
572 unsigned char const *buf,
573 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000574{
575 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000576 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000577
578 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000579 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000580 return( -1 );
581
582 for( i = 0; i < cn_len; ++i )
583 {
584 if( cn[i] == '.' )
585 {
586 cn_idx = i;
587 break;
588 }
589 }
590
591 if( cn_idx == 0 )
592 return( -1 );
593
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000594 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
595 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000596 {
597 return( 0 );
598 }
599
600 return( -1 );
601}
602
603/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200604 * Reset (init or clear) a verify_chain
605 */
606static void x509_crt_verify_chain_reset(
607 mbedtls_x509_crt_verify_chain *ver_chain )
608{
609 size_t i;
610
611 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
612 {
613 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000614 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200615 }
616
617 ver_chain->len = 0;
618}
619
620/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
622 */
623static int x509_get_version( unsigned char **p,
624 const unsigned char *end,
625 int *ver )
626{
627 int ret;
628 size_t len;
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
631 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 {
635 *ver = 0;
636 return( 0 );
637 }
638
Hanno Becker2f472142019-02-12 11:52:10 +0000639 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 }
641
642 end = *p + len;
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
645 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646
647 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 return( MBEDTLS_ERR_X509_INVALID_VERSION +
649 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
651 return( 0 );
652}
653
654/*
655 * Validity ::= SEQUENCE {
656 * notBefore Time,
657 * notAfter Time }
658 */
659static int x509_get_dates( unsigned char **p,
660 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_x509_time *from,
662 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663{
664 int ret;
665 size_t len;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
668 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
669 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
671 end = *p + len;
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 return( ret );
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 return( ret );
678
679 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_X509_INVALID_DATE +
681 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
683 return( 0 );
684}
685
686/*
687 * X.509 v2/v3 unique identifier (not parsed)
688 */
689static int x509_get_uid( unsigned char **p,
690 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692{
693 int ret;
694
695 if( *p == end )
696 return( 0 );
697
698 uid->tag = **p;
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
701 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 return( 0 );
705
Hanno Becker2f472142019-02-12 11:52:10 +0000706 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707 }
708
709 uid->p = *p;
710 *p += uid->len;
711
712 return( 0 );
713}
714
715static int x509_get_basic_constraints( unsigned char **p,
716 const unsigned char *end,
717 int *ca_istrue,
718 int *max_pathlen )
719{
720 int ret;
721 size_t len;
722
723 /*
724 * BasicConstraints ::= SEQUENCE {
725 * cA BOOLEAN DEFAULT FALSE,
726 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
727 */
728 *ca_istrue = 0; /* DEFAULT FALSE */
729 *max_pathlen = 0; /* endless */
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
732 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000733 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734
735 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200736 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
741 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742
743 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000744 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745
746 if( *ca_istrue != 0 )
747 *ca_istrue = 1;
748 }
749
750 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200751 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000754 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755
756 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000757 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758
759 (*max_pathlen)++;
760
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200761 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762}
763
764static int x509_get_ns_cert_type( unsigned char **p,
765 const unsigned char *end,
766 unsigned char *ns_cert_type)
767{
768 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000772 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773
774 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000775 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776
777 /* Get actual bitstring */
778 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200779 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780}
781
782static int x509_get_key_usage( unsigned char **p,
783 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100784 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785{
786 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200787 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000791 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792
793 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000794 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795
796 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200797 *key_usage = 0;
798 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
799 {
800 *key_usage |= (unsigned int) bs.p[i] << (8*i);
801 }
802
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200803 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804}
805
806/*
807 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
808 *
809 * KeyPurposeId ::= OBJECT IDENTIFIER
810 */
811static int x509_get_ext_key_usage( unsigned char **p,
812 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000815 return( mbedtls_asn1_get_sequence_of( p, end, ext_key_usage,
816 MBEDTLS_ASN1_OID ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817}
818
819/*
820 * SubjectAltName ::= GeneralNames
821 *
822 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
823 *
824 * GeneralName ::= CHOICE {
825 * otherName [0] OtherName,
826 * rfc822Name [1] IA5String,
827 * dNSName [2] IA5String,
828 * x400Address [3] ORAddress,
829 * directoryName [4] Name,
830 * ediPartyName [5] EDIPartyName,
831 * uniformResourceIdentifier [6] IA5String,
832 * iPAddress [7] OCTET STRING,
833 * registeredID [8] OBJECT IDENTIFIER }
834 *
835 * OtherName ::= SEQUENCE {
836 * type-id OBJECT IDENTIFIER,
837 * value [0] EXPLICIT ANY DEFINED BY type-id }
838 *
839 * EDIPartyName ::= SEQUENCE {
840 * nameAssigner [0] DirectoryString OPTIONAL,
841 * partyName [1] DirectoryString }
842 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000843 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844 */
Hanno Beckerad462192019-02-21 13:32:31 +0000845static int x509_get_subject_alt_name_cb( void *ctx,
846 int tag,
847 unsigned char *data,
848 size_t data_len )
849{
850 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
851 mbedtls_asn1_sequence *cur = *cur_ptr;
852
Hanno Beckerad462192019-02-21 13:32:31 +0000853 /* Allocate and assign next pointer */
854 if( cur->buf.p != NULL )
855 {
856 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
857 if( cur->next == NULL )
Hanno Becker90b94082019-02-21 21:13:21 +0000858 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Hanno Beckerad462192019-02-21 13:32:31 +0000859 cur = cur->next;
860 }
861
862 cur->buf.tag = tag;
863 cur->buf.p = data;
864 cur->buf.len = data_len;
865
866 *cur_ptr = cur;
867 return( 0 );
868}
869
Hanno Becker5984d302019-02-21 14:46:54 +0000870static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000874 return( mbedtls_asn1_traverse_sequence_of( &p, end,
875 MBEDTLS_ASN1_TAG_CLASS_MASK,
876 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
877 MBEDTLS_ASN1_TAG_VALUE_MASK,
878 2 /* SubjectAlt DNS */,
879 x509_get_subject_alt_name_cb,
880 (void*) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881}
882
883/*
884 * X.509 v3 extensions
885 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000887static int x509_crt_get_ext_cb( void *ctx,
888 int tag,
889 unsigned char *p,
890 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891{
892 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000893 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000895 unsigned char *end, *end_ext_octet;
896 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
897 int is_critical = 0; /* DEFAULT FALSE */
898 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000900 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000901
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000902 /*
903 * Extension ::= SEQUENCE {
904 * extnID OBJECT IDENTIFIER,
905 * critical BOOLEAN DEFAULT FALSE,
906 * extnValue OCTET STRING }
907 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000909 end = p + ext_len;
910
911 /* Get extension ID */
912 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
913 MBEDTLS_ASN1_OID ) ) != 0 )
914 goto err;
915
916 extn_oid.tag = MBEDTLS_ASN1_OID;
917 extn_oid.p = p;
918 p += extn_oid.len;
919
920 /* Get optional critical */
921 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
922 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
923 goto err;
924
925 /* Data should be octet string type */
926 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
927 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
928 goto err;
929
930 end_ext_octet = p + len;
931 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000933 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
934 goto err;
935 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000937 /*
938 * Detect supported extensions
939 */
940 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
941 if( ret != 0 )
942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000944 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000946 /* Data is marked as critical: fail */
947 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
948 goto err;
949 }
950#endif
951 return( 0 );
952 }
953
954 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +0000955 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000956 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
957
Hanno Becker21f55672019-02-15 15:27:59 +0000958 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000959 switch( ext_type )
960 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100961 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000962 {
963 int ca_istrue;
964 int max_pathlen;
965
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000967 ret = x509_get_basic_constraints( &p, end_ext_octet,
968 &ca_istrue,
969 &max_pathlen );
970 if( ret != 0 )
971 goto err;
972
Hanno Becker21f55672019-02-15 15:27:59 +0000973 frame->ca_istrue = ca_istrue;
974 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000976 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000980 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +0000981 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000982 if( ret != 0 )
983 goto err;
984 break;
985
986 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
987 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +0000988 frame->subject_alt_raw.p = p;
989 frame->subject_alt_raw.len = end_ext_octet - p;
990
991 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
992 MBEDTLS_ASN1_TAG_CLASS_MASK,
993 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
994 MBEDTLS_ASN1_TAG_VALUE_MASK,
995 2 /* SubjectAlt DNS */,
996 NULL, NULL );
997 if( ret != 0 )
998 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 break;
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001003 frame->ext_key_usage_raw.p = p;
1004 frame->ext_key_usage_raw.len = end_ext_octet - p;
1005 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001006 {
Hanno Becker21f55672019-02-15 15:27:59 +00001007 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1008 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001009 }
Hanno Becker21f55672019-02-15 15:27:59 +00001010
1011 /* Check structural sanity of extension. */
1012 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1013 0xFF, MBEDTLS_ASN1_OID,
1014 0, 0, NULL, NULL );
1015 if( ret != 0 )
1016 goto err;
1017
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 break;
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001022 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001023 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001024 if( ret != 0 )
1025 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 break;
1027
1028 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001029 /*
1030 * If this is a non-critical extension, which the oid layer
1031 * supports, but there isn't an X.509 parser for it,
1032 * skip the extension.
1033 */
1034#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1035 if( is_critical )
1036 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1037#endif
1038 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 }
1040
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001042
1043err:
1044 return( ret );
1045}
1046
Hanno Becker21f55672019-02-15 15:27:59 +00001047static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001048{
1049 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001050 unsigned char *p = frame->v3_ext.p;
1051 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001052
Hanno Becker21f55672019-02-15 15:27:59 +00001053 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001054 return( 0 );
1055
Hanno Becker21f55672019-02-15 15:27:59 +00001056 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001057 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001058 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001059
1060 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1061 return( ret );
1062 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1063 return( ret );
1064
1065 if( ret != 0 )
1066 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1067
1068 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069}
1070
Hanno Becker21f55672019-02-15 15:27:59 +00001071static int x509_crt_parse_frame( unsigned char *start,
1072 unsigned char *end,
1073 mbedtls_x509_crt_frame *frame )
1074{
1075 int ret;
1076 unsigned char *p;
1077 size_t len;
1078
1079 mbedtls_x509_buf tmp;
1080 unsigned char *tbs_start;
1081
1082 mbedtls_x509_buf outer_sig_alg;
1083 size_t inner_sig_alg_len;
1084 unsigned char *inner_sig_alg_start;
1085
1086 memset( frame, 0, sizeof( *frame ) );
1087
1088 /*
1089 * Certificate ::= SEQUENCE {
1090 * tbsCertificate TBSCertificate,
1091 * signatureAlgorithm AlgorithmIdentifier,
1092 * signatureValue BIT STRING
1093 * }
1094 *
1095 */
1096 p = start;
1097
1098 frame->raw.p = p;
1099 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1100 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1101 {
1102 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1103 }
1104
1105 /* NOTE: We are currently not checking that the `Certificate`
1106 * structure spans the entire buffer. */
1107 end = p + len;
1108 frame->raw.len = end - frame->raw.p;
1109
1110 /*
1111 * TBSCertificate ::= SEQUENCE { ...
1112 */
1113 frame->tbs.p = p;
1114 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1115 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1116 {
1117 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1118 }
1119 tbs_start = p;
1120
1121 /* Breadth-first parsing: Jump over TBS for now. */
1122 p += len;
1123 frame->tbs.len = p - frame->tbs.p;
1124
1125 /*
1126 * AlgorithmIdentifier ::= SEQUENCE { ...
1127 */
1128 outer_sig_alg.p = p;
1129 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1131 {
1132 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1133 }
1134 p += len;
1135 outer_sig_alg.len = p - outer_sig_alg.p;
1136
1137 /*
1138 * signatureValue BIT STRING
1139 */
1140 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1141 if( ret != 0 )
1142 return( ret );
1143 frame->sig.p = tmp.p;
1144 frame->sig.len = tmp.len;
1145
1146 /* Check that we consumed the entire `Certificate` structure. */
1147 if( p != end )
1148 {
1149 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1150 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1151 }
1152
1153 /* Parse TBSCertificate structure
1154 *
1155 * TBSCertificate ::= SEQUENCE {
1156 * version [0] EXPLICIT Version DEFAULT v1,
1157 * serialNumber CertificateSerialNumber,
1158 * signature AlgorithmIdentifier,
1159 * issuer Name,
1160 * validity Validity,
1161 * subject Name,
1162 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1163 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1164 * -- If present, version MUST be v2 or v3
1165 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1166 * -- If present, version MUST be v2 or v3
1167 * extensions [3] EXPLICIT Extensions OPTIONAL
1168 * -- If present, version MUST be v3
1169 * }
1170 */
1171 end = frame->tbs.p + frame->tbs.len;
1172 p = tbs_start;
1173
1174 /*
1175 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1176 */
1177 {
1178 int version;
1179 ret = x509_get_version( &p, end, &version );
1180 if( ret != 0 )
1181 return( ret );
1182
1183 if( version < 0 || version > 2 )
1184 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1185
1186 frame->version = version + 1;
1187 }
1188
1189 /*
1190 * CertificateSerialNumber ::= INTEGER
1191 */
1192 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1193 if( ret != 0 )
1194 return( ret );
1195
1196 frame->serial.p = tmp.p;
1197 frame->serial.len = tmp.len;
1198
1199 /*
1200 * signature AlgorithmIdentifier
1201 */
1202 inner_sig_alg_start = p;
1203 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1204 &frame->sig_pk, NULL );
1205 if( ret != 0 )
1206 return( ret );
1207 inner_sig_alg_len = p - inner_sig_alg_start;
1208
1209 frame->sig_alg.p = inner_sig_alg_start;
1210 frame->sig_alg.len = inner_sig_alg_len;
1211
1212 /* Consistency check:
1213 * Inner and outer AlgorithmIdentifier structures must coincide:
1214 *
1215 * Quoting RFC 5280, Section 4.1.1.2:
1216 * This field MUST contain the same algorithm identifier as the
1217 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1218 */
1219 if( outer_sig_alg.len != inner_sig_alg_len ||
1220 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1221 {
1222 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1223 }
1224
1225 /*
1226 * issuer Name
1227 *
1228 * Name ::= CHOICE { -- only one possibility for now --
1229 * rdnSequence RDNSequence }
1230 *
1231 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1232 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001233 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001234
1235 ret = mbedtls_asn1_get_tag( &p, end, &len,
1236 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1237 if( ret != 0 )
1238 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001239 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001240 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001241
1242 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1243 &frame->issuer_raw,
1244 NULL, NULL );
1245 if( ret != 0 )
1246 return( ret );
1247
Hanno Becker21f55672019-02-15 15:27:59 +00001248 /*
1249 * Validity ::= SEQUENCE { ...
1250 */
1251 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1252 if( ret != 0 )
1253 return( ret );
1254
1255 /*
1256 * subject Name
1257 *
1258 * Name ::= CHOICE { -- only one possibility for now --
1259 * rdnSequence RDNSequence }
1260 *
1261 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1262 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001263 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001264
1265 ret = mbedtls_asn1_get_tag( &p, end, &len,
1266 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1267 if( ret != 0 )
1268 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001269 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001270 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001271
1272 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1273 &frame->subject_raw,
1274 NULL, NULL );
1275 if( ret != 0 )
1276 return( ret );
1277
Hanno Becker21f55672019-02-15 15:27:59 +00001278 /*
1279 * SubjectPublicKeyInfo
1280 */
1281 frame->pubkey_raw.p = p;
1282 ret = mbedtls_asn1_get_tag( &p, end, &len,
1283 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1284 if( ret != 0 )
1285 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1286 p += len;
1287 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1288
1289 /*
1290 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1291 * -- If present, version shall be v2 or v3
1292 */
1293 if( frame->version == 2 || frame->version == 3 )
1294 {
1295 ret = x509_get_uid( &p, end, &tmp, 1 /* implicit tag */ );
1296 if( ret != 0 )
1297 return( ret );
1298
1299 frame->issuer_id.p = tmp.p;
1300 frame->issuer_id.len = tmp.len;
1301 }
1302
1303 /*
1304 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1305 * -- If present, version shall be v2 or v3
1306 */
1307 if( frame->version == 2 || frame->version == 3 )
1308 {
1309 ret = x509_get_uid( &p, end, &tmp, 2 /* implicit tag */ );
1310 if( ret != 0 )
1311 return( ret );
1312
1313 frame->subject_id.p = tmp.p;
1314 frame->subject_id.len = tmp.len;
1315 }
1316
1317 /*
1318 * extensions [3] EXPLICIT Extensions OPTIONAL
1319 * -- If present, version shall be v3
1320 */
1321#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1322 if( frame->version == 3 )
1323#endif
1324 {
1325 if( p != end )
1326 {
1327 ret = mbedtls_asn1_get_tag( &p, end, &len,
1328 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1329 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1330 if( len == 0 )
1331 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1332 if( ret != 0 )
1333 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1334
1335 frame->v3_ext.p = p;
1336 frame->v3_ext.len = len;
1337
1338 p += len;
1339 }
1340
1341 ret = x509_crt_frame_parse_ext( frame );
1342 if( ret != 0 )
1343 return( ret );
1344 }
1345
1346 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1347 if( p != end )
1348 {
1349 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1350 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1351 }
1352
1353 return( 0 );
1354}
1355
1356static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
1357 mbedtls_x509_name *subject )
1358{
Hanno Becker1e11f212019-03-04 14:43:43 +00001359 return( mbedtls_x509_get_name( frame->subject_raw.p,
1360 frame->subject_raw.len,
1361 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001362}
1363
1364static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
1365 mbedtls_x509_name *issuer )
1366{
Hanno Becker1e11f212019-03-04 14:43:43 +00001367 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1368 frame->issuer_raw.len,
1369 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001370}
1371
1372static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
1373 mbedtls_x509_sequence *subject_alt )
1374{
1375 int ret;
1376 unsigned char *p = frame->subject_alt_raw.p;
1377 unsigned char *end = p + frame->subject_alt_raw.len;
1378
1379 memset( subject_alt, 0, sizeof( *subject_alt ) );
1380
1381 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1382 return( 0 );
1383
1384 ret = x509_get_subject_alt_name( p, end, subject_alt );
1385 if( ret != 0 )
1386 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1387 return( ret );
1388}
1389
1390static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
1391 mbedtls_x509_sequence *ext_key_usage )
1392{
1393 int ret;
1394 unsigned char *p = frame->ext_key_usage_raw.p;
1395 unsigned char *end = p + frame->ext_key_usage_raw.len;
1396
1397 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1398
1399 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1400 return( 0 );
1401
1402 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1403 if( ret != 0 )
1404 {
1405 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1406 return( ret );
1407 }
1408
1409 return( 0 );
1410}
1411
Hanno Becker180f7bf2019-02-28 13:23:38 +00001412#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001413static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1414 mbedtls_pk_context *pk )
1415{
1416 unsigned char *p = frame->pubkey_raw.p;
1417 unsigned char *end = p + frame->pubkey_raw.len;
1418 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1419}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001420#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001421
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422/*
1423 * Parse and fill a single X.509 certificate in DER format
1424 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001425static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1426 const unsigned char *buf,
1427 size_t buflen,
1428 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429{
1430 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001431 mbedtls_x509_crt_frame *frame;
1432 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001433
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436
Hanno Becker21f55672019-02-15 15:27:59 +00001437 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438 {
Hanno Becker21f55672019-02-15 15:27:59 +00001439 crt->raw.p = (unsigned char*) buf;
1440 crt->raw.len = buflen;
1441 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001442 }
1443 else
1444 {
Hanno Becker21f55672019-02-15 15:27:59 +00001445 crt->raw.p = mbedtls_calloc( 1, buflen );
1446 if( crt->raw.p == NULL )
1447 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1448 crt->raw.len = buflen;
1449 memcpy( crt->raw.p, buf, buflen );
1450
1451 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001452 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001453
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001454 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1455 if( cache == NULL )
1456 {
1457 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1458 goto exit;
1459 }
1460 crt->cache = cache;
1461 x509_crt_cache_init( cache );
1462
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001463#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1464
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001465 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001466 if( ret != 0 )
1467 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468
Hanno Becker76428352019-03-05 15:29:23 +00001469 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001470
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001471#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001473 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1474 if( frame == NULL )
1475 {
1476 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1477 goto exit;
1478 }
1479 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001480
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001481 ret = x509_crt_parse_frame( crt->raw.p,
1482 crt->raw.p + crt->raw.len,
1483 frame );
1484 if( ret != 0 )
1485 goto exit;
1486
Hanno Becker21f55672019-02-15 15:27:59 +00001487 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1488 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001489 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1490 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1491 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1492 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
1493 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1494 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
1495 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1496 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1497 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001498 crt->valid_from = frame->valid_from;
1499 crt->valid_to = frame->valid_to;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001500 crt->version = frame->version;
1501 crt->ca_istrue = frame->ca_istrue;
1502 crt->max_pathlen = frame->max_pathlen;
1503 crt->ext_types = frame->ext_types;
1504 crt->key_usage = frame->key_usage;
1505 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506
1507 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001508 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001511 {
Hanno Becker21f55672019-02-15 15:27:59 +00001512 /* sig_oid: Previously, needed for convenience in
1513 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001514 unsigned char *tmp = frame->sig_alg.p;
1515 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001516 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517
Hanno Becker21f55672019-02-15 15:27:59 +00001518 ret = mbedtls_x509_get_alg( &tmp, end,
1519 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520 if( ret != 0 )
1521 {
Hanno Becker21f55672019-02-15 15:27:59 +00001522 /* This should never happen, because we check
1523 * the sanity of the AlgorithmIdentifier structure
1524 * during frame parsing. */
1525 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1526 goto exit;
1527 }
1528 crt->sig_oid = sig_oid;
1529
1530 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001531 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001532 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1533 &crt->sig_md, &crt->sig_pk,
1534 &crt->sig_opts );
1535 if( ret != 0 )
1536 {
1537 /* Again, this should never happen. */
1538 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1539 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540 }
1541 }
1542
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001543 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001544 if( ret != 0 )
1545 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001547 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001548 if( ret != 0 )
1549 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001551 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001552 if( ret != 0 )
1553 goto exit;
1554
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001555 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001556 if( ret != 0 )
1557 goto exit;
1558
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001559 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1560 if( ret != 0 )
1561 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001562#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001563
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001564 /* Currently, we accept DER encoded CRTs with trailing garbage
1565 * and promise to not account for the garbage in the `raw` field.
1566 *
1567 * Note that this means that `crt->raw.len` is not necessarily the
1568 * full size of the heap buffer allocated at `crt->raw.p` in case
1569 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1570 * need the size, and the garbage data doesn't need zeroization. */
1571 crt->raw.len = frame->raw.len;
1572
1573 cache->pk_raw = frame->pubkey_raw;
1574
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001575 /* Free the frame before parsing the public key to
1576 * keep peak RAM usage low. This is slightly inefficient
1577 * because the frame will need to be parsed again on the
1578 * first usage of the CRT, but that seems acceptable.
1579 * As soon as the frame gets used multiple times, it
1580 * will be cached by default. */
1581 x509_crt_cache_clear_frame( crt->cache );
1582
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001583 /* The cache just references the PK structure from the legacy
1584 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001585 * the cache.
1586 *
1587 * We're not actually using the parsed PK context here;
1588 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001589 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001590 if( ret != 0 )
1591 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001592 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001593
1594exit:
1595 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
Hanno Becker21f55672019-02-15 15:27:59 +00001598 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599}
1600
1601/*
1602 * Parse one X.509 certificate in DER format from a buffer and add them to a
1603 * chained list
1604 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001605static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1606 const unsigned char *buf,
1607 size_t buflen,
1608 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609{
1610 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
1613 /*
1614 * Check for valid input
1615 */
1616 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618
Hanno Becker371e0e42019-02-25 18:08:59 +00001619 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 {
1621 prev = crt;
1622 crt = crt->next;
1623 }
1624
1625 /*
1626 * Add new certificate on the end of the chain if needed.
1627 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001628 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001630 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631
1632 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001633 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634
1635 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638 }
1639
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001640 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641 {
1642 if( prev )
1643 prev->next = NULL;
1644
1645 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647
1648 return( ret );
1649 }
1650
1651 return( 0 );
1652}
1653
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001654int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1655 const unsigned char *buf,
1656 size_t buflen )
1657{
1658 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1659}
1660
1661int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1662 const unsigned char *buf,
1663 size_t buflen )
1664{
1665 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1666}
1667
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001669 * Parse one or more PEM certificates from a buffer and add them to the chained
1670 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001672int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1673 const unsigned char *buf,
1674 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675{
Janos Follath98e28a72016-05-31 14:03:54 +01001676#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001677 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001679#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680
1681 /*
1682 * Check for valid input
1683 */
1684 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686
1687 /*
1688 * Determine buffer content. Buffer contains either one DER certificate or
1689 * one or more PEM certificates.
1690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001692 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001693 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001696 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1699 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001700#else
1701 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1702#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#if defined(MBEDTLS_PEM_PARSE_C)
1705 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706 {
1707 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001709
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001710 /* 1 rather than 0 since the terminating NULL byte is counted in */
1711 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712 {
1713 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001715
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001716 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001718 "-----BEGIN CERTIFICATE-----",
1719 "-----END CERTIFICATE-----",
1720 buf, NULL, 0, &use_len );
1721
1722 if( ret == 0 )
1723 {
1724 /*
1725 * Was PEM encoded
1726 */
1727 buflen -= use_len;
1728 buf += use_len;
1729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001731 {
1732 return( ret );
1733 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001737
1738 /*
1739 * PEM header and footer were found
1740 */
1741 buflen -= use_len;
1742 buf += use_len;
1743
1744 if( first_error == 0 )
1745 first_error = ret;
1746
Paul Bakker5a5fa922014-09-26 14:53:04 +02001747 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001748 continue;
1749 }
1750 else
1751 break;
1752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756
1757 if( ret != 0 )
1758 {
1759 /*
1760 * Quit parsing on a memory error
1761 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001762 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001763 return( ret );
1764
1765 if( first_error == 0 )
1766 first_error = ret;
1767
1768 total_failed++;
1769 continue;
1770 }
1771
1772 success = 1;
1773 }
1774 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775
1776 if( success )
1777 return( total_failed );
1778 else if( first_error )
1779 return( first_error );
1780 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001782#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001783}
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786/*
1787 * Load one or more certificates and add them to the chained list
1788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001790{
1791 int ret;
1792 size_t n;
1793 unsigned char *buf;
1794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001796 return( ret );
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001800 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802
1803 return( ret );
1804}
1805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001807{
1808 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001809#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810 int w_ret;
1811 WCHAR szDir[MAX_PATH];
1812 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001813 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001814 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815
Paul Bakker9af723c2014-05-01 13:03:14 +02001816 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817 HANDLE hFind;
1818
1819 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001821
Paul Bakker9af723c2014-05-01 13:03:14 +02001822 memset( szDir, 0, sizeof(szDir) );
1823 memset( filename, 0, MAX_PATH );
1824 memcpy( filename, path, len );
1825 filename[len++] = '\\';
1826 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827 filename[len++] = '*';
1828
Simon B3c6b18d2016-11-03 01:11:37 +00001829 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001830 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001831 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833
1834 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001835 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001837
1838 len = MAX_PATH - len;
1839 do
1840 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001841 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001842
1843 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1844 continue;
1845
Paul Bakker9af723c2014-05-01 13:03:14 +02001846 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001847 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001848 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001849 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001850 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001851 {
1852 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1853 goto cleanup;
1854 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001857 if( w_ret < 0 )
1858 ret++;
1859 else
1860 ret += w_ret;
1861 }
1862 while( FindNextFileW( hFind, &file_data ) != 0 );
1863
Paul Bakker66d5d072014-06-17 16:39:18 +02001864 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001866
Ron Eldor36d90422017-01-09 15:09:16 +02001867cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001868 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001869#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001870 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001871 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001872 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001873 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001874 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001875 DIR *dir = opendir( path );
1876
Paul Bakker66d5d072014-06-17 16:39:18 +02001877 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001879
Ron Eldor63140682017-01-09 19:27:59 +02001880#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001881 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001882 {
1883 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001884 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001885 }
Ron Eldor63140682017-01-09 19:27:59 +02001886#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001887
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001888 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001889 {
Andres AGf9113192016-09-02 14:06:04 +01001890 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1891 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001892
Andres AGf9113192016-09-02 14:06:04 +01001893 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001894 {
Andres AGf9113192016-09-02 14:06:04 +01001895 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1896 goto cleanup;
1897 }
1898 else if( stat( entry_name, &sb ) == -1 )
1899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001901 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001902 }
1903
1904 if( !S_ISREG( sb.st_mode ) )
1905 continue;
1906
1907 // Ignore parse errors
1908 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001910 if( t_ret < 0 )
1911 ret++;
1912 else
1913 ret += t_ret;
1914 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001915
1916cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001917 closedir( dir );
1918
Ron Eldor63140682017-01-09 19:27:59 +02001919#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001920 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001922#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001923
Paul Bakkerbe089b02013-10-14 15:51:50 +02001924#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001925
1926 return( ret );
1927}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929
Hanno Becker02a21932019-06-10 15:08:43 +01001930#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001931static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001933{
1934 size_t i;
1935 size_t n = *size;
1936 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001938 const char *sep = "";
1939 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001940
1941 while( cur != NULL )
1942 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001943 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001944 {
1945 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001946 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001947 }
1948
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001949 n -= cur->buf.len + sep_len;
1950 for( i = 0; i < sep_len; i++ )
1951 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001952 for( i = 0; i < cur->buf.len; i++ )
1953 *p++ = cur->buf.p[i];
1954
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001955 sep = ", ";
1956 sep_len = 2;
1957
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001958 cur = cur->next;
1959 }
1960
1961 *p = '\0';
1962
1963 *size = n;
1964 *buf = p;
1965
1966 return( 0 );
1967}
1968
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001969#define PRINT_ITEM(i) \
1970 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001972 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001973 sep = ", "; \
1974 }
1975
1976#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001977 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001978 PRINT_ITEM( name );
1979
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001980static int x509_info_cert_type( char **buf, size_t *size,
1981 unsigned char ns_cert_type )
1982{
1983 int ret;
1984 size_t n = *size;
1985 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001986 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001989 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1991 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1992 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001993 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1994 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1995 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001996
1997 *size = n;
1998 *buf = p;
1999
2000 return( 0 );
2001}
2002
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002003#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002004 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002005 PRINT_ITEM( name );
2006
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002007static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002008 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002009{
2010 int ret;
2011 size_t n = *size;
2012 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002013 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2016 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002017 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2018 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2019 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2021 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002022 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2023 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002024
2025 *size = n;
2026 *buf = p;
2027
2028 return( 0 );
2029}
2030
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002031static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002033{
2034 int ret;
2035 const char *desc;
2036 size_t n = *size;
2037 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002039 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002040
2041 while( cur != NULL )
2042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002044 desc = "???";
2045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002047 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002048
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002049 sep = ", ";
2050
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002051 cur = cur->next;
2052 }
2053
2054 *size = n;
2055 *buf = p;
2056
2057 return( 0 );
2058}
2059
Hanno Becker4f869ed2019-02-24 16:47:57 +00002060typedef struct mbedtls_x509_crt_sig_info
2061{
2062 mbedtls_md_type_t sig_md;
2063 mbedtls_pk_type_t sig_pk;
2064 void *sig_opts;
2065 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
2066 size_t crt_hash_len;
2067 mbedtls_x509_buf_raw sig;
2068 mbedtls_x509_buf_raw issuer_raw;
2069} mbedtls_x509_crt_sig_info;
2070
2071static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2072{
2073#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2074 mbedtls_free( info->sig_opts );
2075#else
2076 ((void) info);
2077#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2078}
2079
2080static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2081 mbedtls_x509_crt_sig_info *info )
2082{
2083 const mbedtls_md_info_t *md_info;
2084
2085 md_info = mbedtls_md_info_from_type( frame->sig_md );
2086 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2087 info->crt_hash ) != 0 )
2088 {
2089 /* Note: this can't happen except after an internal error */
2090 return( -1 );
2091 }
2092
2093 info->crt_hash_len = mbedtls_md_get_size( md_info );
2094
2095 /* Make sure that this function leaves the target structure
2096 * ready to be freed, regardless of success of failure. */
2097 info->sig_opts = NULL;
2098
2099#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2100 {
2101 int ret;
2102 unsigned char *alg_start = frame->sig_alg.p;
2103 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2104
2105 /* Get signature options -- currently only
2106 * necessary for RSASSA-PSS. */
2107 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2108 &info->sig_pk, &info->sig_opts );
2109 if( ret != 0 )
2110 {
2111 /* Note: this can't happen except after an internal error */
2112 return( -1 );
2113 }
2114 }
2115#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2116 info->sig_md = frame->sig_md;
2117 info->sig_pk = frame->sig_pk;
2118#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2119
2120 info->issuer_raw = frame->issuer_raw;
2121 info->sig = frame->sig;
2122 return( 0 );
2123}
2124
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125/*
2126 * Return an informational string about the certificate.
2127 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002128#define BEFORE_COLON 18
2129#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002131 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132{
2133 int ret;
2134 size_t n;
2135 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002137 mbedtls_x509_crt_frame frame;
2138 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002139
Hanno Becker5226c532019-02-27 17:38:40 +00002140 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2141 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002142 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143
2144 p = buf;
2145 n = size;
2146
Hanno Becker4f869ed2019-02-24 16:47:57 +00002147 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002148 mbedtls_pk_init( &pk );
2149
2150 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002151 {
2152 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002153 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Janos Follath98e28a72016-05-31 14:03:54 +01002154
2155 return( (int) ( size - n ) );
2156 }
2157
Hanno Becker5226c532019-02-27 17:38:40 +00002158 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002159 if( ret != 0 )
2160 {
2161 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2162 goto cleanup;
2163 }
2164
Hanno Becker5226c532019-02-27 17:38:40 +00002165 ret = mbedtls_x509_crt_get_subject( crt, &subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002166 if( ret != 0 )
2167 {
2168 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2169 goto cleanup;
2170 }
2171
Hanno Becker5226c532019-02-27 17:38:40 +00002172 ret = mbedtls_x509_crt_get_issuer( crt, &issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002173 if( ret != 0 )
2174 {
2175 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2176 goto cleanup;
2177 }
2178
Hanno Becker5226c532019-02-27 17:38:40 +00002179 ret = mbedtls_x509_crt_get_subject_alt_names( crt, &subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002180 if( ret != 0 )
2181 {
2182 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2183 goto cleanup;
2184 }
2185
Hanno Becker5226c532019-02-27 17:38:40 +00002186 ret = mbedtls_x509_crt_get_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002187 if( ret != 0 )
2188 {
2189 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2190 goto cleanup;
2191 }
2192
Hanno Becker5226c532019-02-27 17:38:40 +00002193 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2194 if( ret != 0 )
2195 {
2196 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2197 goto cleanup;
2198 }
2199
2200 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002201 if( ret != 0 )
2202 {
2203 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2204 goto cleanup;
2205 }
2206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002208 prefix, frame.version );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002209 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002210
Hanno Becker4f869ed2019-02-24 16:47:57 +00002211 {
2212 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002213 serial.p = frame.serial.p;
2214 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002215 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2216 prefix );
2217 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
2218 ret = mbedtls_x509_serial_gets( p, n, &serial );
2219 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
2220 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002223 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Hanno Becker5226c532019-02-27 17:38:40 +00002224 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002225 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002228 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Hanno Becker5226c532019-02-27 17:38:40 +00002229 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002230 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002233 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002234 frame.valid_from.year, frame.valid_from.mon,
2235 frame.valid_from.day, frame.valid_from.hour,
2236 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002237 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002240 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002241 frame.valid_to.year, frame.valid_to.mon,
2242 frame.valid_to.day, frame.valid_to.hour,
2243 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002244 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002247 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002248
Hanno Becker83cd8672019-02-21 17:13:46 +00002249 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2250 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002251 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002252
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002253 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002255 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002256 {
2257 return( ret );
2258 }
2259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002261 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002262 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002263
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002264 /*
2265 * Optional extensions
2266 */
2267
Hanno Becker5226c532019-02-27 17:38:40 +00002268 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002271 frame.ca_istrue ? "true" : "false" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002272 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002273
Hanno Becker5226c532019-02-27 17:38:40 +00002274 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002275 {
Hanno Becker5226c532019-02-27 17:38:40 +00002276 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002277 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002278 }
2279 }
2280
Hanno Becker5226c532019-02-27 17:38:40 +00002281 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002284 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002285
2286 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002287 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002288 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002289 }
2290
Hanno Becker5226c532019-02-27 17:38:40 +00002291 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002294 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002295
Hanno Becker5226c532019-02-27 17:38:40 +00002296 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002297 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002298 }
2299
Hanno Becker5226c532019-02-27 17:38:40 +00002300 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002303 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002304
Hanno Becker5226c532019-02-27 17:38:40 +00002305 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002306 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002307 }
2308
Hanno Becker5226c532019-02-27 17:38:40 +00002309 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002312 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002313
2314 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002315 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002316 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002317 }
2318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002320 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002321
Hanno Becker4f869ed2019-02-24 16:47:57 +00002322 ret = (int) ( size - n );
2323
2324cleanup:
2325
Hanno Becker4f869ed2019-02-24 16:47:57 +00002326 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002327 mbedtls_pk_free( &pk );
2328 mbedtls_x509_name_free( issuer );
2329 mbedtls_x509_name_free( subject );
2330 mbedtls_x509_sequence_free( ext_key_usage );
2331 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002332
2333 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334}
2335
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002336struct x509_crt_verify_string {
2337 int code;
2338 const char *string;
2339};
2340
2341static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002342 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002343 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2344 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2345 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2346 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2347 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002348 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2349 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2350 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002351 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002352 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2353 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2354 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2355 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002356 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2357 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2358 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2359 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2360 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2361 { 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 +01002362 { 0, NULL }
2363};
2364
2365int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002366 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002367{
2368 int ret;
2369 const struct x509_crt_verify_string *cur;
2370 char *p = buf;
2371 size_t n = size;
2372
2373 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2374 {
2375 if( ( flags & cur->code ) == 0 )
2376 continue;
2377
2378 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002379 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002380 flags ^= cur->code;
2381 }
2382
2383 if( flags != 0 )
2384 {
2385 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2386 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002387 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002388 }
2389
2390 return( (int) ( size - n ) );
2391}
Hanno Becker02a21932019-06-10 15:08:43 +01002392#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002395static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2396 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002397{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002398 unsigned int usage_must, usage_may;
2399 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2400 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2401
2402 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2403 return( 0 );
2404
2405 usage_must = usage & ~may_mask;
2406
2407 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2408 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2409
2410 usage_may = usage & may_mask;
2411
2412 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002414
2415 return( 0 );
2416}
Hanno Becker45eedf12019-02-25 13:55:33 +00002417
2418int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2419 unsigned int usage )
2420{
2421 int ret;
2422 mbedtls_x509_crt_frame *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002423 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002424 if( ret != 0 )
2425 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2426
2427 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002428 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002429
2430 return( ret );
2431}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002432#endif
2433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002435typedef struct
2436{
2437 const char *oid;
2438 size_t oid_len;
2439} x509_crt_check_ext_key_usage_cb_ctx_t;
2440
2441static int x509_crt_check_ext_key_usage_cb( void *ctx,
2442 int tag,
2443 unsigned char *data,
2444 size_t data_len )
2445{
2446 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2447 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2448 ((void) tag);
2449
2450 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2451 data, data_len ) == 0 )
2452 {
2453 return( 1 );
2454 }
2455
2456 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2457 data_len ) == 0 )
2458 {
2459 return( 1 );
2460 }
2461
2462 return( 0 );
2463}
2464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002466 const char *usage_oid,
2467 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002468{
Hanno Beckere1956af2019-02-21 14:28:12 +00002469 int ret;
Hanno Beckere9718b42019-02-25 18:11:42 +00002470 mbedtls_x509_crt_frame *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002471 unsigned ext_types;
2472 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002473 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002474
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002475 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002476 if( ret != 0 )
2477 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2478
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002479 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002480 ext_types = frame->ext_types;
2481 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2482 {
2483 p = frame->ext_key_usage_raw.p;
2484 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002485
Hanno Beckere9718b42019-02-25 18:11:42 +00002486 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2487 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2488 x509_crt_check_ext_key_usage_cb,
2489 &cb_ctx );
2490 if( ret == 1 )
2491 ret = 0;
2492 else
2493 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2494 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002495
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002496 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002497 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002502/*
2503 * Return 1 if the certificate is revoked, or 0 otherwise.
2504 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002505static int x509_serial_is_revoked( unsigned char const *serial,
2506 size_t serial_len,
2507 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002510
2511 while( cur != NULL && cur->serial.len != 0 )
2512 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002513 if( serial_len == cur->serial.len &&
2514 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002515 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002516 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002517 return( 1 );
2518 }
2519
2520 cur = cur->next;
2521 }
2522
2523 return( 0 );
2524}
2525
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002526int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2527 const mbedtls_x509_crl *crl )
2528{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002529 int ret;
2530 mbedtls_x509_crt_frame *frame;
2531
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002532 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002533 if( ret != 0 )
2534 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2535
2536 ret = x509_serial_is_revoked( frame->serial.p,
2537 frame->serial.len,
2538 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002539 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002540 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002541}
2542
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002543/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002544 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002545 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002546 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002547static int x509_crt_verifycrl( unsigned char *crt_serial,
2548 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002549 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002550 mbedtls_x509_crl *crl_list,
2551 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002552{
Hanno Beckerbb266132019-02-25 18:12:46 +00002553 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002554 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2556 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002557 mbedtls_x509_buf_raw ca_subject;
2558 mbedtls_pk_context *pk;
2559 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002560
Hanno Beckerbb266132019-02-25 18:12:46 +00002561 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002562 return( flags );
2563
Hanno Beckerbb266132019-02-25 18:12:46 +00002564 {
2565 mbedtls_x509_crt_frame *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002566 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002567 if( ret != 0 )
2568 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2569
2570 ca_subject = ca->subject_raw;
2571
2572 can_sign = 0;
2573 if( x509_crt_check_key_usage_frame( ca,
2574 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2575 {
2576 can_sign = 1;
2577 }
2578
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002579 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002580 }
2581
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002582 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002583 if( ret != 0 )
2584 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2585
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002586 while( crl_list != NULL )
2587 {
2588 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002589 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002590 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002591 {
2592 crl_list = crl_list->next;
2593 continue;
2594 }
2595
2596 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002597 * Check if the CA is configured to sign CRLs
2598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002600 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002603 break;
2604 }
2605#endif
2606
2607 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002608 * Check if CRL is correctly signed by the trusted CA
2609 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002610 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2611 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2612
2613 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2614 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002617 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002618 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002619 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002621 break;
2622 }
2623
Hanno Beckerbb266132019-02-25 18:12:46 +00002624 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002625 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002626
Hanno Beckerbb266132019-02-25 18:12:46 +00002627 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002629 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002632 break;
2633 }
2634
2635 /*
2636 * Check for validity of CRL (Do not drop out)
2637 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002638 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002640
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002641 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002642 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002643
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002644 /*
2645 * Check if certificate is revoked
2646 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002647 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2648 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002651 break;
2652 }
2653
2654 crl_list = crl_list->next;
2655 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002656
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002657 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002658 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002661
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002662/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002663 * Check the signature of a certificate by its parent
2664 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002665static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002666 mbedtls_x509_crt *parent,
2667 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002668{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002669 int ret;
2670 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002671
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002672 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002673 if( ret != 0 )
2674 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2675
2676 /* Skip expensive computation on obvious mismatch */
2677 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002678 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002679 ret = -1;
2680 goto exit;
2681 }
2682
2683#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2684 ((void) rs_ctx);
2685#else
2686 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2687 {
2688 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002689 sig_info->sig_md,
2690 sig_info->crt_hash, sig_info->crt_hash_len,
2691 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002692 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002693 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002694 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002695#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002696 {
2697 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2698 sig_info->sig_opts,
2699 pk,
2700 sig_info->sig_md,
2701 sig_info->crt_hash, sig_info->crt_hash_len,
2702 sig_info->sig.p, sig_info->sig.len );
2703 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002704
Hanno Beckere449e2d2019-02-25 14:45:31 +00002705exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002706 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002707 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002708}
2709
2710/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002711 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2712 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002713 *
2714 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002715 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002716static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2717 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002718 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002719{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002720 int need_ca_bit;
2721
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002722 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002723 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2724 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002725 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002726 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002727 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002728 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002729
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002730 /* Parent must have the basicConstraints CA bit set as a general rule */
2731 need_ca_bit = 1;
2732
2733 /* Exception: v1/v2 certificates that are locally trusted. */
2734 if( top && parent->version < 3 )
2735 need_ca_bit = 0;
2736
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002737 if( need_ca_bit && ! parent->ca_istrue )
2738 return( -1 );
2739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002741 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002742 x509_crt_check_key_usage_frame( parent,
2743 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002744 {
2745 return( -1 );
2746 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002747#endif
2748
2749 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002750}
2751
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002752/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002753 * Find a suitable parent for child in candidates, or return NULL.
2754 *
2755 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002756 * 1. subject name matches child's issuer
2757 * 2. if necessary, the CA bit is set and key usage allows signing certs
2758 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002759 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002760 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002761 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002762 * If there's a suitable candidate which is also time-valid, return the first
2763 * such. Otherwise, return the first suitable candidate (or NULL if there is
2764 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002765 *
2766 * The rationale for this rule is that someone could have a list of trusted
2767 * roots with two versions on the same root with different validity periods.
2768 * (At least one user reported having such a list and wanted it to just work.)
2769 * The reason we don't just require time-validity is that generally there is
2770 * only one version, and if it's expired we want the flags to state that
2771 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002772 *
2773 * The rationale for rule 3 (signature for trusted roots) is that users might
2774 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002775 * way we select the correct one is by checking the signature (as we don't
2776 * rely on key identifier extensions). (This is one way users might choose to
2777 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002778 *
2779 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002780 * - [in] child: certificate for which we're looking for a parent
2781 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002782 * - [out] r_parent: parent found (or NULL)
2783 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002784 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2785 * of the chain, 0 otherwise
2786 * - [in] path_cnt: number of intermediates seen so far
2787 * - [in] self_cnt: number of self-signed intermediates seen so far
2788 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002789 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002790 *
2791 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002792 * - 0 on success
2793 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002794 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002795static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002796 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002797 mbedtls_x509_crt *candidates,
2798 mbedtls_x509_crt **r_parent,
2799 int *r_signature_is_good,
2800 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002801 unsigned path_cnt,
2802 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002803 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002804{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002805 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002806 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002807 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002808
2809#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002810 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002811 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2812 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002813 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002814 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002815 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002816 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002817
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002818 /* clear saved state */
2819 rs_ctx->parent = NULL;
2820 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002821 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002822
2823 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002824 goto check_signature;
2825 }
2826#endif
2827
2828 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002829 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002830
Hanno Becker43bf9002019-02-25 14:46:49 +00002831 for( parent_crt = candidates; parent_crt != NULL;
2832 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002833 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002834 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002835
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002836#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2837check_signature:
2838#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002839
2840 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002841 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002842 mbedtls_x509_crt_frame *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002843
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002844 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002845 if( ret != 0 )
2846 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002847
Hanno Becker43bf9002019-02-25 14:46:49 +00002848 if( mbedtls_x509_time_is_past( &parent->valid_from ) &&
2849 mbedtls_x509_time_is_future( &parent->valid_to ) )
2850 {
2851 parent_valid = 1;
2852 }
2853
2854 /* basic parenting skills (name, CA bit, key usage) */
2855 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2856 parent_match = 1;
2857
2858 /* +1 because the stored max_pathlen is 1 higher
2859 * than the actual value */
2860 if( !( parent->max_pathlen > 0 &&
2861 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2862 {
2863 path_len_ok = 1;
2864 }
2865
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002866 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002867 }
2868
2869 if( parent_match == 0 || path_len_ok == 0 )
2870 continue;
2871
2872 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002873 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002874
2875#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002876 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2877 {
2878 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002879 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002880 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002881 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002882
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002883 return( ret );
2884 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002885#else
2886 (void) ret;
2887#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002888
2889 signature_is_good = ret == 0;
2890 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002891 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002892
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002893 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002894 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002895 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002896 if( fallback_parent == NULL )
2897 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002898 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002899 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002900 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002901
2902 continue;
2903 }
2904
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002905 break;
2906 }
2907
Hanno Becker43bf9002019-02-25 14:46:49 +00002908 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002909 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002910 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002911 *r_signature_is_good = signature_is_good;
2912 }
2913 else
2914 {
2915 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002916 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002917 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002918
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002919 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002920}
2921
2922/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002923 * Find a parent in trusted CAs or the provided chain, or return NULL.
2924 *
2925 * Searches in trusted CAs first, and return the first suitable parent found
2926 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002927 *
2928 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002929 * - [in] child: certificate for which we're looking for a parent, followed
2930 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002931 * - [in] trust_ca: list of locally trusted certificates
2932 * - [out] parent: parent found (or NULL)
2933 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2934 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2935 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2936 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002937 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002938 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002939 *
2940 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002941 * - 0 on success
2942 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002943 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002944static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002945 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002946 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002947 mbedtls_x509_crt *trust_ca,
2948 mbedtls_x509_crt **parent,
2949 int *parent_is_trusted,
2950 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002951 unsigned path_cnt,
2952 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002953 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002954{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002955 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002956 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002957
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002958 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002959
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002960#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002961 /* restore then clear saved state if we have some stored */
2962 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2963 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002964 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002965 rs_ctx->parent_is_trusted = -1;
2966 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002967#endif
2968
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002969 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00002970 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002971
Hanno Becker5299cf82019-02-25 13:50:41 +00002972 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002973 parent, signature_is_good,
2974 *parent_is_trusted,
2975 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002976
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002977#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002978 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2979 {
2980 /* save state */
2981 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002982 return( ret );
2983 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002984#else
2985 (void) ret;
2986#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002987
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002988 /* stop here if found or already in second iteration */
2989 if( *parent != NULL || *parent_is_trusted == 0 )
2990 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002991
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002992 /* prepare second iteration */
2993 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002994 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002995
2996 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01002997 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002998 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002999 *parent_is_trusted = 0;
3000 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003001 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003002
3003 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003004}
3005
3006/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003007 * Check if an end-entity certificate is locally trusted
3008 *
3009 * Currently we require such certificates to be self-signed (actually only
3010 * check for self-issued as self-signatures are not checked)
3011 */
3012static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003013 mbedtls_x509_crt_frame const *crt,
3014 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003015{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003016 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003017
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003018 /* look for an exact match with trusted cert */
3019 for( cur = trust_ca; cur != NULL; cur = cur->next )
3020 {
3021 if( crt->raw.len == cur->raw.len &&
3022 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3023 {
3024 return( 0 );
3025 }
3026 }
3027
3028 /* too bad */
3029 return( -1 );
3030}
3031
3032/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003033 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003034 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003035 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3036 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003037 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003038 * such that every cert in the chain is a child of the next one,
3039 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003040 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003041 * Verify that chain and return it with flags for all issues found.
3042 *
3043 * Special cases:
3044 * - EE == Rj -> return a one-element list containing it
3045 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3046 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003047 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003048 * Tests for (aspects of) this function should include at least:
3049 * - trusted EE
3050 * - EE -> trusted root
3051 * - EE -> intermedate CA -> trusted root
3052 * - if relevant: EE untrusted
3053 * - if relevant: EE -> intermediate, untrusted
3054 * with the aspect under test checked at each relevant level (EE, int, root).
3055 * For some aspects longer chains are required, but usually length 2 is
3056 * enough (but length 1 is not in general).
3057 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003058 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003059 * - [in] crt: the cert list EE, C1, ..., Cn
3060 * - [in] trust_ca: the trusted list R1, ..., Rp
3061 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003062 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003063 * Only valid when return value is 0, may contain garbage otherwise!
3064 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003065 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003066 *
3067 * Return value:
3068 * - non-zero if the chain could not be fully built and examined
3069 * - 0 is the chain was successfully built and examined,
3070 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003071 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003072static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003073 mbedtls_x509_crt *crt,
3074 mbedtls_x509_crt *trust_ca,
3075 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003076 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003077 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003078 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003079{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003080 /* Don't initialize any of those variables here, so that the compiler can
3081 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003082 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003083 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003084 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003085 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003086 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003087 int parent_is_trusted;
3088 int child_is_trusted;
3089 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003090 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003091
3092#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3093 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003094 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003095 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003096 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003097 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003098 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003099
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003100 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003101 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003102 child_crt = cur->crt;
3103
3104 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003105 goto find_parent;
3106 }
3107#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003108
Hanno Becker1e0677a2019-02-25 14:58:22 +00003109 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003110 self_cnt = 0;
3111 parent_is_trusted = 0;
3112 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003113
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003114 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003115#if defined(MBEDTLS_X509_CRL_PARSE_C)
3116 mbedtls_x509_buf_raw child_serial;
3117#endif /* MBEDTLS_X509_CRL_PARSE_C */
3118 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003119
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003120 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003121 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003122 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003123 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003124 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003125
3126#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3127find_parent:
3128#endif
3129
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003130 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003131
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003132 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003133 mbedtls_x509_crt_sig_info child_sig;
3134 {
3135 mbedtls_x509_crt_frame *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003136
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003137 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003138 if( ret != 0 )
3139 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3140
3141 /* Check time-validity (all certificates) */
3142 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3143 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3144 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3145 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
3146
3147 /* Stop here for trusted roots (but not for trusted EE certs) */
3148 if( child_is_trusted )
3149 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003150 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003151 return( 0 );
3152 }
3153
3154 self_issued = 0;
3155 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3156 &child->subject_raw,
3157 NULL, NULL ) == 0 )
3158 {
3159 self_issued = 1;
3160 }
3161
3162 /* Check signature algorithm: MD & PK algs */
3163 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3164 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3165
3166 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3167 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3168
3169 /* Special case: EE certs that are locally trusted */
3170 if( ver_chain->len == 1 && self_issued &&
3171 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3172 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003173 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003174 return( 0 );
3175 }
3176
3177#if defined(MBEDTLS_X509_CRL_PARSE_C)
3178 child_serial = child->serial;
3179#endif /* MBEDTLS_X509_CRL_PARSE_C */
3180
3181 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003182 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003183
Hanno Becker5299cf82019-02-25 13:50:41 +00003184 if( ret != 0 )
3185 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3186 }
3187
3188 /* Look for a parent in trusted CAs or up the chain */
3189 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003190 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003191 &parent_is_trusted, &signature_is_good,
3192 ver_chain->len - 1, self_cnt, rs_ctx );
3193
3194 x509_crt_free_sig_info( &child_sig );
3195 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003196
3197#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003198 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3199 {
3200 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003201 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003202 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003203 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003204 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003205 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003206#else
3207 (void) ret;
3208#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003209
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003210 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003211 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003212 {
3213 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003214 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003215 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003216
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003217 /* Count intermediate self-issued (not necessarily self-signed) certs.
3218 * These can occur with some strategies for key rollover, see [SIRO],
3219 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003220 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003221 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003222
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003223 /* path_cnt is 0 for the first intermediate CA,
3224 * and if parent is trusted it's not an intermediate CA */
3225 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003226 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003227 {
3228 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003229 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003230 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003231
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003232 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003233 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003234 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3235
Hanno Becker58c35642019-02-25 18:13:46 +00003236 {
3237 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003238 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003239 if( ret != 0 )
3240 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3241
3242 /* check size of signing key */
3243 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3244 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3245
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003246 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003247 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003250 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003251 *flags |= x509_crt_verifycrl( child_serial.p,
3252 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003253 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003254#else
3255 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003256#endif
3257
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003258 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003259 child_crt = parent_crt;
3260 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003261 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003262 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003263 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003264}
3265
3266/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003267 * Check for CN match
3268 */
Hanno Becker24926222019-02-21 13:10:55 +00003269static int x509_crt_check_cn( unsigned char const *buf,
3270 size_t buflen,
3271 const char *cn,
3272 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003273{
Hanno Becker24926222019-02-21 13:10:55 +00003274 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003275 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003276 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003277
3278 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003279 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003280 {
3281 return( 0 );
3282 }
3283
3284 return( -1 );
3285}
3286
Hanno Becker8b543b32019-02-21 11:50:44 +00003287/* Returns 1 on a match and 0 on a mismatch.
3288 * This is because this function is used as a callback for
3289 * mbedtls_x509_name_cmp_raw(), which continues the name
3290 * traversal as long as the callback returns 0. */
3291static int x509_crt_check_name( void *ctx,
3292 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003293 mbedtls_x509_buf *val,
3294 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003295{
3296 char const *cn = (char const*) ctx;
3297 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003298 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003299
3300 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003301 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003302 {
3303 return( 1 );
3304 }
3305
3306 return( 0 );
3307}
3308
Hanno Beckerda410822019-02-21 13:36:59 +00003309/* Returns 1 on a match and 0 on a mismatch.
3310 * This is because this function is used as a callback for
3311 * mbedtls_asn1_traverse_sequence_of(), which continues the
3312 * traversal as long as the callback returns 0. */
3313static int x509_crt_subject_alt_check_name( void *ctx,
3314 int tag,
3315 unsigned char *data,
3316 size_t data_len )
3317{
3318 char const *cn = (char const*) ctx;
3319 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003320 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003321
3322 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3323 return( 1 );
3324
3325 return( 0 );
3326}
3327
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003328/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003329 * Verify the requested CN - only call this if cn is not NULL!
3330 */
Hanno Becker082435c2019-02-25 18:14:40 +00003331static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3332 const char *cn,
3333 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003334{
Hanno Beckerda410822019-02-21 13:36:59 +00003335 int ret;
Hanno Becker082435c2019-02-25 18:14:40 +00003336 mbedtls_x509_crt_frame *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003337
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003338 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003339 if( ret != 0 )
3340 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3341
3342 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003343 {
Hanno Becker90b94082019-02-21 21:13:21 +00003344 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003345 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003346 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003347 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003348
Hanno Becker90b94082019-02-21 21:13:21 +00003349 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3350 MBEDTLS_ASN1_TAG_CLASS_MASK,
3351 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3352 MBEDTLS_ASN1_TAG_VALUE_MASK,
3353 2 /* SubjectAlt DNS */,
3354 x509_crt_subject_alt_check_name,
3355 (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003356 }
3357 else
3358 {
Hanno Becker082435c2019-02-25 18:14:40 +00003359 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3360 &frame->subject_raw,
Hanno Becker8b543b32019-02-21 11:50:44 +00003361 x509_crt_check_name, (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003362 }
Hanno Beckerda410822019-02-21 13:36:59 +00003363
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003364 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003365
3366 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3367 * return 1 when finding a name component matching `cn`. */
3368 if( ret == 1 )
3369 return( 0 );
3370
3371 if( ret != 0 )
3372 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3373
3374 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3375 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003376}
3377
3378/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003379 * Merge the flags for all certs in the chain, after calling callback
3380 */
3381static int x509_crt_merge_flags_with_cb(
3382 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003383 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003384 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3385 void *p_vrfy )
3386{
3387 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003388 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003389 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003390 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003391
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003392 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003393 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003394 cur = &ver_chain->items[i-1];
3395 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003396
3397 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003398 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003399 return( ret );
3400
3401 *flags |= cur_flags;
3402 }
3403
3404 return( 0 );
3405}
3406
3407/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003408 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3411 mbedtls_x509_crt *trust_ca,
3412 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003413 const char *cn, uint32_t *flags,
3414 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003415 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003416{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003417 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3418 &mbedtls_x509_crt_profile_default, cn, flags,
3419 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003420}
3421
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003422/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003423 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003424 */
3425int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3426 mbedtls_x509_crt *trust_ca,
3427 mbedtls_x509_crl *ca_crl,
3428 const mbedtls_x509_crt_profile *profile,
3429 const char *cn, uint32_t *flags,
3430 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3431 void *p_vrfy )
3432{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003433 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3434 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3435}
3436
3437/*
3438 * Verify the certificate validity, with profile, restartable version
3439 *
3440 * This function:
3441 * - checks the requested CN (if any)
3442 * - checks the type and size of the EE cert's key,
3443 * as that isn't done as part of chain building/verification currently
3444 * - builds and verifies the chain
3445 * - then calls the callback and merges the flags
3446 */
3447int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3448 mbedtls_x509_crt *trust_ca,
3449 mbedtls_x509_crl *ca_crl,
3450 const mbedtls_x509_crt_profile *profile,
3451 const char *cn, uint32_t *flags,
3452 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3453 void *p_vrfy,
3454 mbedtls_x509_crt_restart_ctx *rs_ctx )
3455{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003456 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003457 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003458 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003459
3460 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003461 ee_flags = 0;
3462 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003463
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003464 if( profile == NULL )
3465 {
3466 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3467 goto exit;
3468 }
3469
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003470 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003471 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003472 {
3473 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3474 if( ret != 0 )
3475 return( ret );
3476 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003477
Hanno Becker87233362019-02-25 18:15:33 +00003478 {
3479 mbedtls_pk_context *pk;
3480 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003481
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003482 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003483 if( ret != 0 )
3484 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003485
Hanno Becker87233362019-02-25 18:15:33 +00003486 /* Check the type and size of the key */
3487 pk_type = mbedtls_pk_get_type( pk );
3488
3489 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3490 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3491
3492 if( x509_profile_check_key( profile, pk ) != 0 )
3493 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3494
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003495 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003496 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003497
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003498 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003499 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003500 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003501
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003502 if( ret != 0 )
3503 goto exit;
3504
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003505 /* Merge end-entity flags */
3506 ver_chain.items[0].flags |= ee_flags;
3507
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003508 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003509 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003510
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003511exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003512#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3513 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3514 mbedtls_x509_crt_restart_free( rs_ctx );
3515#endif
3516
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003517 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3518 * the SSL module for authmode optional, but non-zero return from the
3519 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003520 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3521 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3522
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003523 if( ret != 0 )
3524 {
3525 *flags = (uint32_t) -1;
3526 return( ret );
3527 }
3528
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003529 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003531
3532 return( 0 );
3533}
3534
3535/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003536 * Initialize a certificate chain
3537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003541}
3542
3543/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003544 * Unallocate all certificate data
3545 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 mbedtls_x509_crt *cert_cur = crt;
3550 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003551
3552 if( crt == NULL )
3553 return;
3554
3555 do
3556 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003557 x509_crt_cache_free( cert_cur->cache );
3558 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003559
3560#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3564 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003565#endif
3566
Hanno Becker2bcc7642019-02-26 19:01:00 +00003567 mbedtls_x509_name_free( cert_cur->issuer.next );
3568 mbedtls_x509_name_free( cert_cur->subject.next );
3569 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3570 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003571#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003572
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003573 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003574 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003575 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003577 }
3578
3579 cert_cur = cert_cur->next;
3580 }
3581 while( cert_cur != NULL );
3582
3583 cert_cur = crt;
3584 do
3585 {
3586 cert_prv = cert_cur;
3587 cert_cur = cert_cur->next;
3588
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003589 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003590 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003592 }
3593 while( cert_cur != NULL );
3594}
3595
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003596#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3597/*
3598 * Initialize a restart context
3599 */
3600void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3601{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003602 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003603
3604 ctx->parent = NULL;
3605 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003606 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003607
3608 ctx->parent_is_trusted = -1;
3609
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003610 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003611 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003612 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003613}
3614
3615/*
3616 * Free the components of a restart context
3617 */
3618void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3619{
3620 if( ctx == NULL )
3621 return;
3622
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003623 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003624 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003625}
3626#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628#endif /* MBEDTLS_X509_CRT_PARSE_C */