blob: 4e5f6f50f9ceaae2788bcf085f496817b056fb70 [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,
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100784 uint16_t *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;
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100798 for( i = 0; i < bs.len && i < sizeof( *key_usage ); i++ )
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200799 {
Hanno Beckerfd5c1852019-05-13 12:52:57 +0100800 *key_usage |= (uint16_t) bs.p[i] << ( 8*i );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200801 }
802
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200803 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804}
805
Hanno Becker529f25d2019-05-02 14:48:25 +0100806static int asn1_build_sequence_cb( void *ctx,
807 int tag,
808 unsigned char *data,
809 size_t data_len )
Hanno Becker15b73b42019-05-02 13:21:27 +0100810{
811 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
812 mbedtls_asn1_sequence *cur = *cur_ptr;
813
814 /* Allocate and assign next pointer */
815 if( cur->buf.p != NULL )
816 {
817 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
818 if( cur->next == NULL )
819 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
820 cur = cur->next;
821 }
822
823 cur->buf.tag = tag;
824 cur->buf.p = data;
825 cur->buf.len = data_len;
826
827 *cur_ptr = cur;
828 return( 0 );
829}
830
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831/*
Hanno Becker529f25d2019-05-02 14:48:25 +0100832 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
833 *
834 * KeyPurposeId ::= OBJECT IDENTIFIER
835 */
836static int x509_get_ext_key_usage( unsigned char **p,
837 const unsigned char *end,
838 mbedtls_x509_sequence *ext_key_usage)
839{
840 return( mbedtls_asn1_traverse_sequence_of( p, end,
841 0xFF, MBEDTLS_ASN1_OID,
842 0, 0,
843 asn1_build_sequence_cb,
844 (void*) &ext_key_usage ) );
845}
846
847/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848 * SubjectAltName ::= GeneralNames
849 *
850 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
851 *
852 * GeneralName ::= CHOICE {
853 * otherName [0] OtherName,
854 * rfc822Name [1] IA5String,
855 * dNSName [2] IA5String,
856 * x400Address [3] ORAddress,
857 * directoryName [4] Name,
858 * ediPartyName [5] EDIPartyName,
859 * uniformResourceIdentifier [6] IA5String,
860 * iPAddress [7] OCTET STRING,
861 * registeredID [8] OBJECT IDENTIFIER }
862 *
863 * OtherName ::= SEQUENCE {
864 * type-id OBJECT IDENTIFIER,
865 * value [0] EXPLICIT ANY DEFINED BY type-id }
866 *
867 * EDIPartyName ::= SEQUENCE {
868 * nameAssigner [0] DirectoryString OPTIONAL,
869 * partyName [1] DirectoryString }
870 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000871 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872 */
Hanno Becker5984d302019-02-21 14:46:54 +0000873static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000877 return( mbedtls_asn1_traverse_sequence_of( &p, end,
878 MBEDTLS_ASN1_TAG_CLASS_MASK,
879 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
880 MBEDTLS_ASN1_TAG_VALUE_MASK,
881 2 /* SubjectAlt DNS */,
Hanno Becker529f25d2019-05-02 14:48:25 +0100882 asn1_build_sequence_cb,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000883 (void*) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884}
885
886/*
887 * X.509 v3 extensions
888 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000890static int x509_crt_get_ext_cb( void *ctx,
891 int tag,
892 unsigned char *p,
893 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894{
895 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000896 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000898 unsigned char *end, *end_ext_octet;
899 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
900 int is_critical = 0; /* DEFAULT FALSE */
901 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000903 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000904
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000905 /*
906 * Extension ::= SEQUENCE {
907 * extnID OBJECT IDENTIFIER,
908 * critical BOOLEAN DEFAULT FALSE,
909 * extnValue OCTET STRING }
910 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000912 end = p + ext_len;
913
914 /* Get extension ID */
915 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
916 MBEDTLS_ASN1_OID ) ) != 0 )
917 goto err;
918
919 extn_oid.tag = MBEDTLS_ASN1_OID;
920 extn_oid.p = p;
921 p += extn_oid.len;
922
923 /* Get optional critical */
924 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
925 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
926 goto err;
927
928 /* Data should be octet string type */
929 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
930 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
931 goto err;
932
933 end_ext_octet = p + len;
934 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000936 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
937 goto err;
938 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000940 /*
941 * Detect supported extensions
942 */
943 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
944 if( ret != 0 )
945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000947 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000949 /* Data is marked as critical: fail */
950 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
951 goto err;
952 }
953#endif
954 return( 0 );
955 }
956
957 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +0000958 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000959 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
960
Hanno Becker21f55672019-02-15 15:27:59 +0000961 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000962 switch( ext_type )
963 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100964 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000965 {
966 int ca_istrue;
967 int max_pathlen;
968
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000970 ret = x509_get_basic_constraints( &p, end_ext_octet,
971 &ca_istrue,
972 &max_pathlen );
973 if( ret != 0 )
974 goto err;
975
Hanno Becker21f55672019-02-15 15:27:59 +0000976 frame->ca_istrue = ca_istrue;
977 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000979 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000983 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +0000984 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000985 if( ret != 0 )
986 goto err;
987 break;
988
989 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
990 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +0000991 frame->subject_alt_raw.p = p;
992 frame->subject_alt_raw.len = end_ext_octet - p;
993
994 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
995 MBEDTLS_ASN1_TAG_CLASS_MASK,
996 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
997 MBEDTLS_ASN1_TAG_VALUE_MASK,
998 2 /* SubjectAlt DNS */,
999 NULL, NULL );
1000 if( ret != 0 )
1001 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 break;
1003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +00001006 frame->ext_key_usage_raw.p = p;
1007 frame->ext_key_usage_raw.len = end_ext_octet - p;
1008 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +00001009 {
Hanno Becker21f55672019-02-15 15:27:59 +00001010 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
1011 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +00001012 }
Hanno Becker21f55672019-02-15 15:27:59 +00001013
1014 /* Check structural sanity of extension. */
1015 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
1016 0xFF, MBEDTLS_ASN1_OID,
1017 0, 0, NULL, NULL );
1018 if( ret != 0 )
1019 goto err;
1020
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 break;
1022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001025 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +00001026 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001027 if( ret != 0 )
1028 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 break;
1030
1031 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001032 /*
1033 * If this is a non-critical extension, which the oid layer
1034 * supports, but there isn't an X.509 parser for it,
1035 * skip the extension.
1036 */
1037#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1038 if( is_critical )
1039 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1040#endif
1041 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 }
1043
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001045
1046err:
1047 return( ret );
1048}
1049
Hanno Becker21f55672019-02-15 15:27:59 +00001050static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001051{
1052 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001053 unsigned char *p = frame->v3_ext.p;
1054 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001055
Hanno Becker21f55672019-02-15 15:27:59 +00001056 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001057 return( 0 );
1058
Hanno Becker21f55672019-02-15 15:27:59 +00001059 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001060 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +00001061 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +00001062
1063 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1064 return( ret );
1065 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
1066 return( ret );
1067
1068 if( ret != 0 )
1069 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1070
1071 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072}
1073
Hanno Becker21f55672019-02-15 15:27:59 +00001074static int x509_crt_parse_frame( unsigned char *start,
1075 unsigned char *end,
1076 mbedtls_x509_crt_frame *frame )
1077{
1078 int ret;
1079 unsigned char *p;
1080 size_t len;
1081
1082 mbedtls_x509_buf tmp;
1083 unsigned char *tbs_start;
1084
1085 mbedtls_x509_buf outer_sig_alg;
1086 size_t inner_sig_alg_len;
1087 unsigned char *inner_sig_alg_start;
1088
1089 memset( frame, 0, sizeof( *frame ) );
1090
1091 /*
1092 * Certificate ::= SEQUENCE {
1093 * tbsCertificate TBSCertificate,
1094 * signatureAlgorithm AlgorithmIdentifier,
1095 * signatureValue BIT STRING
1096 * }
1097 *
1098 */
1099 p = start;
1100
1101 frame->raw.p = p;
1102 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1103 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1104 {
1105 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
1106 }
1107
1108 /* NOTE: We are currently not checking that the `Certificate`
1109 * structure spans the entire buffer. */
1110 end = p + len;
1111 frame->raw.len = end - frame->raw.p;
1112
1113 /*
1114 * TBSCertificate ::= SEQUENCE { ...
1115 */
1116 frame->tbs.p = p;
1117 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1118 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1119 {
1120 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1121 }
1122 tbs_start = p;
1123
1124 /* Breadth-first parsing: Jump over TBS for now. */
1125 p += len;
1126 frame->tbs.len = p - frame->tbs.p;
1127
1128 /*
1129 * AlgorithmIdentifier ::= SEQUENCE { ...
1130 */
1131 outer_sig_alg.p = p;
1132 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1133 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1134 {
1135 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
1136 }
1137 p += len;
1138 outer_sig_alg.len = p - outer_sig_alg.p;
1139
1140 /*
1141 * signatureValue BIT STRING
1142 */
1143 ret = mbedtls_x509_get_sig( &p, end, &tmp );
1144 if( ret != 0 )
1145 return( ret );
1146 frame->sig.p = tmp.p;
1147 frame->sig.len = tmp.len;
1148
1149 /* Check that we consumed the entire `Certificate` structure. */
1150 if( p != end )
1151 {
1152 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1153 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1154 }
1155
1156 /* Parse TBSCertificate structure
1157 *
1158 * TBSCertificate ::= SEQUENCE {
1159 * version [0] EXPLICIT Version DEFAULT v1,
1160 * serialNumber CertificateSerialNumber,
1161 * signature AlgorithmIdentifier,
1162 * issuer Name,
1163 * validity Validity,
1164 * subject Name,
1165 * subjectPublicKeyInfo SubjectPublicKeyInfo,
1166 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1167 * -- If present, version MUST be v2 or v3
1168 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1169 * -- If present, version MUST be v2 or v3
1170 * extensions [3] EXPLICIT Extensions OPTIONAL
1171 * -- If present, version MUST be v3
1172 * }
1173 */
1174 end = frame->tbs.p + frame->tbs.len;
1175 p = tbs_start;
1176
1177 /*
1178 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1179 */
1180 {
1181 int version;
1182 ret = x509_get_version( &p, end, &version );
1183 if( ret != 0 )
1184 return( ret );
1185
1186 if( version < 0 || version > 2 )
1187 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
1188
1189 frame->version = version + 1;
1190 }
1191
1192 /*
1193 * CertificateSerialNumber ::= INTEGER
1194 */
1195 ret = mbedtls_x509_get_serial( &p, end, &tmp );
1196 if( ret != 0 )
1197 return( ret );
1198
1199 frame->serial.p = tmp.p;
1200 frame->serial.len = tmp.len;
1201
1202 /*
1203 * signature AlgorithmIdentifier
1204 */
1205 inner_sig_alg_start = p;
1206 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
1207 &frame->sig_pk, NULL );
1208 if( ret != 0 )
1209 return( ret );
1210 inner_sig_alg_len = p - inner_sig_alg_start;
1211
1212 frame->sig_alg.p = inner_sig_alg_start;
1213 frame->sig_alg.len = inner_sig_alg_len;
1214
1215 /* Consistency check:
1216 * Inner and outer AlgorithmIdentifier structures must coincide:
1217 *
1218 * Quoting RFC 5280, Section 4.1.1.2:
1219 * This field MUST contain the same algorithm identifier as the
1220 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
1221 */
1222 if( outer_sig_alg.len != inner_sig_alg_len ||
1223 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
1224 {
1225 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
1226 }
1227
1228 /*
1229 * issuer Name
1230 *
1231 * Name ::= CHOICE { -- only one possibility for now --
1232 * rdnSequence RDNSequence }
1233 *
1234 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1235 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001236 frame->issuer_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001237
1238 ret = mbedtls_asn1_get_tag( &p, end, &len,
1239 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1240 if( ret != 0 )
1241 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001242 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001243 frame->issuer_raw.len = p - frame->issuer_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001244
1245 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
1246 &frame->issuer_raw,
1247 NULL, NULL );
1248 if( ret != 0 )
1249 return( ret );
1250
Hanno Becker21f55672019-02-15 15:27:59 +00001251 /*
1252 * Validity ::= SEQUENCE { ...
1253 */
1254 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
1255 if( ret != 0 )
1256 return( ret );
1257
1258 /*
1259 * subject Name
1260 *
1261 * Name ::= CHOICE { -- only one possibility for now --
1262 * rdnSequence RDNSequence }
1263 *
1264 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
1265 */
Hanno Becker1e11f212019-03-04 14:43:43 +00001266 frame->subject_raw.p = p;
Hanno Becker21f55672019-02-15 15:27:59 +00001267
1268 ret = mbedtls_asn1_get_tag( &p, end, &len,
1269 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1270 if( ret != 0 )
1271 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
Hanno Becker21f55672019-02-15 15:27:59 +00001272 p += len;
Hanno Becker1e11f212019-03-04 14:43:43 +00001273 frame->subject_raw.len = p - frame->subject_raw.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001274
1275 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1276 &frame->subject_raw,
1277 NULL, NULL );
1278 if( ret != 0 )
1279 return( ret );
1280
Hanno Becker21f55672019-02-15 15:27:59 +00001281 /*
1282 * SubjectPublicKeyInfo
1283 */
1284 frame->pubkey_raw.p = p;
1285 ret = mbedtls_asn1_get_tag( &p, end, &len,
1286 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1287 if( ret != 0 )
1288 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1289 p += len;
1290 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1291
1292 /*
1293 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1294 * -- If present, version shall be v2 or v3
1295 */
1296 if( frame->version == 2 || frame->version == 3 )
1297 {
1298 ret = x509_get_uid( &p, end, &tmp, 1 /* implicit tag */ );
1299 if( ret != 0 )
1300 return( ret );
1301
1302 frame->issuer_id.p = tmp.p;
1303 frame->issuer_id.len = tmp.len;
1304 }
1305
1306 /*
1307 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1308 * -- If present, version shall be v2 or v3
1309 */
1310 if( frame->version == 2 || frame->version == 3 )
1311 {
1312 ret = x509_get_uid( &p, end, &tmp, 2 /* implicit tag */ );
1313 if( ret != 0 )
1314 return( ret );
1315
1316 frame->subject_id.p = tmp.p;
1317 frame->subject_id.len = tmp.len;
1318 }
1319
1320 /*
1321 * extensions [3] EXPLICIT Extensions OPTIONAL
1322 * -- If present, version shall be v3
1323 */
1324#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1325 if( frame->version == 3 )
1326#endif
1327 {
1328 if( p != end )
1329 {
1330 ret = mbedtls_asn1_get_tag( &p, end, &len,
1331 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1332 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1333 if( len == 0 )
1334 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1335 if( ret != 0 )
1336 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1337
1338 frame->v3_ext.p = p;
1339 frame->v3_ext.len = len;
1340
1341 p += len;
1342 }
1343
1344 ret = x509_crt_frame_parse_ext( frame );
1345 if( ret != 0 )
1346 return( ret );
1347 }
1348
1349 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1350 if( p != end )
1351 {
1352 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1353 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1354 }
1355
1356 return( 0 );
1357}
1358
1359static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
1360 mbedtls_x509_name *subject )
1361{
Hanno Becker1e11f212019-03-04 14:43:43 +00001362 return( mbedtls_x509_get_name( frame->subject_raw.p,
1363 frame->subject_raw.len,
1364 subject ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001365}
1366
1367static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
1368 mbedtls_x509_name *issuer )
1369{
Hanno Becker1e11f212019-03-04 14:43:43 +00001370 return( mbedtls_x509_get_name( frame->issuer_raw.p,
1371 frame->issuer_raw.len,
1372 issuer ) );
Hanno Becker21f55672019-02-15 15:27:59 +00001373}
1374
1375static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
1376 mbedtls_x509_sequence *subject_alt )
1377{
1378 int ret;
1379 unsigned char *p = frame->subject_alt_raw.p;
1380 unsigned char *end = p + frame->subject_alt_raw.len;
1381
1382 memset( subject_alt, 0, sizeof( *subject_alt ) );
1383
1384 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1385 return( 0 );
1386
1387 ret = x509_get_subject_alt_name( p, end, subject_alt );
1388 if( ret != 0 )
1389 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1390 return( ret );
1391}
1392
1393static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
1394 mbedtls_x509_sequence *ext_key_usage )
1395{
1396 int ret;
1397 unsigned char *p = frame->ext_key_usage_raw.p;
1398 unsigned char *end = p + frame->ext_key_usage_raw.len;
1399
1400 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1401
1402 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1403 return( 0 );
1404
1405 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1406 if( ret != 0 )
1407 {
1408 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1409 return( ret );
1410 }
1411
1412 return( 0 );
1413}
1414
Hanno Becker180f7bf2019-02-28 13:23:38 +00001415#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Hanno Becker21f55672019-02-15 15:27:59 +00001416static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1417 mbedtls_pk_context *pk )
1418{
1419 unsigned char *p = frame->pubkey_raw.p;
1420 unsigned char *end = p + frame->pubkey_raw.len;
1421 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1422}
Hanno Becker180f7bf2019-02-28 13:23:38 +00001423#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Becker21f55672019-02-15 15:27:59 +00001424
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425/*
1426 * Parse and fill a single X.509 certificate in DER format
1427 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001428static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1429 const unsigned char *buf,
1430 size_t buflen,
1431 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432{
1433 int ret;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001434 mbedtls_x509_crt_frame *frame;
1435 mbedtls_x509_crt_cache *cache;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001436
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439
Hanno Becker21f55672019-02-15 15:27:59 +00001440 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441 {
Hanno Becker21f55672019-02-15 15:27:59 +00001442 crt->raw.p = (unsigned char*) buf;
1443 crt->raw.len = buflen;
1444 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001445 }
1446 else
1447 {
Hanno Becker7b8e11e2019-05-03 12:37:12 +01001448 /* Call mbedtls_calloc with buflen + 1 in order to avoid potential
1449 * return of NULL in case of length 0 certificates, which we want
1450 * to cleanly fail with MBEDTLS_ERR_X509_INVALID_FORMAT in the
1451 * core parsing routine, but not here. */
1452 crt->raw.p = mbedtls_calloc( 1, buflen + 1 );
Hanno Becker21f55672019-02-15 15:27:59 +00001453 if( crt->raw.p == NULL )
1454 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1455 crt->raw.len = buflen;
1456 memcpy( crt->raw.p, buf, buflen );
1457
1458 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001459 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001460
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001461 cache = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_cache ) );
1462 if( cache == NULL )
1463 {
1464 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1465 goto exit;
1466 }
1467 crt->cache = cache;
1468 x509_crt_cache_init( cache );
1469
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001470#if defined(MBEDTLS_X509_ON_DEMAND_PARSING)
1471
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001472 ret = mbedtls_x509_crt_cache_provide_frame( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001473 if( ret != 0 )
1474 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475
Hanno Becker76428352019-03-05 15:29:23 +00001476 frame = crt->cache->frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001477
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001478#else /* MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001480 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
1481 if( frame == NULL )
1482 {
1483 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
1484 goto exit;
1485 }
1486 cache->frame = frame;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001487
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001488 ret = x509_crt_parse_frame( crt->raw.p,
1489 crt->raw.p + crt->raw.len,
1490 frame );
1491 if( ret != 0 )
1492 goto exit;
1493
Hanno Becker21f55672019-02-15 15:27:59 +00001494 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1495 * memory matters, the new CRT structure should be used anyway. */
Hanno Becker38f0cb42019-03-04 15:13:45 +00001496 x509_buf_raw_to_buf( &crt->tbs, &frame->tbs );
1497 x509_buf_raw_to_buf( &crt->serial, &frame->serial );
1498 x509_buf_raw_to_buf( &crt->issuer_raw, &frame->issuer_raw );
1499 x509_buf_raw_to_buf( &crt->subject_raw, &frame->subject_raw );
1500 x509_buf_raw_to_buf( &crt->issuer_id, &frame->issuer_id );
1501 x509_buf_raw_to_buf( &crt->subject_id, &frame->subject_id );
1502 x509_buf_raw_to_buf( &crt->pk_raw, &frame->pubkey_raw );
1503 x509_buf_raw_to_buf( &crt->sig, &frame->sig );
1504 x509_buf_raw_to_buf( &crt->v3_ext, &frame->v3_ext );
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001505 crt->valid_from = frame->valid_from;
1506 crt->valid_to = frame->valid_to;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001507 crt->version = frame->version;
1508 crt->ca_istrue = frame->ca_istrue;
1509 crt->max_pathlen = frame->max_pathlen;
1510 crt->ext_types = frame->ext_types;
1511 crt->key_usage = frame->key_usage;
1512 crt->ns_cert_type = frame->ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001513
1514 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001515 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518 {
Hanno Becker21f55672019-02-15 15:27:59 +00001519 /* sig_oid: Previously, needed for convenience in
1520 * mbedtls_x509_crt_info(), now pure legacy burden. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001521 unsigned char *tmp = frame->sig_alg.p;
1522 unsigned char *end = tmp + frame->sig_alg.len;
Hanno Becker21f55672019-02-15 15:27:59 +00001523 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524
Hanno Becker21f55672019-02-15 15:27:59 +00001525 ret = mbedtls_x509_get_alg( &tmp, end,
1526 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527 if( ret != 0 )
1528 {
Hanno Becker21f55672019-02-15 15:27:59 +00001529 /* This should never happen, because we check
1530 * the sanity of the AlgorithmIdentifier structure
1531 * during frame parsing. */
1532 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1533 goto exit;
1534 }
1535 crt->sig_oid = sig_oid;
1536
1537 /* Signature parameters */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001538 tmp = frame->sig_alg.p;
Hanno Becker21f55672019-02-15 15:27:59 +00001539 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1540 &crt->sig_md, &crt->sig_pk,
1541 &crt->sig_opts );
1542 if( ret != 0 )
1543 {
1544 /* Again, this should never happen. */
1545 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1546 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547 }
1548 }
1549
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001550 ret = x509_crt_pk_from_frame( frame, &crt->pk );
Hanno Becker21f55672019-02-15 15:27:59 +00001551 if( ret != 0 )
1552 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001554 ret = x509_crt_subject_from_frame( frame, &crt->subject );
Hanno Becker21f55672019-02-15 15:27:59 +00001555 if( ret != 0 )
1556 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001558 ret = x509_crt_issuer_from_frame( frame, &crt->issuer );
Hanno Becker21f55672019-02-15 15:27:59 +00001559 if( ret != 0 )
1560 goto exit;
1561
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001562 ret = x509_crt_subject_alt_from_frame( frame, &crt->subject_alt_names );
Hanno Becker21f55672019-02-15 15:27:59 +00001563 if( ret != 0 )
1564 goto exit;
1565
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001566 ret = x509_crt_ext_key_usage_from_frame( frame, &crt->ext_key_usage );
1567 if( ret != 0 )
1568 goto exit;
Hanno Becker180f7bf2019-02-28 13:23:38 +00001569#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001570
Hanno Beckerea32d8b2019-03-04 11:52:23 +00001571 /* Currently, we accept DER encoded CRTs with trailing garbage
1572 * and promise to not account for the garbage in the `raw` field.
1573 *
1574 * Note that this means that `crt->raw.len` is not necessarily the
1575 * full size of the heap buffer allocated at `crt->raw.p` in case
1576 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1577 * need the size, and the garbage data doesn't need zeroization. */
1578 crt->raw.len = frame->raw.len;
1579
1580 cache->pk_raw = frame->pubkey_raw;
1581
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001582 /* Free the frame before parsing the public key to
1583 * keep peak RAM usage low. This is slightly inefficient
1584 * because the frame will need to be parsed again on the
1585 * first usage of the CRT, but that seems acceptable.
1586 * As soon as the frame gets used multiple times, it
1587 * will be cached by default. */
1588 x509_crt_cache_clear_frame( crt->cache );
1589
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001590 /* The cache just references the PK structure from the legacy
1591 * implementation, so set up the latter first before setting up
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001592 * the cache.
1593 *
1594 * We're not actually using the parsed PK context here;
1595 * we just parse it to check that it's well-formed. */
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00001596 ret = mbedtls_x509_crt_cache_provide_pk( crt );
Hanno Becker21f55672019-02-15 15:27:59 +00001597 if( ret != 0 )
1598 goto exit;
Hanno Becker7a4de9c2019-02-27 13:12:24 +00001599 x509_crt_cache_clear_pk( crt->cache );
Hanno Becker21f55672019-02-15 15:27:59 +00001600
1601exit:
1602 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001604
Hanno Becker21f55672019-02-15 15:27:59 +00001605 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606}
1607
1608/*
1609 * Parse one X.509 certificate in DER format from a buffer and add them to a
1610 * chained list
1611 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001612static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1613 const unsigned char *buf,
1614 size_t buflen,
1615 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616{
1617 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619
1620 /*
1621 * Check for valid input
1622 */
1623 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625
Hanno Becker371e0e42019-02-25 18:08:59 +00001626 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627 {
1628 prev = crt;
1629 crt = crt->next;
1630 }
1631
1632 /*
1633 * Add new certificate on the end of the chain if needed.
1634 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001635 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001637 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638
1639 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001640 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641
1642 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 }
1646
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001647 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648 {
1649 if( prev )
1650 prev->next = NULL;
1651
1652 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654
1655 return( ret );
1656 }
1657
1658 return( 0 );
1659}
1660
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001661int mbedtls_x509_crt_parse_der_nocopy( 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, 0 ) );
1666}
1667
1668int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1669 const unsigned char *buf,
1670 size_t buflen )
1671{
1672 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1673}
1674
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001676 * Parse one or more PEM certificates from a buffer and add them to the chained
1677 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001679int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1680 const unsigned char *buf,
1681 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682{
Janos Follath98e28a72016-05-31 14:03:54 +01001683#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001684 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001686#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687
1688 /*
1689 * Check for valid input
1690 */
1691 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001693
1694 /*
1695 * Determine buffer content. Buffer contains either one DER certificate or
1696 * one or more PEM certificates.
1697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001699 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001700 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001703 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1706 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001707#else
1708 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1709#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#if defined(MBEDTLS_PEM_PARSE_C)
1712 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713 {
1714 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001716
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001717 /* 1 rather than 0 since the terminating NULL byte is counted in */
1718 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719 {
1720 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001722
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001723 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001725 "-----BEGIN CERTIFICATE-----",
1726 "-----END CERTIFICATE-----",
1727 buf, NULL, 0, &use_len );
1728
1729 if( ret == 0 )
1730 {
1731 /*
1732 * Was PEM encoded
1733 */
1734 buflen -= use_len;
1735 buf += use_len;
1736 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738 {
1739 return( ret );
1740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001744
1745 /*
1746 * PEM header and footer were found
1747 */
1748 buflen -= use_len;
1749 buf += use_len;
1750
1751 if( first_error == 0 )
1752 first_error = ret;
1753
Paul Bakker5a5fa922014-09-26 14:53:04 +02001754 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755 continue;
1756 }
1757 else
1758 break;
1759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001763
1764 if( ret != 0 )
1765 {
1766 /*
1767 * Quit parsing on a memory error
1768 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001769 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770 return( ret );
1771
1772 if( first_error == 0 )
1773 first_error = ret;
1774
1775 total_failed++;
1776 continue;
1777 }
1778
1779 success = 1;
1780 }
1781 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782
1783 if( success )
1784 return( total_failed );
1785 else if( first_error )
1786 return( first_error );
1787 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001789#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001790}
1791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793/*
1794 * Load one or more certificates and add them to the chained list
1795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797{
1798 int ret;
1799 size_t n;
1800 unsigned char *buf;
1801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803 return( ret );
1804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001807 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001809
1810 return( ret );
1811}
1812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814{
1815 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001816#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817 int w_ret;
1818 WCHAR szDir[MAX_PATH];
1819 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001820 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001821 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001822
Paul Bakker9af723c2014-05-01 13:03:14 +02001823 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001824 HANDLE hFind;
1825
1826 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001828
Paul Bakker9af723c2014-05-01 13:03:14 +02001829 memset( szDir, 0, sizeof(szDir) );
1830 memset( filename, 0, MAX_PATH );
1831 memcpy( filename, path, len );
1832 filename[len++] = '\\';
1833 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001834 filename[len++] = '*';
1835
Simon B3c6b18d2016-11-03 01:11:37 +00001836 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001837 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001838 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001840
1841 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001842 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001844
1845 len = MAX_PATH - len;
1846 do
1847 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001848 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849
1850 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1851 continue;
1852
Paul Bakker9af723c2014-05-01 13:03:14 +02001853 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001854 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001855 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001856 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001857 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001858 {
1859 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1860 goto cleanup;
1861 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001864 if( w_ret < 0 )
1865 ret++;
1866 else
1867 ret += w_ret;
1868 }
1869 while( FindNextFileW( hFind, &file_data ) != 0 );
1870
Paul Bakker66d5d072014-06-17 16:39:18 +02001871 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001873
Ron Eldor36d90422017-01-09 15:09:16 +02001874cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001875 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001876#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001877 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001878 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001879 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001880 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001881 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001882 DIR *dir = opendir( path );
1883
Paul Bakker66d5d072014-06-17 16:39:18 +02001884 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001886
Ron Eldor63140682017-01-09 19:27:59 +02001887#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001888 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001889 {
1890 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001891 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001892 }
Ron Eldor63140682017-01-09 19:27:59 +02001893#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001894
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001895 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001896 {
Andres AGf9113192016-09-02 14:06:04 +01001897 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1898 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001899
Andres AGf9113192016-09-02 14:06:04 +01001900 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001901 {
Andres AGf9113192016-09-02 14:06:04 +01001902 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1903 goto cleanup;
1904 }
1905 else if( stat( entry_name, &sb ) == -1 )
1906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001908 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909 }
1910
1911 if( !S_ISREG( sb.st_mode ) )
1912 continue;
1913
1914 // Ignore parse errors
1915 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001917 if( t_ret < 0 )
1918 ret++;
1919 else
1920 ret += t_ret;
1921 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001922
1923cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001924 closedir( dir );
1925
Ron Eldor63140682017-01-09 19:27:59 +02001926#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001927 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001929#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001930
Paul Bakkerbe089b02013-10-14 15:51:50 +02001931#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001932
1933 return( ret );
1934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001936
Hanno Becker02a21932019-06-10 15:08:43 +01001937#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001938static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001940{
1941 size_t i;
1942 size_t n = *size;
1943 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001945 const char *sep = "";
1946 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001947
1948 while( cur != NULL )
1949 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001950 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001951 {
1952 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001953 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001954 }
1955
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001956 n -= cur->buf.len + sep_len;
1957 for( i = 0; i < sep_len; i++ )
1958 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001959 for( i = 0; i < cur->buf.len; i++ )
1960 *p++ = cur->buf.p[i];
1961
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001962 sep = ", ";
1963 sep_len = 2;
1964
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001965 cur = cur->next;
1966 }
1967
1968 *p = '\0';
1969
1970 *size = n;
1971 *buf = p;
1972
1973 return( 0 );
1974}
1975
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001976#define PRINT_ITEM(i) \
1977 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001979 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001980 sep = ", "; \
1981 }
1982
1983#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001984 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001985 PRINT_ITEM( name );
1986
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001987static int x509_info_cert_type( char **buf, size_t *size,
1988 unsigned char ns_cert_type )
1989{
1990 int ret;
1991 size_t n = *size;
1992 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001993 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001996 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1998 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1999 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002000 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
2001 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
2002 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002003
2004 *size = n;
2005 *buf = p;
2006
2007 return( 0 );
2008}
2009
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002010#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01002011 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002012 PRINT_ITEM( name );
2013
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002014static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002015 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002016{
2017 int ret;
2018 size_t n = *size;
2019 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002020 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2023 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002024 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2025 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2026 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2028 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002029 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2030 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002031
2032 *size = n;
2033 *buf = p;
2034
2035 return( 0 );
2036}
2037
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002038static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002040{
2041 int ret;
2042 const char *desc;
2043 size_t n = *size;
2044 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002046 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002047
2048 while( cur != NULL )
2049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002051 desc = "???";
2052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002054 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002055
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002056 sep = ", ";
2057
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002058 cur = cur->next;
2059 }
2060
2061 *size = n;
2062 *buf = p;
2063
2064 return( 0 );
2065}
2066
Hanno Becker4f869ed2019-02-24 16:47:57 +00002067typedef struct mbedtls_x509_crt_sig_info
2068{
2069 mbedtls_md_type_t sig_md;
2070 mbedtls_pk_type_t sig_pk;
2071 void *sig_opts;
2072 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
2073 size_t crt_hash_len;
2074 mbedtls_x509_buf_raw sig;
2075 mbedtls_x509_buf_raw issuer_raw;
2076} mbedtls_x509_crt_sig_info;
2077
2078static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2079{
2080#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2081 mbedtls_free( info->sig_opts );
2082#else
2083 ((void) info);
2084#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2085}
2086
2087static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2088 mbedtls_x509_crt_sig_info *info )
2089{
2090 const mbedtls_md_info_t *md_info;
2091
2092 md_info = mbedtls_md_info_from_type( frame->sig_md );
2093 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2094 info->crt_hash ) != 0 )
2095 {
2096 /* Note: this can't happen except after an internal error */
2097 return( -1 );
2098 }
2099
2100 info->crt_hash_len = mbedtls_md_get_size( md_info );
2101
2102 /* Make sure that this function leaves the target structure
2103 * ready to be freed, regardless of success of failure. */
2104 info->sig_opts = NULL;
2105
2106#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2107 {
2108 int ret;
2109 unsigned char *alg_start = frame->sig_alg.p;
2110 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2111
2112 /* Get signature options -- currently only
2113 * necessary for RSASSA-PSS. */
2114 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2115 &info->sig_pk, &info->sig_opts );
2116 if( ret != 0 )
2117 {
2118 /* Note: this can't happen except after an internal error */
2119 return( -1 );
2120 }
2121 }
2122#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2123 info->sig_md = frame->sig_md;
2124 info->sig_pk = frame->sig_pk;
2125#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2126
2127 info->issuer_raw = frame->issuer_raw;
2128 info->sig = frame->sig;
2129 return( 0 );
2130}
2131
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132/*
2133 * Return an informational string about the certificate.
2134 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002135#define BEFORE_COLON 18
2136#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002138 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002139{
2140 int ret;
2141 size_t n;
2142 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143 char key_size_str[BEFORE_COLON];
Hanno Becker5226c532019-02-27 17:38:40 +00002144 mbedtls_x509_crt_frame frame;
2145 mbedtls_pk_context pk;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002146
Hanno Becker5226c532019-02-27 17:38:40 +00002147 mbedtls_x509_name *issuer = NULL, *subject = NULL;
2148 mbedtls_x509_sequence *ext_key_usage = NULL, *subject_alt_names = NULL;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002149 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002150
2151 p = buf;
2152 n = size;
2153
Hanno Becker4f869ed2019-02-24 16:47:57 +00002154 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
Hanno Becker5226c532019-02-27 17:38:40 +00002155 mbedtls_pk_init( &pk );
2156
2157 if( NULL == crt )
Janos Follath98e28a72016-05-31 14:03:54 +01002158 {
2159 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002160 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Janos Follath98e28a72016-05-31 14:03:54 +01002161
2162 return( (int) ( size - n ) );
2163 }
2164
Hanno Becker5226c532019-02-27 17:38:40 +00002165 ret = mbedtls_x509_crt_get_frame( crt, &frame );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002166 if( ret != 0 )
2167 {
2168 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2169 goto cleanup;
2170 }
2171
Hanno Becker5226c532019-02-27 17:38:40 +00002172 ret = mbedtls_x509_crt_get_subject( crt, &subject );
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_issuer( crt, &issuer );
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_subject_alt_names( crt, &subject_alt_names );
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_ext_key_usage( crt, &ext_key_usage );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002194 if( ret != 0 )
2195 {
2196 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2197 goto cleanup;
2198 }
2199
Hanno Becker5226c532019-02-27 17:38:40 +00002200 ret = mbedtls_x509_crt_get_pk( crt, &pk );
2201 if( ret != 0 )
2202 {
2203 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2204 goto cleanup;
2205 }
2206
2207 ret = x509_crt_get_sig_info( &frame, &sig_info );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002208 if( ret != 0 )
2209 {
2210 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2211 goto cleanup;
2212 }
2213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Hanno Becker5226c532019-02-27 17:38:40 +00002215 prefix, frame.version );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002216 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002217
Hanno Becker4f869ed2019-02-24 16:47:57 +00002218 {
2219 mbedtls_x509_buf serial;
Hanno Becker5226c532019-02-27 17:38:40 +00002220 serial.p = frame.serial.p;
2221 serial.len = frame.serial.len;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002222 ret = mbedtls_snprintf( p, n, "%sserial number : ",
2223 prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002224 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002225 ret = mbedtls_x509_serial_gets( p, n, &serial );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002226 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker4f869ed2019-02-24 16:47:57 +00002227 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002230 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002231 ret = mbedtls_x509_dn_gets( p, n, issuer );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002232 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002235 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Hanno Becker5226c532019-02-27 17:38:40 +00002236 ret = mbedtls_x509_dn_gets( p, n, subject );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002237 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 ret = mbedtls_snprintf( p, n, "\n%sissued 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_from.year, frame.valid_from.mon,
2242 frame.valid_from.day, frame.valid_from.hour,
2243 frame.valid_from.min, frame.valid_from.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002244 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002247 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002248 frame.valid_to.year, frame.valid_to.mon,
2249 frame.valid_to.day, frame.valid_to.hour,
2250 frame.valid_to.min, frame.valid_to.sec );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002251 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002254 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002255
Hanno Becker83cd8672019-02-21 17:13:46 +00002256 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
2257 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002258 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002259
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002260 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker5226c532019-02-27 17:38:40 +00002262 mbedtls_pk_get_name( &pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002263 {
2264 return( ret );
2265 }
2266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker5226c532019-02-27 17:38:40 +00002268 (int) mbedtls_pk_get_bitlen( &pk ) );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002269 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002270
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002271 /*
2272 * Optional extensions
2273 */
2274
Hanno Becker5226c532019-02-27 17:38:40 +00002275 if( frame.ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Hanno Becker5226c532019-02-27 17:38:40 +00002278 frame.ca_istrue ? "true" : "false" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002279 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002280
Hanno Becker5226c532019-02-27 17:38:40 +00002281 if( frame.max_pathlen > 0 )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002282 {
Hanno Becker5226c532019-02-27 17:38:40 +00002283 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", frame.max_pathlen - 1 );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002284 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002285 }
2286 }
2287
Hanno Becker5226c532019-02-27 17:38:40 +00002288 if( frame.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002291 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002292
2293 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002294 subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002295 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002296 }
2297
Hanno Becker5226c532019-02-27 17:38:40 +00002298 if( frame.ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002301 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002302
Hanno Becker5226c532019-02-27 17:38:40 +00002303 if( ( ret = x509_info_cert_type( &p, &n, frame.ns_cert_type ) ) != 0 )
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002304 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002305 }
2306
Hanno Becker5226c532019-02-27 17:38:40 +00002307 if( frame.ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002310 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002311
Hanno Becker5226c532019-02-27 17:38:40 +00002312 if( ( ret = x509_info_key_usage( &p, &n, frame.key_usage ) ) != 0 )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002313 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002314 }
2315
Hanno Becker5226c532019-02-27 17:38:40 +00002316 if( frame.ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002319 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002320
2321 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker5226c532019-02-27 17:38:40 +00002322 ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002323 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002324 }
2325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker54f1c2c2019-05-13 11:58:47 +01002327 MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002328
Hanno Becker4f869ed2019-02-24 16:47:57 +00002329 ret = (int) ( size - n );
2330
2331cleanup:
2332
Hanno Becker4f869ed2019-02-24 16:47:57 +00002333 x509_crt_free_sig_info( &sig_info );
Hanno Becker5226c532019-02-27 17:38:40 +00002334 mbedtls_pk_free( &pk );
2335 mbedtls_x509_name_free( issuer );
2336 mbedtls_x509_name_free( subject );
2337 mbedtls_x509_sequence_free( ext_key_usage );
2338 mbedtls_x509_sequence_free( subject_alt_names );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002339
2340 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341}
2342
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002343struct x509_crt_verify_string {
2344 int code;
2345 const char *string;
2346};
2347
2348static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002349 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002350 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2351 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2352 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2353 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2354 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002355 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2356 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2357 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002358 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002359 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2360 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2361 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2362 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002363 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2364 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2365 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2366 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2367 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2368 { 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 +01002369 { 0, NULL }
2370};
2371
2372int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002373 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002374{
2375 int ret;
2376 const struct x509_crt_verify_string *cur;
2377 char *p = buf;
2378 size_t n = size;
2379
2380 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2381 {
2382 if( ( flags & cur->code ) == 0 )
2383 continue;
2384
2385 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002386 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002387 flags ^= cur->code;
2388 }
2389
2390 if( flags != 0 )
2391 {
2392 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2393 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002394 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002395 }
2396
2397 return( (int) ( size - n ) );
2398}
Hanno Becker02a21932019-06-10 15:08:43 +01002399#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002402static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2403 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002404{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002405 unsigned int usage_must, usage_may;
2406 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2407 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2408
2409 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2410 return( 0 );
2411
2412 usage_must = usage & ~may_mask;
2413
2414 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2415 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2416
2417 usage_may = usage & may_mask;
2418
2419 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002421
2422 return( 0 );
2423}
Hanno Becker45eedf12019-02-25 13:55:33 +00002424
2425int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2426 unsigned int usage )
2427{
2428 int ret;
2429 mbedtls_x509_crt_frame *frame;
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002430 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker45eedf12019-02-25 13:55:33 +00002431 if( ret != 0 )
2432 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2433
2434 ret = x509_crt_check_key_usage_frame( frame, usage );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002435 mbedtls_x509_crt_frame_release( crt );
Hanno Becker45eedf12019-02-25 13:55:33 +00002436
2437 return( ret );
2438}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002439#endif
2440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002442typedef struct
2443{
2444 const char *oid;
2445 size_t oid_len;
2446} x509_crt_check_ext_key_usage_cb_ctx_t;
2447
2448static int x509_crt_check_ext_key_usage_cb( void *ctx,
2449 int tag,
2450 unsigned char *data,
2451 size_t data_len )
2452{
2453 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2454 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2455 ((void) tag);
2456
2457 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2458 data, data_len ) == 0 )
2459 {
2460 return( 1 );
2461 }
2462
2463 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2464 data_len ) == 0 )
2465 {
2466 return( 1 );
2467 }
2468
2469 return( 0 );
2470}
2471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002473 const char *usage_oid,
2474 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002475{
Hanno Beckere1956af2019-02-21 14:28:12 +00002476 int ret;
Hanno Beckere9718b42019-02-25 18:11:42 +00002477 mbedtls_x509_crt_frame *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002478 unsigned ext_types;
2479 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002480 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002481
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002482 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Beckere9718b42019-02-25 18:11:42 +00002483 if( ret != 0 )
2484 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2485
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002486 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002487 ext_types = frame->ext_types;
2488 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2489 {
2490 p = frame->ext_key_usage_raw.p;
2491 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002492
Hanno Beckere9718b42019-02-25 18:11:42 +00002493 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2494 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2495 x509_crt_check_ext_key_usage_cb,
2496 &cb_ctx );
2497 if( ret == 1 )
2498 ret = 0;
2499 else
2500 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2501 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002502
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002503 mbedtls_x509_crt_frame_release( crt );
Hanno Beckere9718b42019-02-25 18:11:42 +00002504 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002509/*
2510 * Return 1 if the certificate is revoked, or 0 otherwise.
2511 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002512static int x509_serial_is_revoked( unsigned char const *serial,
2513 size_t serial_len,
2514 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002515{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002517
2518 while( cur != NULL && cur->serial.len != 0 )
2519 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002520 if( serial_len == cur->serial.len &&
2521 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002522 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002523 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002524 return( 1 );
2525 }
2526
2527 cur = cur->next;
2528 }
2529
2530 return( 0 );
2531}
2532
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002533int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2534 const mbedtls_x509_crl *crl )
2535{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002536 int ret;
2537 mbedtls_x509_crt_frame *frame;
2538
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002539 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002540 if( ret != 0 )
2541 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2542
2543 ret = x509_serial_is_revoked( frame->serial.p,
2544 frame->serial.len,
2545 crl );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002546 mbedtls_x509_crt_frame_release( crt );
Hanno Becker79ae5b62019-02-25 18:12:00 +00002547 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002548}
2549
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002550/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002551 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002552 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002553 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002554static int x509_crt_verifycrl( unsigned char *crt_serial,
2555 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002556 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002557 mbedtls_x509_crl *crl_list,
2558 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002559{
Hanno Beckerbb266132019-02-25 18:12:46 +00002560 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002561 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2563 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002564 mbedtls_x509_buf_raw ca_subject;
2565 mbedtls_pk_context *pk;
2566 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002567
Hanno Beckerbb266132019-02-25 18:12:46 +00002568 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002569 return( flags );
2570
Hanno Beckerbb266132019-02-25 18:12:46 +00002571 {
2572 mbedtls_x509_crt_frame *ca;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002573 ret = mbedtls_x509_crt_frame_acquire( ca_crt, &ca );
Hanno Beckerbb266132019-02-25 18:12:46 +00002574 if( ret != 0 )
2575 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2576
2577 ca_subject = ca->subject_raw;
2578
2579 can_sign = 0;
2580 if( x509_crt_check_key_usage_frame( ca,
2581 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2582 {
2583 can_sign = 1;
2584 }
2585
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002586 mbedtls_x509_crt_frame_release( ca_crt );
Hanno Beckerbb266132019-02-25 18:12:46 +00002587 }
2588
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002589 ret = mbedtls_x509_crt_pk_acquire( ca_crt, &pk );
Hanno Beckerbb266132019-02-25 18:12:46 +00002590 if( ret != 0 )
2591 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2592
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002593 while( crl_list != NULL )
2594 {
2595 if( crl_list->version == 0 ||
Hanno Becker1e11f212019-03-04 14:43:43 +00002596 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw,
Hanno Beckerbb266132019-02-25 18:12:46 +00002597 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002598 {
2599 crl_list = crl_list->next;
2600 continue;
2601 }
2602
2603 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002604 * Check if the CA is configured to sign CRLs
2605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002607 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002610 break;
2611 }
2612#endif
2613
2614 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002615 * Check if CRL is correctly signed by the trusted CA
2616 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002617 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2618 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2619
2620 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2621 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002624 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002625 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002626 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002628 break;
2629 }
2630
Hanno Beckerbb266132019-02-25 18:12:46 +00002631 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002632 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002633
Hanno Beckerbb266132019-02-25 18:12:46 +00002634 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002636 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002639 break;
2640 }
2641
2642 /*
2643 * Check for validity of CRL (Do not drop out)
2644 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002645 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002647
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002648 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002649 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002650
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002651 /*
2652 * Check if certificate is revoked
2653 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002654 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2655 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002658 break;
2659 }
2660
2661 crl_list = crl_list->next;
2662 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002663
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002664 mbedtls_x509_crt_pk_release( ca_crt );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002665 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002668
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002669/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002670 * Check the signature of a certificate by its parent
2671 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002672static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002673 mbedtls_x509_crt *parent,
2674 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002675{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002676 int ret;
2677 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002678
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002679 ret = mbedtls_x509_crt_pk_acquire( parent, &pk );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002680 if( ret != 0 )
2681 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2682
2683 /* Skip expensive computation on obvious mismatch */
2684 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002685 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002686 ret = -1;
2687 goto exit;
2688 }
2689
2690#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2691 ((void) rs_ctx);
2692#else
2693 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2694 {
2695 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002696 sig_info->sig_md,
2697 sig_info->crt_hash, sig_info->crt_hash_len,
2698 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002699 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002700 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002701 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002702#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002703 {
2704 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2705 sig_info->sig_opts,
2706 pk,
2707 sig_info->sig_md,
2708 sig_info->crt_hash, sig_info->crt_hash_len,
2709 sig_info->sig.p, sig_info->sig.len );
2710 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002711
Hanno Beckere449e2d2019-02-25 14:45:31 +00002712exit:
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002713 mbedtls_x509_crt_pk_release( parent );
Hanno Beckere449e2d2019-02-25 14:45:31 +00002714 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002715}
2716
2717/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002718 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2719 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002720 *
2721 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002722 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002723static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2724 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002725 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002726{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002727 int need_ca_bit;
2728
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002729 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002730 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2731 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002732 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002733 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002734 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002735 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002736
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002737 /* Parent must have the basicConstraints CA bit set as a general rule */
2738 need_ca_bit = 1;
2739
2740 /* Exception: v1/v2 certificates that are locally trusted. */
2741 if( top && parent->version < 3 )
2742 need_ca_bit = 0;
2743
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002744 if( need_ca_bit && ! parent->ca_istrue )
2745 return( -1 );
2746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002748 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002749 x509_crt_check_key_usage_frame( parent,
2750 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002751 {
2752 return( -1 );
2753 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002754#endif
2755
2756 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002757}
2758
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002759/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002760 * Find a suitable parent for child in candidates, or return NULL.
2761 *
2762 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002763 * 1. subject name matches child's issuer
2764 * 2. if necessary, the CA bit is set and key usage allows signing certs
2765 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002766 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002767 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002768 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002769 * If there's a suitable candidate which is also time-valid, return the first
2770 * such. Otherwise, return the first suitable candidate (or NULL if there is
2771 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002772 *
2773 * The rationale for this rule is that someone could have a list of trusted
2774 * roots with two versions on the same root with different validity periods.
2775 * (At least one user reported having such a list and wanted it to just work.)
2776 * The reason we don't just require time-validity is that generally there is
2777 * only one version, and if it's expired we want the flags to state that
2778 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002779 *
2780 * The rationale for rule 3 (signature for trusted roots) is that users might
2781 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002782 * way we select the correct one is by checking the signature (as we don't
2783 * rely on key identifier extensions). (This is one way users might choose to
2784 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002785 *
2786 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002787 * - [in] child: certificate for which we're looking for a parent
2788 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002789 * - [out] r_parent: parent found (or NULL)
2790 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002791 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2792 * of the chain, 0 otherwise
2793 * - [in] path_cnt: number of intermediates seen so far
2794 * - [in] self_cnt: number of self-signed intermediates seen so far
2795 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002796 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002797 *
2798 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002799 * - 0 on success
2800 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002801 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002802static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002803 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002804 mbedtls_x509_crt *candidates,
2805 mbedtls_x509_crt **r_parent,
2806 int *r_signature_is_good,
2807 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002808 unsigned path_cnt,
2809 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002810 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002811{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002812 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002813 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002814 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002815
2816#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002817 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002818 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2819 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002820 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002821 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002822 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002823 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002824
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002825 /* clear saved state */
2826 rs_ctx->parent = NULL;
2827 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002828 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002829
2830 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002831 goto check_signature;
2832 }
2833#endif
2834
2835 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002836 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002837
Hanno Becker43bf9002019-02-25 14:46:49 +00002838 for( parent_crt = candidates; parent_crt != NULL;
2839 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002840 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002841 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002842
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002843#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2844check_signature:
2845#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002846
2847 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002848 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002849 mbedtls_x509_crt_frame *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002850
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00002851 ret = mbedtls_x509_crt_frame_acquire( parent_crt, &parent );
Hanno Becker43bf9002019-02-25 14:46:49 +00002852 if( ret != 0 )
2853 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002854
Hanno Becker43bf9002019-02-25 14:46:49 +00002855 if( mbedtls_x509_time_is_past( &parent->valid_from ) &&
2856 mbedtls_x509_time_is_future( &parent->valid_to ) )
2857 {
2858 parent_valid = 1;
2859 }
2860
2861 /* basic parenting skills (name, CA bit, key usage) */
2862 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2863 parent_match = 1;
2864
2865 /* +1 because the stored max_pathlen is 1 higher
2866 * than the actual value */
2867 if( !( parent->max_pathlen > 0 &&
2868 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2869 {
2870 path_len_ok = 1;
2871 }
2872
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00002873 mbedtls_x509_crt_frame_release( parent_crt );
Hanno Beckera788cab2019-02-24 17:47:46 +00002874 }
2875
2876 if( parent_match == 0 || path_len_ok == 0 )
2877 continue;
2878
2879 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002880 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002881
2882#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002883 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2884 {
2885 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002886 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002887 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002888 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002889
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002890 return( ret );
2891 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002892#else
2893 (void) ret;
2894#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002895
2896 signature_is_good = ret == 0;
2897 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002898 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002899
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002900 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002901 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002902 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002903 if( fallback_parent == NULL )
2904 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002905 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002906 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002907 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002908
2909 continue;
2910 }
2911
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002912 break;
2913 }
2914
Hanno Becker43bf9002019-02-25 14:46:49 +00002915 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002916 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002917 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002918 *r_signature_is_good = signature_is_good;
2919 }
2920 else
2921 {
2922 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002923 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002924 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002925
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002926 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002927}
2928
2929/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002930 * Find a parent in trusted CAs or the provided chain, or return NULL.
2931 *
2932 * Searches in trusted CAs first, and return the first suitable parent found
2933 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002934 *
2935 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002936 * - [in] child: certificate for which we're looking for a parent, followed
2937 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002938 * - [in] trust_ca: list of locally trusted certificates
2939 * - [out] parent: parent found (or NULL)
2940 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2941 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2942 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2943 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002944 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002945 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002946 *
2947 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002948 * - 0 on success
2949 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002950 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002951static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002952 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002953 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002954 mbedtls_x509_crt *trust_ca,
2955 mbedtls_x509_crt **parent,
2956 int *parent_is_trusted,
2957 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002958 unsigned path_cnt,
2959 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002960 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002961{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002962 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002963 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002964
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002965 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002966
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002967#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002968 /* restore then clear saved state if we have some stored */
2969 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2970 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002971 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002972 rs_ctx->parent_is_trusted = -1;
2973 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002974#endif
2975
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002976 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00002977 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002978
Hanno Becker5299cf82019-02-25 13:50:41 +00002979 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002980 parent, signature_is_good,
2981 *parent_is_trusted,
2982 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002983
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002984#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002985 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2986 {
2987 /* save state */
2988 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002989 return( ret );
2990 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002991#else
2992 (void) ret;
2993#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002994
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002995 /* stop here if found or already in second iteration */
2996 if( *parent != NULL || *parent_is_trusted == 0 )
2997 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002998
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002999 /* prepare second iteration */
3000 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003001 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003002
3003 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01003004 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003005 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02003006 *parent_is_trusted = 0;
3007 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02003008 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003009
3010 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02003011}
3012
3013/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003014 * Check if an end-entity certificate is locally trusted
3015 *
3016 * Currently we require such certificates to be self-signed (actually only
3017 * check for self-issued as self-signatures are not checked)
3018 */
3019static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00003020 mbedtls_x509_crt_frame const *crt,
3021 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003022{
Hanno Becker1e0677a2019-02-25 14:58:22 +00003023 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003024
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003025 /* look for an exact match with trusted cert */
3026 for( cur = trust_ca; cur != NULL; cur = cur->next )
3027 {
3028 if( crt->raw.len == cur->raw.len &&
3029 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
3030 {
3031 return( 0 );
3032 }
3033 }
3034
3035 /* too bad */
3036 return( -1 );
3037}
3038
3039/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003040 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003041 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003042 * Given a peer-provided list of certificates EE, C1, ..., Cn and
3043 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003044 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003045 * such that every cert in the chain is a child of the next one,
3046 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003047 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003048 * Verify that chain and return it with flags for all issues found.
3049 *
3050 * Special cases:
3051 * - EE == Rj -> return a one-element list containing it
3052 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
3053 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003054 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02003055 * Tests for (aspects of) this function should include at least:
3056 * - trusted EE
3057 * - EE -> trusted root
3058 * - EE -> intermedate CA -> trusted root
3059 * - if relevant: EE untrusted
3060 * - if relevant: EE -> intermediate, untrusted
3061 * with the aspect under test checked at each relevant level (EE, int, root).
3062 * For some aspects longer chains are required, but usually length 2 is
3063 * enough (but length 1 is not in general).
3064 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003065 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003066 * - [in] crt: the cert list EE, C1, ..., Cn
3067 * - [in] trust_ca: the trusted list R1, ..., Rp
3068 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003069 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003070 * Only valid when return value is 0, may contain garbage otherwise!
3071 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003072 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003073 *
3074 * Return value:
3075 * - non-zero if the chain could not be fully built and examined
3076 * - 0 is the chain was successfully built and examined,
3077 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02003078 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003079static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003080 mbedtls_x509_crt *crt,
3081 mbedtls_x509_crt *trust_ca,
3082 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003083 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003084 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003085 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003086{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003087 /* Don't initialize any of those variables here, so that the compiler can
3088 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003089 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003090 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003091 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003092 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00003093 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003094 int parent_is_trusted;
3095 int child_is_trusted;
3096 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003097 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003098
3099#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3100 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003101 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003102 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02003103 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003104 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003105 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003106
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003107 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003108 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003109 child_crt = cur->crt;
3110
3111 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003112 goto find_parent;
3113 }
3114#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003115
Hanno Becker1e0677a2019-02-25 14:58:22 +00003116 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003117 self_cnt = 0;
3118 parent_is_trusted = 0;
3119 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02003120
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003121 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00003122#if defined(MBEDTLS_X509_CRL_PARSE_C)
3123 mbedtls_x509_buf_raw child_serial;
3124#endif /* MBEDTLS_X509_CRL_PARSE_C */
3125 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00003126
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003127 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003128 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00003129 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003130 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003131 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00003132
3133#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3134find_parent:
3135#endif
3136
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003137 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003138
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003139 {
Hanno Becker5299cf82019-02-25 13:50:41 +00003140 mbedtls_x509_crt_sig_info child_sig;
3141 {
3142 mbedtls_x509_crt_frame *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003143
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003144 ret = mbedtls_x509_crt_frame_acquire( child_crt, &child );
Hanno Becker5299cf82019-02-25 13:50:41 +00003145 if( ret != 0 )
3146 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3147
3148 /* Check time-validity (all certificates) */
3149 if( mbedtls_x509_time_is_past( &child->valid_to ) )
3150 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
3151 if( mbedtls_x509_time_is_future( &child->valid_from ) )
3152 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
3153
3154 /* Stop here for trusted roots (but not for trusted EE certs) */
3155 if( child_is_trusted )
3156 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003157 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003158 return( 0 );
3159 }
3160
3161 self_issued = 0;
3162 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
3163 &child->subject_raw,
3164 NULL, NULL ) == 0 )
3165 {
3166 self_issued = 1;
3167 }
3168
3169 /* Check signature algorithm: MD & PK algs */
3170 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
3171 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
3172
3173 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
3174 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3175
3176 /* Special case: EE certs that are locally trusted */
3177 if( ver_chain->len == 1 && self_issued &&
3178 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
3179 {
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003180 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003181 return( 0 );
3182 }
3183
3184#if defined(MBEDTLS_X509_CRL_PARSE_C)
3185 child_serial = child->serial;
3186#endif /* MBEDTLS_X509_CRL_PARSE_C */
3187
3188 ret = x509_crt_get_sig_info( child, &child_sig );
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003189 mbedtls_x509_crt_frame_release( child_crt );
Hanno Becker5299cf82019-02-25 13:50:41 +00003190
Hanno Becker5299cf82019-02-25 13:50:41 +00003191 if( ret != 0 )
3192 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3193 }
3194
3195 /* Look for a parent in trusted CAs or up the chain */
3196 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00003197 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00003198 &parent_is_trusted, &signature_is_good,
3199 ver_chain->len - 1, self_cnt, rs_ctx );
3200
3201 x509_crt_free_sig_info( &child_sig );
3202 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003203
3204#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003205 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3206 {
3207 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003208 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003209 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003210 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00003211 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003212 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003213#else
3214 (void) ret;
3215#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003216
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003217 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00003218 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003219 {
3220 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00003221 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003222 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003223
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003224 /* Count intermediate self-issued (not necessarily self-signed) certs.
3225 * These can occur with some strategies for key rollover, see [SIRO],
3226 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00003227 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003228 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003229
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003230 /* path_cnt is 0 for the first intermediate CA,
3231 * and if parent is trusted it's not an intermediate CA */
3232 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003233 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003234 {
3235 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00003236 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003237 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003238
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003239 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003240 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003241 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
3242
Hanno Becker58c35642019-02-25 18:13:46 +00003243 {
3244 mbedtls_pk_context *parent_pk;
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003245 ret = mbedtls_x509_crt_pk_acquire( parent_crt, &parent_pk );
Hanno Becker58c35642019-02-25 18:13:46 +00003246 if( ret != 0 )
3247 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3248
3249 /* check size of signing key */
3250 if( x509_profile_check_key( profile, parent_pk ) != 0 )
3251 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3252
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003253 mbedtls_x509_crt_pk_release( parent_crt );
Hanno Becker58c35642019-02-25 18:13:46 +00003254 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003257 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00003258 *flags |= x509_crt_verifycrl( child_serial.p,
3259 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00003260 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003261#else
3262 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003263#endif
3264
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003265 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00003266 child_crt = parent_crt;
3267 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003268 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003269 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003270 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003271}
3272
3273/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003274 * Check for CN match
3275 */
Hanno Becker24926222019-02-21 13:10:55 +00003276static int x509_crt_check_cn( unsigned char const *buf,
3277 size_t buflen,
3278 const char *cn,
3279 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003280{
Hanno Becker24926222019-02-21 13:10:55 +00003281 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00003282 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003283 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003284
3285 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00003286 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003287 {
3288 return( 0 );
3289 }
3290
3291 return( -1 );
3292}
3293
Hanno Becker8b543b32019-02-21 11:50:44 +00003294/* Returns 1 on a match and 0 on a mismatch.
3295 * This is because this function is used as a callback for
3296 * mbedtls_x509_name_cmp_raw(), which continues the name
3297 * traversal as long as the callback returns 0. */
3298static int x509_crt_check_name( void *ctx,
3299 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00003300 mbedtls_x509_buf *val,
3301 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00003302{
3303 char const *cn = (char const*) ctx;
3304 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003305 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003306
3307 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003308 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003309 {
3310 return( 1 );
3311 }
3312
3313 return( 0 );
3314}
3315
Hanno Beckerda410822019-02-21 13:36:59 +00003316/* Returns 1 on a match and 0 on a mismatch.
3317 * This is because this function is used as a callback for
3318 * mbedtls_asn1_traverse_sequence_of(), which continues the
3319 * traversal as long as the callback returns 0. */
3320static int x509_crt_subject_alt_check_name( void *ctx,
3321 int tag,
3322 unsigned char *data,
3323 size_t data_len )
3324{
3325 char const *cn = (char const*) ctx;
3326 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003327 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003328
3329 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3330 return( 1 );
3331
3332 return( 0 );
3333}
3334
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003335/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003336 * Verify the requested CN - only call this if cn is not NULL!
3337 */
Hanno Becker082435c2019-02-25 18:14:40 +00003338static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3339 const char *cn,
3340 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003341{
Hanno Beckerda410822019-02-21 13:36:59 +00003342 int ret;
Hanno Becker082435c2019-02-25 18:14:40 +00003343 mbedtls_x509_crt_frame *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003344
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003345 ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
Hanno Becker082435c2019-02-25 18:14:40 +00003346 if( ret != 0 )
3347 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3348
3349 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003350 {
Hanno Becker90b94082019-02-21 21:13:21 +00003351 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003352 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003353 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003354 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003355
Hanno Becker90b94082019-02-21 21:13:21 +00003356 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3357 MBEDTLS_ASN1_TAG_CLASS_MASK,
3358 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3359 MBEDTLS_ASN1_TAG_VALUE_MASK,
3360 2 /* SubjectAlt DNS */,
3361 x509_crt_subject_alt_check_name,
3362 (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003363 }
3364 else
3365 {
Hanno Becker082435c2019-02-25 18:14:40 +00003366 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3367 &frame->subject_raw,
Hanno Becker8b543b32019-02-21 11:50:44 +00003368 x509_crt_check_name, (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003369 }
Hanno Beckerda410822019-02-21 13:36:59 +00003370
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003371 mbedtls_x509_crt_frame_release( crt );
Hanno Becker082435c2019-02-25 18:14:40 +00003372
3373 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3374 * return 1 when finding a name component matching `cn`. */
3375 if( ret == 1 )
3376 return( 0 );
3377
3378 if( ret != 0 )
3379 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3380
3381 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3382 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003383}
3384
3385/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003386 * Merge the flags for all certs in the chain, after calling callback
3387 */
3388static int x509_crt_merge_flags_with_cb(
3389 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003390 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003391 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3392 void *p_vrfy )
3393{
3394 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003395 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003396 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003397 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003398
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003399 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003400 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003401 cur = &ver_chain->items[i-1];
3402 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003403
3404 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003405 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003406 return( ret );
3407
3408 *flags |= cur_flags;
3409 }
3410
3411 return( 0 );
3412}
3413
3414/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003415 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3418 mbedtls_x509_crt *trust_ca,
3419 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003420 const char *cn, uint32_t *flags,
3421 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003422 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003423{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003424 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3425 &mbedtls_x509_crt_profile_default, cn, flags,
3426 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003427}
3428
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003429/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003430 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003431 */
3432int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3433 mbedtls_x509_crt *trust_ca,
3434 mbedtls_x509_crl *ca_crl,
3435 const mbedtls_x509_crt_profile *profile,
3436 const char *cn, uint32_t *flags,
3437 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3438 void *p_vrfy )
3439{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003440 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3441 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3442}
3443
3444/*
3445 * Verify the certificate validity, with profile, restartable version
3446 *
3447 * This function:
3448 * - checks the requested CN (if any)
3449 * - checks the type and size of the EE cert's key,
3450 * as that isn't done as part of chain building/verification currently
3451 * - builds and verifies the chain
3452 * - then calls the callback and merges the flags
3453 */
3454int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3455 mbedtls_x509_crt *trust_ca,
3456 mbedtls_x509_crl *ca_crl,
3457 const mbedtls_x509_crt_profile *profile,
3458 const char *cn, uint32_t *flags,
3459 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3460 void *p_vrfy,
3461 mbedtls_x509_crt_restart_ctx *rs_ctx )
3462{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003463 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003464 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003465 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003466
3467 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003468 ee_flags = 0;
3469 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003470
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003471 if( profile == NULL )
3472 {
3473 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3474 goto exit;
3475 }
3476
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003477 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003478 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003479 {
3480 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3481 if( ret != 0 )
3482 return( ret );
3483 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003484
Hanno Becker87233362019-02-25 18:15:33 +00003485 {
3486 mbedtls_pk_context *pk;
3487 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003488
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003489 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
Hanno Becker87233362019-02-25 18:15:33 +00003490 if( ret != 0 )
3491 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003492
Hanno Becker87233362019-02-25 18:15:33 +00003493 /* Check the type and size of the key */
3494 pk_type = mbedtls_pk_get_type( pk );
3495
3496 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3497 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3498
3499 if( x509_profile_check_key( profile, pk ) != 0 )
3500 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3501
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00003502 mbedtls_x509_crt_pk_release( crt );
Hanno Becker87233362019-02-25 18:15:33 +00003503 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003504
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003505 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003506 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003507 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003508
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003509 if( ret != 0 )
3510 goto exit;
3511
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003512 /* Merge end-entity flags */
3513 ver_chain.items[0].flags |= ee_flags;
3514
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003515 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003516 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003517
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003518exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003519#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3520 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3521 mbedtls_x509_crt_restart_free( rs_ctx );
3522#endif
3523
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003524 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3525 * the SSL module for authmode optional, but non-zero return from the
3526 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003527 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3528 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3529
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003530 if( ret != 0 )
3531 {
3532 *flags = (uint32_t) -1;
3533 return( ret );
3534 }
3535
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003536 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003538
3539 return( 0 );
3540}
3541
3542/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003543 * Initialize a certificate chain
3544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003546{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003548}
3549
3550/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003551 * Unallocate all certificate data
3552 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 mbedtls_x509_crt *cert_cur = crt;
3557 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003558
3559 if( crt == NULL )
3560 return;
3561
3562 do
3563 {
Hanno Beckerb6c39fc2019-02-25 13:50:14 +00003564 x509_crt_cache_free( cert_cur->cache );
3565 mbedtls_free( cert_cur->cache );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003566
3567#if !defined(MBEDTLS_X509_ON_DEMAND_PARSING)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3571 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003572#endif
3573
Hanno Becker2bcc7642019-02-26 19:01:00 +00003574 mbedtls_x509_name_free( cert_cur->issuer.next );
3575 mbedtls_x509_name_free( cert_cur->subject.next );
3576 mbedtls_x509_sequence_free( cert_cur->ext_key_usage.next );
3577 mbedtls_x509_sequence_free( cert_cur->subject_alt_names.next );
Hanno Becker180f7bf2019-02-28 13:23:38 +00003578#endif /* !MBEDTLS_X509_ON_DEMAND_PARSING */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003579
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003580 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003581 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003582 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003584 }
3585
3586 cert_cur = cert_cur->next;
3587 }
3588 while( cert_cur != NULL );
3589
3590 cert_cur = crt;
3591 do
3592 {
3593 cert_prv = cert_cur;
3594 cert_cur = cert_cur->next;
3595
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003596 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003597 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003599 }
3600 while( cert_cur != NULL );
3601}
3602
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003603#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3604/*
3605 * Initialize a restart context
3606 */
3607void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3608{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003609 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003610
3611 ctx->parent = NULL;
3612 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003613 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003614
3615 ctx->parent_is_trusted = -1;
3616
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003617 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003618 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003619 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003620}
3621
3622/*
3623 * Free the components of a restart context
3624 */
3625void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3626{
3627 if( ctx == NULL )
3628 return;
3629
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003630 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003631 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003632}
3633#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635#endif /* MBEDTLS_X509_CRT_PARSE_C */