blob: bd3a7da2172fafe030749d19c25e230d765e0372 [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,
2251 mbedtls_x509_crt *ca,
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{
2255 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2257 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002258
2259 if( ca == NULL )
2260 return( flags );
2261
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002262 while( crl_list != NULL )
2263 {
2264 if( crl_list->version == 0 ||
Hanno Becker7dee12a2019-02-21 13:58:38 +00002265 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw_no_hdr,
Hanno Becker67284cc2019-02-21 14:31:51 +00002266 &ca->subject_raw_no_hdr,
2267 NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002268 {
2269 crl_list = crl_list->next;
2270 continue;
2271 }
2272
2273 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002274 * Check if the CA is configured to sign CRLs
2275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002277 if( mbedtls_x509_crt_check_key_usage( ca,
2278 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002281 break;
2282 }
2283#endif
2284
2285 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002286 * Check if CRL is correctly signed by the trusted CA
2287 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002288 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2289 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2290
2291 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2292 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002295 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002296 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002297 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002299 break;
2300 }
2301
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002302 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002303 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2306 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002307 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002310 break;
2311 }
2312
2313 /*
2314 * Check for validity of CRL (Do not drop out)
2315 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002316 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002318
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002319 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002320 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002321
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002322 /*
2323 * Check if certificate is revoked
2324 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002325 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2326 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329 break;
2330 }
2331
2332 crl_list = crl_list->next;
2333 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002334
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002335 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002338
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002339/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002340 * Check the signature of a certificate by its parent
2341 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002342static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002343 mbedtls_x509_crt *parent,
2344 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002345{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002346 int ret;
2347 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002348
Hanno Beckere449e2d2019-02-25 14:45:31 +00002349 ret = x509_crt_pk_acquire( parent, &pk );
2350 if( ret != 0 )
2351 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2352
2353 /* Skip expensive computation on obvious mismatch */
2354 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002355 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002356 ret = -1;
2357 goto exit;
2358 }
2359
2360#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2361 ((void) rs_ctx);
2362#else
2363 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2364 {
2365 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002366 sig_info->sig_md,
2367 sig_info->crt_hash, sig_info->crt_hash_len,
2368 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002369 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002370 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002371 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002372#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002373 {
2374 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2375 sig_info->sig_opts,
2376 pk,
2377 sig_info->sig_md,
2378 sig_info->crt_hash, sig_info->crt_hash_len,
2379 sig_info->sig.p, sig_info->sig.len );
2380 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002381
Hanno Beckere449e2d2019-02-25 14:45:31 +00002382exit:
2383 x509_crt_pk_release( parent, pk );
2384 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002385}
2386
2387/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002388 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2389 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002390 *
2391 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002392 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002393static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2394 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002395 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002396{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002397 int need_ca_bit;
2398
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002399 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002400 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2401 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002402 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002403 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002404 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002405 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002406
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002407 /* Parent must have the basicConstraints CA bit set as a general rule */
2408 need_ca_bit = 1;
2409
2410 /* Exception: v1/v2 certificates that are locally trusted. */
2411 if( top && parent->version < 3 )
2412 need_ca_bit = 0;
2413
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002414 if( need_ca_bit && ! parent->ca_istrue )
2415 return( -1 );
2416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002418 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002419 x509_crt_check_key_usage_frame( parent,
2420 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002421 {
2422 return( -1 );
2423 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002424#endif
2425
2426 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002427}
2428
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002429/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002430 * Find a suitable parent for child in candidates, or return NULL.
2431 *
2432 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002433 * 1. subject name matches child's issuer
2434 * 2. if necessary, the CA bit is set and key usage allows signing certs
2435 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002436 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002437 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002438 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002439 * If there's a suitable candidate which is also time-valid, return the first
2440 * such. Otherwise, return the first suitable candidate (or NULL if there is
2441 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002442 *
2443 * The rationale for this rule is that someone could have a list of trusted
2444 * roots with two versions on the same root with different validity periods.
2445 * (At least one user reported having such a list and wanted it to just work.)
2446 * The reason we don't just require time-validity is that generally there is
2447 * only one version, and if it's expired we want the flags to state that
2448 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002449 *
2450 * The rationale for rule 3 (signature for trusted roots) is that users might
2451 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002452 * way we select the correct one is by checking the signature (as we don't
2453 * rely on key identifier extensions). (This is one way users might choose to
2454 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002455 *
2456 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002457 * - [in] child: certificate for which we're looking for a parent
2458 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002459 * - [out] r_parent: parent found (or NULL)
2460 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002461 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2462 * of the chain, 0 otherwise
2463 * - [in] path_cnt: number of intermediates seen so far
2464 * - [in] self_cnt: number of self-signed intermediates seen so far
2465 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002466 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002467 *
2468 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002469 * - 0 on success
2470 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002471 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002472static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002473 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002474 mbedtls_x509_crt *candidates,
2475 mbedtls_x509_crt **r_parent,
2476 int *r_signature_is_good,
2477 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002478 unsigned path_cnt,
2479 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002480 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002481{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002482 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002483 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002484 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002485
2486#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002487 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002488 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2489 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002490 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002491 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002492 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002493 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002494
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002495 /* clear saved state */
2496 rs_ctx->parent = NULL;
2497 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002498 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002499
2500 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002501 goto check_signature;
2502 }
2503#endif
2504
2505 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002506 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002507
Hanno Becker43bf9002019-02-25 14:46:49 +00002508 for( parent_crt = candidates; parent_crt != NULL;
2509 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002510 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002511 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002512
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002513#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2514check_signature:
2515#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002516
2517 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002518 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002519 mbedtls_x509_crt_frame *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002520
Hanno Becker43bf9002019-02-25 14:46:49 +00002521 ret = x509_crt_frame_acquire( parent_crt, &parent );
2522 if( ret != 0 )
2523 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002524
Hanno Becker43bf9002019-02-25 14:46:49 +00002525 if( mbedtls_x509_time_is_past( &parent->valid_from ) &&
2526 mbedtls_x509_time_is_future( &parent->valid_to ) )
2527 {
2528 parent_valid = 1;
2529 }
2530
2531 /* basic parenting skills (name, CA bit, key usage) */
2532 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2533 parent_match = 1;
2534
2535 /* +1 because the stored max_pathlen is 1 higher
2536 * than the actual value */
2537 if( !( parent->max_pathlen > 0 &&
2538 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2539 {
2540 path_len_ok = 1;
2541 }
2542
2543 x509_crt_frame_release( parent_crt, parent );
Hanno Beckera788cab2019-02-24 17:47:46 +00002544 }
2545
2546 if( parent_match == 0 || path_len_ok == 0 )
2547 continue;
2548
2549 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002550 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002551
2552#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002553 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2554 {
2555 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002556 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002557 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002558 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002559
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002560 return( ret );
2561 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002562#else
2563 (void) ret;
2564#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002565
2566 signature_is_good = ret == 0;
2567 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002568 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002569
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002570 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002571 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002572 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002573 if( fallback_parent == NULL )
2574 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002575 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002576 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002577 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002578
2579 continue;
2580 }
2581
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002582 break;
2583 }
2584
Hanno Becker43bf9002019-02-25 14:46:49 +00002585 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002586 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002587 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002588 *r_signature_is_good = signature_is_good;
2589 }
2590 else
2591 {
2592 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002593 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002594 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002595
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002596 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002597}
2598
2599/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002600 * Find a parent in trusted CAs or the provided chain, or return NULL.
2601 *
2602 * Searches in trusted CAs first, and return the first suitable parent found
2603 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002604 *
2605 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002606 * - [in] child: certificate for which we're looking for a parent, followed
2607 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002608 * - [in] trust_ca: list of locally trusted certificates
2609 * - [out] parent: parent found (or NULL)
2610 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2611 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2612 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2613 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002614 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002615 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002616 *
2617 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002618 * - 0 on success
2619 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002620 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002621static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002622 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002623 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002624 mbedtls_x509_crt *trust_ca,
2625 mbedtls_x509_crt **parent,
2626 int *parent_is_trusted,
2627 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002628 unsigned path_cnt,
2629 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002630 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002631{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002632 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002633 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002634
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002635 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002636
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002637#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002638 /* restore then clear saved state if we have some stored */
2639 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2640 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002641 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002642 rs_ctx->parent_is_trusted = -1;
2643 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002644#endif
2645
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002646 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00002647 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002648
Hanno Becker5299cf82019-02-25 13:50:41 +00002649 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002650 parent, signature_is_good,
2651 *parent_is_trusted,
2652 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002653
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002654#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002655 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2656 {
2657 /* save state */
2658 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002659 return( ret );
2660 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002661#else
2662 (void) ret;
2663#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002664
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002665 /* stop here if found or already in second iteration */
2666 if( *parent != NULL || *parent_is_trusted == 0 )
2667 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002668
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002669 /* prepare second iteration */
2670 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002671 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002672
2673 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01002674 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002675 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002676 *parent_is_trusted = 0;
2677 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002678 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002679
2680 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002681}
2682
2683/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002684 * Check if an end-entity certificate is locally trusted
2685 *
2686 * Currently we require such certificates to be self-signed (actually only
2687 * check for self-issued as self-signatures are not checked)
2688 */
2689static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00002690 mbedtls_x509_crt_frame const *crt,
2691 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002692{
Hanno Becker1e0677a2019-02-25 14:58:22 +00002693 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002694
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002695 /* look for an exact match with trusted cert */
2696 for( cur = trust_ca; cur != NULL; cur = cur->next )
2697 {
2698 if( crt->raw.len == cur->raw.len &&
2699 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2700 {
2701 return( 0 );
2702 }
2703 }
2704
2705 /* too bad */
2706 return( -1 );
2707}
2708
2709/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002710 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002711 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002712 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2713 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002714 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002715 * such that every cert in the chain is a child of the next one,
2716 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002717 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002718 * Verify that chain and return it with flags for all issues found.
2719 *
2720 * Special cases:
2721 * - EE == Rj -> return a one-element list containing it
2722 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2723 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002724 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002725 * Tests for (aspects of) this function should include at least:
2726 * - trusted EE
2727 * - EE -> trusted root
2728 * - EE -> intermedate CA -> trusted root
2729 * - if relevant: EE untrusted
2730 * - if relevant: EE -> intermediate, untrusted
2731 * with the aspect under test checked at each relevant level (EE, int, root).
2732 * For some aspects longer chains are required, but usually length 2 is
2733 * enough (but length 1 is not in general).
2734 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002735 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002736 * - [in] crt: the cert list EE, C1, ..., Cn
2737 * - [in] trust_ca: the trusted list R1, ..., Rp
2738 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002739 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002740 * Only valid when return value is 0, may contain garbage otherwise!
2741 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002742 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002743 *
2744 * Return value:
2745 * - non-zero if the chain could not be fully built and examined
2746 * - 0 is the chain was successfully built and examined,
2747 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002748 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002749static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002750 mbedtls_x509_crt *crt,
2751 mbedtls_x509_crt *trust_ca,
2752 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002753 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002754 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002755 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002756{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002757 /* Don't initialize any of those variables here, so that the compiler can
2758 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002759 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002760 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002761 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00002762 mbedtls_x509_crt *child_crt;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002763 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002764 int parent_is_trusted;
2765 int child_is_trusted;
2766 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002767 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002768
2769#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2770 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002771 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002772 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002773 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002774 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002775 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002776
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002777 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002778 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00002779 child_crt = cur->crt;
2780
2781 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002782 goto find_parent;
2783 }
2784#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002785
Hanno Becker1e0677a2019-02-25 14:58:22 +00002786 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002787 self_cnt = 0;
2788 parent_is_trusted = 0;
2789 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002790
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002791 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00002792#if defined(MBEDTLS_X509_CRL_PARSE_C)
2793 mbedtls_x509_buf_raw child_serial;
2794#endif /* MBEDTLS_X509_CRL_PARSE_C */
2795 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00002796
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002797 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002798 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00002799 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002800 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002801 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00002802
2803#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2804find_parent:
2805#endif
2806
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002807 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002808
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002809 {
Hanno Becker5299cf82019-02-25 13:50:41 +00002810 mbedtls_x509_crt_sig_info child_sig;
2811 {
2812 mbedtls_x509_crt_frame *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002813
Hanno Becker5299cf82019-02-25 13:50:41 +00002814 ret = x509_crt_frame_acquire( child_crt, &child );
2815 if( ret != 0 )
2816 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2817
2818 /* Check time-validity (all certificates) */
2819 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2820 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2821 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2822 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
2823
2824 /* Stop here for trusted roots (but not for trusted EE certs) */
2825 if( child_is_trusted )
2826 {
2827 x509_crt_frame_release( child_crt, child );
2828 return( 0 );
2829 }
2830
2831 self_issued = 0;
2832 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
2833 &child->subject_raw,
2834 NULL, NULL ) == 0 )
2835 {
2836 self_issued = 1;
2837 }
2838
2839 /* Check signature algorithm: MD & PK algs */
2840 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2841 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2842
2843 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2844 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2845
2846 /* Special case: EE certs that are locally trusted */
2847 if( ver_chain->len == 1 && self_issued &&
2848 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2849 {
2850 x509_crt_frame_release( child_crt, child );
2851 return( 0 );
2852 }
2853
2854#if defined(MBEDTLS_X509_CRL_PARSE_C)
2855 child_serial = child->serial;
2856#endif /* MBEDTLS_X509_CRL_PARSE_C */
2857
2858 ret = x509_crt_get_sig_info( child, &child_sig );
2859
2860 x509_crt_frame_release( child_crt, child );
2861 if( ret != 0 )
2862 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2863 }
2864
2865 /* Look for a parent in trusted CAs or up the chain */
2866 ret = x509_crt_find_parent( &child_sig, child_crt->next,
2867 trust_ca, &parent,
2868 &parent_is_trusted, &signature_is_good,
2869 ver_chain->len - 1, self_cnt, rs_ctx );
2870
2871 x509_crt_free_sig_info( &child_sig );
2872 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002873
2874#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002875 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2876 {
2877 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002878 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002879 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002880 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00002881 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002882 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002883#else
2884 (void) ret;
2885#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002886
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002887 /* No parent? We're done here */
2888 if( parent == NULL )
2889 {
2890 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00002891 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002892 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002893
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002894 /* Count intermediate self-issued (not necessarily self-signed) certs.
2895 * These can occur with some strategies for key rollover, see [SIRO],
2896 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00002897 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002898 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002899
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002900 /* path_cnt is 0 for the first intermediate CA,
2901 * and if parent is trusted it's not an intermediate CA */
2902 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002903 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002904 {
2905 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00002906 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002907 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002908
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002909 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002910 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002911 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2912
2913 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002914 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002915 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002918 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00002919 *flags |= x509_crt_verifycrl( child_serial.p,
2920 child_serial.len,
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002921 parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002922#else
2923 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002924#endif
2925
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002926 /* prepare for next iteration */
Hanno Becker1e0677a2019-02-25 14:58:22 +00002927 child_crt = parent;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002928 parent = NULL;
2929 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002930 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002931 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002932}
2933
2934/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002935 * Check for CN match
2936 */
Hanno Becker24926222019-02-21 13:10:55 +00002937static int x509_crt_check_cn( unsigned char const *buf,
2938 size_t buflen,
2939 const char *cn,
2940 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002941{
Hanno Becker24926222019-02-21 13:10:55 +00002942 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00002943 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002944 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002945
2946 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00002947 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002948 {
2949 return( 0 );
2950 }
2951
2952 return( -1 );
2953}
2954
Hanno Becker8b543b32019-02-21 11:50:44 +00002955/* Returns 1 on a match and 0 on a mismatch.
2956 * This is because this function is used as a callback for
2957 * mbedtls_x509_name_cmp_raw(), which continues the name
2958 * traversal as long as the callback returns 0. */
2959static int x509_crt_check_name( void *ctx,
2960 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00002961 mbedtls_x509_buf *val,
2962 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00002963{
2964 char const *cn = (char const*) ctx;
2965 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00002966 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00002967
2968 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00002969 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00002970 {
2971 return( 1 );
2972 }
2973
2974 return( 0 );
2975}
2976
Hanno Beckerda410822019-02-21 13:36:59 +00002977/* Returns 1 on a match and 0 on a mismatch.
2978 * This is because this function is used as a callback for
2979 * mbedtls_asn1_traverse_sequence_of(), which continues the
2980 * traversal as long as the callback returns 0. */
2981static int x509_crt_subject_alt_check_name( void *ctx,
2982 int tag,
2983 unsigned char *data,
2984 size_t data_len )
2985{
2986 char const *cn = (char const*) ctx;
2987 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00002988 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00002989
2990 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
2991 return( 1 );
2992
2993 return( 0 );
2994}
2995
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002996/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002997 * Verify the requested CN - only call this if cn is not NULL!
2998 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002999static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003000 const char *cn,
3001 uint32_t *flags )
3002{
Hanno Beckerda410822019-02-21 13:36:59 +00003003 int ret;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003004
3005 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3006 {
Hanno Becker90b94082019-02-21 21:13:21 +00003007 unsigned char *p =
3008 crt->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00003009 const unsigned char *end =
3010 crt->subject_alt_raw.p + crt->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003011
Hanno Becker90b94082019-02-21 21:13:21 +00003012 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
3013 MBEDTLS_ASN1_TAG_CLASS_MASK,
3014 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
3015 MBEDTLS_ASN1_TAG_VALUE_MASK,
3016 2 /* SubjectAlt DNS */,
3017 x509_crt_subject_alt_check_name,
3018 (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003019 }
3020 else
3021 {
Hanno Becker8b543b32019-02-21 11:50:44 +00003022 ret = mbedtls_x509_name_cmp_raw( &crt->subject_raw_no_hdr,
3023 &crt->subject_raw_no_hdr,
3024 x509_crt_check_name, (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003025 }
Hanno Beckerda410822019-02-21 13:36:59 +00003026
3027 if( ret != 1 )
3028 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003029}
3030
3031/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003032 * Merge the flags for all certs in the chain, after calling callback
3033 */
3034static int x509_crt_merge_flags_with_cb(
3035 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003036 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003037 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3038 void *p_vrfy )
3039{
3040 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003041 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003042 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003043 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003044
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003045 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003046 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003047 cur = &ver_chain->items[i-1];
3048 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003049
3050 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003051 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003052 return( ret );
3053
3054 *flags |= cur_flags;
3055 }
3056
3057 return( 0 );
3058}
3059
3060/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003061 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3064 mbedtls_x509_crt *trust_ca,
3065 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003066 const char *cn, uint32_t *flags,
3067 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02003068 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003069{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003070 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3071 &mbedtls_x509_crt_profile_default, cn, flags,
3072 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003073}
3074
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003075/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003076 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02003077 */
3078int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3079 mbedtls_x509_crt *trust_ca,
3080 mbedtls_x509_crl *ca_crl,
3081 const mbedtls_x509_crt_profile *profile,
3082 const char *cn, uint32_t *flags,
3083 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3084 void *p_vrfy )
3085{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003086 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
3087 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
3088}
3089
3090/*
3091 * Verify the certificate validity, with profile, restartable version
3092 *
3093 * This function:
3094 * - checks the requested CN (if any)
3095 * - checks the type and size of the EE cert's key,
3096 * as that isn't done as part of chain building/verification currently
3097 * - builds and verifies the chain
3098 * - then calls the callback and merges the flags
3099 */
3100int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3101 mbedtls_x509_crt *trust_ca,
3102 mbedtls_x509_crl *ca_crl,
3103 const mbedtls_x509_crt_profile *profile,
3104 const char *cn, uint32_t *flags,
3105 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3106 void *p_vrfy,
3107 mbedtls_x509_crt_restart_ctx *rs_ctx )
3108{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003109 int ret;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003110 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003111 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003112 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003113
3114 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003115 ee_flags = 0;
3116 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003117
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003118 if( profile == NULL )
3119 {
3120 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3121 goto exit;
3122 }
3123
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003124 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003125 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003126 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003127
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003128 /* Check the type and size of the key */
3129 pk_type = mbedtls_pk_get_type( &crt->pk );
3130
3131 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003132 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003133
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003134 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003135 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003136
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003137 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003138 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003139 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003140
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003141 if( ret != 0 )
3142 goto exit;
3143
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003144 /* Merge end-entity flags */
3145 ver_chain.items[0].flags |= ee_flags;
3146
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003147 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003148 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003149
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003150exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003151#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3152 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3153 mbedtls_x509_crt_restart_free( rs_ctx );
3154#endif
3155
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003156 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3157 * the SSL module for authmode optional, but non-zero return from the
3158 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003159 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3160 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3161
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003162 if( ret != 0 )
3163 {
3164 *flags = (uint32_t) -1;
3165 return( ret );
3166 }
3167
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003168 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003170
3171 return( 0 );
3172}
3173
3174/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003175 * Initialize a certificate chain
3176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003180}
3181
3182/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003183 * Unallocate all certificate data
3184 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003185
3186static void x509_free_sequence( mbedtls_x509_sequence *seq )
3187{
3188 while( seq != NULL )
3189 {
3190 mbedtls_x509_sequence *next = seq->next;
3191 mbedtls_platform_zeroize( seq, sizeof( *seq ) );
3192 mbedtls_free( seq );
3193 seq = next;
3194 }
3195}
3196
3197static void x509_free_name( mbedtls_x509_name *name )
3198{
3199 while( name != NULL )
3200 {
3201 mbedtls_x509_name *next = name->next;
3202 mbedtls_platform_zeroize( name, sizeof( *name ) );
3203 mbedtls_free( name );
3204 name = next;
3205 }
3206}
3207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 mbedtls_x509_crt *cert_cur = crt;
3211 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003212
3213 if( crt == NULL )
3214 return;
3215
3216 do
3217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3221 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003222#endif
3223
Hanno Beckercd03bb22019-02-15 17:15:53 +00003224 x509_free_name( cert_cur->issuer.next );
3225 x509_free_name( cert_cur->subject.next );
3226 x509_free_sequence( cert_cur->ext_key_usage.next );
3227 x509_free_sequence( cert_cur->subject_alt_names.next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003228
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003229 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003230 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003231 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003233 }
3234
3235 cert_cur = cert_cur->next;
3236 }
3237 while( cert_cur != NULL );
3238
3239 cert_cur = crt;
3240 do
3241 {
3242 cert_prv = cert_cur;
3243 cert_cur = cert_cur->next;
3244
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003245 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003246 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003248 }
3249 while( cert_cur != NULL );
3250}
3251
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003252#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3253/*
3254 * Initialize a restart context
3255 */
3256void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3257{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003258 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003259
3260 ctx->parent = NULL;
3261 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003262 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003263
3264 ctx->parent_is_trusted = -1;
3265
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003266 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003267 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003268 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003269}
3270
3271/*
3272 * Free the components of a restart context
3273 */
3274void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3275{
3276 if( ctx == NULL )
3277 return;
3278
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003279 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003280 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003281}
3282#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284#endif /* MBEDTLS_X509_CRT_PARSE_C */