blob: abd70e1e4067ebe96ccfd384ab1513b34d407fe2 [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
50#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>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020077#if defined(__MBED__)
78#include <platform/mbed_retarget.h>
79#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020081#endif /* __MBED__ */
Rich Evans00ab4702015-02-06 13:43:58 +000082#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#endif
84
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020085/*
86 * Item in a verification chain: cert and flags for it
87 */
88typedef struct {
89 mbedtls_x509_crt *crt;
90 uint32_t flags;
91} x509_crt_verify_chain_item;
92
93/*
94 * Max size of verification chain: end-entity + intermediates + trusted root
95 */
96#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020097
Gilles Peskineffb92da2021-06-02 00:03:26 +020098/* Default profile. Do not remove items unless there are serious security
99 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200100const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
101{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200102 /* Hashes from SHA-256 and above. Note that this selection
103 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
107 0xFFFFFFF, /* Any PK alg */
108#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200109 /* Curves at or above 128-bit security level. Note that this selection
110 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200111 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
118#else
119 0,
120#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200121 2048,
122};
123
Gilles Peskineffb92da2021-06-02 00:03:26 +0200124/* Next-generation profile. Currently identical to the default, but may
125 * be tightened at any time. */
126const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
127 mbedtls_x509_crt_profile_default;
128
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200129/*
130 * NSA Suite B Profile
131 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200132const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
133{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200134 /* Only SHA-256 and 384 */
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
137 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200138 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200140#if defined(MBEDTLS_ECP_C)
141 /* Only NIST P-256 and P-384 */
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
143 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
144#else
145 0,
146#endif
147 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200148};
149
150/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200151 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200152 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200153 */
154static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
155 mbedtls_md_type_t md_alg )
156{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200157 if( md_alg == MBEDTLS_MD_NONE )
158 return( -1 );
159
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200160 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
161 return( 0 );
162
163 return( -1 );
164}
165
166/*
167 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200168 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200169 */
170static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
171 mbedtls_pk_type_t pk_alg )
172{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200173 if( pk_alg == MBEDTLS_PK_NONE )
174 return( -1 );
175
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200176 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
177 return( 0 );
178
179 return( -1 );
180}
181
182/*
183 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200184 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 */
186static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187 const mbedtls_pk_context *pk )
188{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200189 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200190
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191#if defined(MBEDTLS_RSA_C)
192 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
193 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200194 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195 return( 0 );
196
197 return( -1 );
198 }
199#endif
200
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200201#if defined(MBEDTLS_ECP_C)
202 if( pk_alg == MBEDTLS_PK_ECDSA ||
203 pk_alg == MBEDTLS_PK_ECKEY ||
204 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200205 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200206 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200207
Philippe Antoineb5b25432018-05-11 11:06:29 +0200208 if( gid == MBEDTLS_ECP_DP_NONE )
209 return( -1 );
210
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
212 return( 0 );
213
214 return( -1 );
215 }
216#endif
217
218 return( -1 );
219}
220
221/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000222 * Like memcmp, but case-insensitive and always returns -1 if different
223 */
224static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
225{
226 size_t i;
227 unsigned char diff;
228 const unsigned char *n1 = s1, *n2 = s2;
229
230 for( i = 0; i < len; i++ )
231 {
232 diff = n1[i] ^ n2[i];
233
234 if( diff == 0 )
235 continue;
236
237 if( diff == 32 &&
238 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
239 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
240 {
241 continue;
242 }
243
244 return( -1 );
245 }
246
247 return( 0 );
248}
249
250/*
251 * Return 0 if name matches wildcard, -1 otherwise
252 */
253static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
254{
255 size_t i;
256 size_t cn_idx = 0, cn_len = strlen( cn );
257
258 /* We can't have a match if there is no wildcard to match */
259 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
260 return( -1 );
261
262 for( i = 0; i < cn_len; ++i )
263 {
264 if( cn[i] == '.' )
265 {
266 cn_idx = i;
267 break;
268 }
269 }
270
271 if( cn_idx == 0 )
272 return( -1 );
273
274 if( cn_len - cn_idx == name->len - 1 &&
275 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
276 {
277 return( 0 );
278 }
279
280 return( -1 );
281}
282
283/*
284 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
285 * variations (but not all).
286 *
287 * Return 0 if equal, -1 otherwise.
288 */
289static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
290{
291 if( a->tag == b->tag &&
292 a->len == b->len &&
293 memcmp( a->p, b->p, b->len ) == 0 )
294 {
295 return( 0 );
296 }
297
298 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
299 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
300 a->len == b->len &&
301 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
302 {
303 return( 0 );
304 }
305
306 return( -1 );
307}
308
309/*
310 * Compare two X.509 Names (aka rdnSequence).
311 *
312 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
313 * we sometimes return unequal when the full algorithm would return equal,
314 * but never the other way. (In particular, we don't do Unicode normalisation
315 * or space folding.)
316 *
317 * Return 0 if equal, -1 otherwise.
318 */
319static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
320{
321 /* Avoid recursion, it might not be optimised by the compiler */
322 while( a != NULL || b != NULL )
323 {
324 if( a == NULL || b == NULL )
325 return( -1 );
326
327 /* type */
328 if( a->oid.tag != b->oid.tag ||
329 a->oid.len != b->oid.len ||
330 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
331 {
332 return( -1 );
333 }
334
335 /* value */
336 if( x509_string_cmp( &a->val, &b->val ) != 0 )
337 return( -1 );
338
339 /* structure of the list of sets */
340 if( a->next_merged != b->next_merged )
341 return( -1 );
342
343 a = a->next;
344 b = b->next;
345 }
346
347 /* a == NULL == b */
348 return( 0 );
349}
350
351/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200352 * Reset (init or clear) a verify_chain
353 */
354static void x509_crt_verify_chain_reset(
355 mbedtls_x509_crt_verify_chain *ver_chain )
356{
357 size_t i;
358
359 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
360 {
361 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000362 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200363 }
364
365 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000366
367#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
368 ver_chain->trust_ca_cb_result = NULL;
369#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200370}
371
372/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
374 */
375static int x509_get_version( unsigned char **p,
376 const unsigned char *end,
377 int *ver )
378{
Janos Follath865b3eb2019-12-16 11:46:15 +0000379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 size_t len;
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
383 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 {
387 *ver = 0;
388 return( 0 );
389 }
390
Chris Jones9f7a6932021-04-14 12:12:09 +0100391 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392 }
393
394 end = *p + len;
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100397 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398
399 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100400 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
401 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
403 return( 0 );
404}
405
406/*
407 * Validity ::= SEQUENCE {
408 * notBefore Time,
409 * notAfter Time }
410 */
411static int x509_get_dates( unsigned char **p,
412 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_x509_time *from,
414 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415{
Janos Follath865b3eb2019-12-16 11:46:15 +0000416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 size_t len;
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
420 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100421 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422
423 end = *p + len;
424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 return( ret );
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429 return( ret );
430
431 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100432 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
433 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
435 return( 0 );
436}
437
438/*
439 * X.509 v2/v3 unique identifier (not parsed)
440 */
441static int x509_get_uid( unsigned char **p,
442 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444{
Janos Follath865b3eb2019-12-16 11:46:15 +0000445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446
447 if( *p == end )
448 return( 0 );
449
450 uid->tag = **p;
451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
453 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456 return( 0 );
457
Chris Jones9f7a6932021-04-14 12:12:09 +0100458 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 }
460
461 uid->p = *p;
462 *p += uid->len;
463
464 return( 0 );
465}
466
467static int x509_get_basic_constraints( unsigned char **p,
468 const unsigned char *end,
469 int *ca_istrue,
470 int *max_pathlen )
471{
Janos Follath865b3eb2019-12-16 11:46:15 +0000472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473 size_t len;
474
475 /*
476 * BasicConstraints ::= SEQUENCE {
477 * cA BOOLEAN DEFAULT FALSE,
478 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
479 */
480 *ca_istrue = 0; /* DEFAULT FALSE */
481 *max_pathlen = 0; /* endless */
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
484 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100485 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486
487 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200488 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
493 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
495 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100496 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 if( *ca_istrue != 0 )
499 *ca_istrue = 1;
500 }
501
502 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200503 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100506 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507
508 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100509 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
510 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511
Andrzej Kurek16050742020-04-14 09:49:52 -0400512 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
513 * overflow, which is an undefined behavior. */
514 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100515 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
516 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400517
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 (*max_pathlen)++;
519
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200520 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521}
522
523static int x509_get_ns_cert_type( unsigned char **p,
524 const unsigned char *end,
525 unsigned char *ns_cert_type)
526{
Janos Follath865b3eb2019-12-16 11:46:15 +0000527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100531 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532
533 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100534 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
535 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
537 /* Get actual bitstring */
538 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200539 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540}
541
542static int x509_get_key_usage( unsigned char **p,
543 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100544 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545{
Janos Follath865b3eb2019-12-16 11:46:15 +0000546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200547 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100551 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
553 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100554 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
555 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
557 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200558 *key_usage = 0;
559 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
560 {
561 *key_usage |= (unsigned int) bs.p[i] << (8*i);
562 }
563
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200564 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565}
566
567/*
568 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
569 *
570 * KeyPurposeId ::= OBJECT IDENTIFIER
571 */
572static int x509_get_ext_key_usage( unsigned char **p,
573 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575{
Janos Follath865b3eb2019-12-16 11:46:15 +0000576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100579 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
581 /* Sequence length must be >= 1 */
582 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100583 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
584 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200586 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587}
588
589/*
590 * SubjectAltName ::= GeneralNames
591 *
592 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
593 *
594 * GeneralName ::= CHOICE {
595 * otherName [0] OtherName,
596 * rfc822Name [1] IA5String,
597 * dNSName [2] IA5String,
598 * x400Address [3] ORAddress,
599 * directoryName [4] Name,
600 * ediPartyName [5] EDIPartyName,
601 * uniformResourceIdentifier [6] IA5String,
602 * iPAddress [7] OCTET STRING,
603 * registeredID [8] OBJECT IDENTIFIER }
604 *
605 * OtherName ::= SEQUENCE {
606 * type-id OBJECT IDENTIFIER,
607 * value [0] EXPLICIT ANY DEFINED BY type-id }
608 *
609 * EDIPartyName ::= SEQUENCE {
610 * nameAssigner [0] DirectoryString OPTIONAL,
611 * partyName [1] DirectoryString }
612 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300613 * NOTE: we list all types, but only use dNSName and otherName
614 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 */
616static int x509_get_subject_alt_name( unsigned char **p,
617 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619{
Janos Follath865b3eb2019-12-16 11:46:15 +0000620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625
626 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
628 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100629 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630
631 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100632 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
633 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634
635 while( *p < end )
636 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300637 mbedtls_x509_subject_alternative_name dummy_san_buf;
638 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
639
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 tag = **p;
641 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100643 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100645 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
646 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
647 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100648 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
649 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100650 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
Ron Eldordbbd9662019-05-15 15:46:03 +0300652 /*
irwir74aee1c2019-09-21 18:21:48 +0300653 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300654 */
655 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
656 /*
657 * In case the extension is malformed, return an error,
658 * and clear the allocated sequences.
659 */
660 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
661 {
662 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
663 mbedtls_x509_sequence *seq_prv;
664 while( seq_cur != NULL )
665 {
666 seq_prv = seq_cur;
667 seq_cur = seq_cur->next;
668 mbedtls_platform_zeroize( seq_prv,
669 sizeof( mbedtls_x509_sequence ) );
670 mbedtls_free( seq_prv );
671 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300672 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300673 return( ret );
674 }
675
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200677 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100679 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100681
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200682 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683
684 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100685 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
686 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688 cur = cur->next;
689 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200690
691 buf = &(cur->buf);
692 buf->tag = tag;
693 buf->p = *p;
694 buf->len = tag_len;
695 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696 }
697
698 /* Set final sequence entry's next pointer to NULL */
699 cur->next = NULL;
700
701 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100702 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
703 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704
705 return( 0 );
706}
707
708/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200709 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
710 *
711 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
712 *
713 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
714 *
715 * PolicyInformation ::= SEQUENCE {
716 * policyIdentifier CertPolicyId,
717 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
718 * PolicyQualifierInfo OPTIONAL }
719 *
720 * CertPolicyId ::= OBJECT IDENTIFIER
721 *
722 * PolicyQualifierInfo ::= SEQUENCE {
723 * policyQualifierId PolicyQualifierId,
724 * qualifier ANY DEFINED BY policyQualifierId }
725 *
726 * -- policyQualifierIds for Internet policy qualifiers
727 *
728 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
729 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
730 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
731 *
732 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
733 *
734 * Qualifier ::= CHOICE {
735 * cPSuri CPSuri,
736 * userNotice UserNotice }
737 *
738 * CPSuri ::= IA5String
739 *
740 * UserNotice ::= SEQUENCE {
741 * noticeRef NoticeReference OPTIONAL,
742 * explicitText DisplayText OPTIONAL }
743 *
744 * NoticeReference ::= SEQUENCE {
745 * organization DisplayText,
746 * noticeNumbers SEQUENCE OF INTEGER }
747 *
748 * DisplayText ::= CHOICE {
749 * ia5String IA5String (SIZE (1..200)),
750 * visibleString VisibleString (SIZE (1..200)),
751 * bmpString BMPString (SIZE (1..200)),
752 * utf8String UTF8String (SIZE (1..200)) }
753 *
754 * NOTE: we only parse and use anyPolicy without qualifiers at this point
755 * as defined in RFC 5280.
756 */
757static int x509_get_certificate_policies( unsigned char **p,
758 const unsigned char *end,
759 mbedtls_x509_sequence *certificate_policies )
760{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300761 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200762 size_t len;
763 mbedtls_asn1_buf *buf;
764 mbedtls_asn1_sequence *cur = certificate_policies;
765
766 /* Get main sequence tag */
767 ret = mbedtls_asn1_get_tag( p, end, &len,
768 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
769 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100770 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200771
772 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100773 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
774 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200775
776 /*
777 * Cannot be an empty sequence.
778 */
779 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100780 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
781 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200782
783 while( *p < end )
784 {
785 mbedtls_x509_buf policy_oid;
786 const unsigned char *policy_end;
787
788 /*
789 * Get the policy sequence
790 */
791 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
792 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100793 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200794
795 policy_end = *p + len;
796
Ron Eldor08063792019-05-13 16:38:39 +0300797 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200798 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100799 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200800
801 policy_oid.tag = MBEDTLS_ASN1_OID;
802 policy_oid.len = len;
803 policy_oid.p = *p;
804
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300805 /*
806 * Only AnyPolicy is currently supported when enforcing policy.
807 */
808 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
809 {
810 /*
811 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200812 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300813 */
814 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
815 }
816
Ron Eldor74d9acc2019-03-21 14:00:03 +0200817 /* Allocate and assign next pointer */
818 if( cur->buf.p != NULL )
819 {
820 if( cur->next != NULL )
821 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
822
823 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
824
825 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100826 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
827 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200828
829 cur = cur->next;
830 }
831
832 buf = &( cur->buf );
833 buf->tag = policy_oid.tag;
834 buf->p = policy_oid.p;
835 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300836
837 *p += len;
838
839 /*
840 * If there is an optional qualifier, then *p < policy_end
841 * Check the Qualifier len to verify it doesn't exceed policy_end.
842 */
843 if( *p < policy_end )
844 {
845 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
846 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100847 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300848 /*
849 * Skip the optional policy qualifiers.
850 */
851 *p += len;
852 }
853
854 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100855 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
856 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200857 }
858
859 /* Set final sequence entry's next pointer to NULL */
860 cur->next = NULL;
861
862 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100863 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
864 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200865
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300866 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200867}
868
869/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 * X.509 v3 extensions
871 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872 */
873static int x509_get_crt_ext( unsigned char **p,
874 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200875 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200876 mbedtls_x509_crt_ext_cb_t cb,
877 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878{
Janos Follath865b3eb2019-12-16 11:46:15 +0000879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200881 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882
Hanno Becker12f62fb2019-02-12 17:22:36 +0000883 if( *p == end )
884 return( 0 );
885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888
Hanno Becker12f62fb2019-02-12 17:22:36 +0000889 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 while( *p < end )
891 {
892 /*
893 * Extension ::= SEQUENCE {
894 * extnID OBJECT IDENTIFIER,
895 * critical BOOLEAN DEFAULT FALSE,
896 * extnValue OCTET STRING }
897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 int is_critical = 0; /* DEFAULT FALSE */
900 int ext_type = 0;
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
903 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100904 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905
906 end_ext_data = *p + len;
907
908 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200909 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200910 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100911 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
k-stachowiak463928a2018-07-24 12:50:59 +0200913 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 extn_oid.p = *p;
915 *p += extn_oid.len;
916
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
919 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100920 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921
922 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
924 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100925 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200927 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 end_ext_octet = *p + len;
929
930 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100931 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
932 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
934 /*
935 * Detect supported extensions
936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939 if( ret != 0 )
940 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200941 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200942 if( cb != NULL )
943 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200944 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200945 if( ret != 0 && is_critical )
946 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200947 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200948 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200949 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200950
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 /* No parser found, skip extension */
952 *p = end_ext_octet;
953
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954 if( is_critical )
955 {
956 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100957 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
958 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 continue;
961 }
962
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100963 /* Forbid repeated extensions */
964 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100966
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 crt->ext_types |= ext_type;
968
969 switch( ext_type )
970 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100971 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972 /* Parse basic constraints */
973 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
974 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200975 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 break;
977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 /* Parse key usage */
980 if( ( ret = x509_get_key_usage( p, end_ext_octet,
981 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200982 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 break;
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 /* Parse extended key usage */
987 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
988 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200989 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 break;
991
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100992 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 /* Parse subject alt name */
994 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
995 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200996 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 break;
998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000 /* Parse netscape certificate type */
1001 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1002 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001003 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 break;
1005
Ron Eldor74d9acc2019-03-21 14:00:03 +02001006 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1007 /* Parse certificate policies type */
1008 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1009 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001010 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001011 /* Give the callback (if any) a chance to handle the extension
1012 * if it contains unsupported policies */
1013 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1014 cb( p_ctx, crt, &extn_oid, is_critical,
1015 start_ext_octet, end_ext_octet ) == 0 )
1016 break;
1017
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001018 if( is_critical )
1019 return( ret );
1020 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001021 /*
Ron Eldora2913912019-05-16 16:17:38 +03001022 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1023 * cannot interpret or enforce the policy. However, it is up to
1024 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001025 * unless the extension is critical.
1026 */
1027 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1028 return( ret );
1029 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001030 break;
1031
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001033 /*
1034 * If this is a non-critical extension, which the oid layer
1035 * supports, but there isn't an x509 parser for it,
1036 * skip the extension.
1037 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001038 if( is_critical )
1039 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1040 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001041 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 }
1043 }
1044
1045 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001046 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1047 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048
1049 return( 0 );
1050}
1051
1052/*
1053 * Parse and fill a single X.509 certificate in DER format
1054 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001055static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1056 const unsigned char *buf,
1057 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001058 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001059 mbedtls_x509_crt_ext_cb_t cb,
1060 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061{
Janos Follath865b3eb2019-12-16 11:46:15 +00001062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 size_t len;
1064 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1068 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1069 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071 /*
1072 * Check for valid input
1073 */
1074 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001077 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001078 p = (unsigned char*) buf;
1079 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080 end = p + len;
1081
1082 /*
1083 * Certificate ::= SEQUENCE {
1084 * tbsCertificate TBSCertificate,
1085 * signatureAlgorithm AlgorithmIdentifier,
1086 * signatureValue BIT STRING }
1087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1089 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_x509_crt_free( crt );
1092 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093 }
1094
Janos Follathcc0e49d2016-02-17 14:34:12 +00001095 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001096 crt->raw.len = crt_end - buf;
1097 if( make_copy != 0 )
1098 {
1099 /* Create and populate a new buffer for the raw field. */
1100 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1101 if( crt->raw.p == NULL )
1102 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1103
1104 memcpy( crt->raw.p, buf, crt->raw.len );
1105 crt->own_buffer = 1;
1106
1107 p += crt->raw.len - len;
1108 end = crt_end = p + len;
1109 }
1110 else
1111 {
1112 crt->raw.p = (unsigned char*) buf;
1113 crt->own_buffer = 0;
1114 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001115
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116 /*
1117 * TBSCertificate ::= SEQUENCE {
1118 */
1119 crt->tbs.p = p;
1120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001125 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126 }
1127
1128 end = p + len;
1129 crt->tbs.len = end - crt->tbs.p;
1130
1131 /*
1132 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1133 *
1134 * CertificateSerialNumber ::= INTEGER
1135 *
1136 * signature AlgorithmIdentifier
1137 */
1138 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1140 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001141 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144 return( ret );
1145 }
1146
Andres AG7ca4a032017-03-09 16:16:11 +00001147 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 mbedtls_x509_crt_free( crt );
1150 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001151 }
1152
Andres AG7ca4a032017-03-09 16:16:11 +00001153 crt->version++;
1154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001156 &crt->sig_md, &crt->sig_pk,
1157 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 return( ret );
1161 }
1162
1163 /*
1164 * issuer Name
1165 */
1166 crt->issuer_raw.p = p;
1167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1169 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001172 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 }
1174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 return( ret );
1179 }
1180
1181 crt->issuer_raw.len = p - crt->issuer_raw.p;
1182
1183 /*
1184 * Validity ::= SEQUENCE {
1185 * notBefore Time,
1186 * notAfter Time }
1187 *
1188 */
1189 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1190 &crt->valid_to ) ) != 0 )
1191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193 return( ret );
1194 }
1195
1196 /*
1197 * subject Name
1198 */
1199 crt->subject_raw.p = p;
1200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1202 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001205 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206 }
1207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211 return( ret );
1212 }
1213
1214 crt->subject_raw.len = p - crt->subject_raw.p;
1215
1216 /*
1217 * SubjectPublicKeyInfo
1218 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001219 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001223 return( ret );
1224 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001225 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226
1227 /*
1228 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1229 * -- If present, version shall be v2 or v3
1230 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1231 * -- If present, version shall be v2 or v3
1232 * extensions [3] EXPLICIT Extensions OPTIONAL
1233 * -- If present, version shall be v3
1234 */
1235 if( crt->version == 2 || crt->version == 3 )
1236 {
1237 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1238 if( ret != 0 )
1239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 return( ret );
1242 }
1243 }
1244
1245 if( crt->version == 2 || crt->version == 3 )
1246 {
1247 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1248 if( ret != 0 )
1249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 return( ret );
1252 }
1253 }
1254
1255 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001256 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001257 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258 if( ret != 0 )
1259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261 return( ret );
1262 }
1263 }
1264
1265 if( p != end )
1266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001268 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1269 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001270 }
1271
1272 end = crt_end;
1273
1274 /*
1275 * }
1276 * -- end of TBSCertificate
1277 *
1278 * signatureAlgorithm AlgorithmIdentifier,
1279 * signatureValue BIT STRING
1280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 return( ret );
1285 }
1286
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001287 if( crt->sig_oid.len != sig_oid2.len ||
1288 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001289 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001290 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001291 ( sig_params1.len != 0 &&
1292 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_x509_crt_free( crt );
1295 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296 }
1297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 return( ret );
1302 }
1303
1304 if( p != end )
1305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001307 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1308 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 }
1310
1311 return( 0 );
1312}
1313
1314/*
1315 * Parse one X.509 certificate in DER format from a buffer and add them to a
1316 * chained list
1317 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001318static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1319 const unsigned char *buf,
1320 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001321 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001322 mbedtls_x509_crt_ext_cb_t cb,
1323 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001324{
Janos Follath865b3eb2019-12-16 11:46:15 +00001325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327
1328 /*
1329 * Check for valid input
1330 */
1331 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001333
1334 while( crt->version != 0 && crt->next != NULL )
1335 {
1336 prev = crt;
1337 crt = crt->next;
1338 }
1339
1340 /*
1341 * Add new certificate on the end of the chain if needed.
1342 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001343 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001345 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346
1347 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001348 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349
1350 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353 }
1354
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001355 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001356 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357 {
1358 if( prev )
1359 prev->next = NULL;
1360
1361 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363
1364 return( ret );
1365 }
1366
1367 return( 0 );
1368}
1369
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001370int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1371 const unsigned char *buf,
1372 size_t buflen )
1373{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001374 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001375}
1376
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001377int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1378 const unsigned char *buf,
1379 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001380 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001381 mbedtls_x509_crt_ext_cb_t cb,
1382 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001383{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001384 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001385}
1386
1387int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1388 const unsigned char *buf,
1389 size_t buflen )
1390{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001391 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001392}
1393
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001395 * Parse one or more PEM certificates from a buffer and add them to the chained
1396 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001397 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001398int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1399 const unsigned char *buf,
1400 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001401{
Janos Follath98e28a72016-05-31 14:03:54 +01001402#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001403 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001405#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406
1407 /*
1408 * Check for valid input
1409 */
1410 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412
1413 /*
1414 * Determine buffer content. Buffer contains either one DER certificate or
1415 * one or more PEM certificates.
1416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001418 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001419 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001422 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1425 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001426#else
1427 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1428#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_PEM_PARSE_C)
1431 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001436 /* 1 rather than 0 since the terminating NULL byte is counted in */
1437 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438 {
1439 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001442 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444 "-----BEGIN CERTIFICATE-----",
1445 "-----END CERTIFICATE-----",
1446 buf, NULL, 0, &use_len );
1447
1448 if( ret == 0 )
1449 {
1450 /*
1451 * Was PEM encoded
1452 */
1453 buflen -= use_len;
1454 buf += use_len;
1455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457 {
1458 return( ret );
1459 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463
1464 /*
1465 * PEM header and footer were found
1466 */
1467 buflen -= use_len;
1468 buf += use_len;
1469
1470 if( first_error == 0 )
1471 first_error = ret;
1472
Paul Bakker5a5fa922014-09-26 14:53:04 +02001473 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474 continue;
1475 }
1476 else
1477 break;
1478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
1483 if( ret != 0 )
1484 {
1485 /*
1486 * Quit parsing on a memory error
1487 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001488 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489 return( ret );
1490
1491 if( first_error == 0 )
1492 first_error = ret;
1493
1494 total_failed++;
1495 continue;
1496 }
1497
1498 success = 1;
1499 }
1500 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501
1502 if( success )
1503 return( total_failed );
1504 else if( first_error )
1505 return( first_error );
1506 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001508#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509}
1510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512/*
1513 * Load one or more certificates and add them to the chained list
1514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516{
Janos Follath865b3eb2019-12-16 11:46:15 +00001517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518 size_t n;
1519 unsigned char *buf;
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522 return( ret );
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001526 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528
1529 return( ret );
1530}
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533{
1534 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001535#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536 int w_ret;
1537 WCHAR szDir[MAX_PATH];
1538 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001539 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001540 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541
Paul Bakker9af723c2014-05-01 13:03:14 +02001542 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543 HANDLE hFind;
1544
1545 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
Paul Bakker9af723c2014-05-01 13:03:14 +02001548 memset( szDir, 0, sizeof(szDir) );
1549 memset( filename, 0, MAX_PATH );
1550 memcpy( filename, path, len );
1551 filename[len++] = '\\';
1552 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553 filename[len++] = '*';
1554
Simon B3c6b18d2016-11-03 01:11:37 +00001555 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001556 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001557 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
1560 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001561 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563
1564 len = MAX_PATH - len;
1565 do
1566 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001567 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568
1569 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1570 continue;
1571
Paul Bakker9af723c2014-05-01 13:03:14 +02001572 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001573 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001574 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001575 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001576 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001577 {
1578 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1579 goto cleanup;
1580 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583 if( w_ret < 0 )
1584 ret++;
1585 else
1586 ret += w_ret;
1587 }
1588 while( FindNextFileW( hFind, &file_data ) != 0 );
1589
Paul Bakker66d5d072014-06-17 16:39:18 +02001590 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592
Ron Eldor36d90422017-01-09 15:09:16 +02001593cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001595#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001596 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001597 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001599 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001600 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601 DIR *dir = opendir( path );
1602
Paul Bakker66d5d072014-06-17 16:39:18 +02001603 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605
Ron Eldor63140682017-01-09 19:27:59 +02001606#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001607 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001608 {
1609 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001610 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001611 }
Ron Eldor63140682017-01-09 19:27:59 +02001612#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001613
Paul Elliottfb91a482021-03-05 14:17:51 +00001614 memset( &sb, 0, sizeof( sb ) );
1615
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001616 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 {
Andres AGf9113192016-09-02 14:06:04 +01001618 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1619 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620
Andres AGf9113192016-09-02 14:06:04 +01001621 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622 {
Andres AGf9113192016-09-02 14:06:04 +01001623 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1624 goto cleanup;
1625 }
1626 else if( stat( entry_name, &sb ) == -1 )
1627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001629 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630 }
1631
1632 if( !S_ISREG( sb.st_mode ) )
1633 continue;
1634
1635 // Ignore parse errors
1636 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638 if( t_ret < 0 )
1639 ret++;
1640 else
1641 ret += t_ret;
1642 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001643
1644cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001645 closedir( dir );
1646
Ron Eldor63140682017-01-09 19:27:59 +02001647#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001648 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001650#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001651
Paul Bakkerbe089b02013-10-14 15:51:50 +02001652#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653
1654 return( ret );
1655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001658/*
1659 * OtherName ::= SEQUENCE {
1660 * type-id OBJECT IDENTIFIER,
1661 * value [0] EXPLICIT ANY DEFINED BY type-id }
1662 *
1663 * HardwareModuleName ::= SEQUENCE {
1664 * hwType OBJECT IDENTIFIER,
1665 * hwSerialNum OCTET STRING }
1666 *
1667 * NOTE: we currently only parse and use otherName of type HwModuleName,
1668 * as defined in RFC 4108.
1669 */
1670static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1671 mbedtls_x509_san_other_name *other_name )
1672{
Janos Follathab23cd12019-05-09 13:53:57 +01001673 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001674 size_t len;
1675 unsigned char *p = subject_alt_name->p;
1676 const unsigned char *end = p + subject_alt_name->len;
1677 mbedtls_x509_buf cur_oid;
1678
1679 if( ( subject_alt_name->tag &
1680 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1681 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1682 {
1683 /*
1684 * The given subject alternative name is not of type "othername".
1685 */
1686 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1687 }
1688
1689 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1690 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001691 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001692
1693 cur_oid.tag = MBEDTLS_ASN1_OID;
1694 cur_oid.p = p;
1695 cur_oid.len = len;
1696
1697 /*
1698 * Only HwModuleName is currently supported.
1699 */
1700 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1701 {
1702 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1703 }
1704
Ron Eldor890819a2019-05-13 19:03:04 +03001705 if( p + len >= end )
1706 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001707 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001708 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1709 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001710 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001711 p += len;
1712 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1713 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001714 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001715
1716 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1717 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001718 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001719
1720 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001721 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001722
1723 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1724 other_name->value.hardware_module_name.oid.p = p;
1725 other_name->value.hardware_module_name.oid.len = len;
1726
Ron Eldor890819a2019-05-13 19:03:04 +03001727 if( p + len >= end )
1728 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001729 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001730 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1731 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001732 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001733 p += len;
1734 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1735 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001736 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001737
1738 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1739 other_name->value.hardware_module_name.val.p = p;
1740 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001741 p += len;
1742 if( p != end )
1743 {
Ron Eldor890819a2019-05-13 19:03:04 +03001744 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001745 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001746 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1747 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001748 }
1749 return( 0 );
1750}
1751
Peter Kolbus9a969b62018-12-11 13:55:56 -06001752int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1753 mbedtls_x509_subject_alternative_name *san )
1754{
1755 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1756 switch( san_buf->tag &
1757 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1758 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1759 {
1760 /*
1761 * otherName
1762 */
1763 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1764 {
1765 mbedtls_x509_san_other_name other_name;
1766
1767 ret = x509_get_other_name( san_buf, &other_name );
1768 if( ret != 0 )
1769 return( ret );
1770
1771 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1772 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1773 memcpy( &san->san.other_name,
1774 &other_name, sizeof( other_name ) );
1775
1776 }
1777 break;
1778
1779 /*
1780 * dNSName
1781 */
1782 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1783 {
1784 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1785 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1786
1787 memcpy( &san->san.unstructured_name,
1788 san_buf, sizeof( *san_buf ) );
1789
1790 }
1791 break;
1792
1793 /*
1794 * Type not supported
1795 */
1796 default:
1797 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1798 }
1799 return( 0 );
1800}
1801
1802#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001803static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001804 const mbedtls_x509_sequence
1805 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001806 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001807{
Janos Follath865b3eb2019-12-16 11:46:15 +00001808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001809 size_t n = *size;
1810 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001812 mbedtls_x509_subject_alternative_name san;
1813 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001814
1815 while( cur != NULL )
1816 {
Ron Eldor890819a2019-05-13 19:03:04 +03001817 memset( &san, 0, sizeof( san ) );
1818 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1819 if( parse_ret != 0 )
1820 {
1821 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1822 {
1823 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1824 MBEDTLS_X509_SAFE_SNPRINTF;
1825 }
1826 else
1827 {
1828 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1829 MBEDTLS_X509_SAFE_SNPRINTF;
1830 }
1831 cur = cur->next;
1832 continue;
1833 }
1834
1835 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001836 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001837 /*
1838 * otherName
1839 */
Ron Eldora2913912019-05-16 16:17:38 +03001840 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001841 {
1842 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1843
1844 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1845 MBEDTLS_X509_SAFE_SNPRINTF;
1846
1847 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1848 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001849 {
Ron Eldor890819a2019-05-13 19:03:04 +03001850 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1851 MBEDTLS_X509_SAFE_SNPRINTF;
1852 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001853 MBEDTLS_X509_SAFE_SNPRINTF;
1854
Ron Eldor890819a2019-05-13 19:03:04 +03001855 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1856 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001857
Ron Eldor890819a2019-05-13 19:03:04 +03001858 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1859 MBEDTLS_X509_SAFE_SNPRINTF;
1860
1861 if( other_name->value.hardware_module_name.val.len >= n )
1862 {
1863 *p = '\0';
1864 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001865 }
1866
Ron Eldora2913912019-05-16 16:17:38 +03001867 memcpy( p, other_name->value.hardware_module_name.val.p,
1868 other_name->value.hardware_module_name.val.len );
1869 p += other_name->value.hardware_module_name.val.len;
1870
Ron Eldor890819a2019-05-13 19:03:04 +03001871 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001872
Ron Eldor890819a2019-05-13 19:03:04 +03001873 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1874 }
1875 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001876
1877 /*
1878 * dNSName
1879 */
Ron Eldora2913912019-05-16 16:17:38 +03001880 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001881 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001882 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1883 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001884 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001885 {
1886 *p = '\0';
1887 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1888 }
Ron Eldora2913912019-05-16 16:17:38 +03001889
1890 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1891 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001892 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001893 }
1894 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001895
1896 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001897 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001898 */
1899 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001900 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1901 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001902 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001903 }
1904
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001905 cur = cur->next;
1906 }
1907
1908 *p = '\0';
1909
1910 *size = n;
1911 *buf = p;
1912
1913 return( 0 );
1914}
1915
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001916#define PRINT_ITEM(i) \
1917 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001919 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001920 sep = ", "; \
1921 }
1922
1923#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001924 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001925 PRINT_ITEM( name );
1926
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001927static int x509_info_cert_type( char **buf, size_t *size,
1928 unsigned char ns_cert_type )
1929{
Janos Follath865b3eb2019-12-16 11:46:15 +00001930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001931 size_t n = *size;
1932 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001933 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001936 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1938 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1939 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001940 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1941 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1942 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001943
1944 *size = n;
1945 *buf = p;
1946
1947 return( 0 );
1948}
1949
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001950#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001951 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001952 PRINT_ITEM( name );
1953
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001954static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001955 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001956{
Janos Follath865b3eb2019-12-16 11:46:15 +00001957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001958 size_t n = *size;
1959 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001960 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1963 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001964 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1965 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1966 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1968 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001969 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1970 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001971
1972 *size = n;
1973 *buf = p;
1974
1975 return( 0 );
1976}
1977
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001978static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001980{
Janos Follath865b3eb2019-12-16 11:46:15 +00001981 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001982 const char *desc;
1983 size_t n = *size;
1984 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001986 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001987
1988 while( cur != NULL )
1989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001991 desc = "???";
1992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001994 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001995
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001996 sep = ", ";
1997
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001998 cur = cur->next;
1999 }
2000
2001 *size = n;
2002 *buf = p;
2003
2004 return( 0 );
2005}
2006
Ron Eldor74d9acc2019-03-21 14:00:03 +02002007static int x509_info_cert_policies( char **buf, size_t *size,
2008 const mbedtls_x509_sequence *certificate_policies )
2009{
Janos Follath865b3eb2019-12-16 11:46:15 +00002010 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002011 const char *desc;
2012 size_t n = *size;
2013 char *p = *buf;
2014 const mbedtls_x509_sequence *cur = certificate_policies;
2015 const char *sep = "";
2016
2017 while( cur != NULL )
2018 {
2019 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2020 desc = "???";
2021
2022 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2023 MBEDTLS_X509_SAFE_SNPRINTF;
2024
2025 sep = ", ";
2026
2027 cur = cur->next;
2028 }
2029
2030 *size = n;
2031 *buf = p;
2032
2033 return( 0 );
2034}
2035
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002036/*
2037 * Return an informational string about the certificate.
2038 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002039#define BEFORE_COLON 18
2040#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2042 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043{
Janos Follath865b3eb2019-12-16 11:46:15 +00002044 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002045 size_t n;
2046 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002047 char key_size_str[BEFORE_COLON];
2048
2049 p = buf;
2050 n = size;
2051
Janos Follath98e28a72016-05-31 14:03:54 +01002052 if( NULL == crt )
2053 {
2054 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2055 MBEDTLS_X509_SAFE_SNPRINTF;
2056
2057 return( (int) ( size - n ) );
2058 }
2059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002062 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002064 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002065 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002068 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002071 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002073 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002076 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002078 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002081 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2082 crt->valid_from.year, crt->valid_from.mon,
2083 crt->valid_from.day, crt->valid_from.hour,
2084 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002085 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2089 crt->valid_to.year, crt->valid_to.mon,
2090 crt->valid_to.day, crt->valid_to.hour,
2091 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002092 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002095 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002098 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002099 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002100
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002101 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2103 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104 {
2105 return( ret );
2106 }
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002109 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002110 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002112 /*
2113 * Optional extensions
2114 */
2115
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002116 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002119 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002120 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002121
2122 if( crt->max_pathlen > 0 )
2123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002125 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002126 }
2127 }
2128
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002129 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002130 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002131 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002132 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002133
2134 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002135 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002136 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002137 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002138 }
2139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002143 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002144
2145 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2146 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002147 }
2148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002152 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002153
2154 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2155 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002156 }
2157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002161 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002162
2163 if( ( ret = x509_info_ext_key_usage( &p, &n,
2164 &crt->ext_key_usage ) ) != 0 )
2165 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002166 }
2167
Ron Eldor74d9acc2019-03-21 14:00:03 +02002168 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2169 {
2170 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2171 MBEDTLS_X509_SAFE_SNPRINTF;
2172
2173 if( ( ret = x509_info_cert_policies( &p, &n,
2174 &crt->certificate_policies ) ) != 0 )
2175 return( ret );
2176 }
2177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002179 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002180
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002181 return( (int) ( size - n ) );
2182}
2183
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002184struct x509_crt_verify_string {
2185 int code;
2186 const char *string;
2187};
2188
Hanno Becker7ac83f92020-10-09 10:48:22 +01002189#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002190static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002191 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002192 { 0, NULL }
2193};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002194#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002195
2196int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002197 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002198{
Janos Follath865b3eb2019-12-16 11:46:15 +00002199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002200 const struct x509_crt_verify_string *cur;
2201 char *p = buf;
2202 size_t n = size;
2203
2204 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2205 {
2206 if( ( flags & cur->code ) == 0 )
2207 continue;
2208
2209 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002210 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002211 flags ^= cur->code;
2212 }
2213
2214 if( flags != 0 )
2215 {
2216 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2217 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002218 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002219 }
2220
2221 return( (int) ( size - n ) );
2222}
Hanno Becker612a2f12020-10-09 09:19:39 +01002223#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002224
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002225int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2226 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002227{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002228 unsigned int usage_must, usage_may;
2229 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2230 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2231
2232 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2233 return( 0 );
2234
2235 usage_must = usage & ~may_mask;
2236
2237 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2238 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2239
2240 usage_may = usage & may_mask;
2241
2242 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002244
2245 return( 0 );
2246}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002249 const char *usage_oid,
2250 size_t usage_len )
2251{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002253
2254 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002256 return( 0 );
2257
2258 /*
2259 * Look for the requested usage (or wildcard ANY) in our list
2260 */
2261 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002264
2265 if( cur_oid->len == usage_len &&
2266 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2267 {
2268 return( 0 );
2269 }
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002272 return( 0 );
2273 }
2274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002276}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002279/*
2280 * Return 1 if the certificate is revoked, or 0 otherwise.
2281 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002282int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002283{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002285
2286 while( cur != NULL && cur->serial.len != 0 )
2287 {
2288 if( crt->serial.len == cur->serial.len &&
2289 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2290 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002291 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002292 }
2293
2294 cur = cur->next;
2295 }
2296
2297 return( 0 );
2298}
2299
2300/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002301 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002302 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002305 mbedtls_x509_crl *crl_list,
2306 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002307{
2308 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2310 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002311
2312 if( ca == NULL )
2313 return( flags );
2314
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002315 while( crl_list != NULL )
2316 {
2317 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002318 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002319 {
2320 crl_list = crl_list->next;
2321 continue;
2322 }
2323
2324 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002325 * Check if the CA is configured to sign CRLs
2326 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002327 if( mbedtls_x509_crt_check_key_usage( ca,
2328 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002331 break;
2332 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002333
2334 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335 * Check if CRL is correctly signed by the trusted CA
2336 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002337 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2338 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2339
2340 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2341 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002344 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002346 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002348 break;
2349 }
2350
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002351 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002352 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2355 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002356 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002359 break;
2360 }
2361
2362 /*
2363 * Check for validity of CRL (Do not drop out)
2364 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002365 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002367
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002368 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002369 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002370
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371 /*
2372 * Check if certificate is revoked
2373 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002374 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002377 break;
2378 }
2379
2380 crl_list = crl_list->next;
2381 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002382
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002383 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002386
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002387/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002388 * Check the signature of a certificate by its parent
2389 */
2390static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002391 mbedtls_x509_crt *parent,
2392 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002393{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002394 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002395 size_t hash_len;
2396#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2397 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002398 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002399 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002400
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002401 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002402 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002403 return( -1 );
2404#else
Jaeden Amero34973232019-02-20 10:32:28 +00002405 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002406 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2407
2408 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2409 return( -1 );
2410
2411 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2412 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002413 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002414 return( -1 );
2415 }
2416
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002417 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2418 != PSA_SUCCESS )
2419 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002420 return( -1 );
2421 }
2422#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002423 /* Skip expensive computation on obvious mismatch */
2424 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002425 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002426
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002427#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002428 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2429 {
2430 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002431 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002432 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002433 }
2434#else
2435 (void) rs_ctx;
2436#endif
2437
2438 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002439 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002440 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002441}
2442
2443/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002444 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2445 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002446 *
2447 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2450 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002451 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002452{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002453 int need_ca_bit;
2454
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002455 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002456 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002457 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002458
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002459 /* Parent must have the basicConstraints CA bit set as a general rule */
2460 need_ca_bit = 1;
2461
2462 /* Exception: v1/v2 certificates that are locally trusted. */
2463 if( top && parent->version < 3 )
2464 need_ca_bit = 0;
2465
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002466 if( need_ca_bit && ! parent->ca_istrue )
2467 return( -1 );
2468
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002469 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002471 {
2472 return( -1 );
2473 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002474
2475 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002476}
2477
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002478/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002479 * Find a suitable parent for child in candidates, or return NULL.
2480 *
2481 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002482 * 1. subject name matches child's issuer
2483 * 2. if necessary, the CA bit is set and key usage allows signing certs
2484 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002485 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002486 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002487 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002488 * If there's a suitable candidate which is also time-valid, return the first
2489 * such. Otherwise, return the first suitable candidate (or NULL if there is
2490 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002491 *
2492 * The rationale for this rule is that someone could have a list of trusted
2493 * roots with two versions on the same root with different validity periods.
2494 * (At least one user reported having such a list and wanted it to just work.)
2495 * The reason we don't just require time-validity is that generally there is
2496 * only one version, and if it's expired we want the flags to state that
2497 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002498 *
2499 * The rationale for rule 3 (signature for trusted roots) is that users might
2500 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002501 * way we select the correct one is by checking the signature (as we don't
2502 * rely on key identifier extensions). (This is one way users might choose to
2503 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002504 *
2505 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002506 * - [in] child: certificate for which we're looking for a parent
2507 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002508 * - [out] r_parent: parent found (or NULL)
2509 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002510 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2511 * of the chain, 0 otherwise
2512 * - [in] path_cnt: number of intermediates seen so far
2513 * - [in] self_cnt: number of self-signed intermediates seen so far
2514 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002515 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002516 *
2517 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002518 * - 0 on success
2519 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002520 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002521static int x509_crt_find_parent_in(
2522 mbedtls_x509_crt *child,
2523 mbedtls_x509_crt *candidates,
2524 mbedtls_x509_crt **r_parent,
2525 int *r_signature_is_good,
2526 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002527 unsigned path_cnt,
2528 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002529 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002530{
Janos Follath865b3eb2019-12-16 11:46:15 +00002531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002532 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002533 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002534
2535#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002536 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002537 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2538 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002539 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002540 parent = rs_ctx->parent;
2541 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002542 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002543
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002544 /* clear saved state */
2545 rs_ctx->parent = NULL;
2546 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002547 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002548
2549 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002550 goto check_signature;
2551 }
2552#endif
2553
2554 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002555 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002556
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002557 for( parent = candidates; parent != NULL; parent = parent->next )
2558 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002559 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002560 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002561 continue;
2562
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002563 /* +1 because stored max_pathlen is 1 higher that the actual value */
2564 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002565 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002566 {
2567 continue;
2568 }
2569
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002570 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002571#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2572check_signature:
2573#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002574 ret = x509_crt_check_signature( child, parent, rs_ctx );
2575
2576#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002577 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2578 {
2579 /* save state */
2580 rs_ctx->parent = parent;
2581 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002582 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002583
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002584 return( ret );
2585 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002586#else
2587 (void) ret;
2588#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002589
2590 signature_is_good = ret == 0;
2591 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002592 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002593
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002594 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002595 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2596 mbedtls_x509_time_is_future( &parent->valid_from ) )
2597 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002598 if( fallback_parent == NULL )
2599 {
2600 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002601 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002602 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002603
2604 continue;
2605 }
2606
Andy Gross1f627142019-01-30 10:25:53 -06002607 *r_parent = parent;
2608 *r_signature_is_good = signature_is_good;
2609
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002610 break;
2611 }
2612
Andy Gross1f627142019-01-30 10:25:53 -06002613 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002614 {
2615 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002616 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002617 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002618
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002619 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002620}
2621
2622/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002623 * Find a parent in trusted CAs or the provided chain, or return NULL.
2624 *
2625 * Searches in trusted CAs first, and return the first suitable parent found
2626 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002627 *
2628 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002629 * - [in] child: certificate for which we're looking for a parent, followed
2630 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002631 * - [in] trust_ca: list of locally trusted certificates
2632 * - [out] parent: parent found (or NULL)
2633 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2634 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2635 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2636 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002637 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002638 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002639 *
2640 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002641 * - 0 on success
2642 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002643 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002644static int x509_crt_find_parent(
2645 mbedtls_x509_crt *child,
2646 mbedtls_x509_crt *trust_ca,
2647 mbedtls_x509_crt **parent,
2648 int *parent_is_trusted,
2649 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002650 unsigned path_cnt,
2651 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002652 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002653{
Janos Follath865b3eb2019-12-16 11:46:15 +00002654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002655 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002656
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002657 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002658
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002659#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002660 /* restore then clear saved state if we have some stored */
2661 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2662 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002663 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002664 rs_ctx->parent_is_trusted = -1;
2665 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002666#endif
2667
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002668 while( 1 ) {
2669 search_list = *parent_is_trusted ? trust_ca : child->next;
2670
2671 ret = x509_crt_find_parent_in( child, search_list,
2672 parent, signature_is_good,
2673 *parent_is_trusted,
2674 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002675
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002676#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002677 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2678 {
2679 /* save state */
2680 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002681 return( ret );
2682 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002683#else
2684 (void) ret;
2685#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002686
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002687 /* stop here if found or already in second iteration */
2688 if( *parent != NULL || *parent_is_trusted == 0 )
2689 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002690
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002691 /* prepare second iteration */
2692 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002693 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002694
2695 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002696 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002697 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002698 *parent_is_trusted = 0;
2699 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002700 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002701
2702 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002703}
2704
2705/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002706 * Check if an end-entity certificate is locally trusted
2707 *
2708 * Currently we require such certificates to be self-signed (actually only
2709 * check for self-issued as self-signatures are not checked)
2710 */
2711static int x509_crt_check_ee_locally_trusted(
2712 mbedtls_x509_crt *crt,
2713 mbedtls_x509_crt *trust_ca )
2714{
2715 mbedtls_x509_crt *cur;
2716
2717 /* must be self-issued */
2718 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2719 return( -1 );
2720
2721 /* look for an exact match with trusted cert */
2722 for( cur = trust_ca; cur != NULL; cur = cur->next )
2723 {
2724 if( crt->raw.len == cur->raw.len &&
2725 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2726 {
2727 return( 0 );
2728 }
2729 }
2730
2731 /* too bad */
2732 return( -1 );
2733}
2734
2735/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002736 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002737 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002738 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2739 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002740 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002741 * such that every cert in the chain is a child of the next one,
2742 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002743 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002744 * Verify that chain and return it with flags for all issues found.
2745 *
2746 * Special cases:
2747 * - EE == Rj -> return a one-element list containing it
2748 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2749 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002750 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002751 * Tests for (aspects of) this function should include at least:
2752 * - trusted EE
2753 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002754 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002755 * - if relevant: EE untrusted
2756 * - if relevant: EE -> intermediate, untrusted
2757 * with the aspect under test checked at each relevant level (EE, int, root).
2758 * For some aspects longer chains are required, but usually length 2 is
2759 * enough (but length 1 is not in general).
2760 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002761 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002762 * - [in] crt: the cert list EE, C1, ..., Cn
2763 * - [in] trust_ca: the trusted list R1, ..., Rp
2764 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002765 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002766 * Only valid when return value is 0, may contain garbage otherwise!
2767 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002768 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002769 *
2770 * Return value:
2771 * - non-zero if the chain could not be fully built and examined
2772 * - 0 is the chain was successfully built and examined,
2773 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002774 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002775static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002776 mbedtls_x509_crt *crt,
2777 mbedtls_x509_crt *trust_ca,
2778 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002779 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2780 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002781 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002782 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002783 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002784{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002785 /* Don't initialize any of those variables here, so that the compiler can
2786 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002788 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002789 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002790 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002791 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002792 int parent_is_trusted;
2793 int child_is_trusted;
2794 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002795 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002796 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002797
2798#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2799 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002800 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002801 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002802 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002803 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002804 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002805
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002806 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002807 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002808 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002809 flags = &cur->flags;
2810
2811 goto find_parent;
2812 }
2813#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002814
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002815 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002816 self_cnt = 0;
2817 parent_is_trusted = 0;
2818 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002819
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002820 while( 1 ) {
2821 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002822 cur = &ver_chain->items[ver_chain->len];
2823 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002824 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002825 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002826 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002827
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002828 /* Check time-validity (all certificates) */
2829 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2830 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002831
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002832 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2833 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002834
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002835 /* Stop here for trusted roots (but not for trusted EE certs) */
2836 if( child_is_trusted )
2837 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002838
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002839 /* 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;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002842
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002843 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2844 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002845
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002846 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002847 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002848 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2849 {
2850 return( 0 );
2851 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002852
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002853#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2854find_parent:
2855#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002856
2857 /* Obtain list of potential trusted signers from CA callback,
2858 * or use statically provided list. */
2859#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2860 if( f_ca_cb != NULL )
2861 {
2862 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2863 mbedtls_free( ver_chain->trust_ca_cb_result );
2864 ver_chain->trust_ca_cb_result = NULL;
2865
2866 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2867 if( ret != 0 )
2868 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2869
2870 cur_trust_ca = ver_chain->trust_ca_cb_result;
2871 }
2872 else
2873#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2874 {
2875 ((void) f_ca_cb);
2876 ((void) p_ca_cb);
2877 cur_trust_ca = trust_ca;
2878 }
2879
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002880 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002881 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002882 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002883 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002884
2885#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002886 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2887 {
2888 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002889 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002890 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002891 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002892
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002893 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002894 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002895#else
2896 (void) ret;
2897#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002898
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002899 /* No parent? We're done here */
2900 if( parent == NULL )
2901 {
2902 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2903 return( 0 );
2904 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002905
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002906 /* Count intermediate self-issued (not necessarily self-signed) certs.
2907 * These can occur with some strategies for key rollover, see [SIRO],
2908 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002909 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002910 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2911 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002912 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002913 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002914
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002915 /* path_cnt is 0 for the first intermediate CA,
2916 * and if parent is trusted it's not an intermediate CA */
2917 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002918 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002919 {
2920 /* return immediately to avoid overflow the chain array */
2921 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2922 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002923
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002924 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002925 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002926 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2927
2928 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002929 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002930 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002933 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002934 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002935#else
2936 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002937#endif
2938
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002939 /* prepare for next iteration */
2940 child = parent;
2941 parent = NULL;
2942 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002943 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002944 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002945}
2946
2947/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002948 * Check for CN match
2949 */
2950static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2951 const char *cn, size_t cn_len )
2952{
2953 /* try exact match */
2954 if( name->len == cn_len &&
2955 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2956 {
2957 return( 0 );
2958 }
2959
2960 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002961 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002962 {
2963 return( 0 );
2964 }
2965
2966 return( -1 );
2967}
2968
2969/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002970 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2971 */
2972static int x509_crt_check_san( const mbedtls_x509_buf *name,
2973 const char *cn, size_t cn_len )
2974{
2975 const unsigned char san_type = (unsigned char) name->tag &
2976 MBEDTLS_ASN1_TAG_VALUE_MASK;
2977
2978 /* dNSName */
2979 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
2980 return( x509_crt_check_cn( name, cn, cn_len ) );
2981
2982 /* (We may handle other types here later.) */
2983
2984 /* Unrecognized type */
2985 return( -1 );
2986}
2987
2988/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002989 * Verify the requested CN - only call this if cn is not NULL!
2990 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002991static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002992 const char *cn,
2993 uint32_t *flags )
2994{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002995 const mbedtls_x509_name *name;
2996 const mbedtls_x509_sequence *cur;
2997 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002998
2999 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3000 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003001 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003002 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003003 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003004 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003005 }
3006
3007 if( cur == NULL )
3008 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3009 }
3010 else
3011 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003012 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003013 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003014 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3015 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003016 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003017 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003018 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003019 }
3020
3021 if( name == NULL )
3022 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3023 }
3024}
3025
3026/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003027 * Merge the flags for all certs in the chain, after calling callback
3028 */
3029static int x509_crt_merge_flags_with_cb(
3030 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003031 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003032 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3033 void *p_vrfy )
3034{
Janos Follath865b3eb2019-12-16 11:46:15 +00003035 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003036 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003037 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003038 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003039
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003040 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003041 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003042 cur = &ver_chain->items[i-1];
3043 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003044
3045 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003046 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003047 return( ret );
3048
3049 *flags |= cur_flags;
3050 }
3051
3052 return( 0 );
3053}
3054
3055/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003056 * Verify the certificate validity, with profile, restartable version
3057 *
3058 * This function:
3059 * - checks the requested CN (if any)
3060 * - checks the type and size of the EE cert's key,
3061 * as that isn't done as part of chain building/verification currently
3062 * - builds and verifies the chain
3063 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003064 *
3065 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3066 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3067 * verification routine to search for trusted signers, and CRLs will
3068 * be disabled. Otherwise, `trust_ca` will be used as the static list
3069 * of trusted signers, and `ca_crl` will be use as the static list
3070 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003071 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003072static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003073 mbedtls_x509_crt *trust_ca,
3074 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003075 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3076 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003077 const mbedtls_x509_crt_profile *profile,
3078 const char *cn, uint32_t *flags,
3079 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3080 void *p_vrfy,
3081 mbedtls_x509_crt_restart_ctx *rs_ctx )
3082{
Janos Follath865b3eb2019-12-16 11:46:15 +00003083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003084 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003085 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003086 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003087
3088 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003089 ee_flags = 0;
3090 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003091
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003092 if( profile == NULL )
3093 {
3094 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3095 goto exit;
3096 }
3097
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003098 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003099 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003100 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003101
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003102 /* Check the type and size of the key */
3103 pk_type = mbedtls_pk_get_type( &crt->pk );
3104
3105 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003106 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003107
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003108 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003109 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003110
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003111 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003112 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3113 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003114 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003115
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003116 if( ret != 0 )
3117 goto exit;
3118
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003119 /* Merge end-entity flags */
3120 ver_chain.items[0].flags |= ee_flags;
3121
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003122 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003123 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003124
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003125exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003126
3127#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3128 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3129 mbedtls_free( ver_chain.trust_ca_cb_result );
3130 ver_chain.trust_ca_cb_result = NULL;
3131#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3132
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003133#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3134 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3135 mbedtls_x509_crt_restart_free( rs_ctx );
3136#endif
3137
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003138 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3139 * the SSL module for authmode optional, but non-zero return from the
3140 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003141 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3142 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3143
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003144 if( ret != 0 )
3145 {
3146 *flags = (uint32_t) -1;
3147 return( ret );
3148 }
3149
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003150 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003152
3153 return( 0 );
3154}
3155
Hanno Becker3116fb32019-03-28 13:34:42 +00003156
3157/*
3158 * Verify the certificate validity (default profile, not restartable)
3159 */
3160int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3161 mbedtls_x509_crt *trust_ca,
3162 mbedtls_x509_crl *ca_crl,
3163 const char *cn, uint32_t *flags,
3164 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3165 void *p_vrfy )
3166{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003167 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003168 NULL, NULL,
3169 &mbedtls_x509_crt_profile_default,
3170 cn, flags,
3171 f_vrfy, p_vrfy, NULL ) );
3172}
3173
3174/*
3175 * Verify the certificate validity (user-chosen profile, not restartable)
3176 */
3177int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3178 mbedtls_x509_crt *trust_ca,
3179 mbedtls_x509_crl *ca_crl,
3180 const mbedtls_x509_crt_profile *profile,
3181 const char *cn, uint32_t *flags,
3182 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3183 void *p_vrfy )
3184{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003185 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003186 NULL, NULL,
3187 profile, cn, flags,
3188 f_vrfy, p_vrfy, NULL ) );
3189}
3190
3191#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3192/*
3193 * Verify the certificate validity (user-chosen profile, CA callback,
3194 * not restartable).
3195 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003196int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003197 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3198 void *p_ca_cb,
3199 const mbedtls_x509_crt_profile *profile,
3200 const char *cn, uint32_t *flags,
3201 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3202 void *p_vrfy )
3203{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003204 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003205 f_ca_cb, p_ca_cb,
3206 profile, cn, flags,
3207 f_vrfy, p_vrfy, NULL ) );
3208}
3209#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3210
3211int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3212 mbedtls_x509_crt *trust_ca,
3213 mbedtls_x509_crl *ca_crl,
3214 const mbedtls_x509_crt_profile *profile,
3215 const char *cn, uint32_t *flags,
3216 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3217 void *p_vrfy,
3218 mbedtls_x509_crt_restart_ctx *rs_ctx )
3219{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003220 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003221 NULL, NULL,
3222 profile, cn, flags,
3223 f_vrfy, p_vrfy, rs_ctx ) );
3224}
3225
3226
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003227/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003228 * Initialize a certificate chain
3229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003233}
3234
3235/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003236 * Unallocate all certificate data
3237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 mbedtls_x509_crt *cert_cur = crt;
3241 mbedtls_x509_crt *cert_prv;
3242 mbedtls_x509_name *name_cur;
3243 mbedtls_x509_name *name_prv;
3244 mbedtls_x509_sequence *seq_cur;
3245 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003246
3247 if( crt == NULL )
3248 return;
3249
3250 do
3251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3255 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003256#endif
3257
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003258 name_cur = cert_cur->issuer.next;
3259 while( name_cur != NULL )
3260 {
3261 name_prv = name_cur;
3262 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003263 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003265 }
3266
3267 name_cur = cert_cur->subject.next;
3268 while( name_cur != NULL )
3269 {
3270 name_prv = name_cur;
3271 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003272 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003274 }
3275
3276 seq_cur = cert_cur->ext_key_usage.next;
3277 while( seq_cur != NULL )
3278 {
3279 seq_prv = seq_cur;
3280 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003281 mbedtls_platform_zeroize( seq_prv,
3282 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003284 }
3285
3286 seq_cur = cert_cur->subject_alt_names.next;
3287 while( seq_cur != NULL )
3288 {
3289 seq_prv = seq_cur;
3290 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003291 mbedtls_platform_zeroize( seq_prv,
3292 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003294 }
3295
Ron Eldor74d9acc2019-03-21 14:00:03 +02003296 seq_cur = cert_cur->certificate_policies.next;
3297 while( seq_cur != NULL )
3298 {
3299 seq_prv = seq_cur;
3300 seq_cur = seq_cur->next;
3301 mbedtls_platform_zeroize( seq_prv,
3302 sizeof( mbedtls_x509_sequence ) );
3303 mbedtls_free( seq_prv );
3304 }
3305
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003306 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003307 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003308 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003310 }
3311
3312 cert_cur = cert_cur->next;
3313 }
3314 while( cert_cur != NULL );
3315
3316 cert_cur = crt;
3317 do
3318 {
3319 cert_prv = cert_cur;
3320 cert_cur = cert_cur->next;
3321
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003322 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003323 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003325 }
3326 while( cert_cur != NULL );
3327}
3328
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003329#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3330/*
3331 * Initialize a restart context
3332 */
3333void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3334{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003335 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003336
3337 ctx->parent = NULL;
3338 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003339 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003340
3341 ctx->parent_is_trusted = -1;
3342
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003343 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003344 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003345 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003346}
3347
3348/*
3349 * Free the components of a restart context
3350 */
3351void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3352{
3353 if( ctx == NULL )
3354 return;
3355
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003356 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003357 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003358}
3359#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361#endif /* MBEDTLS_X509_CRT_PARSE_C */