blob: 59692476bcbb6fe9bad9691328d7350f2397eb81 [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{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200102 /* Hashes from SHA-256 and above */
103 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
106 0xFFFFFFF, /* Any PK alg */
107#if defined(MBEDTLS_ECP_C)
108 /* Curves at or above 128-bit security level */
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
116#else
117 0,
118#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200119 2048,
120};
121
Gilles Peskineffb92da2021-06-02 00:03:26 +0200122/* Next-generation profile. Currently identical to the default, but may
123 * be tightened at any time. */
124const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
125 mbedtls_x509_crt_profile_default;
126
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200127/*
128 * NSA Suite B Profile
129 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200130const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
131{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200132 /* Only SHA-256 and 384 */
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
135 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200136 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200138#if defined(MBEDTLS_ECP_C)
139 /* Only NIST P-256 and P-384 */
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
142#else
143 0,
144#endif
145 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200146};
147
148/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200149 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200150 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200151 */
152static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
153 mbedtls_md_type_t md_alg )
154{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200155 if( md_alg == MBEDTLS_MD_NONE )
156 return( -1 );
157
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200158 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
159 return( 0 );
160
161 return( -1 );
162}
163
164/*
165 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200166 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200167 */
168static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
169 mbedtls_pk_type_t pk_alg )
170{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200171 if( pk_alg == MBEDTLS_PK_NONE )
172 return( -1 );
173
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200174 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
175 return( 0 );
176
177 return( -1 );
178}
179
180/*
181 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200182 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183 */
184static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 const mbedtls_pk_context *pk )
186{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200187 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200188
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200189#if defined(MBEDTLS_RSA_C)
190 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
191 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200192 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200193 return( 0 );
194
195 return( -1 );
196 }
197#endif
198
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200199#if defined(MBEDTLS_ECP_C)
200 if( pk_alg == MBEDTLS_PK_ECDSA ||
201 pk_alg == MBEDTLS_PK_ECKEY ||
202 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200203 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200204 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200205
Philippe Antoineb5b25432018-05-11 11:06:29 +0200206 if( gid == MBEDTLS_ECP_DP_NONE )
207 return( -1 );
208
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200209 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
210 return( 0 );
211
212 return( -1 );
213 }
214#endif
215
216 return( -1 );
217}
218
219/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000220 * Like memcmp, but case-insensitive and always returns -1 if different
221 */
222static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
223{
224 size_t i;
225 unsigned char diff;
226 const unsigned char *n1 = s1, *n2 = s2;
227
228 for( i = 0; i < len; i++ )
229 {
230 diff = n1[i] ^ n2[i];
231
232 if( diff == 0 )
233 continue;
234
235 if( diff == 32 &&
236 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
237 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
238 {
239 continue;
240 }
241
242 return( -1 );
243 }
244
245 return( 0 );
246}
247
248/*
249 * Return 0 if name matches wildcard, -1 otherwise
250 */
251static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
252{
253 size_t i;
254 size_t cn_idx = 0, cn_len = strlen( cn );
255
256 /* We can't have a match if there is no wildcard to match */
257 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
258 return( -1 );
259
260 for( i = 0; i < cn_len; ++i )
261 {
262 if( cn[i] == '.' )
263 {
264 cn_idx = i;
265 break;
266 }
267 }
268
269 if( cn_idx == 0 )
270 return( -1 );
271
272 if( cn_len - cn_idx == name->len - 1 &&
273 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
274 {
275 return( 0 );
276 }
277
278 return( -1 );
279}
280
281/*
282 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
283 * variations (but not all).
284 *
285 * Return 0 if equal, -1 otherwise.
286 */
287static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
288{
289 if( a->tag == b->tag &&
290 a->len == b->len &&
291 memcmp( a->p, b->p, b->len ) == 0 )
292 {
293 return( 0 );
294 }
295
296 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
297 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
298 a->len == b->len &&
299 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
300 {
301 return( 0 );
302 }
303
304 return( -1 );
305}
306
307/*
308 * Compare two X.509 Names (aka rdnSequence).
309 *
310 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
311 * we sometimes return unequal when the full algorithm would return equal,
312 * but never the other way. (In particular, we don't do Unicode normalisation
313 * or space folding.)
314 *
315 * Return 0 if equal, -1 otherwise.
316 */
317static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
318{
319 /* Avoid recursion, it might not be optimised by the compiler */
320 while( a != NULL || b != NULL )
321 {
322 if( a == NULL || b == NULL )
323 return( -1 );
324
325 /* type */
326 if( a->oid.tag != b->oid.tag ||
327 a->oid.len != b->oid.len ||
328 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
329 {
330 return( -1 );
331 }
332
333 /* value */
334 if( x509_string_cmp( &a->val, &b->val ) != 0 )
335 return( -1 );
336
337 /* structure of the list of sets */
338 if( a->next_merged != b->next_merged )
339 return( -1 );
340
341 a = a->next;
342 b = b->next;
343 }
344
345 /* a == NULL == b */
346 return( 0 );
347}
348
349/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200350 * Reset (init or clear) a verify_chain
351 */
352static void x509_crt_verify_chain_reset(
353 mbedtls_x509_crt_verify_chain *ver_chain )
354{
355 size_t i;
356
357 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
358 {
359 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000360 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200361 }
362
363 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000364
365#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
366 ver_chain->trust_ca_cb_result = NULL;
367#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200368}
369
370/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
372 */
373static int x509_get_version( unsigned char **p,
374 const unsigned char *end,
375 int *ver )
376{
Janos Follath865b3eb2019-12-16 11:46:15 +0000377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378 size_t len;
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
381 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 {
385 *ver = 0;
386 return( 0 );
387 }
388
Chris Jones9f7a6932021-04-14 12:12:09 +0100389 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 }
391
392 end = *p + len;
393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100395 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396
397 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100398 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
399 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400
401 return( 0 );
402}
403
404/*
405 * Validity ::= SEQUENCE {
406 * notBefore Time,
407 * notAfter Time }
408 */
409static int x509_get_dates( unsigned char **p,
410 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_x509_time *from,
412 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413{
Janos Follath865b3eb2019-12-16 11:46:15 +0000414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 size_t len;
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
418 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100419 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
421 end = *p + len;
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 return( ret );
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427 return( ret );
428
429 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100430 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
431 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 return( 0 );
434}
435
436/*
437 * X.509 v2/v3 unique identifier (not parsed)
438 */
439static int x509_get_uid( unsigned char **p,
440 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442{
Janos Follath865b3eb2019-12-16 11:46:15 +0000443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445 if( *p == end )
446 return( 0 );
447
448 uid->tag = **p;
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
451 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 return( 0 );
455
Chris Jones9f7a6932021-04-14 12:12:09 +0100456 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457 }
458
459 uid->p = *p;
460 *p += uid->len;
461
462 return( 0 );
463}
464
465static int x509_get_basic_constraints( unsigned char **p,
466 const unsigned char *end,
467 int *ca_istrue,
468 int *max_pathlen )
469{
Janos Follath865b3eb2019-12-16 11:46:15 +0000470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 size_t len;
472
473 /*
474 * BasicConstraints ::= SEQUENCE {
475 * cA BOOLEAN DEFAULT FALSE,
476 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
477 */
478 *ca_istrue = 0; /* DEFAULT FALSE */
479 *max_pathlen = 0; /* endless */
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
482 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100483 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
485 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200486 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
491 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492
493 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100494 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495
496 if( *ca_istrue != 0 )
497 *ca_istrue = 1;
498 }
499
500 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200501 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100504 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
506 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100507 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
508 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
Andrzej Kurek16050742020-04-14 09:49:52 -0400510 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
511 * overflow, which is an undefined behavior. */
512 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100513 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
514 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400515
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516 (*max_pathlen)++;
517
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200518 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519}
520
521static int x509_get_ns_cert_type( unsigned char **p,
522 const unsigned char *end,
523 unsigned char *ns_cert_type)
524{
Janos Follath865b3eb2019-12-16 11:46:15 +0000525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100529 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
531 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100532 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
533 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534
535 /* Get actual bitstring */
536 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200537 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538}
539
540static int x509_get_key_usage( unsigned char **p,
541 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100542 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543{
Janos Follath865b3eb2019-12-16 11:46:15 +0000544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200545 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100549 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550
551 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100552 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
553 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
555 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200556 *key_usage = 0;
557 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
558 {
559 *key_usage |= (unsigned int) bs.p[i] << (8*i);
560 }
561
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200562 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563}
564
565/*
566 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
567 *
568 * KeyPurposeId ::= OBJECT IDENTIFIER
569 */
570static int x509_get_ext_key_usage( unsigned char **p,
571 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573{
Janos Follath865b3eb2019-12-16 11:46:15 +0000574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100577 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579 /* Sequence length must be >= 1 */
580 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100581 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
582 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200584 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585}
586
587/*
588 * SubjectAltName ::= GeneralNames
589 *
590 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
591 *
592 * GeneralName ::= CHOICE {
593 * otherName [0] OtherName,
594 * rfc822Name [1] IA5String,
595 * dNSName [2] IA5String,
596 * x400Address [3] ORAddress,
597 * directoryName [4] Name,
598 * ediPartyName [5] EDIPartyName,
599 * uniformResourceIdentifier [6] IA5String,
600 * iPAddress [7] OCTET STRING,
601 * registeredID [8] OBJECT IDENTIFIER }
602 *
603 * OtherName ::= SEQUENCE {
604 * type-id OBJECT IDENTIFIER,
605 * value [0] EXPLICIT ANY DEFINED BY type-id }
606 *
607 * EDIPartyName ::= SEQUENCE {
608 * nameAssigner [0] DirectoryString OPTIONAL,
609 * partyName [1] DirectoryString }
610 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300611 * NOTE: we list all types, but only use dNSName and otherName
612 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613 */
614static int x509_get_subject_alt_name( unsigned char **p,
615 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617{
Janos Follath865b3eb2019-12-16 11:46:15 +0000618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623
624 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
626 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100627 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628
629 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100630 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
631 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632
633 while( *p < end )
634 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300635 mbedtls_x509_subject_alternative_name dummy_san_buf;
636 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
637
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638 tag = **p;
639 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100641 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100643 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
644 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
645 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100646 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
647 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100648 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649
Ron Eldordbbd9662019-05-15 15:46:03 +0300650 /*
irwir74aee1c2019-09-21 18:21:48 +0300651 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300652 */
653 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
654 /*
655 * In case the extension is malformed, return an error,
656 * and clear the allocated sequences.
657 */
658 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
659 {
660 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
661 mbedtls_x509_sequence *seq_prv;
662 while( seq_cur != NULL )
663 {
664 seq_prv = seq_cur;
665 seq_cur = seq_cur->next;
666 mbedtls_platform_zeroize( seq_prv,
667 sizeof( mbedtls_x509_sequence ) );
668 mbedtls_free( seq_prv );
669 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300670 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300671 return( ret );
672 }
673
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200675 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100677 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100679
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200680 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100683 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
684 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 cur = cur->next;
687 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200688
689 buf = &(cur->buf);
690 buf->tag = tag;
691 buf->p = *p;
692 buf->len = tag_len;
693 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 }
695
696 /* Set final sequence entry's next pointer to NULL */
697 cur->next = NULL;
698
699 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100700 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
701 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
703 return( 0 );
704}
705
706/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200707 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
708 *
709 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
710 *
711 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
712 *
713 * PolicyInformation ::= SEQUENCE {
714 * policyIdentifier CertPolicyId,
715 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
716 * PolicyQualifierInfo OPTIONAL }
717 *
718 * CertPolicyId ::= OBJECT IDENTIFIER
719 *
720 * PolicyQualifierInfo ::= SEQUENCE {
721 * policyQualifierId PolicyQualifierId,
722 * qualifier ANY DEFINED BY policyQualifierId }
723 *
724 * -- policyQualifierIds for Internet policy qualifiers
725 *
726 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
727 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
728 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
729 *
730 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
731 *
732 * Qualifier ::= CHOICE {
733 * cPSuri CPSuri,
734 * userNotice UserNotice }
735 *
736 * CPSuri ::= IA5String
737 *
738 * UserNotice ::= SEQUENCE {
739 * noticeRef NoticeReference OPTIONAL,
740 * explicitText DisplayText OPTIONAL }
741 *
742 * NoticeReference ::= SEQUENCE {
743 * organization DisplayText,
744 * noticeNumbers SEQUENCE OF INTEGER }
745 *
746 * DisplayText ::= CHOICE {
747 * ia5String IA5String (SIZE (1..200)),
748 * visibleString VisibleString (SIZE (1..200)),
749 * bmpString BMPString (SIZE (1..200)),
750 * utf8String UTF8String (SIZE (1..200)) }
751 *
752 * NOTE: we only parse and use anyPolicy without qualifiers at this point
753 * as defined in RFC 5280.
754 */
755static int x509_get_certificate_policies( unsigned char **p,
756 const unsigned char *end,
757 mbedtls_x509_sequence *certificate_policies )
758{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300759 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200760 size_t len;
761 mbedtls_asn1_buf *buf;
762 mbedtls_asn1_sequence *cur = certificate_policies;
763
764 /* Get main sequence tag */
765 ret = mbedtls_asn1_get_tag( p, end, &len,
766 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
767 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100768 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200769
770 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100771 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
772 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200773
774 /*
775 * Cannot be an empty sequence.
776 */
777 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100778 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
779 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200780
781 while( *p < end )
782 {
783 mbedtls_x509_buf policy_oid;
784 const unsigned char *policy_end;
785
786 /*
787 * Get the policy sequence
788 */
789 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
790 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100791 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200792
793 policy_end = *p + len;
794
Ron Eldor08063792019-05-13 16:38:39 +0300795 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200796 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100797 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200798
799 policy_oid.tag = MBEDTLS_ASN1_OID;
800 policy_oid.len = len;
801 policy_oid.p = *p;
802
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300803 /*
804 * Only AnyPolicy is currently supported when enforcing policy.
805 */
806 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
807 {
808 /*
809 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200810 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300811 */
812 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
813 }
814
Ron Eldor74d9acc2019-03-21 14:00:03 +0200815 /* Allocate and assign next pointer */
816 if( cur->buf.p != NULL )
817 {
818 if( cur->next != NULL )
819 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
820
821 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
822
823 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100824 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
825 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200826
827 cur = cur->next;
828 }
829
830 buf = &( cur->buf );
831 buf->tag = policy_oid.tag;
832 buf->p = policy_oid.p;
833 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300834
835 *p += len;
836
837 /*
838 * If there is an optional qualifier, then *p < policy_end
839 * Check the Qualifier len to verify it doesn't exceed policy_end.
840 */
841 if( *p < policy_end )
842 {
843 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
844 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100845 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300846 /*
847 * Skip the optional policy qualifiers.
848 */
849 *p += len;
850 }
851
852 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100853 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
854 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200855 }
856
857 /* Set final sequence entry's next pointer to NULL */
858 cur->next = NULL;
859
860 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100861 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
862 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200863
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300864 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200865}
866
867/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 * X.509 v3 extensions
869 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 */
871static int x509_get_crt_ext( unsigned char **p,
872 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200873 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200874 mbedtls_x509_crt_ext_cb_t cb,
875 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876{
Janos Follath865b3eb2019-12-16 11:46:15 +0000877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200879 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880
Hanno Becker12f62fb2019-02-12 17:22:36 +0000881 if( *p == end )
882 return( 0 );
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886
Hanno Becker12f62fb2019-02-12 17:22:36 +0000887 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 while( *p < end )
889 {
890 /*
891 * Extension ::= SEQUENCE {
892 * extnID OBJECT IDENTIFIER,
893 * critical BOOLEAN DEFAULT FALSE,
894 * extnValue OCTET STRING }
895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 int is_critical = 0; /* DEFAULT FALSE */
898 int ext_type = 0;
899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
901 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100902 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903
904 end_ext_data = *p + len;
905
906 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200907 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200908 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100909 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910
k-stachowiak463928a2018-07-24 12:50:59 +0200911 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 extn_oid.p = *p;
913 *p += extn_oid.len;
914
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
917 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100918 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
920 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
922 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100923 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200925 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926 end_ext_octet = *p + len;
927
928 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100929 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
930 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931
932 /*
933 * Detect supported extensions
934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936
937 if( ret != 0 )
938 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200939 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200940 if( cb != NULL )
941 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200942 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200943 if( ret != 0 && is_critical )
944 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200945 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200946 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200947 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200948
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 /* No parser found, skip extension */
950 *p = end_ext_octet;
951
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 if( is_critical )
953 {
954 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100955 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
956 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 continue;
959 }
960
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100961 /* Forbid repeated extensions */
962 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100964
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 crt->ext_types |= ext_type;
966
967 switch( ext_type )
968 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100969 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 /* Parse basic constraints */
971 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
972 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200973 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 break;
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 /* Parse key usage */
978 if( ( ret = x509_get_key_usage( p, end_ext_octet,
979 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200980 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 break;
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 /* Parse extended key usage */
985 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
986 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200987 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 break;
989
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100990 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 /* Parse subject alt name */
992 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
993 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200994 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 break;
996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 /* Parse netscape certificate type */
999 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1000 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001001 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 break;
1003
Ron Eldor74d9acc2019-03-21 14:00:03 +02001004 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1005 /* Parse certificate policies type */
1006 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1007 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001008 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001009 /* Give the callback (if any) a chance to handle the extension
1010 * if it contains unsupported policies */
1011 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1012 cb( p_ctx, crt, &extn_oid, is_critical,
1013 start_ext_octet, end_ext_octet ) == 0 )
1014 break;
1015
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001016 if( is_critical )
1017 return( ret );
1018 else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001019 /*
Ron Eldora2913912019-05-16 16:17:38 +03001020 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1021 * cannot interpret or enforce the policy. However, it is up to
1022 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001023 * unless the extension is critical.
1024 */
1025 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1026 return( ret );
1027 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001028 break;
1029
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001031 /*
1032 * If this is a non-critical extension, which the oid layer
1033 * supports, but there isn't an x509 parser for it,
1034 * skip the extension.
1035 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001036 if( is_critical )
1037 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1038 else
Ron Eldordf48efa2019-04-08 13:28:24 +03001039 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040 }
1041 }
1042
1043 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001044 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1045 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046
1047 return( 0 );
1048}
1049
1050/*
1051 * Parse and fill a single X.509 certificate in DER format
1052 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001053static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1054 const unsigned char *buf,
1055 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001056 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001057 mbedtls_x509_crt_ext_cb_t cb,
1058 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059{
Janos Follath865b3eb2019-12-16 11:46:15 +00001060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061 size_t len;
1062 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1066 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1067 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068
1069 /*
1070 * Check for valid input
1071 */
1072 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001075 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001076 p = (unsigned char*) buf;
1077 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078 end = p + len;
1079
1080 /*
1081 * Certificate ::= SEQUENCE {
1082 * tbsCertificate TBSCertificate,
1083 * signatureAlgorithm AlgorithmIdentifier,
1084 * signatureValue BIT STRING }
1085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1087 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_x509_crt_free( crt );
1090 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091 }
1092
Janos Follathcc0e49d2016-02-17 14:34:12 +00001093 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001094 crt->raw.len = crt_end - buf;
1095 if( make_copy != 0 )
1096 {
1097 /* Create and populate a new buffer for the raw field. */
1098 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1099 if( crt->raw.p == NULL )
1100 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1101
1102 memcpy( crt->raw.p, buf, crt->raw.len );
1103 crt->own_buffer = 1;
1104
1105 p += crt->raw.len - len;
1106 end = crt_end = p + len;
1107 }
1108 else
1109 {
1110 crt->raw.p = (unsigned char*) buf;
1111 crt->own_buffer = 0;
1112 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001113
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114 /*
1115 * TBSCertificate ::= SEQUENCE {
1116 */
1117 crt->tbs.p = p;
1118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1120 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001123 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 }
1125
1126 end = p + len;
1127 crt->tbs.len = end - crt->tbs.p;
1128
1129 /*
1130 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1131 *
1132 * CertificateSerialNumber ::= INTEGER
1133 *
1134 * signature AlgorithmIdentifier
1135 */
1136 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1138 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001139 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 return( ret );
1143 }
1144
Andres AG7ca4a032017-03-09 16:16:11 +00001145 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 mbedtls_x509_crt_free( crt );
1148 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 }
1150
Andres AG7ca4a032017-03-09 16:16:11 +00001151 crt->version++;
1152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001154 &crt->sig_md, &crt->sig_pk,
1155 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 return( ret );
1159 }
1160
1161 /*
1162 * issuer Name
1163 */
1164 crt->issuer_raw.p = p;
1165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1167 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001170 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 }
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 return( ret );
1177 }
1178
1179 crt->issuer_raw.len = p - crt->issuer_raw.p;
1180
1181 /*
1182 * Validity ::= SEQUENCE {
1183 * notBefore Time,
1184 * notAfter Time }
1185 *
1186 */
1187 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1188 &crt->valid_to ) ) != 0 )
1189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 return( ret );
1192 }
1193
1194 /*
1195 * subject Name
1196 */
1197 crt->subject_raw.p = p;
1198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001203 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 }
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 return( ret );
1210 }
1211
1212 crt->subject_raw.len = p - crt->subject_raw.p;
1213
1214 /*
1215 * SubjectPublicKeyInfo
1216 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001217 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221 return( ret );
1222 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001223 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224
1225 /*
1226 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1227 * -- If present, version shall be v2 or v3
1228 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1229 * -- If present, version shall be v2 or v3
1230 * extensions [3] EXPLICIT Extensions OPTIONAL
1231 * -- If present, version shall be v3
1232 */
1233 if( crt->version == 2 || crt->version == 3 )
1234 {
1235 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1236 if( ret != 0 )
1237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 return( ret );
1240 }
1241 }
1242
1243 if( crt->version == 2 || crt->version == 3 )
1244 {
1245 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1246 if( ret != 0 )
1247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249 return( ret );
1250 }
1251 }
1252
1253 if( crt->version == 3 )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001254 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001255 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256 if( ret != 0 )
1257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259 return( ret );
1260 }
1261 }
1262
1263 if( p != end )
1264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001266 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1267 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 }
1269
1270 end = crt_end;
1271
1272 /*
1273 * }
1274 * -- end of TBSCertificate
1275 *
1276 * signatureAlgorithm AlgorithmIdentifier,
1277 * signatureValue BIT STRING
1278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 return( ret );
1283 }
1284
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001285 if( crt->sig_oid.len != sig_oid2.len ||
1286 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001287 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001288 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001289 ( sig_params1.len != 0 &&
1290 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_x509_crt_free( crt );
1293 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 }
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 return( ret );
1300 }
1301
1302 if( p != end )
1303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001305 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1306 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 }
1308
1309 return( 0 );
1310}
1311
1312/*
1313 * Parse one X.509 certificate in DER format from a buffer and add them to a
1314 * chained list
1315 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001316static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1317 const unsigned char *buf,
1318 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001319 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001320 mbedtls_x509_crt_ext_cb_t cb,
1321 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001322{
Janos Follath865b3eb2019-12-16 11:46:15 +00001323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325
1326 /*
1327 * Check for valid input
1328 */
1329 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331
1332 while( crt->version != 0 && crt->next != NULL )
1333 {
1334 prev = crt;
1335 crt = crt->next;
1336 }
1337
1338 /*
1339 * Add new certificate on the end of the chain if needed.
1340 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001341 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001343 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344
1345 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001346 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347
1348 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351 }
1352
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001353 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001354 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355 {
1356 if( prev )
1357 prev->next = NULL;
1358
1359 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361
1362 return( ret );
1363 }
1364
1365 return( 0 );
1366}
1367
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001368int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1369 const unsigned char *buf,
1370 size_t buflen )
1371{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001372 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001373}
1374
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001375int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1376 const unsigned char *buf,
1377 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001378 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001379 mbedtls_x509_crt_ext_cb_t cb,
1380 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001381{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001382 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001383}
1384
1385int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1386 const unsigned char *buf,
1387 size_t buflen )
1388{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001389 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001390}
1391
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001392/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001393 * Parse one or more PEM certificates from a buffer and add them to the chained
1394 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001396int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1397 const unsigned char *buf,
1398 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399{
Janos Follath98e28a72016-05-31 14:03:54 +01001400#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001401 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001403#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404
1405 /*
1406 * Check for valid input
1407 */
1408 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410
1411 /*
1412 * Determine buffer content. Buffer contains either one DER certificate or
1413 * one or more PEM certificates.
1414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001416 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001417 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001420 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1423 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001424#else
1425 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1426#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_PEM_PARSE_C)
1429 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001434 /* 1 rather than 0 since the terminating NULL byte is counted in */
1435 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436 {
1437 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001440 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442 "-----BEGIN CERTIFICATE-----",
1443 "-----END CERTIFICATE-----",
1444 buf, NULL, 0, &use_len );
1445
1446 if( ret == 0 )
1447 {
1448 /*
1449 * Was PEM encoded
1450 */
1451 buflen -= use_len;
1452 buf += use_len;
1453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455 {
1456 return( ret );
1457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461
1462 /*
1463 * PEM header and footer were found
1464 */
1465 buflen -= use_len;
1466 buf += use_len;
1467
1468 if( first_error == 0 )
1469 first_error = ret;
1470
Paul Bakker5a5fa922014-09-26 14:53:04 +02001471 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 continue;
1473 }
1474 else
1475 break;
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480
1481 if( ret != 0 )
1482 {
1483 /*
1484 * Quit parsing on a memory error
1485 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001486 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001487 return( ret );
1488
1489 if( first_error == 0 )
1490 first_error = ret;
1491
1492 total_failed++;
1493 continue;
1494 }
1495
1496 success = 1;
1497 }
1498 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499
1500 if( success )
1501 return( total_failed );
1502 else if( first_error )
1503 return( first_error );
1504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001506#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507}
1508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510/*
1511 * Load one or more certificates and add them to the chained list
1512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514{
Janos Follath865b3eb2019-12-16 11:46:15 +00001515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 size_t n;
1517 unsigned char *buf;
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520 return( ret );
1521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001524 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526
1527 return( ret );
1528}
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531{
1532 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001533#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534 int w_ret;
1535 WCHAR szDir[MAX_PATH];
1536 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001537 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001538 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539
Paul Bakker9af723c2014-05-01 13:03:14 +02001540 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541 HANDLE hFind;
1542
1543 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545
Paul Bakker9af723c2014-05-01 13:03:14 +02001546 memset( szDir, 0, sizeof(szDir) );
1547 memset( filename, 0, MAX_PATH );
1548 memcpy( filename, path, len );
1549 filename[len++] = '\\';
1550 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 filename[len++] = '*';
1552
Simon B3c6b18d2016-11-03 01:11:37 +00001553 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001554 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001555 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557
1558 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001559 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
1562 len = MAX_PATH - len;
1563 do
1564 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001565 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566
1567 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1568 continue;
1569
Paul Bakker9af723c2014-05-01 13:03:14 +02001570 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001571 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001572 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001573 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001574 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001575 {
1576 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1577 goto cleanup;
1578 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581 if( w_ret < 0 )
1582 ret++;
1583 else
1584 ret += w_ret;
1585 }
1586 while( FindNextFileW( hFind, &file_data ) != 0 );
1587
Paul Bakker66d5d072014-06-17 16:39:18 +02001588 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Ron Eldor36d90422017-01-09 15:09:16 +02001591cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001593#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001594 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001595 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001597 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001598 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599 DIR *dir = opendir( path );
1600
Paul Bakker66d5d072014-06-17 16:39:18 +02001601 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603
Ron Eldor63140682017-01-09 19:27:59 +02001604#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001605 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001606 {
1607 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001608 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001609 }
Ron Eldor63140682017-01-09 19:27:59 +02001610#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001611
Paul Elliottfb91a482021-03-05 14:17:51 +00001612 memset( &sb, 0, sizeof( sb ) );
1613
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001614 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615 {
Andres AGf9113192016-09-02 14:06:04 +01001616 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1617 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618
Andres AGf9113192016-09-02 14:06:04 +01001619 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 {
Andres AGf9113192016-09-02 14:06:04 +01001621 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1622 goto cleanup;
1623 }
1624 else if( stat( entry_name, &sb ) == -1 )
1625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001627 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 }
1629
1630 if( !S_ISREG( sb.st_mode ) )
1631 continue;
1632
1633 // Ignore parse errors
1634 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 if( t_ret < 0 )
1637 ret++;
1638 else
1639 ret += t_ret;
1640 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001641
1642cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001643 closedir( dir );
1644
Ron Eldor63140682017-01-09 19:27:59 +02001645#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001646 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001648#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001649
Paul Bakkerbe089b02013-10-14 15:51:50 +02001650#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651
1652 return( ret );
1653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001656/*
1657 * OtherName ::= SEQUENCE {
1658 * type-id OBJECT IDENTIFIER,
1659 * value [0] EXPLICIT ANY DEFINED BY type-id }
1660 *
1661 * HardwareModuleName ::= SEQUENCE {
1662 * hwType OBJECT IDENTIFIER,
1663 * hwSerialNum OCTET STRING }
1664 *
1665 * NOTE: we currently only parse and use otherName of type HwModuleName,
1666 * as defined in RFC 4108.
1667 */
1668static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1669 mbedtls_x509_san_other_name *other_name )
1670{
Janos Follathab23cd12019-05-09 13:53:57 +01001671 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001672 size_t len;
1673 unsigned char *p = subject_alt_name->p;
1674 const unsigned char *end = p + subject_alt_name->len;
1675 mbedtls_x509_buf cur_oid;
1676
1677 if( ( subject_alt_name->tag &
1678 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1679 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1680 {
1681 /*
1682 * The given subject alternative name is not of type "othername".
1683 */
1684 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1685 }
1686
1687 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1688 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001689 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001690
1691 cur_oid.tag = MBEDTLS_ASN1_OID;
1692 cur_oid.p = p;
1693 cur_oid.len = len;
1694
1695 /*
1696 * Only HwModuleName is currently supported.
1697 */
1698 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1699 {
1700 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1701 }
1702
Ron Eldor890819a2019-05-13 19:03:04 +03001703 if( p + len >= end )
1704 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001705 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001706 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1707 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001708 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001709 p += len;
1710 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1711 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001712 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001713
1714 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1715 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001716 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001717
1718 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001719 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001720
1721 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1722 other_name->value.hardware_module_name.oid.p = p;
1723 other_name->value.hardware_module_name.oid.len = len;
1724
Ron Eldor890819a2019-05-13 19:03:04 +03001725 if( p + len >= end )
1726 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001727 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001728 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1729 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001730 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001731 p += len;
1732 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1733 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001734 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001735
1736 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1737 other_name->value.hardware_module_name.val.p = p;
1738 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001739 p += len;
1740 if( p != end )
1741 {
Ron Eldor890819a2019-05-13 19:03:04 +03001742 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001743 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001744 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1745 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001746 }
1747 return( 0 );
1748}
1749
Peter Kolbus9a969b62018-12-11 13:55:56 -06001750int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1751 mbedtls_x509_subject_alternative_name *san )
1752{
1753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1754 switch( san_buf->tag &
1755 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1756 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1757 {
1758 /*
1759 * otherName
1760 */
1761 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1762 {
1763 mbedtls_x509_san_other_name other_name;
1764
1765 ret = x509_get_other_name( san_buf, &other_name );
1766 if( ret != 0 )
1767 return( ret );
1768
1769 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1770 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1771 memcpy( &san->san.other_name,
1772 &other_name, sizeof( other_name ) );
1773
1774 }
1775 break;
1776
1777 /*
1778 * dNSName
1779 */
1780 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1781 {
1782 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1783 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1784
1785 memcpy( &san->san.unstructured_name,
1786 san_buf, sizeof( *san_buf ) );
1787
1788 }
1789 break;
1790
1791 /*
1792 * Type not supported
1793 */
1794 default:
1795 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1796 }
1797 return( 0 );
1798}
1799
1800#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001801static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001802 const mbedtls_x509_sequence
1803 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001804 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001805{
Janos Follath865b3eb2019-12-16 11:46:15 +00001806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001807 size_t n = *size;
1808 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001810 mbedtls_x509_subject_alternative_name san;
1811 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001812
1813 while( cur != NULL )
1814 {
Ron Eldor890819a2019-05-13 19:03:04 +03001815 memset( &san, 0, sizeof( san ) );
1816 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1817 if( parse_ret != 0 )
1818 {
1819 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1820 {
1821 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1822 MBEDTLS_X509_SAFE_SNPRINTF;
1823 }
1824 else
1825 {
1826 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1827 MBEDTLS_X509_SAFE_SNPRINTF;
1828 }
1829 cur = cur->next;
1830 continue;
1831 }
1832
1833 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001834 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001835 /*
1836 * otherName
1837 */
Ron Eldora2913912019-05-16 16:17:38 +03001838 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001839 {
1840 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1841
1842 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1843 MBEDTLS_X509_SAFE_SNPRINTF;
1844
1845 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1846 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001847 {
Ron Eldor890819a2019-05-13 19:03:04 +03001848 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1849 MBEDTLS_X509_SAFE_SNPRINTF;
1850 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001851 MBEDTLS_X509_SAFE_SNPRINTF;
1852
Ron Eldor890819a2019-05-13 19:03:04 +03001853 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1854 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001855
Ron Eldor890819a2019-05-13 19:03:04 +03001856 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1857 MBEDTLS_X509_SAFE_SNPRINTF;
1858
1859 if( other_name->value.hardware_module_name.val.len >= n )
1860 {
1861 *p = '\0';
1862 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001863 }
1864
Ron Eldora2913912019-05-16 16:17:38 +03001865 memcpy( p, other_name->value.hardware_module_name.val.p,
1866 other_name->value.hardware_module_name.val.len );
1867 p += other_name->value.hardware_module_name.val.len;
1868
Ron Eldor890819a2019-05-13 19:03:04 +03001869 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001870
Ron Eldor890819a2019-05-13 19:03:04 +03001871 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1872 }
1873 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001874
1875 /*
1876 * dNSName
1877 */
Ron Eldora2913912019-05-16 16:17:38 +03001878 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001879 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001880 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1881 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001882 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001883 {
1884 *p = '\0';
1885 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1886 }
Ron Eldora2913912019-05-16 16:17:38 +03001887
1888 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1889 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001890 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001891 }
1892 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001893
1894 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001895 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001896 */
1897 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001898 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1899 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001900 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001901 }
1902
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001903 cur = cur->next;
1904 }
1905
1906 *p = '\0';
1907
1908 *size = n;
1909 *buf = p;
1910
1911 return( 0 );
1912}
1913
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001914#define PRINT_ITEM(i) \
1915 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001917 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001918 sep = ", "; \
1919 }
1920
1921#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001922 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001923 PRINT_ITEM( name );
1924
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001925static int x509_info_cert_type( char **buf, size_t *size,
1926 unsigned char ns_cert_type )
1927{
Janos Follath865b3eb2019-12-16 11:46:15 +00001928 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001929 size_t n = *size;
1930 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001931 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001934 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1936 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1937 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001938 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1939 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1940 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001941
1942 *size = n;
1943 *buf = p;
1944
1945 return( 0 );
1946}
1947
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001948#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001949 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001950 PRINT_ITEM( name );
1951
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001952static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001953 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001954{
Janos Follath865b3eb2019-12-16 11:46:15 +00001955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001956 size_t n = *size;
1957 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001958 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1961 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001962 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1963 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1964 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1966 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001967 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1968 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001969
1970 *size = n;
1971 *buf = p;
1972
1973 return( 0 );
1974}
1975
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001976static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001978{
Janos Follath865b3eb2019-12-16 11:46:15 +00001979 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001980 const char *desc;
1981 size_t n = *size;
1982 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001984 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001985
1986 while( cur != NULL )
1987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001989 desc = "???";
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001992 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001993
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001994 sep = ", ";
1995
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001996 cur = cur->next;
1997 }
1998
1999 *size = n;
2000 *buf = p;
2001
2002 return( 0 );
2003}
2004
Ron Eldor74d9acc2019-03-21 14:00:03 +02002005static int x509_info_cert_policies( char **buf, size_t *size,
2006 const mbedtls_x509_sequence *certificate_policies )
2007{
Janos Follath865b3eb2019-12-16 11:46:15 +00002008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002009 const char *desc;
2010 size_t n = *size;
2011 char *p = *buf;
2012 const mbedtls_x509_sequence *cur = certificate_policies;
2013 const char *sep = "";
2014
2015 while( cur != NULL )
2016 {
2017 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2018 desc = "???";
2019
2020 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2021 MBEDTLS_X509_SAFE_SNPRINTF;
2022
2023 sep = ", ";
2024
2025 cur = cur->next;
2026 }
2027
2028 *size = n;
2029 *buf = p;
2030
2031 return( 0 );
2032}
2033
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002034/*
2035 * Return an informational string about the certificate.
2036 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002037#define BEFORE_COLON 18
2038#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2040 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002041{
Janos Follath865b3eb2019-12-16 11:46:15 +00002042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043 size_t n;
2044 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002045 char key_size_str[BEFORE_COLON];
2046
2047 p = buf;
2048 n = size;
2049
Janos Follath98e28a72016-05-31 14:03:54 +01002050 if( NULL == crt )
2051 {
2052 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2053 MBEDTLS_X509_SAFE_SNPRINTF;
2054
2055 return( (int) ( size - n ) );
2056 }
2057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002060 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002062 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002063 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002066 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002069 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002071 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002074 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002076 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2080 crt->valid_from.year, crt->valid_from.mon,
2081 crt->valid_from.day, crt->valid_from.hour,
2082 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002083 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2087 crt->valid_to.year, crt->valid_to.mon,
2088 crt->valid_to.day, crt->valid_to.hour,
2089 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002090 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002093 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002096 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002097 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002099 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2101 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002102 {
2103 return( ret );
2104 }
2105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002107 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002108 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002110 /*
2111 * Optional extensions
2112 */
2113
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002114 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002117 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002118 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002119
2120 if( crt->max_pathlen > 0 )
2121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002123 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002124 }
2125 }
2126
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002127 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002128 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002129 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002130 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002131
2132 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002133 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002134 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002135 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002136 }
2137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002141 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002142
2143 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2144 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002145 }
2146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002150 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002151
2152 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2153 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002154 }
2155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002159 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002160
2161 if( ( ret = x509_info_ext_key_usage( &p, &n,
2162 &crt->ext_key_usage ) ) != 0 )
2163 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002164 }
2165
Ron Eldor74d9acc2019-03-21 14:00:03 +02002166 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2167 {
2168 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2169 MBEDTLS_X509_SAFE_SNPRINTF;
2170
2171 if( ( ret = x509_info_cert_policies( &p, &n,
2172 &crt->certificate_policies ) ) != 0 )
2173 return( ret );
2174 }
2175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002177 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002178
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002179 return( (int) ( size - n ) );
2180}
2181
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002182struct x509_crt_verify_string {
2183 int code;
2184 const char *string;
2185};
2186
Hanno Becker7ac83f92020-10-09 10:48:22 +01002187#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002188static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002189 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002190 { 0, NULL }
2191};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002192#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002193
2194int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002195 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002196{
Janos Follath865b3eb2019-12-16 11:46:15 +00002197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002198 const struct x509_crt_verify_string *cur;
2199 char *p = buf;
2200 size_t n = size;
2201
2202 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2203 {
2204 if( ( flags & cur->code ) == 0 )
2205 continue;
2206
2207 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002208 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002209 flags ^= cur->code;
2210 }
2211
2212 if( flags != 0 )
2213 {
2214 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2215 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002216 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002217 }
2218
2219 return( (int) ( size - n ) );
2220}
Hanno Becker612a2f12020-10-09 09:19:39 +01002221#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002222
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002223int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2224 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002225{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002226 unsigned int usage_must, usage_may;
2227 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2228 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2229
2230 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2231 return( 0 );
2232
2233 usage_must = usage & ~may_mask;
2234
2235 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2236 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2237
2238 usage_may = usage & may_mask;
2239
2240 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002242
2243 return( 0 );
2244}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002247 const char *usage_oid,
2248 size_t usage_len )
2249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002251
2252 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002254 return( 0 );
2255
2256 /*
2257 * Look for the requested usage (or wildcard ANY) in our list
2258 */
2259 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002262
2263 if( cur_oid->len == usage_len &&
2264 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2265 {
2266 return( 0 );
2267 }
2268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002270 return( 0 );
2271 }
2272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002274}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002277/*
2278 * Return 1 if the certificate is revoked, or 0 otherwise.
2279 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002280int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002281{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002283
2284 while( cur != NULL && cur->serial.len != 0 )
2285 {
2286 if( crt->serial.len == cur->serial.len &&
2287 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2288 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002289 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002290 }
2291
2292 cur = cur->next;
2293 }
2294
2295 return( 0 );
2296}
2297
2298/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002299 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002300 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002303 mbedtls_x509_crl *crl_list,
2304 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305{
2306 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2308 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002309
2310 if( ca == NULL )
2311 return( flags );
2312
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002313 while( crl_list != NULL )
2314 {
2315 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002316 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317 {
2318 crl_list = crl_list->next;
2319 continue;
2320 }
2321
2322 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002323 * Check if the CA is configured to sign CRLs
2324 */
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002325 if( mbedtls_x509_crt_check_key_usage( ca,
2326 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002329 break;
2330 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002331
2332 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333 * Check if CRL is correctly signed by the trusted CA
2334 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002335 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2336 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2337
2338 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2339 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002342 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002343 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002344 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002346 break;
2347 }
2348
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002349 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002350 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2353 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002354 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002357 break;
2358 }
2359
2360 /*
2361 * Check for validity of CRL (Do not drop out)
2362 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002363 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002365
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002366 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002367 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002368
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369 /*
2370 * Check if certificate is revoked
2371 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002372 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002375 break;
2376 }
2377
2378 crl_list = crl_list->next;
2379 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002380
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002381 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002384
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002385/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002386 * Check the signature of a certificate by its parent
2387 */
2388static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002389 mbedtls_x509_crt *parent,
2390 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002391{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002392 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002393 size_t hash_len;
2394#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2395 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002396 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002397 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002398
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002399 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002400 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002401 return( -1 );
2402#else
Jaeden Amero34973232019-02-20 10:32:28 +00002403 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002404 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2405
2406 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2407 return( -1 );
2408
2409 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2410 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002411 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002412 return( -1 );
2413 }
2414
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002415 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2416 != PSA_SUCCESS )
2417 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002418 return( -1 );
2419 }
2420#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002421 /* Skip expensive computation on obvious mismatch */
2422 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002423 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002424
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002425#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002426 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2427 {
2428 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002429 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002430 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002431 }
2432#else
2433 (void) rs_ctx;
2434#endif
2435
2436 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002437 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002438 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002439}
2440
2441/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002442 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2443 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002444 *
2445 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2448 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002449 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002450{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002451 int need_ca_bit;
2452
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002453 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002454 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002455 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002456
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002457 /* Parent must have the basicConstraints CA bit set as a general rule */
2458 need_ca_bit = 1;
2459
2460 /* Exception: v1/v2 certificates that are locally trusted. */
2461 if( top && parent->version < 3 )
2462 need_ca_bit = 0;
2463
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002464 if( need_ca_bit && ! parent->ca_istrue )
2465 return( -1 );
2466
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002467 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002469 {
2470 return( -1 );
2471 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002472
2473 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002474}
2475
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002476/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002477 * Find a suitable parent for child in candidates, or return NULL.
2478 *
2479 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002480 * 1. subject name matches child's issuer
2481 * 2. if necessary, the CA bit is set and key usage allows signing certs
2482 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002483 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002484 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002485 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002486 * If there's a suitable candidate which is also time-valid, return the first
2487 * such. Otherwise, return the first suitable candidate (or NULL if there is
2488 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002489 *
2490 * The rationale for this rule is that someone could have a list of trusted
2491 * roots with two versions on the same root with different validity periods.
2492 * (At least one user reported having such a list and wanted it to just work.)
2493 * The reason we don't just require time-validity is that generally there is
2494 * only one version, and if it's expired we want the flags to state that
2495 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002496 *
2497 * The rationale for rule 3 (signature for trusted roots) is that users might
2498 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002499 * way we select the correct one is by checking the signature (as we don't
2500 * rely on key identifier extensions). (This is one way users might choose to
2501 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002502 *
2503 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002504 * - [in] child: certificate for which we're looking for a parent
2505 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002506 * - [out] r_parent: parent found (or NULL)
2507 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002508 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2509 * of the chain, 0 otherwise
2510 * - [in] path_cnt: number of intermediates seen so far
2511 * - [in] self_cnt: number of self-signed intermediates seen so far
2512 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002513 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002514 *
2515 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002516 * - 0 on success
2517 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002518 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002519static int x509_crt_find_parent_in(
2520 mbedtls_x509_crt *child,
2521 mbedtls_x509_crt *candidates,
2522 mbedtls_x509_crt **r_parent,
2523 int *r_signature_is_good,
2524 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002525 unsigned path_cnt,
2526 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002527 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002528{
Janos Follath865b3eb2019-12-16 11:46:15 +00002529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002530 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002531 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002532
2533#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002534 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002535 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2536 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002537 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002538 parent = rs_ctx->parent;
2539 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002540 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002541
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002542 /* clear saved state */
2543 rs_ctx->parent = NULL;
2544 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002545 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002546
2547 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002548 goto check_signature;
2549 }
2550#endif
2551
2552 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002553 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002554
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002555 for( parent = candidates; parent != NULL; parent = parent->next )
2556 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002557 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002558 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002559 continue;
2560
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002561 /* +1 because stored max_pathlen is 1 higher that the actual value */
2562 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002563 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002564 {
2565 continue;
2566 }
2567
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002568 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002569#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2570check_signature:
2571#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002572 ret = x509_crt_check_signature( child, parent, rs_ctx );
2573
2574#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002575 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2576 {
2577 /* save state */
2578 rs_ctx->parent = parent;
2579 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002580 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002581
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002582 return( ret );
2583 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002584#else
2585 (void) ret;
2586#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002587
2588 signature_is_good = ret == 0;
2589 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002590 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002591
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002592 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002593 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2594 mbedtls_x509_time_is_future( &parent->valid_from ) )
2595 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002596 if( fallback_parent == NULL )
2597 {
2598 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002599 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002600 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002601
2602 continue;
2603 }
2604
Andy Gross1f627142019-01-30 10:25:53 -06002605 *r_parent = parent;
2606 *r_signature_is_good = signature_is_good;
2607
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002608 break;
2609 }
2610
Andy Gross1f627142019-01-30 10:25:53 -06002611 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002612 {
2613 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002614 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002615 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002616
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002617 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002618}
2619
2620/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002621 * Find a parent in trusted CAs or the provided chain, or return NULL.
2622 *
2623 * Searches in trusted CAs first, and return the first suitable parent found
2624 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002625 *
2626 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002627 * - [in] child: certificate for which we're looking for a parent, followed
2628 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002629 * - [in] trust_ca: list of locally trusted certificates
2630 * - [out] parent: parent found (or NULL)
2631 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2632 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2633 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2634 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002635 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002636 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002637 *
2638 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002639 * - 0 on success
2640 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002641 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002642static int x509_crt_find_parent(
2643 mbedtls_x509_crt *child,
2644 mbedtls_x509_crt *trust_ca,
2645 mbedtls_x509_crt **parent,
2646 int *parent_is_trusted,
2647 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002648 unsigned path_cnt,
2649 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002650 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002651{
Janos Follath865b3eb2019-12-16 11:46:15 +00002652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002653 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002654
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002655 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002656
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002657#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002658 /* restore then clear saved state if we have some stored */
2659 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2660 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002661 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002662 rs_ctx->parent_is_trusted = -1;
2663 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002664#endif
2665
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002666 while( 1 ) {
2667 search_list = *parent_is_trusted ? trust_ca : child->next;
2668
2669 ret = x509_crt_find_parent_in( child, search_list,
2670 parent, signature_is_good,
2671 *parent_is_trusted,
2672 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002673
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002674#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002675 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2676 {
2677 /* save state */
2678 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002679 return( ret );
2680 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002681#else
2682 (void) ret;
2683#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002684
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002685 /* stop here if found or already in second iteration */
2686 if( *parent != NULL || *parent_is_trusted == 0 )
2687 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002688
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002689 /* prepare second iteration */
2690 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002691 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002692
2693 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002694 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002695 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002696 *parent_is_trusted = 0;
2697 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002698 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002699
2700 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002701}
2702
2703/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002704 * Check if an end-entity certificate is locally trusted
2705 *
2706 * Currently we require such certificates to be self-signed (actually only
2707 * check for self-issued as self-signatures are not checked)
2708 */
2709static int x509_crt_check_ee_locally_trusted(
2710 mbedtls_x509_crt *crt,
2711 mbedtls_x509_crt *trust_ca )
2712{
2713 mbedtls_x509_crt *cur;
2714
2715 /* must be self-issued */
2716 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2717 return( -1 );
2718
2719 /* look for an exact match with trusted cert */
2720 for( cur = trust_ca; cur != NULL; cur = cur->next )
2721 {
2722 if( crt->raw.len == cur->raw.len &&
2723 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2724 {
2725 return( 0 );
2726 }
2727 }
2728
2729 /* too bad */
2730 return( -1 );
2731}
2732
2733/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002734 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002735 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002736 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2737 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002738 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002739 * such that every cert in the chain is a child of the next one,
2740 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002741 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002742 * Verify that chain and return it with flags for all issues found.
2743 *
2744 * Special cases:
2745 * - EE == Rj -> return a one-element list containing it
2746 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2747 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002748 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002749 * Tests for (aspects of) this function should include at least:
2750 * - trusted EE
2751 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002752 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002753 * - if relevant: EE untrusted
2754 * - if relevant: EE -> intermediate, untrusted
2755 * with the aspect under test checked at each relevant level (EE, int, root).
2756 * For some aspects longer chains are required, but usually length 2 is
2757 * enough (but length 1 is not in general).
2758 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002759 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002760 * - [in] crt: the cert list EE, C1, ..., Cn
2761 * - [in] trust_ca: the trusted list R1, ..., Rp
2762 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002763 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002764 * Only valid when return value is 0, may contain garbage otherwise!
2765 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002766 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002767 *
2768 * Return value:
2769 * - non-zero if the chain could not be fully built and examined
2770 * - 0 is the chain was successfully built and examined,
2771 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002772 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002773static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002774 mbedtls_x509_crt *crt,
2775 mbedtls_x509_crt *trust_ca,
2776 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002777 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2778 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002779 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002780 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002781 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002782{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002783 /* Don't initialize any of those variables here, so that the compiler can
2784 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002786 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002787 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002788 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002789 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002790 int parent_is_trusted;
2791 int child_is_trusted;
2792 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002793 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002794 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002795
2796#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2797 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002798 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002799 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002800 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002801 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002802 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002803
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002804 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002805 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002806 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002807 flags = &cur->flags;
2808
2809 goto find_parent;
2810 }
2811#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002812
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002813 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002814 self_cnt = 0;
2815 parent_is_trusted = 0;
2816 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002817
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002818 while( 1 ) {
2819 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002820 cur = &ver_chain->items[ver_chain->len];
2821 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002822 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002823 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002824 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002825
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002826 /* Check time-validity (all certificates) */
2827 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2828 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002829
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002830 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2831 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002832
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002833 /* Stop here for trusted roots (but not for trusted EE certs) */
2834 if( child_is_trusted )
2835 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002836
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002837 /* Check signature algorithm: MD & PK algs */
2838 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2839 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002840
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002841 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2842 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002843
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002844 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002845 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002846 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2847 {
2848 return( 0 );
2849 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002850
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002851#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2852find_parent:
2853#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002854
2855 /* Obtain list of potential trusted signers from CA callback,
2856 * or use statically provided list. */
2857#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2858 if( f_ca_cb != NULL )
2859 {
2860 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2861 mbedtls_free( ver_chain->trust_ca_cb_result );
2862 ver_chain->trust_ca_cb_result = NULL;
2863
2864 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2865 if( ret != 0 )
2866 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2867
2868 cur_trust_ca = ver_chain->trust_ca_cb_result;
2869 }
2870 else
2871#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2872 {
2873 ((void) f_ca_cb);
2874 ((void) p_ca_cb);
2875 cur_trust_ca = trust_ca;
2876 }
2877
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002878 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002879 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002880 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002881 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002882
2883#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002884 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2885 {
2886 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002887 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002888 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002889 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002890
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002891 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002892 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002893#else
2894 (void) ret;
2895#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002896
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002897 /* No parent? We're done here */
2898 if( parent == NULL )
2899 {
2900 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2901 return( 0 );
2902 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002903
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002904 /* Count intermediate self-issued (not necessarily self-signed) certs.
2905 * These can occur with some strategies for key rollover, see [SIRO],
2906 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002907 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002908 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2909 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002910 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002911 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002912
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002913 /* path_cnt is 0 for the first intermediate CA,
2914 * and if parent is trusted it's not an intermediate CA */
2915 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002916 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002917 {
2918 /* return immediately to avoid overflow the chain array */
2919 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2920 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002921
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002922 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002923 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002924 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2925
2926 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002927 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002928 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002931 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002932 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002933#else
2934 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002935#endif
2936
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002937 /* prepare for next iteration */
2938 child = parent;
2939 parent = NULL;
2940 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002941 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002942 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002943}
2944
2945/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002946 * Check for CN match
2947 */
2948static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2949 const char *cn, size_t cn_len )
2950{
2951 /* try exact match */
2952 if( name->len == cn_len &&
2953 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2954 {
2955 return( 0 );
2956 }
2957
2958 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002959 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002960 {
2961 return( 0 );
2962 }
2963
2964 return( -1 );
2965}
2966
2967/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002968 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2969 */
2970static int x509_crt_check_san( const mbedtls_x509_buf *name,
2971 const char *cn, size_t cn_len )
2972{
2973 const unsigned char san_type = (unsigned char) name->tag &
2974 MBEDTLS_ASN1_TAG_VALUE_MASK;
2975
2976 /* dNSName */
2977 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
2978 return( x509_crt_check_cn( name, cn, cn_len ) );
2979
2980 /* (We may handle other types here later.) */
2981
2982 /* Unrecognized type */
2983 return( -1 );
2984}
2985
2986/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002987 * Verify the requested CN - only call this if cn is not NULL!
2988 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002989static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002990 const char *cn,
2991 uint32_t *flags )
2992{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002993 const mbedtls_x509_name *name;
2994 const mbedtls_x509_sequence *cur;
2995 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002996
2997 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2998 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002999 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003000 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003001 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003002 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003003 }
3004
3005 if( cur == NULL )
3006 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3007 }
3008 else
3009 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003010 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003011 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003012 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3013 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003014 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003015 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003016 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003017 }
3018
3019 if( name == NULL )
3020 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3021 }
3022}
3023
3024/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003025 * Merge the flags for all certs in the chain, after calling callback
3026 */
3027static int x509_crt_merge_flags_with_cb(
3028 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003029 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003030 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3031 void *p_vrfy )
3032{
Janos Follath865b3eb2019-12-16 11:46:15 +00003033 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003034 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003035 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003036 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003037
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003038 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003039 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003040 cur = &ver_chain->items[i-1];
3041 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003042
3043 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003044 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003045 return( ret );
3046
3047 *flags |= cur_flags;
3048 }
3049
3050 return( 0 );
3051}
3052
3053/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003054 * Verify the certificate validity, with profile, restartable version
3055 *
3056 * This function:
3057 * - checks the requested CN (if any)
3058 * - checks the type and size of the EE cert's key,
3059 * as that isn't done as part of chain building/verification currently
3060 * - builds and verifies the chain
3061 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003062 *
3063 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3064 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3065 * verification routine to search for trusted signers, and CRLs will
3066 * be disabled. Otherwise, `trust_ca` will be used as the static list
3067 * of trusted signers, and `ca_crl` will be use as the static list
3068 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003069 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003070static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003071 mbedtls_x509_crt *trust_ca,
3072 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003073 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3074 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003075 const mbedtls_x509_crt_profile *profile,
3076 const char *cn, uint32_t *flags,
3077 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3078 void *p_vrfy,
3079 mbedtls_x509_crt_restart_ctx *rs_ctx )
3080{
Janos Follath865b3eb2019-12-16 11:46:15 +00003081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003082 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003083 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003084 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003085
3086 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003087 ee_flags = 0;
3088 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003089
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003090 if( profile == NULL )
3091 {
3092 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3093 goto exit;
3094 }
3095
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003096 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003097 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003098 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003099
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003100 /* Check the type and size of the key */
3101 pk_type = mbedtls_pk_get_type( &crt->pk );
3102
3103 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003104 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003105
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003106 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003107 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003108
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003109 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003110 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3111 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003112 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003113
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003114 if( ret != 0 )
3115 goto exit;
3116
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003117 /* Merge end-entity flags */
3118 ver_chain.items[0].flags |= ee_flags;
3119
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003120 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003121 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003122
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003123exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003124
3125#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3126 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3127 mbedtls_free( ver_chain.trust_ca_cb_result );
3128 ver_chain.trust_ca_cb_result = NULL;
3129#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3130
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003131#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3132 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3133 mbedtls_x509_crt_restart_free( rs_ctx );
3134#endif
3135
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003136 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3137 * the SSL module for authmode optional, but non-zero return from the
3138 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003139 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3140 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3141
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003142 if( ret != 0 )
3143 {
3144 *flags = (uint32_t) -1;
3145 return( ret );
3146 }
3147
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003148 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003150
3151 return( 0 );
3152}
3153
Hanno Becker3116fb32019-03-28 13:34:42 +00003154
3155/*
3156 * Verify the certificate validity (default profile, not restartable)
3157 */
3158int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3159 mbedtls_x509_crt *trust_ca,
3160 mbedtls_x509_crl *ca_crl,
3161 const char *cn, uint32_t *flags,
3162 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3163 void *p_vrfy )
3164{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003165 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003166 NULL, NULL,
3167 &mbedtls_x509_crt_profile_default,
3168 cn, flags,
3169 f_vrfy, p_vrfy, NULL ) );
3170}
3171
3172/*
3173 * Verify the certificate validity (user-chosen profile, not restartable)
3174 */
3175int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3176 mbedtls_x509_crt *trust_ca,
3177 mbedtls_x509_crl *ca_crl,
3178 const mbedtls_x509_crt_profile *profile,
3179 const char *cn, uint32_t *flags,
3180 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3181 void *p_vrfy )
3182{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003183 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003184 NULL, NULL,
3185 profile, cn, flags,
3186 f_vrfy, p_vrfy, NULL ) );
3187}
3188
3189#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3190/*
3191 * Verify the certificate validity (user-chosen profile, CA callback,
3192 * not restartable).
3193 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003194int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003195 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3196 void *p_ca_cb,
3197 const mbedtls_x509_crt_profile *profile,
3198 const char *cn, uint32_t *flags,
3199 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3200 void *p_vrfy )
3201{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003202 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003203 f_ca_cb, p_ca_cb,
3204 profile, cn, flags,
3205 f_vrfy, p_vrfy, NULL ) );
3206}
3207#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3208
3209int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3210 mbedtls_x509_crt *trust_ca,
3211 mbedtls_x509_crl *ca_crl,
3212 const mbedtls_x509_crt_profile *profile,
3213 const char *cn, uint32_t *flags,
3214 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3215 void *p_vrfy,
3216 mbedtls_x509_crt_restart_ctx *rs_ctx )
3217{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003218 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003219 NULL, NULL,
3220 profile, cn, flags,
3221 f_vrfy, p_vrfy, rs_ctx ) );
3222}
3223
3224
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003225/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003226 * Initialize a certificate chain
3227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003231}
3232
3233/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003234 * Unallocate all certificate data
3235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003237{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 mbedtls_x509_crt *cert_cur = crt;
3239 mbedtls_x509_crt *cert_prv;
3240 mbedtls_x509_name *name_cur;
3241 mbedtls_x509_name *name_prv;
3242 mbedtls_x509_sequence *seq_cur;
3243 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003244
3245 if( crt == NULL )
3246 return;
3247
3248 do
3249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3253 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003254#endif
3255
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003256 name_cur = cert_cur->issuer.next;
3257 while( name_cur != NULL )
3258 {
3259 name_prv = name_cur;
3260 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003261 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003263 }
3264
3265 name_cur = cert_cur->subject.next;
3266 while( name_cur != NULL )
3267 {
3268 name_prv = name_cur;
3269 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003270 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003272 }
3273
3274 seq_cur = cert_cur->ext_key_usage.next;
3275 while( seq_cur != NULL )
3276 {
3277 seq_prv = seq_cur;
3278 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003279 mbedtls_platform_zeroize( seq_prv,
3280 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003282 }
3283
3284 seq_cur = cert_cur->subject_alt_names.next;
3285 while( seq_cur != NULL )
3286 {
3287 seq_prv = seq_cur;
3288 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003289 mbedtls_platform_zeroize( seq_prv,
3290 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003292 }
3293
Ron Eldor74d9acc2019-03-21 14:00:03 +02003294 seq_cur = cert_cur->certificate_policies.next;
3295 while( seq_cur != NULL )
3296 {
3297 seq_prv = seq_cur;
3298 seq_cur = seq_cur->next;
3299 mbedtls_platform_zeroize( seq_prv,
3300 sizeof( mbedtls_x509_sequence ) );
3301 mbedtls_free( seq_prv );
3302 }
3303
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003304 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003305 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003306 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003308 }
3309
3310 cert_cur = cert_cur->next;
3311 }
3312 while( cert_cur != NULL );
3313
3314 cert_cur = crt;
3315 do
3316 {
3317 cert_prv = cert_cur;
3318 cert_cur = cert_cur->next;
3319
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003320 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003321 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003323 }
3324 while( cert_cur != NULL );
3325}
3326
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003327#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3328/*
3329 * Initialize a restart context
3330 */
3331void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3332{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003333 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003334
3335 ctx->parent = NULL;
3336 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003337 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003338
3339 ctx->parent_is_trusted = -1;
3340
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003341 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003342 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003343 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003344}
3345
3346/*
3347 * Free the components of a restart context
3348 */
3349void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3350{
3351 if( ctx == NULL )
3352 return;
3353
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003354 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003355 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003356}
3357#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359#endif /* MBEDTLS_X509_CRT_PARSE_C */