blob: bf62f65d90c24d4e48e85696c209b55c5efa0428 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020030 *
31 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/x509_crt.h"
43#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050044#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000045
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
77#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Hanno Becker21f55672019-02-15 15:27:59 +000081static int x509_crt_parse_frame( unsigned char *start,
82 unsigned char *end,
83 mbedtls_x509_crt_frame *frame );
84static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
85 mbedtls_x509_name *subject );
86static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
87 mbedtls_x509_name *issuer );
88static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
89 mbedtls_x509_sequence *subject_alt );
90static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
91 mbedtls_x509_sequence *ext_key_usage );
92
Hanno Becker337088a2019-02-25 14:53:14 +000093static void x509_free_sequence( mbedtls_x509_sequence *seq );
94static void x509_free_name( mbedtls_x509_name *name );
95
Hanno Becker5299cf82019-02-25 13:50:41 +000096static int x509_crt_pk_acquire( mbedtls_x509_crt *crt,
97 mbedtls_pk_context **pk );
98static int x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
99 mbedtls_x509_crt_frame **frame );
100static void x509_crt_pk_release( mbedtls_x509_crt *crt,
101 mbedtls_pk_context *pk );
102static void x509_crt_frame_release( mbedtls_x509_crt *crt,
103 mbedtls_pk_context *pk );
104
Hanno Becker337088a2019-02-25 14:53:14 +0000105static int x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
106 mbedtls_x509_crt_frame **frame_ptr )
107{
108 int ret;
109 mbedtls_x509_crt_frame *frame;
110
111 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
112 if( frame == NULL )
113 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
114
115 ret = x509_crt_parse_frame( crt->raw.p, crt->raw.p + crt->raw.len,
116 frame );
117 if( ret != 0 )
118 return( ret );
119
120 *frame_ptr = frame;
121 return( 0 );
122}
123
124static void x509_crt_frame_release( mbedtls_x509_crt const *crt,
125 mbedtls_x509_crt_frame *frame )
126{
127 ((void) crt);
128 if( frame == NULL )
129 return;
130 mbedtls_free( frame );
131}
132static int x509_crt_pk_acquire( mbedtls_x509_crt *crt,
133 mbedtls_pk_context **pk )
134{
135 *pk = &crt->pk;
136 return( 0 );
137}
138
139static void x509_crt_pk_release( mbedtls_x509_crt *crt,
140 mbedtls_pk_context *pk )
141{
142 ((void) crt);
143 ((void) pk);
144 return;
145}
146
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200147/*
148 * Item in a verification chain: cert and flags for it
149 */
150typedef struct {
151 mbedtls_x509_crt *crt;
152 uint32_t flags;
153} x509_crt_verify_chain_item;
154
155/*
156 * Max size of verification chain: end-entity + intermediates + trusted root
157 */
158#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200159
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200161 * Default profile
162 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
164{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200165#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200166 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200167 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200168#endif
169 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200170 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
171 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
172 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
173 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
174 0xFFFFFFF, /* Any PK alg */
175 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200176 2048,
177};
178
179/*
180 * Next-default profile
181 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200182const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
183{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200184 /* Hashes from SHA-256 and above */
185 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
186 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
187 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
188 0xFFFFFFF, /* Any PK alg */
189#if defined(MBEDTLS_ECP_C)
190 /* Curves at or above 128-bit security level */
191 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
192 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
193 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
194 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
195 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
196 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
197 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
198#else
199 0,
200#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200201 2048,
202};
203
204/*
205 * NSA Suite B Profile
206 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200207const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
208{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200209 /* Only SHA-256 and 384 */
210 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
211 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
212 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200213 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
214 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200215#if defined(MBEDTLS_ECP_C)
216 /* Only NIST P-256 and P-384 */
217 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
218 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
219#else
220 0,
221#endif
222 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200223};
224
225/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200227 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200228 */
229static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
230 mbedtls_md_type_t md_alg )
231{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200232 if( md_alg == MBEDTLS_MD_NONE )
233 return( -1 );
234
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200235 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
236 return( 0 );
237
238 return( -1 );
239}
240
241/*
242 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200243 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200244 */
245static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
246 mbedtls_pk_type_t pk_alg )
247{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200248 if( pk_alg == MBEDTLS_PK_NONE )
249 return( -1 );
250
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200251 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
252 return( 0 );
253
254 return( -1 );
255}
256
257/*
258 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200259 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200260 */
261static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200262 const mbedtls_pk_context *pk )
263{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200264 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200265
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200266#if defined(MBEDTLS_RSA_C)
267 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
268 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200269 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200270 return( 0 );
271
272 return( -1 );
273 }
274#endif
275
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200276#if defined(MBEDTLS_ECP_C)
277 if( pk_alg == MBEDTLS_PK_ECDSA ||
278 pk_alg == MBEDTLS_PK_ECKEY ||
279 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200280 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200281 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200282
Philippe Antoineb5b25432018-05-11 11:06:29 +0200283 if( gid == MBEDTLS_ECP_DP_NONE )
284 return( -1 );
285
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200286 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
287 return( 0 );
288
289 return( -1 );
290 }
291#endif
292
293 return( -1 );
294}
295
296/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000297 * Return 0 if name matches wildcard, -1 otherwise
298 */
Hanno Becker24926222019-02-21 13:10:55 +0000299static int x509_check_wildcard( char const *cn,
300 size_t cn_len,
301 unsigned char const *buf,
302 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000303{
304 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000305 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000306
307 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000308 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000309 return( -1 );
310
311 for( i = 0; i < cn_len; ++i )
312 {
313 if( cn[i] == '.' )
314 {
315 cn_idx = i;
316 break;
317 }
318 }
319
320 if( cn_idx == 0 )
321 return( -1 );
322
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000323 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
324 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000325 {
326 return( 0 );
327 }
328
329 return( -1 );
330}
331
332/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200333 * Reset (init or clear) a verify_chain
334 */
335static void x509_crt_verify_chain_reset(
336 mbedtls_x509_crt_verify_chain *ver_chain )
337{
338 size_t i;
339
340 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
341 {
342 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000343 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200344 }
345
346 ver_chain->len = 0;
347}
348
349/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
351 */
352static int x509_get_version( unsigned char **p,
353 const unsigned char *end,
354 int *ver )
355{
356 int ret;
357 size_t len;
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
360 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 {
364 *ver = 0;
365 return( 0 );
366 }
367
Hanno Becker2f472142019-02-12 11:52:10 +0000368 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369 }
370
371 end = *p + len;
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
374 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
376 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 return( MBEDTLS_ERR_X509_INVALID_VERSION +
378 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
380 return( 0 );
381}
382
383/*
384 * Validity ::= SEQUENCE {
385 * notBefore Time,
386 * notAfter Time }
387 */
388static int x509_get_dates( unsigned char **p,
389 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_x509_time *from,
391 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392{
393 int ret;
394 size_t len;
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
397 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
398 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399
400 end = *p + len;
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 return( ret );
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 return( ret );
407
408 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 return( MBEDTLS_ERR_X509_INVALID_DATE +
410 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
412 return( 0 );
413}
414
415/*
416 * X.509 v2/v3 unique identifier (not parsed)
417 */
418static int x509_get_uid( unsigned char **p,
419 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421{
422 int ret;
423
424 if( *p == end )
425 return( 0 );
426
427 uid->tag = **p;
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
430 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 return( 0 );
434
Hanno Becker2f472142019-02-12 11:52:10 +0000435 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 }
437
438 uid->p = *p;
439 *p += uid->len;
440
441 return( 0 );
442}
443
444static int x509_get_basic_constraints( unsigned char **p,
445 const unsigned char *end,
446 int *ca_istrue,
447 int *max_pathlen )
448{
449 int ret;
450 size_t len;
451
452 /*
453 * BasicConstraints ::= SEQUENCE {
454 * cA BOOLEAN DEFAULT FALSE,
455 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
456 */
457 *ca_istrue = 0; /* DEFAULT FALSE */
458 *max_pathlen = 0; /* endless */
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
461 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000462 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
464 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200465 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
470 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
472 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000473 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
475 if( *ca_istrue != 0 )
476 *ca_istrue = 1;
477 }
478
479 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200480 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000483 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
485 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000486 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487
488 (*max_pathlen)++;
489
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200490 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491}
492
493static int x509_get_ns_cert_type( unsigned char **p,
494 const unsigned char *end,
495 unsigned char *ns_cert_type)
496{
497 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000501 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
503 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000504 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
506 /* Get actual bitstring */
507 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200508 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509}
510
511static int x509_get_key_usage( unsigned char **p,
512 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100513 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514{
515 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200516 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000520 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521
522 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000523 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
525 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200526 *key_usage = 0;
527 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
528 {
529 *key_usage |= (unsigned int) bs.p[i] << (8*i);
530 }
531
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200532 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533}
534
535/*
536 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
537 *
538 * KeyPurposeId ::= OBJECT IDENTIFIER
539 */
540static int x509_get_ext_key_usage( unsigned char **p,
541 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000544 return( mbedtls_asn1_get_sequence_of( p, end, ext_key_usage,
545 MBEDTLS_ASN1_OID ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546}
547
548/*
549 * SubjectAltName ::= GeneralNames
550 *
551 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
552 *
553 * GeneralName ::= CHOICE {
554 * otherName [0] OtherName,
555 * rfc822Name [1] IA5String,
556 * dNSName [2] IA5String,
557 * x400Address [3] ORAddress,
558 * directoryName [4] Name,
559 * ediPartyName [5] EDIPartyName,
560 * uniformResourceIdentifier [6] IA5String,
561 * iPAddress [7] OCTET STRING,
562 * registeredID [8] OBJECT IDENTIFIER }
563 *
564 * OtherName ::= SEQUENCE {
565 * type-id OBJECT IDENTIFIER,
566 * value [0] EXPLICIT ANY DEFINED BY type-id }
567 *
568 * EDIPartyName ::= SEQUENCE {
569 * nameAssigner [0] DirectoryString OPTIONAL,
570 * partyName [1] DirectoryString }
571 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000572 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573 */
Hanno Beckerad462192019-02-21 13:32:31 +0000574static int x509_get_subject_alt_name_cb( void *ctx,
575 int tag,
576 unsigned char *data,
577 size_t data_len )
578{
579 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
580 mbedtls_asn1_sequence *cur = *cur_ptr;
581
Hanno Beckerad462192019-02-21 13:32:31 +0000582 /* Allocate and assign next pointer */
583 if( cur->buf.p != NULL )
584 {
585 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
586 if( cur->next == NULL )
Hanno Becker90b94082019-02-21 21:13:21 +0000587 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Hanno Beckerad462192019-02-21 13:32:31 +0000588 cur = cur->next;
589 }
590
591 cur->buf.tag = tag;
592 cur->buf.p = data;
593 cur->buf.len = data_len;
594
595 *cur_ptr = cur;
596 return( 0 );
597}
598
Hanno Becker5984d302019-02-21 14:46:54 +0000599static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000603 return( mbedtls_asn1_traverse_sequence_of( &p, end,
604 MBEDTLS_ASN1_TAG_CLASS_MASK,
605 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
606 MBEDTLS_ASN1_TAG_VALUE_MASK,
607 2 /* SubjectAlt DNS */,
608 x509_get_subject_alt_name_cb,
609 (void*) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610}
611
612/*
613 * X.509 v3 extensions
614 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000616static int x509_crt_get_ext_cb( void *ctx,
617 int tag,
618 unsigned char *p,
619 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620{
621 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000622 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000624 unsigned char *end, *end_ext_octet;
625 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
626 int is_critical = 0; /* DEFAULT FALSE */
627 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000629 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000630
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000631 /*
632 * Extension ::= SEQUENCE {
633 * extnID OBJECT IDENTIFIER,
634 * critical BOOLEAN DEFAULT FALSE,
635 * extnValue OCTET STRING }
636 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000638 end = p + ext_len;
639
640 /* Get extension ID */
641 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
642 MBEDTLS_ASN1_OID ) ) != 0 )
643 goto err;
644
645 extn_oid.tag = MBEDTLS_ASN1_OID;
646 extn_oid.p = p;
647 p += extn_oid.len;
648
649 /* Get optional critical */
650 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
651 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
652 goto err;
653
654 /* Data should be octet string type */
655 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
656 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
657 goto err;
658
659 end_ext_octet = p + len;
660 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000662 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
663 goto err;
664 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000666 /*
667 * Detect supported extensions
668 */
669 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
670 if( ret != 0 )
671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000673 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000675 /* Data is marked as critical: fail */
676 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
677 goto err;
678 }
679#endif
680 return( 0 );
681 }
682
683 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +0000684 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000685 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
686
Hanno Becker21f55672019-02-15 15:27:59 +0000687 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000688 switch( ext_type )
689 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100690 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000691 {
692 int ca_istrue;
693 int max_pathlen;
694
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000696 ret = x509_get_basic_constraints( &p, end_ext_octet,
697 &ca_istrue,
698 &max_pathlen );
699 if( ret != 0 )
700 goto err;
701
Hanno Becker21f55672019-02-15 15:27:59 +0000702 frame->ca_istrue = ca_istrue;
703 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000705 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000709 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +0000710 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000711 if( ret != 0 )
712 goto err;
713 break;
714
715 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
716 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +0000717 frame->subject_alt_raw.p = p;
718 frame->subject_alt_raw.len = end_ext_octet - p;
719
720 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
721 MBEDTLS_ASN1_TAG_CLASS_MASK,
722 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
723 MBEDTLS_ASN1_TAG_VALUE_MASK,
724 2 /* SubjectAlt DNS */,
725 NULL, NULL );
726 if( ret != 0 )
727 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 break;
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +0000732 frame->ext_key_usage_raw.p = p;
733 frame->ext_key_usage_raw.len = end_ext_octet - p;
734 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +0000735 {
Hanno Becker21f55672019-02-15 15:27:59 +0000736 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
737 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +0000738 }
Hanno Becker21f55672019-02-15 15:27:59 +0000739
740 /* Check structural sanity of extension. */
741 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
742 0xFF, MBEDTLS_ASN1_OID,
743 0, 0, NULL, NULL );
744 if( ret != 0 )
745 goto err;
746
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747 break;
748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000751 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +0000752 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000753 if( ret != 0 )
754 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 break;
756
757 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000758 /*
759 * If this is a non-critical extension, which the oid layer
760 * supports, but there isn't an X.509 parser for it,
761 * skip the extension.
762 */
763#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
764 if( is_critical )
765 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
766#endif
767 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 }
769
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000771
772err:
773 return( ret );
774}
775
Hanno Becker21f55672019-02-15 15:27:59 +0000776static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000777{
778 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000779 unsigned char *p = frame->v3_ext.p;
780 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000781
Hanno Becker21f55672019-02-15 15:27:59 +0000782 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000783 return( 0 );
784
Hanno Becker21f55672019-02-15 15:27:59 +0000785 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000786 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +0000787 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000788
789 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
790 return( ret );
791 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
792 return( ret );
793
794 if( ret != 0 )
795 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
796
797 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798}
799
Hanno Becker21f55672019-02-15 15:27:59 +0000800static int x509_crt_parse_frame( unsigned char *start,
801 unsigned char *end,
802 mbedtls_x509_crt_frame *frame )
803{
804 int ret;
805 unsigned char *p;
806 size_t len;
807
808 mbedtls_x509_buf tmp;
809 unsigned char *tbs_start;
810
811 mbedtls_x509_buf outer_sig_alg;
812 size_t inner_sig_alg_len;
813 unsigned char *inner_sig_alg_start;
814
815 memset( frame, 0, sizeof( *frame ) );
816
817 /*
818 * Certificate ::= SEQUENCE {
819 * tbsCertificate TBSCertificate,
820 * signatureAlgorithm AlgorithmIdentifier,
821 * signatureValue BIT STRING
822 * }
823 *
824 */
825 p = start;
826
827 frame->raw.p = p;
828 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
829 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
830 {
831 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
832 }
833
834 /* NOTE: We are currently not checking that the `Certificate`
835 * structure spans the entire buffer. */
836 end = p + len;
837 frame->raw.len = end - frame->raw.p;
838
839 /*
840 * TBSCertificate ::= SEQUENCE { ...
841 */
842 frame->tbs.p = p;
843 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
844 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
845 {
846 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
847 }
848 tbs_start = p;
849
850 /* Breadth-first parsing: Jump over TBS for now. */
851 p += len;
852 frame->tbs.len = p - frame->tbs.p;
853
854 /*
855 * AlgorithmIdentifier ::= SEQUENCE { ...
856 */
857 outer_sig_alg.p = p;
858 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
859 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
860 {
861 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
862 }
863 p += len;
864 outer_sig_alg.len = p - outer_sig_alg.p;
865
866 /*
867 * signatureValue BIT STRING
868 */
869 ret = mbedtls_x509_get_sig( &p, end, &tmp );
870 if( ret != 0 )
871 return( ret );
872 frame->sig.p = tmp.p;
873 frame->sig.len = tmp.len;
874
875 /* Check that we consumed the entire `Certificate` structure. */
876 if( p != end )
877 {
878 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
879 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
880 }
881
882 /* Parse TBSCertificate structure
883 *
884 * TBSCertificate ::= SEQUENCE {
885 * version [0] EXPLICIT Version DEFAULT v1,
886 * serialNumber CertificateSerialNumber,
887 * signature AlgorithmIdentifier,
888 * issuer Name,
889 * validity Validity,
890 * subject Name,
891 * subjectPublicKeyInfo SubjectPublicKeyInfo,
892 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
893 * -- If present, version MUST be v2 or v3
894 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
895 * -- If present, version MUST be v2 or v3
896 * extensions [3] EXPLICIT Extensions OPTIONAL
897 * -- If present, version MUST be v3
898 * }
899 */
900 end = frame->tbs.p + frame->tbs.len;
901 p = tbs_start;
902
903 /*
904 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
905 */
906 {
907 int version;
908 ret = x509_get_version( &p, end, &version );
909 if( ret != 0 )
910 return( ret );
911
912 if( version < 0 || version > 2 )
913 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
914
915 frame->version = version + 1;
916 }
917
918 /*
919 * CertificateSerialNumber ::= INTEGER
920 */
921 ret = mbedtls_x509_get_serial( &p, end, &tmp );
922 if( ret != 0 )
923 return( ret );
924
925 frame->serial.p = tmp.p;
926 frame->serial.len = tmp.len;
927
928 /*
929 * signature AlgorithmIdentifier
930 */
931 inner_sig_alg_start = p;
932 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
933 &frame->sig_pk, NULL );
934 if( ret != 0 )
935 return( ret );
936 inner_sig_alg_len = p - inner_sig_alg_start;
937
938 frame->sig_alg.p = inner_sig_alg_start;
939 frame->sig_alg.len = inner_sig_alg_len;
940
941 /* Consistency check:
942 * Inner and outer AlgorithmIdentifier structures must coincide:
943 *
944 * Quoting RFC 5280, Section 4.1.1.2:
945 * This field MUST contain the same algorithm identifier as the
946 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
947 */
948 if( outer_sig_alg.len != inner_sig_alg_len ||
949 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
950 {
951 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
952 }
953
954 /*
955 * issuer Name
956 *
957 * Name ::= CHOICE { -- only one possibility for now --
958 * rdnSequence RDNSequence }
959 *
960 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
961 */
962 frame->issuer_raw_with_hdr.p = p;
963
964 ret = mbedtls_asn1_get_tag( &p, end, &len,
965 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
966 if( ret != 0 )
967 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
968 frame->issuer_raw.p = p;
969 frame->issuer_raw.len = len;
970 p += len;
971
972 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
973 &frame->issuer_raw,
974 NULL, NULL );
975 if( ret != 0 )
976 return( ret );
977
978 frame->issuer_raw_with_hdr.len = p - frame->issuer_raw_with_hdr.p;
979
980 /*
981 * Validity ::= SEQUENCE { ...
982 */
983 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
984 if( ret != 0 )
985 return( ret );
986
987 /*
988 * subject Name
989 *
990 * Name ::= CHOICE { -- only one possibility for now --
991 * rdnSequence RDNSequence }
992 *
993 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
994 */
995 frame->subject_raw_with_hdr.p = p;
996
997 ret = mbedtls_asn1_get_tag( &p, end, &len,
998 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
999 if( ret != 0 )
1000 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1001 frame->subject_raw.p = p;
1002 frame->subject_raw.len = len;
1003 p += len;
1004
1005 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1006 &frame->subject_raw,
1007 NULL, NULL );
1008 if( ret != 0 )
1009 return( ret );
1010
1011 frame->subject_raw_with_hdr.len = p - frame->subject_raw_with_hdr.p;
1012
1013 /*
1014 * SubjectPublicKeyInfo
1015 */
1016 frame->pubkey_raw.p = p;
1017 ret = mbedtls_asn1_get_tag( &p, end, &len,
1018 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1019 if( ret != 0 )
1020 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1021 p += len;
1022 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1023
1024 /*
1025 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1026 * -- If present, version shall be v2 or v3
1027 */
1028 if( frame->version == 2 || frame->version == 3 )
1029 {
1030 ret = x509_get_uid( &p, end, &tmp, 1 /* implicit tag */ );
1031 if( ret != 0 )
1032 return( ret );
1033
1034 frame->issuer_id.p = tmp.p;
1035 frame->issuer_id.len = tmp.len;
1036 }
1037
1038 /*
1039 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1040 * -- If present, version shall be v2 or v3
1041 */
1042 if( frame->version == 2 || frame->version == 3 )
1043 {
1044 ret = x509_get_uid( &p, end, &tmp, 2 /* implicit tag */ );
1045 if( ret != 0 )
1046 return( ret );
1047
1048 frame->subject_id.p = tmp.p;
1049 frame->subject_id.len = tmp.len;
1050 }
1051
1052 /*
1053 * extensions [3] EXPLICIT Extensions OPTIONAL
1054 * -- If present, version shall be v3
1055 */
1056#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1057 if( frame->version == 3 )
1058#endif
1059 {
1060 if( p != end )
1061 {
1062 ret = mbedtls_asn1_get_tag( &p, end, &len,
1063 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1064 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1065 if( len == 0 )
1066 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1067 if( ret != 0 )
1068 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1069
1070 frame->v3_ext.p = p;
1071 frame->v3_ext.len = len;
1072
1073 p += len;
1074 }
1075
1076 ret = x509_crt_frame_parse_ext( frame );
1077 if( ret != 0 )
1078 return( ret );
1079 }
1080
1081 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1082 if( p != end )
1083 {
1084 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1085 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1086 }
1087
1088 return( 0 );
1089}
1090
1091static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
1092 mbedtls_x509_name *subject )
1093{
1094 unsigned char *p = frame->subject_raw.p;
1095 unsigned char *end = p + frame->subject_raw.len;
1096
1097 return( mbedtls_x509_get_name( &p, end, subject ) );
1098}
1099
1100static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
1101 mbedtls_x509_name *issuer )
1102{
1103 unsigned char *p = frame->issuer_raw.p;
1104 unsigned char *end = p + frame->issuer_raw.len;
1105
1106 return( mbedtls_x509_get_name( &p, end, issuer ) );
1107}
1108
1109static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
1110 mbedtls_x509_sequence *subject_alt )
1111{
1112 int ret;
1113 unsigned char *p = frame->subject_alt_raw.p;
1114 unsigned char *end = p + frame->subject_alt_raw.len;
1115
1116 memset( subject_alt, 0, sizeof( *subject_alt ) );
1117
1118 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1119 return( 0 );
1120
1121 ret = x509_get_subject_alt_name( p, end, subject_alt );
1122 if( ret != 0 )
1123 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1124 return( ret );
1125}
1126
1127static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
1128 mbedtls_x509_sequence *ext_key_usage )
1129{
1130 int ret;
1131 unsigned char *p = frame->ext_key_usage_raw.p;
1132 unsigned char *end = p + frame->ext_key_usage_raw.len;
1133
1134 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1135
1136 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1137 return( 0 );
1138
1139 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1140 if( ret != 0 )
1141 {
1142 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1143 return( ret );
1144 }
1145
1146 return( 0 );
1147}
1148
1149static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1150 mbedtls_pk_context *pk )
1151{
1152 unsigned char *p = frame->pubkey_raw.p;
1153 unsigned char *end = p + frame->pubkey_raw.len;
1154 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1155}
1156
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157/*
1158 * Parse and fill a single X.509 certificate in DER format
1159 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001160static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1161 const unsigned char *buf,
1162 size_t buflen,
1163 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164{
1165 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001166 mbedtls_x509_crt_frame frame;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001167
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170
Hanno Becker21f55672019-02-15 15:27:59 +00001171 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 {
Hanno Becker21f55672019-02-15 15:27:59 +00001173 crt->raw.p = (unsigned char*) buf;
1174 crt->raw.len = buflen;
1175 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001176 }
1177 else
1178 {
Hanno Becker21f55672019-02-15 15:27:59 +00001179 crt->raw.p = mbedtls_calloc( 1, buflen );
1180 if( crt->raw.p == NULL )
1181 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1182 crt->raw.len = buflen;
1183 memcpy( crt->raw.p, buf, buflen );
1184
1185 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001186 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001187
Hanno Becker21f55672019-02-15 15:27:59 +00001188 /* Parse CRT frame.
1189 * This omits:
1190 * - Issuer, Subject
1191 * - ExtKeyUsage, SubjectAltNames,
1192 * - Time
1193 * - PK
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 */
Hanno Becker21f55672019-02-15 15:27:59 +00001195 ret = x509_crt_parse_frame( crt->raw.p,
1196 crt->raw.p + crt->raw.len,
1197 &frame );
1198 if( ret != 0 )
1199 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200
Hanno Becker21f55672019-02-15 15:27:59 +00001201 /* Currently, we accept DER encoded CRTs with trailing garbage
1202 * and promise to not account for the garbage in the `raw` field.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 *
Hanno Becker21f55672019-02-15 15:27:59 +00001204 * Note that this means that `crt->raw.len` is not necessarily the
1205 * full size of the heap buffer allocated at `crt->raw.p` in case
1206 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1207 * need the size, and the garbage data doesn't need zeroization. */
1208 crt->raw.len = frame->raw.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209
Hanno Becker21f55672019-02-15 15:27:59 +00001210 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1211 * memory matters, the new CRT structure should be used anyway. */
1212 crt->tbs.p = frame.tbs.p;
1213 crt->tbs.len = frame.tbs.len;
1214 crt->serial.p = frame.serial.p;
1215 crt->serial.len = frame.serial.len;
1216 crt->issuer_raw.p = frame.issuer_raw_with_hdr.p;
1217 crt->issuer_raw.len = frame.issuer_raw_with_hdr.len;
1218 crt->subject_raw.p = frame.subject_raw_with_hdr.p;
1219 crt->subject_raw.len = frame.subject_raw_with_hdr.len;
1220 crt->issuer_raw_no_hdr = frame.issuer_raw;
1221 crt->subject_raw_no_hdr = frame.subject_raw;
1222 crt->issuer_id.p = frame.issuer_id.p;
1223 crt->issuer_id.len = frame.issuer_id.len;
1224 crt->subject_id.p = frame.subject_id.p;
1225 crt->subject_id.len = frame.subject_id.len;
1226 crt->pk_raw.p = frame.pubkey_raw.p;
1227 crt->pk_raw.len = frame.pubkey_raw.len;
1228 crt->ext_key_usage_raw = frame.ext_key_usage_raw;
1229 crt->subject_alt_raw = frame.subject_alt_raw;
1230 crt->sig.p = frame.sig.p;
1231 crt->sig.len = frame.sig.len;
1232 crt->valid_from = frame.valid_from;
1233 crt->valid_to = frame.valid_to;
1234 crt->v3_ext.p = frame.v3_ext.p;
1235 crt->v3_ext.len = frame.v3_ext.len;
1236 crt->version = frame.version;
1237 crt->ca_istrue = frame.ca_istrue;
1238 crt->max_pathlen = frame.max_pathlen;
1239 crt->ext_types = frame.ext_types;
1240 crt->key_usage = frame.key_usage;
1241 crt->ns_cert_type = frame.ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242
1243 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001244 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 {
Hanno Becker21f55672019-02-15 15:27:59 +00001248 /* sig_oid: Previously, needed for convenience in
1249 * mbedtls_x509_crt_info(), now pure legacy burden. */
1250 unsigned char *tmp = frame.sig_alg.p;
1251 unsigned char *end = tmp + frame.sig_alg.len;
1252 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253
Hanno Becker21f55672019-02-15 15:27:59 +00001254 ret = mbedtls_x509_get_alg( &tmp, end,
1255 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256 if( ret != 0 )
1257 {
Hanno Becker21f55672019-02-15 15:27:59 +00001258 /* This should never happen, because we check
1259 * the sanity of the AlgorithmIdentifier structure
1260 * during frame parsing. */
1261 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1262 goto exit;
1263 }
1264 crt->sig_oid = sig_oid;
1265
1266 /* Signature parameters */
1267 tmp = frame.sig_alg.p;
1268 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1269 &crt->sig_md, &crt->sig_pk,
1270 &crt->sig_opts );
1271 if( ret != 0 )
1272 {
1273 /* Again, this should never happen. */
1274 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1275 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 }
1277 }
1278
Hanno Becker21f55672019-02-15 15:27:59 +00001279 ret = x509_crt_pk_from_frame( &frame, &crt->pk );
1280 if( ret != 0 )
1281 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282
Hanno Becker21f55672019-02-15 15:27:59 +00001283 ret = x509_crt_subject_from_frame( &frame, &crt->subject );
1284 if( ret != 0 )
1285 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286
Hanno Becker21f55672019-02-15 15:27:59 +00001287 ret = x509_crt_issuer_from_frame( &frame, &crt->issuer );
1288 if( ret != 0 )
1289 goto exit;
1290
1291 ret = x509_crt_subject_alt_from_frame( &frame, &crt->subject_alt_names );
1292 if( ret != 0 )
1293 goto exit;
1294
1295 ret = x509_crt_ext_key_usage_from_frame( &frame, &crt->ext_key_usage );
1296 if( ret != 0 )
1297 goto exit;
1298
1299exit:
1300 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302
Hanno Becker21f55672019-02-15 15:27:59 +00001303 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304}
1305
1306/*
1307 * Parse one X.509 certificate in DER format from a buffer and add them to a
1308 * chained list
1309 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001310static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1311 const unsigned char *buf,
1312 size_t buflen,
1313 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314{
1315 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317
1318 /*
1319 * Check for valid input
1320 */
1321 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001323
Hanno Becker371e0e42019-02-25 18:08:59 +00001324 while( crt->raw.p != NULL && crt->next != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325 {
1326 prev = crt;
1327 crt = crt->next;
1328 }
1329
1330 /*
1331 * Add new certificate on the end of the chain if needed.
1332 */
Hanno Becker371e0e42019-02-25 18:08:59 +00001333 if( crt->raw.p != NULL && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001335 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336
1337 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001338 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001339
1340 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343 }
1344
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001345 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346 {
1347 if( prev )
1348 prev->next = NULL;
1349
1350 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
1353 return( ret );
1354 }
1355
1356 return( 0 );
1357}
1358
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001359int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1360 const unsigned char *buf,
1361 size_t buflen )
1362{
1363 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1364}
1365
1366int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1367 const unsigned char *buf,
1368 size_t buflen )
1369{
1370 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1371}
1372
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001374 * Parse one or more PEM certificates from a buffer and add them to the chained
1375 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001377int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1378 const unsigned char *buf,
1379 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380{
Janos Follath98e28a72016-05-31 14:03:54 +01001381#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001382 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001384#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385
1386 /*
1387 * Check for valid input
1388 */
1389 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391
1392 /*
1393 * Determine buffer content. Buffer contains either one DER certificate or
1394 * one or more PEM certificates.
1395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001397 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001398 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001401 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1404 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001405#else
1406 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1407#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_PEM_PARSE_C)
1410 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411 {
1412 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001415 /* 1 rather than 0 since the terminating NULL byte is counted in */
1416 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417 {
1418 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001421 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423 "-----BEGIN CERTIFICATE-----",
1424 "-----END CERTIFICATE-----",
1425 buf, NULL, 0, &use_len );
1426
1427 if( ret == 0 )
1428 {
1429 /*
1430 * Was PEM encoded
1431 */
1432 buflen -= use_len;
1433 buf += use_len;
1434 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436 {
1437 return( ret );
1438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
1443 /*
1444 * PEM header and footer were found
1445 */
1446 buflen -= use_len;
1447 buf += use_len;
1448
1449 if( first_error == 0 )
1450 first_error = ret;
1451
Paul Bakker5a5fa922014-09-26 14:53:04 +02001452 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 continue;
1454 }
1455 else
1456 break;
1457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461
1462 if( ret != 0 )
1463 {
1464 /*
1465 * Quit parsing on a memory error
1466 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001467 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 return( ret );
1469
1470 if( first_error == 0 )
1471 first_error = ret;
1472
1473 total_failed++;
1474 continue;
1475 }
1476
1477 success = 1;
1478 }
1479 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480
1481 if( success )
1482 return( total_failed );
1483 else if( first_error )
1484 return( first_error );
1485 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001487#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488}
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491/*
1492 * Load one or more certificates and add them to the chained list
1493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495{
1496 int ret;
1497 size_t n;
1498 unsigned char *buf;
1499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501 return( ret );
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001505 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507
1508 return( ret );
1509}
1510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512{
1513 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001514#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515 int w_ret;
1516 WCHAR szDir[MAX_PATH];
1517 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001518 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001519 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520
Paul Bakker9af723c2014-05-01 13:03:14 +02001521 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522 HANDLE hFind;
1523
1524 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526
Paul Bakker9af723c2014-05-01 13:03:14 +02001527 memset( szDir, 0, sizeof(szDir) );
1528 memset( filename, 0, MAX_PATH );
1529 memcpy( filename, path, len );
1530 filename[len++] = '\\';
1531 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532 filename[len++] = '*';
1533
Simon B3c6b18d2016-11-03 01:11:37 +00001534 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001535 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001536 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538
1539 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001540 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542
1543 len = MAX_PATH - len;
1544 do
1545 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001546 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
1548 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1549 continue;
1550
Paul Bakker9af723c2014-05-01 13:03:14 +02001551 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001552 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001553 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001554 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001555 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001556 {
1557 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1558 goto cleanup;
1559 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562 if( w_ret < 0 )
1563 ret++;
1564 else
1565 ret += w_ret;
1566 }
1567 while( FindNextFileW( hFind, &file_data ) != 0 );
1568
Paul Bakker66d5d072014-06-17 16:39:18 +02001569 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571
Ron Eldor36d90422017-01-09 15:09:16 +02001572cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001574#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001575 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001576 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001578 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001579 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580 DIR *dir = opendir( path );
1581
Paul Bakker66d5d072014-06-17 16:39:18 +02001582 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584
Ron Eldor63140682017-01-09 19:27:59 +02001585#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001586 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001587 {
1588 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001589 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001590 }
Ron Eldor63140682017-01-09 19:27:59 +02001591#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001592
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001593 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 {
Andres AGf9113192016-09-02 14:06:04 +01001595 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1596 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
Andres AGf9113192016-09-02 14:06:04 +01001598 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599 {
Andres AGf9113192016-09-02 14:06:04 +01001600 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1601 goto cleanup;
1602 }
1603 else if( stat( entry_name, &sb ) == -1 )
1604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001606 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607 }
1608
1609 if( !S_ISREG( sb.st_mode ) )
1610 continue;
1611
1612 // Ignore parse errors
1613 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615 if( t_ret < 0 )
1616 ret++;
1617 else
1618 ret += t_ret;
1619 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001620
1621cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001622 closedir( dir );
1623
Ron Eldor63140682017-01-09 19:27:59 +02001624#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001625 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001627#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001628
Paul Bakkerbe089b02013-10-14 15:51:50 +02001629#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630
1631 return( ret );
1632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634
Hanno Becker02a21932019-06-10 15:08:43 +01001635#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001636static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001638{
1639 size_t i;
1640 size_t n = *size;
1641 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001643 const char *sep = "";
1644 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001645
1646 while( cur != NULL )
1647 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001648 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001649 {
1650 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001651 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001652 }
1653
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001654 n -= cur->buf.len + sep_len;
1655 for( i = 0; i < sep_len; i++ )
1656 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001657 for( i = 0; i < cur->buf.len; i++ )
1658 *p++ = cur->buf.p[i];
1659
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001660 sep = ", ";
1661 sep_len = 2;
1662
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001663 cur = cur->next;
1664 }
1665
1666 *p = '\0';
1667
1668 *size = n;
1669 *buf = p;
1670
1671 return( 0 );
1672}
1673
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001674#define PRINT_ITEM(i) \
1675 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001677 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001678 sep = ", "; \
1679 }
1680
1681#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001682 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001683 PRINT_ITEM( name );
1684
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001685static int x509_info_cert_type( char **buf, size_t *size,
1686 unsigned char ns_cert_type )
1687{
1688 int ret;
1689 size_t n = *size;
1690 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001691 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001694 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1696 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1697 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001698 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1699 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1700 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001701
1702 *size = n;
1703 *buf = p;
1704
1705 return( 0 );
1706}
1707
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001708#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001709 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001710 PRINT_ITEM( name );
1711
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001712static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001713 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001714{
1715 int ret;
1716 size_t n = *size;
1717 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001718 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1721 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001722 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1723 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1724 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1726 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001727 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1728 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001729
1730 *size = n;
1731 *buf = p;
1732
1733 return( 0 );
1734}
1735
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001736static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001738{
1739 int ret;
1740 const char *desc;
1741 size_t n = *size;
1742 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001744 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001745
1746 while( cur != NULL )
1747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001749 desc = "???";
1750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001752 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001753
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001754 sep = ", ";
1755
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001756 cur = cur->next;
1757 }
1758
1759 *size = n;
1760 *buf = p;
1761
1762 return( 0 );
1763}
1764
Hanno Becker4f869ed2019-02-24 16:47:57 +00001765typedef struct mbedtls_x509_crt_sig_info
1766{
1767 mbedtls_md_type_t sig_md;
1768 mbedtls_pk_type_t sig_pk;
1769 void *sig_opts;
1770 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
1771 size_t crt_hash_len;
1772 mbedtls_x509_buf_raw sig;
1773 mbedtls_x509_buf_raw issuer_raw;
1774} mbedtls_x509_crt_sig_info;
1775
1776static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
1777{
1778#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
1779 mbedtls_free( info->sig_opts );
1780#else
1781 ((void) info);
1782#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
1783}
1784
1785static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
1786 mbedtls_x509_crt_sig_info *info )
1787{
1788 const mbedtls_md_info_t *md_info;
1789
1790 md_info = mbedtls_md_info_from_type( frame->sig_md );
1791 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
1792 info->crt_hash ) != 0 )
1793 {
1794 /* Note: this can't happen except after an internal error */
1795 return( -1 );
1796 }
1797
1798 info->crt_hash_len = mbedtls_md_get_size( md_info );
1799
1800 /* Make sure that this function leaves the target structure
1801 * ready to be freed, regardless of success of failure. */
1802 info->sig_opts = NULL;
1803
1804#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
1805 {
1806 int ret;
1807 unsigned char *alg_start = frame->sig_alg.p;
1808 unsigned char *alg_end = alg_start + frame->sig_alg.len;
1809
1810 /* Get signature options -- currently only
1811 * necessary for RSASSA-PSS. */
1812 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
1813 &info->sig_pk, &info->sig_opts );
1814 if( ret != 0 )
1815 {
1816 /* Note: this can't happen except after an internal error */
1817 return( -1 );
1818 }
1819 }
1820#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
1821 info->sig_md = frame->sig_md;
1822 info->sig_pk = frame->sig_pk;
1823#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
1824
1825 info->issuer_raw = frame->issuer_raw;
1826 info->sig = frame->sig;
1827 return( 0 );
1828}
1829
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001830/*
1831 * Return an informational string about the certificate.
1832 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001833#define BEFORE_COLON 18
1834#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
Hanno Becker4f869ed2019-02-24 16:47:57 +00001836 const mbedtls_x509_crt *crt_raw )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001837{
1838 int ret;
1839 size_t n;
1840 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001841 char key_size_str[BEFORE_COLON];
Hanno Becker4f869ed2019-02-24 16:47:57 +00001842 mbedtls_x509_crt_frame *crt;
1843 mbedtls_pk_context *pk;
1844
1845 mbedtls_x509_name issuer, subject;
1846 mbedtls_x509_sequence ext_key_usage, subject_alt_names;
1847 mbedtls_x509_crt_sig_info sig_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001848
1849 p = buf;
1850 n = size;
1851
Hanno Becker4f869ed2019-02-24 16:47:57 +00001852 memset( &sig_info, 0, sizeof( mbedtls_x509_crt_sig_info ) );
1853 if( NULL == crt_raw )
Janos Follath98e28a72016-05-31 14:03:54 +01001854 {
1855 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001856 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Janos Follath98e28a72016-05-31 14:03:54 +01001857
1858 return( (int) ( size - n ) );
1859 }
1860
Hanno Becker4f869ed2019-02-24 16:47:57 +00001861 ret = x509_crt_frame_acquire( crt_raw, &crt );
1862 if( ret != 0 )
1863 return( MBEDTLS_ERR_X509_FATAL_ERROR );
1864
1865 ret = x509_crt_pk_acquire( (mbedtls_x509_crt*) crt_raw, &pk );
1866 if( ret != 0 )
1867 {
1868 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1869 goto cleanup;
1870 }
1871
1872 ret = x509_crt_get_sig_info( crt, &sig_info );
1873 if( ret != 0 )
1874 {
1875 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1876 goto cleanup;
1877 }
1878
1879 ret = x509_crt_subject_from_frame( crt, &subject );
1880 if( ret != 0 )
1881 {
1882 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1883 goto cleanup;
1884 }
1885
1886 ret = x509_crt_issuer_from_frame( crt, &issuer );
1887 if( ret != 0 )
1888 {
1889 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1890 goto cleanup;
1891 }
1892
1893 ret = x509_crt_subject_alt_from_frame( crt, &subject_alt_names );
1894 if( ret != 0 )
1895 {
1896 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1897 goto cleanup;
1898 }
1899
1900 ret = x509_crt_ext_key_usage_from_frame( crt, &ext_key_usage );
1901 if( ret != 0 )
1902 {
1903 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1904 goto cleanup;
1905 }
1906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001908 prefix, crt->version );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001909 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001910
Hanno Becker4f869ed2019-02-24 16:47:57 +00001911 {
1912 mbedtls_x509_buf serial;
1913 serial.p = crt->serial.p;
1914 serial.len = crt->serial.len;
1915 ret = mbedtls_snprintf( p, n, "%sserial number : ",
1916 prefix );
1917 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
1918 ret = mbedtls_x509_serial_gets( p, n, &serial );
1919 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
1920 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001923 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
1924 ret = mbedtls_x509_dn_gets( p, n, &issuer );
1925 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001928 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
1929 ret = mbedtls_x509_dn_gets( p, n, &subject );
1930 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001933 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1934 crt->valid_from.year, crt->valid_from.mon,
1935 crt->valid_from.day, crt->valid_from.hour,
1936 crt->valid_from.min, crt->valid_from.sec );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001937 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001940 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1941 crt->valid_to.year, crt->valid_to.mon,
1942 crt->valid_to.day, crt->valid_to.hour,
1943 crt->valid_to.min, crt->valid_to.sec );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001944 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001947 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948
Hanno Becker83cd8672019-02-21 17:13:46 +00001949 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
1950 sig_info.sig_md, sig_info.sig_opts );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001951 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001952
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001953 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Hanno Becker4f869ed2019-02-24 16:47:57 +00001955 mbedtls_pk_get_name( pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001956 {
1957 return( ret );
1958 }
1959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Hanno Becker4f869ed2019-02-24 16:47:57 +00001961 (int) mbedtls_pk_get_bitlen( pk ) );
1962 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001963
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001964 /*
1965 * Optional extensions
1966 */
1967
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001968 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001971 crt->ca_istrue ? "true" : "false" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001972 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001973
1974 if( crt->max_pathlen > 0 )
1975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001977 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001978 }
1979 }
1980
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001981 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001984 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001985
1986 if( ( ret = x509_info_subject_alt_name( &p, &n,
Hanno Becker4f869ed2019-02-24 16:47:57 +00001987 &subject_alt_names ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001988 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001989 }
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00001994 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001995
1996 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1997 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001998 }
1999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002003 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002004
2005 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2006 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002007 }
2008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002012 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002013
2014 if( ( ret = x509_info_ext_key_usage( &p, &n,
Hanno Becker4f869ed2019-02-24 16:47:57 +00002015 &ext_key_usage ) ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002016 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002017 }
2018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 ret = mbedtls_snprintf( p, n, "\n" );
Hanno Becker4f869ed2019-02-24 16:47:57 +00002020 MBEDTLS_X509_SAFE_SNPRINTF_WITH_ERROR;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002021
Hanno Becker4f869ed2019-02-24 16:47:57 +00002022 ret = (int) ( size - n );
2023
2024cleanup:
2025
2026 x509_crt_frame_release( crt_raw, crt );
2027 x509_crt_pk_release( (mbedtls_x509_crt*) crt_raw, pk );
2028
2029 x509_crt_free_sig_info( &sig_info );
2030 x509_free_name( issuer.next );
2031 x509_free_name( subject.next );
2032 x509_free_sequence( ext_key_usage.next );
2033 x509_free_sequence( subject_alt_names.next );
2034
2035 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002036}
2037
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002038struct x509_crt_verify_string {
2039 int code;
2040 const char *string;
2041};
2042
2043static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002044 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002045 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2046 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2047 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2048 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2049 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002050 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2051 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2052 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002053 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002054 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2055 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2056 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2057 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002058 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2059 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2060 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2061 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2062 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2063 { 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 +01002064 { 0, NULL }
2065};
2066
2067int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002068 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002069{
2070 int ret;
2071 const struct x509_crt_verify_string *cur;
2072 char *p = buf;
2073 size_t n = size;
2074
2075 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2076 {
2077 if( ( flags & cur->code ) == 0 )
2078 continue;
2079
2080 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002081 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002082 flags ^= cur->code;
2083 }
2084
2085 if( flags != 0 )
2086 {
2087 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2088 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002089 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002090 }
2091
2092 return( (int) ( size - n ) );
2093}
Hanno Becker02a21932019-06-10 15:08:43 +01002094#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Becker45eedf12019-02-25 13:55:33 +00002097static int x509_crt_check_key_usage_frame( const mbedtls_x509_crt_frame *crt,
2098 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002099{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002100 unsigned int usage_must, usage_may;
2101 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2102 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2103
2104 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2105 return( 0 );
2106
2107 usage_must = usage & ~may_mask;
2108
2109 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2110 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2111
2112 usage_may = usage & may_mask;
2113
2114 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002116
2117 return( 0 );
2118}
Hanno Becker45eedf12019-02-25 13:55:33 +00002119
2120int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2121 unsigned int usage )
2122{
2123 int ret;
2124 mbedtls_x509_crt_frame *frame;
2125 ret = x509_crt_frame_acquire( crt, (mbedtls_x509_crt_frame**) &frame );
2126 if( ret != 0 )
2127 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2128
2129 ret = x509_crt_check_key_usage_frame( frame, usage );
2130 x509_crt_frame_release( crt, (mbedtls_x509_crt_frame*) frame );
2131
2132 return( ret );
2133}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002134#endif
2135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002137typedef struct
2138{
2139 const char *oid;
2140 size_t oid_len;
2141} x509_crt_check_ext_key_usage_cb_ctx_t;
2142
2143static int x509_crt_check_ext_key_usage_cb( void *ctx,
2144 int tag,
2145 unsigned char *data,
2146 size_t data_len )
2147{
2148 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
2149 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
2150 ((void) tag);
2151
2152 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2153 data, data_len ) == 0 )
2154 {
2155 return( 1 );
2156 }
2157
2158 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2159 data_len ) == 0 )
2160 {
2161 return( 1 );
2162 }
2163
2164 return( 0 );
2165}
2166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002168 const char *usage_oid,
2169 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002170{
Hanno Beckere1956af2019-02-21 14:28:12 +00002171 int ret;
Hanno Beckere9718b42019-02-25 18:11:42 +00002172 mbedtls_x509_crt_frame *frame;
Hanno Beckere1956af2019-02-21 14:28:12 +00002173 unsigned ext_types;
2174 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002175 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002176
Hanno Beckere9718b42019-02-25 18:11:42 +00002177 ret = x509_crt_frame_acquire( crt, &frame );
2178 if( ret != 0 )
2179 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2180
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002181 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere9718b42019-02-25 18:11:42 +00002182 ext_types = frame->ext_types;
2183 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) != 0 )
2184 {
2185 p = frame->ext_key_usage_raw.p;
2186 end = p + frame->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002187
Hanno Beckere9718b42019-02-25 18:11:42 +00002188 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2189 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2190 x509_crt_check_ext_key_usage_cb,
2191 &cb_ctx );
2192 if( ret == 1 )
2193 ret = 0;
2194 else
2195 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2196 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002197
Hanno Beckere9718b42019-02-25 18:11:42 +00002198 x509_crt_frame_release( crt, frame );
2199 return( ret );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002204/*
2205 * Return 1 if the certificate is revoked, or 0 otherwise.
2206 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002207static int x509_serial_is_revoked( unsigned char const *serial,
2208 size_t serial_len,
2209 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002212
2213 while( cur != NULL && cur->serial.len != 0 )
2214 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002215 if( serial_len == cur->serial.len &&
2216 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002217 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002218 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002219 return( 1 );
2220 }
2221
2222 cur = cur->next;
2223 }
2224
2225 return( 0 );
2226}
2227
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002228int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2229 const mbedtls_x509_crl *crl )
2230{
Hanno Becker79ae5b62019-02-25 18:12:00 +00002231 int ret;
2232 mbedtls_x509_crt_frame *frame;
2233
2234 ret = x509_crt_frame_acquire( crt, &frame );
2235 if( ret != 0 )
2236 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2237
2238 ret = x509_serial_is_revoked( frame->serial.p,
2239 frame->serial.len,
2240 crl );
2241 x509_crt_frame_release( crt, frame );
2242 return( ret );
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002243}
2244
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002245/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002246 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002247 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002248 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002249static int x509_crt_verifycrl( unsigned char *crt_serial,
2250 size_t crt_serial_len,
Hanno Beckerbb266132019-02-25 18:12:46 +00002251 mbedtls_x509_crt *ca_crt,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002252 mbedtls_x509_crl *crl_list,
2253 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002254{
Hanno Beckerbb266132019-02-25 18:12:46 +00002255 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002256 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2258 const mbedtls_md_info_t *md_info;
Hanno Beckerbb266132019-02-25 18:12:46 +00002259 mbedtls_x509_buf_raw ca_subject;
2260 mbedtls_pk_context *pk;
2261 int can_sign;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002262
Hanno Beckerbb266132019-02-25 18:12:46 +00002263 if( ca_crt == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002264 return( flags );
2265
Hanno Beckerbb266132019-02-25 18:12:46 +00002266 {
2267 mbedtls_x509_crt_frame *ca;
2268 ret = x509_crt_frame_acquire( ca_crt, &ca );
2269 if( ret != 0 )
2270 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2271
2272 ca_subject = ca->subject_raw;
2273
2274 can_sign = 0;
2275 if( x509_crt_check_key_usage_frame( ca,
2276 MBEDTLS_X509_KU_CRL_SIGN ) == 0 )
2277 {
2278 can_sign = 1;
2279 }
2280
2281 x509_crt_frame_release( ca_crt, ca );
2282 }
2283
2284 ret = x509_crt_pk_acquire( ca_crt, &pk );
2285 if( ret != 0 )
2286 return( MBEDTLS_X509_BADCRL_NOT_TRUSTED );
2287
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002288 while( crl_list != NULL )
2289 {
2290 if( crl_list->version == 0 ||
Hanno Becker7dee12a2019-02-21 13:58:38 +00002291 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw_no_hdr,
Hanno Beckerbb266132019-02-25 18:12:46 +00002292 &ca_subject, NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002293 {
2294 crl_list = crl_list->next;
2295 continue;
2296 }
2297
2298 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002299 * Check if the CA is configured to sign CRLs
2300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerbb266132019-02-25 18:12:46 +00002302 if( !can_sign )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002305 break;
2306 }
2307#endif
2308
2309 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002310 * Check if CRL is correctly signed by the trusted CA
2311 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002312 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2313 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2314
2315 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2316 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002319 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002320 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002321 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323 break;
2324 }
2325
Hanno Beckerbb266132019-02-25 18:12:46 +00002326 if( x509_profile_check_key( profile, pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002327 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002328
Hanno Beckerbb266132019-02-25 18:12:46 +00002329 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, pk,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002331 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334 break;
2335 }
2336
2337 /*
2338 * Check for validity of CRL (Do not drop out)
2339 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002340 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002342
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002343 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002344 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002345
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002346 /*
2347 * Check if certificate is revoked
2348 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002349 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2350 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002353 break;
2354 }
2355
2356 crl_list = crl_list->next;
2357 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002358
Hanno Beckerbb266132019-02-25 18:12:46 +00002359 x509_crt_pk_release( ca_crt, pk );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002360 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002363
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002364/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002365 * Check the signature of a certificate by its parent
2366 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002367static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002368 mbedtls_x509_crt *parent,
2369 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002370{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002371 int ret;
2372 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002373
Hanno Beckere449e2d2019-02-25 14:45:31 +00002374 ret = x509_crt_pk_acquire( parent, &pk );
2375 if( ret != 0 )
2376 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2377
2378 /* Skip expensive computation on obvious mismatch */
2379 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002380 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002381 ret = -1;
2382 goto exit;
2383 }
2384
2385#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2386 ((void) rs_ctx);
2387#else
2388 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2389 {
2390 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002391 sig_info->sig_md,
2392 sig_info->crt_hash, sig_info->crt_hash_len,
2393 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002394 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002395 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002396 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002397#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002398 {
2399 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2400 sig_info->sig_opts,
2401 pk,
2402 sig_info->sig_md,
2403 sig_info->crt_hash, sig_info->crt_hash_len,
2404 sig_info->sig.p, sig_info->sig.len );
2405 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002406
Hanno Beckere449e2d2019-02-25 14:45:31 +00002407exit:
2408 x509_crt_pk_release( parent, pk );
2409 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002410}
2411
2412/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002413 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2414 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002415 *
2416 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002417 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002418static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2419 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002420 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002421{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002422 int need_ca_bit;
2423
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002424 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002425 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2426 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002427 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002428 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002429 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002430 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002431
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002432 /* Parent must have the basicConstraints CA bit set as a general rule */
2433 need_ca_bit = 1;
2434
2435 /* Exception: v1/v2 certificates that are locally trusted. */
2436 if( top && parent->version < 3 )
2437 need_ca_bit = 0;
2438
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002439 if( need_ca_bit && ! parent->ca_istrue )
2440 return( -1 );
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002443 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002444 x509_crt_check_key_usage_frame( parent,
2445 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002446 {
2447 return( -1 );
2448 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002449#endif
2450
2451 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002452}
2453
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002454/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002455 * Find a suitable parent for child in candidates, or return NULL.
2456 *
2457 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002458 * 1. subject name matches child's issuer
2459 * 2. if necessary, the CA bit is set and key usage allows signing certs
2460 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002461 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002462 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002463 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002464 * If there's a suitable candidate which is also time-valid, return the first
2465 * such. Otherwise, return the first suitable candidate (or NULL if there is
2466 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002467 *
2468 * The rationale for this rule is that someone could have a list of trusted
2469 * roots with two versions on the same root with different validity periods.
2470 * (At least one user reported having such a list and wanted it to just work.)
2471 * The reason we don't just require time-validity is that generally there is
2472 * only one version, and if it's expired we want the flags to state that
2473 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002474 *
2475 * The rationale for rule 3 (signature for trusted roots) is that users might
2476 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002477 * way we select the correct one is by checking the signature (as we don't
2478 * rely on key identifier extensions). (This is one way users might choose to
2479 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002480 *
2481 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002482 * - [in] child: certificate for which we're looking for a parent
2483 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002484 * - [out] r_parent: parent found (or NULL)
2485 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002486 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2487 * of the chain, 0 otherwise
2488 * - [in] path_cnt: number of intermediates seen so far
2489 * - [in] self_cnt: number of self-signed intermediates seen so far
2490 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002491 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002492 *
2493 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002494 * - 0 on success
2495 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002496 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002497static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002498 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002499 mbedtls_x509_crt *candidates,
2500 mbedtls_x509_crt **r_parent,
2501 int *r_signature_is_good,
2502 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002503 unsigned path_cnt,
2504 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002505 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002506{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002507 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002508 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002509 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002510
2511#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002512 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002513 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2514 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002515 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002516 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002517 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002518 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002519
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002520 /* clear saved state */
2521 rs_ctx->parent = NULL;
2522 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002523 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002524
2525 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002526 goto check_signature;
2527 }
2528#endif
2529
2530 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002531 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002532
Hanno Becker43bf9002019-02-25 14:46:49 +00002533 for( parent_crt = candidates; parent_crt != NULL;
2534 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002535 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002536 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002537
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002538#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2539check_signature:
2540#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002541
2542 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002543 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002544 mbedtls_x509_crt_frame *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002545
Hanno Becker43bf9002019-02-25 14:46:49 +00002546 ret = x509_crt_frame_acquire( parent_crt, &parent );
2547 if( ret != 0 )
2548 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002549
Hanno Becker43bf9002019-02-25 14:46:49 +00002550 if( mbedtls_x509_time_is_past( &parent->valid_from ) &&
2551 mbedtls_x509_time_is_future( &parent->valid_to ) )
2552 {
2553 parent_valid = 1;
2554 }
2555
2556 /* basic parenting skills (name, CA bit, key usage) */
2557 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2558 parent_match = 1;
2559
2560 /* +1 because the stored max_pathlen is 1 higher
2561 * than the actual value */
2562 if( !( parent->max_pathlen > 0 &&
2563 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2564 {
2565 path_len_ok = 1;
2566 }
2567
2568 x509_crt_frame_release( parent_crt, parent );
Hanno Beckera788cab2019-02-24 17:47:46 +00002569 }
2570
2571 if( parent_match == 0 || path_len_ok == 0 )
2572 continue;
2573
2574 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002575 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002576
2577#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002578 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2579 {
2580 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002581 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002583 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002584
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002585 return( ret );
2586 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002587#else
2588 (void) ret;
2589#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002590
2591 signature_is_good = ret == 0;
2592 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002593 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002594
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002595 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002596 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002597 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002598 if( fallback_parent == NULL )
2599 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002600 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002601 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002602 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002603
2604 continue;
2605 }
2606
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002607 break;
2608 }
2609
Hanno Becker43bf9002019-02-25 14:46:49 +00002610 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002611 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002612 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002613 *r_signature_is_good = signature_is_good;
2614 }
2615 else
2616 {
2617 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002618 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002619 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002620
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002621 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002622}
2623
2624/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002625 * Find a parent in trusted CAs or the provided chain, or return NULL.
2626 *
2627 * Searches in trusted CAs first, and return the first suitable parent found
2628 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002629 *
2630 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002631 * - [in] child: certificate for which we're looking for a parent, followed
2632 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002633 * - [in] trust_ca: list of locally trusted certificates
2634 * - [out] parent: parent found (or NULL)
2635 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2636 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2637 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2638 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002639 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002640 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002641 *
2642 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002643 * - 0 on success
2644 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002645 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002646static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002647 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002648 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002649 mbedtls_x509_crt *trust_ca,
2650 mbedtls_x509_crt **parent,
2651 int *parent_is_trusted,
2652 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002653 unsigned path_cnt,
2654 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002655 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002656{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002657 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002658 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002659
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002660 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002661
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002662#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002663 /* restore then clear saved state if we have some stored */
2664 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2665 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002666 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002667 rs_ctx->parent_is_trusted = -1;
2668 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002669#endif
2670
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002671 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00002672 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002673
Hanno Becker5299cf82019-02-25 13:50:41 +00002674 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002675 parent, signature_is_good,
2676 *parent_is_trusted,
2677 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002678
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002679#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002680 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2681 {
2682 /* save state */
2683 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002684 return( ret );
2685 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002686#else
2687 (void) ret;
2688#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002689
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002690 /* stop here if found or already in second iteration */
2691 if( *parent != NULL || *parent_is_trusted == 0 )
2692 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002693
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002694 /* prepare second iteration */
2695 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002696 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002697
2698 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01002699 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002700 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002701 *parent_is_trusted = 0;
2702 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002703 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002704
2705 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002706}
2707
2708/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002709 * Check if an end-entity certificate is locally trusted
2710 *
2711 * Currently we require such certificates to be self-signed (actually only
2712 * check for self-issued as self-signatures are not checked)
2713 */
2714static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00002715 mbedtls_x509_crt_frame const *crt,
2716 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002717{
Hanno Becker1e0677a2019-02-25 14:58:22 +00002718 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002719
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002720 /* look for an exact match with trusted cert */
2721 for( cur = trust_ca; cur != NULL; cur = cur->next )
2722 {
2723 if( crt->raw.len == cur->raw.len &&
2724 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2725 {
2726 return( 0 );
2727 }
2728 }
2729
2730 /* too bad */
2731 return( -1 );
2732}
2733
2734/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002735 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002736 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002737 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2738 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002739 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002740 * such that every cert in the chain is a child of the next one,
2741 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002742 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002743 * Verify that chain and return it with flags for all issues found.
2744 *
2745 * Special cases:
2746 * - EE == Rj -> return a one-element list containing it
2747 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2748 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002749 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002750 * Tests for (aspects of) this function should include at least:
2751 * - trusted EE
2752 * - EE -> trusted root
2753 * - EE -> intermedate CA -> trusted root
2754 * - if relevant: EE untrusted
2755 * - if relevant: EE -> intermediate, untrusted
2756 * with the aspect under test checked at each relevant level (EE, int, root).
2757 * For some aspects longer chains are required, but usually length 2 is
2758 * enough (but length 1 is not in general).
2759 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002760 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002761 * - [in] crt: the cert list EE, C1, ..., Cn
2762 * - [in] trust_ca: the trusted list R1, ..., Rp
2763 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002764 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002765 * Only valid when return value is 0, may contain garbage otherwise!
2766 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002767 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002768 *
2769 * Return value:
2770 * - non-zero if the chain could not be fully built and examined
2771 * - 0 is the chain was successfully built and examined,
2772 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002773 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002774static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002775 mbedtls_x509_crt *crt,
2776 mbedtls_x509_crt *trust_ca,
2777 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002778 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002779 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002780 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002781{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002782 /* Don't initialize any of those variables here, so that the compiler can
2783 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002784 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002785 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002786 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00002787 mbedtls_x509_crt *child_crt;
Hanno Becker58c35642019-02-25 18:13:46 +00002788 mbedtls_x509_crt *parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002789 int parent_is_trusted;
2790 int child_is_trusted;
2791 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002792 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002793
2794#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2795 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002796 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002797 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002798 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002799 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002800 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002801
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002802 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002803 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00002804 child_crt = cur->crt;
2805
2806 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002807 goto find_parent;
2808 }
2809#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002810
Hanno Becker1e0677a2019-02-25 14:58:22 +00002811 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002812 self_cnt = 0;
2813 parent_is_trusted = 0;
2814 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002815
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002816 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00002817#if defined(MBEDTLS_X509_CRL_PARSE_C)
2818 mbedtls_x509_buf_raw child_serial;
2819#endif /* MBEDTLS_X509_CRL_PARSE_C */
2820 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00002821
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002822 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002823 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00002824 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002825 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002826 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00002827
2828#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2829find_parent:
2830#endif
2831
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002832 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002833
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002834 {
Hanno Becker5299cf82019-02-25 13:50:41 +00002835 mbedtls_x509_crt_sig_info child_sig;
2836 {
2837 mbedtls_x509_crt_frame *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002838
Hanno Becker5299cf82019-02-25 13:50:41 +00002839 ret = x509_crt_frame_acquire( child_crt, &child );
2840 if( ret != 0 )
2841 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2842
2843 /* Check time-validity (all certificates) */
2844 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2845 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2846 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2847 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
2848
2849 /* Stop here for trusted roots (but not for trusted EE certs) */
2850 if( child_is_trusted )
2851 {
2852 x509_crt_frame_release( child_crt, child );
2853 return( 0 );
2854 }
2855
2856 self_issued = 0;
2857 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
2858 &child->subject_raw,
2859 NULL, NULL ) == 0 )
2860 {
2861 self_issued = 1;
2862 }
2863
2864 /* Check signature algorithm: MD & PK algs */
2865 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2866 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2867
2868 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2869 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2870
2871 /* Special case: EE certs that are locally trusted */
2872 if( ver_chain->len == 1 && self_issued &&
2873 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2874 {
2875 x509_crt_frame_release( child_crt, child );
2876 return( 0 );
2877 }
2878
2879#if defined(MBEDTLS_X509_CRL_PARSE_C)
2880 child_serial = child->serial;
2881#endif /* MBEDTLS_X509_CRL_PARSE_C */
2882
2883 ret = x509_crt_get_sig_info( child, &child_sig );
2884
2885 x509_crt_frame_release( child_crt, child );
2886 if( ret != 0 )
2887 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2888 }
2889
2890 /* Look for a parent in trusted CAs or up the chain */
2891 ret = x509_crt_find_parent( &child_sig, child_crt->next,
Hanno Becker58c35642019-02-25 18:13:46 +00002892 trust_ca, &parent_crt,
Hanno Becker5299cf82019-02-25 13:50:41 +00002893 &parent_is_trusted, &signature_is_good,
2894 ver_chain->len - 1, self_cnt, rs_ctx );
2895
2896 x509_crt_free_sig_info( &child_sig );
2897 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002898
2899#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002900 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2901 {
2902 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002903 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002904 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002905 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00002906 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002907 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002908#else
2909 (void) ret;
2910#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002911
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002912 /* No parent? We're done here */
Hanno Becker58c35642019-02-25 18:13:46 +00002913 if( parent_crt == NULL )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002914 {
2915 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00002916 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002917 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002918
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002919 /* Count intermediate self-issued (not necessarily self-signed) certs.
2920 * These can occur with some strategies for key rollover, see [SIRO],
2921 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00002922 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002923 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002924
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002925 /* path_cnt is 0 for the first intermediate CA,
2926 * and if parent is trusted it's not an intermediate CA */
2927 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002928 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002929 {
2930 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00002931 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002932 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002933
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002934 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002935 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002936 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2937
Hanno Becker58c35642019-02-25 18:13:46 +00002938 {
2939 mbedtls_pk_context *parent_pk;
2940 ret = x509_crt_pk_acquire( parent_crt, &parent_pk );
2941 if( ret != 0 )
2942 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2943
2944 /* check size of signing key */
2945 if( x509_profile_check_key( profile, parent_pk ) != 0 )
2946 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2947
2948 x509_crt_pk_release( parent_crt, parent_pk );
2949 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002952 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00002953 *flags |= x509_crt_verifycrl( child_serial.p,
2954 child_serial.len,
Hanno Becker58c35642019-02-25 18:13:46 +00002955 parent_crt, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002956#else
2957 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002958#endif
2959
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 /* prepare for next iteration */
Hanno Becker58c35642019-02-25 18:13:46 +00002961 child_crt = parent_crt;
2962 parent_crt = NULL;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002963 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002964 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002966}
2967
2968/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002969 * Check for CN match
2970 */
Hanno Becker24926222019-02-21 13:10:55 +00002971static int x509_crt_check_cn( unsigned char const *buf,
2972 size_t buflen,
2973 const char *cn,
2974 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002975{
Hanno Becker24926222019-02-21 13:10:55 +00002976 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00002977 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002978 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002979
2980 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00002981 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002982 {
2983 return( 0 );
2984 }
2985
2986 return( -1 );
2987}
2988
Hanno Becker8b543b32019-02-21 11:50:44 +00002989/* Returns 1 on a match and 0 on a mismatch.
2990 * This is because this function is used as a callback for
2991 * mbedtls_x509_name_cmp_raw(), which continues the name
2992 * traversal as long as the callback returns 0. */
2993static int x509_crt_check_name( void *ctx,
2994 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00002995 mbedtls_x509_buf *val,
2996 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00002997{
2998 char const *cn = (char const*) ctx;
2999 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00003000 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00003001
3002 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00003003 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00003004 {
3005 return( 1 );
3006 }
3007
3008 return( 0 );
3009}
3010
Hanno Beckerda410822019-02-21 13:36:59 +00003011/* Returns 1 on a match and 0 on a mismatch.
3012 * This is because this function is used as a callback for
3013 * mbedtls_asn1_traverse_sequence_of(), which continues the
3014 * traversal as long as the callback returns 0. */
3015static int x509_crt_subject_alt_check_name( void *ctx,
3016 int tag,
3017 unsigned char *data,
3018 size_t data_len )
3019{
3020 char const *cn = (char const*) ctx;
3021 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00003022 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00003023
3024 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
3025 return( 1 );
3026
3027 return( 0 );
3028}
3029
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003030/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003031 * Verify the requested CN - only call this if cn is not NULL!
3032 */
Hanno Becker082435c2019-02-25 18:14:40 +00003033static int x509_crt_verify_name( const mbedtls_x509_crt *crt,
3034 const char *cn,
3035 uint32_t *flags )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003036{
Hanno Beckerda410822019-02-21 13:36:59 +00003037 int ret;
Hanno Becker082435c2019-02-25 18:14:40 +00003038 mbedtls_x509_crt_frame *frame;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003039
Hanno Becker082435c2019-02-25 18:14:40 +00003040 ret = x509_crt_frame_acquire( crt, &frame );
3041 if( ret != 0 )
3042 return( MBEDTLS_ERR_X509_FATAL_ERROR );
3043
3044 if( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003045 {
Hanno Becker90b94082019-02-21 21:13:21 +00003046 unsigned char *p =
Hanno Becker082435c2019-02-25 18:14:40 +00003047 frame->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003048 const unsigned char *end =
Hanno Becker082435c2019-02-25 18:14:40 +00003049 frame->subject_alt_raw.p + frame->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050
Hanno Becker90b94082019-02-21 21:13:21 +00003051 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3052 MBEDTLS_ASN1_TAG_CLASS_MASK,
3053 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3054 MBEDTLS_ASN1_TAG_VALUE_MASK,
3055 2 /* SubjectAlt DNS */,
3056 x509_crt_subject_alt_check_name,
3057 (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003058 }
3059 else
3060 {
Hanno Becker082435c2019-02-25 18:14:40 +00003061 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
3062 &frame->subject_raw,
Hanno Becker8b543b32019-02-21 11:50:44 +00003063 x509_crt_check_name, (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003064 }
Hanno Beckerda410822019-02-21 13:36:59 +00003065
Hanno Becker082435c2019-02-25 18:14:40 +00003066 x509_crt_frame_release( crt, frame );
3067
3068 /* x509_crt_check_name() and x509_crt_subject_alt_check_name()
3069 * return 1 when finding a name component matching `cn`. */
3070 if( ret == 1 )
3071 return( 0 );
3072
3073 if( ret != 0 )
3074 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3075
3076 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3077 return( ret );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003078}
3079
3080/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003081 * Merge the flags for all certs in the chain, after calling callback
3082 */
3083static int x509_crt_merge_flags_with_cb(
3084 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003085 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003086 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3087 void *p_vrfy )
3088{
3089 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003090 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003091 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003092 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003093
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003094 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003095 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003096 cur = &ver_chain->items[i-1];
3097 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003098
3099 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003100 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003101 return( ret );
3102
3103 *flags |= cur_flags;
3104 }
3105
3106 return( 0 );
3107}
3108
3109/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003110 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3113 mbedtls_x509_crt *trust_ca,
3114 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003115 const char *cn, uint32_t *flags,
3116 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003117 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003118{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003119 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3120 &mbedtls_x509_crt_profile_default, cn, flags,
3121 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003122}
3123
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003124/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003125 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003126 */
3127int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3128 mbedtls_x509_crt *trust_ca,
3129 mbedtls_x509_crl *ca_crl,
3130 const mbedtls_x509_crt_profile *profile,
3131 const char *cn, uint32_t *flags,
3132 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3133 void *p_vrfy )
3134{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003135 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3136 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3137}
3138
3139/*
3140 * Verify the certificate validity, with profile, restartable version
3141 *
3142 * This function:
3143 * - checks the requested CN (if any)
3144 * - checks the type and size of the EE cert's key,
3145 * as that isn't done as part of chain building/verification currently
3146 * - builds and verifies the chain
3147 * - then calls the callback and merges the flags
3148 */
3149int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3150 mbedtls_x509_crt *trust_ca,
3151 mbedtls_x509_crl *ca_crl,
3152 const mbedtls_x509_crt_profile *profile,
3153 const char *cn, uint32_t *flags,
3154 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3155 void *p_vrfy,
3156 mbedtls_x509_crt_restart_ctx *rs_ctx )
3157{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003158 int ret;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003159 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003160 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003161
3162 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003163 ee_flags = 0;
3164 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003165
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003166 if( profile == NULL )
3167 {
3168 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3169 goto exit;
3170 }
3171
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003172 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003173 if( cn != NULL )
Hanno Becker87233362019-02-25 18:15:33 +00003174 {
3175 ret = x509_crt_verify_name( crt, cn, &ee_flags );
3176 if( ret != 0 )
3177 return( ret );
3178 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003179
Hanno Becker87233362019-02-25 18:15:33 +00003180 {
3181 mbedtls_pk_context *pk;
3182 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003183
Hanno Becker87233362019-02-25 18:15:33 +00003184 ret = x509_crt_pk_acquire( crt, &pk );
3185 if( ret != 0 )
3186 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003187
Hanno Becker87233362019-02-25 18:15:33 +00003188 /* Check the type and size of the key */
3189 pk_type = mbedtls_pk_get_type( pk );
3190
3191 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
3192 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3193
3194 if( x509_profile_check_key( profile, pk ) != 0 )
3195 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3196
3197 x509_crt_pk_release( crt, pk );
3198 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003199
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003200 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003201 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003202 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003203
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003204 if( ret != 0 )
3205 goto exit;
3206
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003207 /* Merge end-entity flags */
3208 ver_chain.items[0].flags |= ee_flags;
3209
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003210 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003211 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003212
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003213exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003214#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3215 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3216 mbedtls_x509_crt_restart_free( rs_ctx );
3217#endif
3218
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003219 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3220 * the SSL module for authmode optional, but non-zero return from the
3221 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003222 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3223 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3224
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003225 if( ret != 0 )
3226 {
3227 *flags = (uint32_t) -1;
3228 return( ret );
3229 }
3230
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003231 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003233
3234 return( 0 );
3235}
3236
3237/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003238 * Initialize a certificate chain
3239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003243}
3244
3245/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003246 * Unallocate all certificate data
3247 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003248
3249static void x509_free_sequence( mbedtls_x509_sequence *seq )
3250{
3251 while( seq != NULL )
3252 {
3253 mbedtls_x509_sequence *next = seq->next;
3254 mbedtls_platform_zeroize( seq, sizeof( *seq ) );
3255 mbedtls_free( seq );
3256 seq = next;
3257 }
3258}
3259
3260static void x509_free_name( mbedtls_x509_name *name )
3261{
3262 while( name != NULL )
3263 {
3264 mbedtls_x509_name *next = name->next;
3265 mbedtls_platform_zeroize( name, sizeof( *name ) );
3266 mbedtls_free( name );
3267 name = next;
3268 }
3269}
3270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 mbedtls_x509_crt *cert_cur = crt;
3274 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003275
3276 if( crt == NULL )
3277 return;
3278
3279 do
3280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3284 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003285#endif
3286
Hanno Beckercd03bb22019-02-15 17:15:53 +00003287 x509_free_name( cert_cur->issuer.next );
3288 x509_free_name( cert_cur->subject.next );
3289 x509_free_sequence( cert_cur->ext_key_usage.next );
3290 x509_free_sequence( cert_cur->subject_alt_names.next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003291
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003292 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003293 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003294 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003296 }
3297
3298 cert_cur = cert_cur->next;
3299 }
3300 while( cert_cur != NULL );
3301
3302 cert_cur = crt;
3303 do
3304 {
3305 cert_prv = cert_cur;
3306 cert_cur = cert_cur->next;
3307
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003308 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003309 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003311 }
3312 while( cert_cur != NULL );
3313}
3314
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003315#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3316/*
3317 * Initialize a restart context
3318 */
3319void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3320{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003321 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003322
3323 ctx->parent = NULL;
3324 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003325 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003326
3327 ctx->parent_is_trusted = -1;
3328
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003329 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003330 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003331 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003332}
3333
3334/*
3335 * Free the components of a restart context
3336 */
3337void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3338{
3339 if( ctx == NULL )
3340 return;
3341
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003342 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003343 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003344}
3345#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347#endif /* MBEDTLS_X509_CRT_PARSE_C */