blob: c7ee5c181c8beca195c4873cd1f842f325f5c131 [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é-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010056#endif
57
Daniel Axtens301db662020-05-28 11:43:41 +100058#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010059#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#include <windows.h>
61#else
62#include <time.h>
63#endif
Daniel Axtens301db662020-05-28 11:43:41 +100064#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000067#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020068#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#include <sys/types.h>
70#include <sys/stat.h>
71#include <dirent.h>
Eduardo Silva32ffb2b2019-04-25 10:43:26 -060072#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000073#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif
75
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020076/*
77 * Item in a verification chain: cert and flags for it
78 */
79typedef struct {
80 mbedtls_x509_crt *crt;
81 uint32_t flags;
82} x509_crt_verify_chain_item;
83
84/*
85 * Max size of verification chain: end-entity + intermediates + trusted root
86 */
87#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020088
Gilles Peskine0ecd7192021-06-07 21:24:26 +020089/* Default profile. Do not remove items unless there are serious security
90 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020091const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
92{
Gilles Peskine5e79cb32017-05-04 16:17:21 +020093 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020094 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
95 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
96 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
97 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
98 0xFFFFFFF, /* Any PK alg */
99 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200100 2048,
101};
102
103/*
104 * Next-default profile
105 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200106const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
107{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 /* Hashes from SHA-256 and above */
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
112 0xFFFFFFF, /* Any PK alg */
113#if defined(MBEDTLS_ECP_C)
114 /* Curves at or above 128-bit security level */
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
122#else
123 0,
124#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200125 2048,
126};
127
128/*
129 * NSA Suite B Profile
130 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200131const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
132{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200133 /* Only SHA-256 and 384 */
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
136 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200137 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200139#if defined(MBEDTLS_ECP_C)
140 /* Only NIST P-256 and P-384 */
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
143#else
144 0,
145#endif
146 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147};
148
149/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200150 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200151 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200152 */
153static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
154 mbedtls_md_type_t md_alg )
155{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200156 if( md_alg == MBEDTLS_MD_NONE )
157 return( -1 );
158
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200159 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
160 return( 0 );
161
162 return( -1 );
163}
164
165/*
166 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200167 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200168 */
169static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
170 mbedtls_pk_type_t pk_alg )
171{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200172 if( pk_alg == MBEDTLS_PK_NONE )
173 return( -1 );
174
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200175 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
176 return( 0 );
177
178 return( -1 );
179}
180
181/*
182 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200183 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184 */
185static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200186 const mbedtls_pk_context *pk )
187{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200188 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200189
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190#if defined(MBEDTLS_RSA_C)
191 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
192 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200193 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194 return( 0 );
195
196 return( -1 );
197 }
198#endif
199
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200200#if defined(MBEDTLS_ECP_C)
201 if( pk_alg == MBEDTLS_PK_ECDSA ||
202 pk_alg == MBEDTLS_PK_ECKEY ||
203 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200204 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200205 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206
Philippe Antoineb5b25432018-05-11 11:06:29 +0200207 if( gid == MBEDTLS_ECP_DP_NONE )
208 return( -1 );
209
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
211 return( 0 );
212
213 return( -1 );
214 }
215#endif
216
217 return( -1 );
218}
219
220/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000221 * Like memcmp, but case-insensitive and always returns -1 if different
222 */
223static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
224{
225 size_t i;
226 unsigned char diff;
227 const unsigned char *n1 = s1, *n2 = s2;
228
229 for( i = 0; i < len; i++ )
230 {
231 diff = n1[i] ^ n2[i];
232
233 if( diff == 0 )
234 continue;
235
236 if( diff == 32 &&
237 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
238 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
239 {
240 continue;
241 }
242
243 return( -1 );
244 }
245
246 return( 0 );
247}
248
249/*
250 * Return 0 if name matches wildcard, -1 otherwise
251 */
252static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
253{
254 size_t i;
255 size_t cn_idx = 0, cn_len = strlen( cn );
256
257 /* We can't have a match if there is no wildcard to match */
258 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
259 return( -1 );
260
261 for( i = 0; i < cn_len; ++i )
262 {
263 if( cn[i] == '.' )
264 {
265 cn_idx = i;
266 break;
267 }
268 }
269
270 if( cn_idx == 0 )
271 return( -1 );
272
273 if( cn_len - cn_idx == name->len - 1 &&
274 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
275 {
276 return( 0 );
277 }
278
279 return( -1 );
280}
281
282/*
283 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
284 * variations (but not all).
285 *
286 * Return 0 if equal, -1 otherwise.
287 */
288static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
289{
290 if( a->tag == b->tag &&
291 a->len == b->len &&
292 memcmp( a->p, b->p, b->len ) == 0 )
293 {
294 return( 0 );
295 }
296
297 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
298 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
299 a->len == b->len &&
300 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
301 {
302 return( 0 );
303 }
304
305 return( -1 );
306}
307
308/*
309 * Compare two X.509 Names (aka rdnSequence).
310 *
311 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
312 * we sometimes return unequal when the full algorithm would return equal,
313 * but never the other way. (In particular, we don't do Unicode normalisation
314 * or space folding.)
315 *
316 * Return 0 if equal, -1 otherwise.
317 */
318static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
319{
320 /* Avoid recursion, it might not be optimised by the compiler */
321 while( a != NULL || b != NULL )
322 {
323 if( a == NULL || b == NULL )
324 return( -1 );
325
326 /* type */
327 if( a->oid.tag != b->oid.tag ||
328 a->oid.len != b->oid.len ||
329 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
330 {
331 return( -1 );
332 }
333
334 /* value */
335 if( x509_string_cmp( &a->val, &b->val ) != 0 )
336 return( -1 );
337
338 /* structure of the list of sets */
339 if( a->next_merged != b->next_merged )
340 return( -1 );
341
342 a = a->next;
343 b = b->next;
344 }
345
346 /* a == NULL == b */
347 return( 0 );
348}
349
350/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200351 * Reset (init or clear) a verify_chain
352 */
353static void x509_crt_verify_chain_reset(
354 mbedtls_x509_crt_verify_chain *ver_chain )
355{
356 size_t i;
357
358 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
359 {
360 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000361 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200362 }
363
364 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000365
366#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
367 ver_chain->trust_ca_cb_result = NULL;
368#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200369}
370
371/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
373 */
374static int x509_get_version( unsigned char **p,
375 const unsigned char *end,
376 int *ver )
377{
Janos Follath865b3eb2019-12-16 11:46:15 +0000378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 size_t len;
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
382 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385 {
386 *ver = 0;
387 return( 0 );
388 }
389
Chris Jones9f7a6932021-04-14 12:12:09 +0100390 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 }
392
393 end = *p + len;
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100396 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397
398 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100399 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
400 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401
402 return( 0 );
403}
404
405/*
406 * Validity ::= SEQUENCE {
407 * notBefore Time,
408 * notAfter Time }
409 */
410static int x509_get_dates( unsigned char **p,
411 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_x509_time *from,
413 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414{
Janos Follath865b3eb2019-12-16 11:46:15 +0000415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416 size_t len;
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
419 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100420 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
422 end = *p + len;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425 return( ret );
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428 return( ret );
429
430 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100431 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
432 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
434 return( 0 );
435}
436
437/*
438 * X.509 v2/v3 unique identifier (not parsed)
439 */
440static int x509_get_uid( unsigned char **p,
441 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443{
Janos Follath865b3eb2019-12-16 11:46:15 +0000444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
446 if( *p == end )
447 return( 0 );
448
449 uid->tag = **p;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
452 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 return( 0 );
456
Chris Jones9f7a6932021-04-14 12:12:09 +0100457 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 }
459
460 uid->p = *p;
461 *p += uid->len;
462
463 return( 0 );
464}
465
466static int x509_get_basic_constraints( unsigned char **p,
467 const unsigned char *end,
468 int *ca_istrue,
469 int *max_pathlen )
470{
Janos Follath865b3eb2019-12-16 11:46:15 +0000471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472 size_t len;
473
474 /*
475 * BasicConstraints ::= SEQUENCE {
476 * cA BOOLEAN DEFAULT FALSE,
477 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
478 */
479 *ca_istrue = 0; /* DEFAULT FALSE */
480 *max_pathlen = 0; /* endless */
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
483 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100484 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485
486 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200487 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
492 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
494 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100495 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
497 if( *ca_istrue != 0 )
498 *ca_istrue = 1;
499 }
500
501 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200502 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100505 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100508 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
509 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510
Andrzej Kurek16050742020-04-14 09:49:52 -0400511 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
512 * overflow, which is an undefined behavior. */
513 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100514 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
515 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400516
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517 (*max_pathlen)++;
518
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200519 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520}
521
522static int x509_get_ns_cert_type( unsigned char **p,
523 const unsigned char *end,
524 unsigned char *ns_cert_type)
525{
Janos Follath865b3eb2019-12-16 11:46:15 +0000526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100530 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531
532 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100533 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
534 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
536 /* Get actual bitstring */
537 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200538 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539}
540
541static int x509_get_key_usage( unsigned char **p,
542 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100543 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544{
Janos Follath865b3eb2019-12-16 11:46:15 +0000545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200546 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100550 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
552 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100553 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
554 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
556 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200557 *key_usage = 0;
558 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
559 {
560 *key_usage |= (unsigned int) bs.p[i] << (8*i);
561 }
562
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200563 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564}
565
566/*
567 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
568 *
569 * KeyPurposeId ::= OBJECT IDENTIFIER
570 */
571static int x509_get_ext_key_usage( unsigned char **p,
572 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574{
Janos Follath865b3eb2019-12-16 11:46:15 +0000575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100578 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
580 /* Sequence length must be >= 1 */
581 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100582 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
583 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200585 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586}
587
588/*
589 * SubjectAltName ::= GeneralNames
590 *
591 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
592 *
593 * GeneralName ::= CHOICE {
594 * otherName [0] OtherName,
595 * rfc822Name [1] IA5String,
596 * dNSName [2] IA5String,
597 * x400Address [3] ORAddress,
598 * directoryName [4] Name,
599 * ediPartyName [5] EDIPartyName,
600 * uniformResourceIdentifier [6] IA5String,
601 * iPAddress [7] OCTET STRING,
602 * registeredID [8] OBJECT IDENTIFIER }
603 *
604 * OtherName ::= SEQUENCE {
605 * type-id OBJECT IDENTIFIER,
606 * value [0] EXPLICIT ANY DEFINED BY type-id }
607 *
608 * EDIPartyName ::= SEQUENCE {
609 * nameAssigner [0] DirectoryString OPTIONAL,
610 * partyName [1] DirectoryString }
611 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300612 * NOTE: we list all types, but only use dNSName and otherName
613 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614 */
615static int x509_get_subject_alt_name( unsigned char **p,
616 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618{
Janos Follath865b3eb2019-12-16 11:46:15 +0000619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624
625 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
627 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100628 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629
630 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100631 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
632 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633
634 while( *p < end )
635 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300636 mbedtls_x509_subject_alternative_name dummy_san_buf;
637 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
638
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 tag = **p;
640 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100642 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100644 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
645 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
646 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100647 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
648 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100649 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
Ron Eldordbbd9662019-05-15 15:46:03 +0300651 /*
irwir74aee1c2019-09-21 18:21:48 +0300652 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300653 */
654 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
655 /*
656 * In case the extension is malformed, return an error,
657 * and clear the allocated sequences.
658 */
659 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
660 {
661 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
662 mbedtls_x509_sequence *seq_prv;
663 while( seq_cur != NULL )
664 {
665 seq_prv = seq_cur;
666 seq_cur = seq_cur->next;
667 mbedtls_platform_zeroize( seq_prv,
668 sizeof( mbedtls_x509_sequence ) );
669 mbedtls_free( seq_prv );
670 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300671 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300672 return( ret );
673 }
674
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200676 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100678 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100680
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200681 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
683 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100684 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
685 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 cur = cur->next;
688 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200689
690 buf = &(cur->buf);
691 buf->tag = tag;
692 buf->p = *p;
693 buf->len = tag_len;
694 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 }
696
697 /* Set final sequence entry's next pointer to NULL */
698 cur->next = NULL;
699
700 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100701 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
702 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703
704 return( 0 );
705}
706
707/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200708 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
709 *
710 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
711 *
712 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
713 *
714 * PolicyInformation ::= SEQUENCE {
715 * policyIdentifier CertPolicyId,
716 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
717 * PolicyQualifierInfo OPTIONAL }
718 *
719 * CertPolicyId ::= OBJECT IDENTIFIER
720 *
721 * PolicyQualifierInfo ::= SEQUENCE {
722 * policyQualifierId PolicyQualifierId,
723 * qualifier ANY DEFINED BY policyQualifierId }
724 *
725 * -- policyQualifierIds for Internet policy qualifiers
726 *
727 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
728 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
729 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
730 *
731 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
732 *
733 * Qualifier ::= CHOICE {
734 * cPSuri CPSuri,
735 * userNotice UserNotice }
736 *
737 * CPSuri ::= IA5String
738 *
739 * UserNotice ::= SEQUENCE {
740 * noticeRef NoticeReference OPTIONAL,
741 * explicitText DisplayText OPTIONAL }
742 *
743 * NoticeReference ::= SEQUENCE {
744 * organization DisplayText,
745 * noticeNumbers SEQUENCE OF INTEGER }
746 *
747 * DisplayText ::= CHOICE {
748 * ia5String IA5String (SIZE (1..200)),
749 * visibleString VisibleString (SIZE (1..200)),
750 * bmpString BMPString (SIZE (1..200)),
751 * utf8String UTF8String (SIZE (1..200)) }
752 *
753 * NOTE: we only parse and use anyPolicy without qualifiers at this point
754 * as defined in RFC 5280.
755 */
756static int x509_get_certificate_policies( unsigned char **p,
757 const unsigned char *end,
758 mbedtls_x509_sequence *certificate_policies )
759{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300760 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200761 size_t len;
762 mbedtls_asn1_buf *buf;
763 mbedtls_asn1_sequence *cur = certificate_policies;
764
765 /* Get main sequence tag */
766 ret = mbedtls_asn1_get_tag( p, end, &len,
767 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
768 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100769 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200770
771 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100772 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
773 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200774
775 /*
776 * Cannot be an empty sequence.
777 */
778 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100779 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
780 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200781
782 while( *p < end )
783 {
784 mbedtls_x509_buf policy_oid;
785 const unsigned char *policy_end;
786
787 /*
788 * Get the policy sequence
789 */
790 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
791 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100792 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200793
794 policy_end = *p + len;
795
Ron Eldor08063792019-05-13 16:38:39 +0300796 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200797 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100798 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200799
800 policy_oid.tag = MBEDTLS_ASN1_OID;
801 policy_oid.len = len;
802 policy_oid.p = *p;
803
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300804 /*
805 * Only AnyPolicy is currently supported when enforcing policy.
806 */
807 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
808 {
809 /*
810 * Set the parsing return code but continue parsing, in case this
811 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
812 * is configured.
813 */
814 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
815 }
816
Ron Eldor74d9acc2019-03-21 14:00:03 +0200817 /* Allocate and assign next pointer */
818 if( cur->buf.p != NULL )
819 {
820 if( cur->next != NULL )
821 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
822
823 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
824
825 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100826 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
827 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200828
829 cur = cur->next;
830 }
831
832 buf = &( cur->buf );
833 buf->tag = policy_oid.tag;
834 buf->p = policy_oid.p;
835 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300836
837 *p += len;
838
839 /*
840 * If there is an optional qualifier, then *p < policy_end
841 * Check the Qualifier len to verify it doesn't exceed policy_end.
842 */
843 if( *p < policy_end )
844 {
845 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
846 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100847 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300848 /*
849 * Skip the optional policy qualifiers.
850 */
851 *p += len;
852 }
853
854 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100855 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
856 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200857 }
858
859 /* Set final sequence entry's next pointer to NULL */
860 cur->next = NULL;
861
862 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100863 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
864 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200865
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300866 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200867}
868
869/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 * X.509 v3 extensions
871 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872 */
873static int x509_get_crt_ext( unsigned char **p,
874 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200875 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200876 mbedtls_x509_crt_ext_cb_t cb,
877 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878{
Janos Follath865b3eb2019-12-16 11:46:15 +0000879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200881 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882
Hanno Becker12f62fb2019-02-12 17:22:36 +0000883 if( *p == end )
884 return( 0 );
885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888
Hanno Becker12f62fb2019-02-12 17:22:36 +0000889 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 while( *p < end )
891 {
892 /*
893 * Extension ::= SEQUENCE {
894 * extnID OBJECT IDENTIFIER,
895 * critical BOOLEAN DEFAULT FALSE,
896 * extnValue OCTET STRING }
897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 int is_critical = 0; /* DEFAULT FALSE */
900 int ext_type = 0;
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
903 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100904 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905
906 end_ext_data = *p + len;
907
908 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200909 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200910 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100911 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
k-stachowiak463928a2018-07-24 12:50:59 +0200913 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 extn_oid.p = *p;
915 *p += extn_oid.len;
916
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
919 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100920 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921
922 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
924 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100925 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200927 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 end_ext_octet = *p + len;
929
930 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100931 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
932 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
934 /*
935 * Detect supported extensions
936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939 if( ret != 0 )
940 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200941 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200942 if( cb != NULL )
943 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200944 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200945 if( ret != 0 && is_critical )
946 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200947 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200948 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200949 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200950
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 /* No parser found, skip extension */
952 *p = end_ext_octet;
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 if( is_critical )
956 {
957 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100958 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
959 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 }
961#endif
962 continue;
963 }
964
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100965 /* Forbid repeated extensions */
966 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100968
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 crt->ext_types |= ext_type;
970
971 switch( ext_type )
972 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100973 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 /* Parse basic constraints */
975 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
976 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200977 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 break;
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 /* Parse key usage */
982 if( ( ret = x509_get_key_usage( p, end_ext_octet,
983 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200984 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 break;
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 /* Parse extended key usage */
989 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
990 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200991 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 break;
993
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100994 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 /* Parse subject alt name */
996 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
997 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200998 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 break;
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 /* Parse netscape certificate type */
1003 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1004 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001005 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 break;
1007
Ron Eldor74d9acc2019-03-21 14:00:03 +02001008 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1009 /* Parse certificate policies type */
1010 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1011 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001012 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001013 /* Give the callback (if any) a chance to handle the extension
1014 * if it contains unsupported policies */
1015 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1016 cb( p_ctx, crt, &extn_oid, is_critical,
1017 start_ext_octet, end_ext_octet ) == 0 )
1018 break;
1019
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001020#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1021 if( is_critical )
1022 return( ret );
1023 else
1024#endif
1025 /*
Ron Eldora2913912019-05-16 16:17:38 +03001026 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1027 * cannot interpret or enforce the policy. However, it is up to
1028 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001029 * unless the extension is critical.
1030 */
1031 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1032 return( ret );
1033 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001034 break;
1035
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001037 /*
1038 * If this is a non-critical extension, which the oid layer
1039 * supports, but there isn't an x509 parser for it,
1040 * skip the extension.
1041 */
1042#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1043 if( is_critical )
1044 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1045 else
1046#endif
1047 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 }
1049 }
1050
1051 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001052 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1053 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054
1055 return( 0 );
1056}
1057
1058/*
1059 * Parse and fill a single X.509 certificate in DER format
1060 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001061static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1062 const unsigned char *buf,
1063 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001064 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001065 mbedtls_x509_crt_ext_cb_t cb,
1066 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067{
Janos Follath865b3eb2019-12-16 11:46:15 +00001068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069 size_t len;
1070 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1074 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1075 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076
1077 /*
1078 * Check for valid input
1079 */
1080 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001083 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001084 p = (unsigned char*) buf;
1085 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 end = p + len;
1087
1088 /*
1089 * Certificate ::= SEQUENCE {
1090 * tbsCertificate TBSCertificate,
1091 * signatureAlgorithm AlgorithmIdentifier,
1092 * signatureValue BIT STRING }
1093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1095 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_x509_crt_free( crt );
1098 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 }
1100
Janos Follathcc0e49d2016-02-17 14:34:12 +00001101 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001102 crt->raw.len = crt_end - buf;
1103 if( make_copy != 0 )
1104 {
1105 /* Create and populate a new buffer for the raw field. */
1106 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1107 if( crt->raw.p == NULL )
1108 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1109
1110 memcpy( crt->raw.p, buf, crt->raw.len );
1111 crt->own_buffer = 1;
1112
1113 p += crt->raw.len - len;
1114 end = crt_end = p + len;
1115 }
1116 else
1117 {
1118 crt->raw.p = (unsigned char*) buf;
1119 crt->own_buffer = 0;
1120 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001121
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 /*
1123 * TBSCertificate ::= SEQUENCE {
1124 */
1125 crt->tbs.p = p;
1126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1128 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001131 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 }
1133
1134 end = p + len;
1135 crt->tbs.len = end - crt->tbs.p;
1136
1137 /*
1138 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1139 *
1140 * CertificateSerialNumber ::= INTEGER
1141 *
1142 * signature AlgorithmIdentifier
1143 */
1144 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1146 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001147 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 return( ret );
1151 }
1152
Andres AG7ca4a032017-03-09 16:16:11 +00001153 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 mbedtls_x509_crt_free( crt );
1156 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 }
1158
Andres AG7ca4a032017-03-09 16:16:11 +00001159 crt->version++;
1160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001162 &crt->sig_md, &crt->sig_pk,
1163 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 return( ret );
1167 }
1168
1169 /*
1170 * issuer Name
1171 */
1172 crt->issuer_raw.p = p;
1173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1175 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001178 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 }
1180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 return( ret );
1185 }
1186
1187 crt->issuer_raw.len = p - crt->issuer_raw.p;
1188
1189 /*
1190 * Validity ::= SEQUENCE {
1191 * notBefore Time,
1192 * notAfter Time }
1193 *
1194 */
1195 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1196 &crt->valid_to ) ) != 0 )
1197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199 return( ret );
1200 }
1201
1202 /*
1203 * subject Name
1204 */
1205 crt->subject_raw.p = p;
1206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001211 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 }
1213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217 return( ret );
1218 }
1219
1220 crt->subject_raw.len = p - crt->subject_raw.p;
1221
1222 /*
1223 * SubjectPublicKeyInfo
1224 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001225 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 return( ret );
1230 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001231 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232
1233 /*
1234 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1235 * -- If present, version shall be v2 or v3
1236 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1237 * -- If present, version shall be v2 or v3
1238 * extensions [3] EXPLICIT Extensions OPTIONAL
1239 * -- If present, version shall be v3
1240 */
1241 if( crt->version == 2 || crt->version == 3 )
1242 {
1243 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1244 if( ret != 0 )
1245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 return( ret );
1248 }
1249 }
1250
1251 if( crt->version == 2 || crt->version == 3 )
1252 {
1253 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1254 if( ret != 0 )
1255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001257 return( ret );
1258 }
1259 }
1260
David Horstmannab617512022-10-27 11:45:01 +01001261 int extensions_allowed = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
David Horstmannab617512022-10-27 11:45:01 +01001263 if( crt->version != 3 )
1264 extensions_allowed = 0;
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001265#endif
David Horstmannab617512022-10-27 11:45:01 +01001266 if( extensions_allowed )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001267 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001268 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 if( ret != 0 )
1270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 return( ret );
1273 }
1274 }
1275
1276 if( p != end )
1277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001279 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1280 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 }
1282
1283 end = crt_end;
1284
1285 /*
1286 * }
1287 * -- end of TBSCertificate
1288 *
1289 * signatureAlgorithm AlgorithmIdentifier,
1290 * signatureValue BIT STRING
1291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 return( ret );
1296 }
1297
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001298 if( crt->sig_oid.len != sig_oid2.len ||
1299 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001300 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001301 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001302 ( sig_params1.len != 0 &&
1303 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 mbedtls_x509_crt_free( crt );
1306 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 }
1308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 return( ret );
1313 }
1314
1315 if( p != end )
1316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001318 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1319 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 }
1321
1322 return( 0 );
1323}
1324
1325/*
1326 * Parse one X.509 certificate in DER format from a buffer and add them to a
1327 * chained list
1328 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001329static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1330 const unsigned char *buf,
1331 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001332 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001333 mbedtls_x509_crt_ext_cb_t cb,
1334 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335{
Janos Follath865b3eb2019-12-16 11:46:15 +00001336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001338
1339 /*
1340 * Check for valid input
1341 */
1342 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344
1345 while( crt->version != 0 && crt->next != NULL )
1346 {
1347 prev = crt;
1348 crt = crt->next;
1349 }
1350
1351 /*
1352 * Add new certificate on the end of the chain if needed.
1353 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001354 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001356 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357
1358 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001359 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360
1361 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364 }
1365
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001366 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001367 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368 {
1369 if( prev )
1370 prev->next = NULL;
1371
1372 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374
1375 return( ret );
1376 }
1377
1378 return( 0 );
1379}
1380
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001381int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1382 const unsigned char *buf,
1383 size_t buflen )
1384{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001385 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001386}
1387
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001388int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1389 const unsigned char *buf,
1390 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001391 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001392 mbedtls_x509_crt_ext_cb_t cb,
1393 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001394{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001395 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001396}
1397
1398int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1399 const unsigned char *buf,
1400 size_t buflen )
1401{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001402 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001403}
1404
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001406 * Parse one or more PEM certificates from a buffer and add them to the chained
1407 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001409int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1410 const unsigned char *buf,
1411 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412{
Janos Follath98e28a72016-05-31 14:03:54 +01001413#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001414 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001416#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417
1418 /*
1419 * Check for valid input
1420 */
1421 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423
1424 /*
1425 * Determine buffer content. Buffer contains either one DER certificate or
1426 * one or more PEM certificates.
1427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001429 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001430 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001433 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1436 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001437#else
1438 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1439#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_PEM_PARSE_C)
1442 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001447 /* 1 rather than 0 since the terminating NULL byte is counted in */
1448 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449 {
1450 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455 "-----BEGIN CERTIFICATE-----",
1456 "-----END CERTIFICATE-----",
1457 buf, NULL, 0, &use_len );
1458
1459 if( ret == 0 )
1460 {
1461 /*
1462 * Was PEM encoded
1463 */
1464 buflen -= use_len;
1465 buf += use_len;
1466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 {
1469 return( ret );
1470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474
1475 /*
1476 * PEM header and footer were found
1477 */
1478 buflen -= use_len;
1479 buf += use_len;
1480
1481 if( first_error == 0 )
1482 first_error = ret;
1483
Paul Bakker5a5fa922014-09-26 14:53:04 +02001484 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485 continue;
1486 }
1487 else
1488 break;
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
1494 if( ret != 0 )
1495 {
1496 /*
1497 * Quit parsing on a memory error
1498 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001499 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001500 return( ret );
1501
1502 if( first_error == 0 )
1503 first_error = ret;
1504
1505 total_failed++;
1506 continue;
1507 }
1508
1509 success = 1;
1510 }
1511 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512
1513 if( success )
1514 return( total_failed );
1515 else if( first_error )
1516 return( first_error );
1517 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001519#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520}
1521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523/*
1524 * Load one or more certificates and add them to the chained list
1525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527{
Janos Follath865b3eb2019-12-16 11:46:15 +00001528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529 size_t n;
1530 unsigned char *buf;
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533 return( ret );
1534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001537 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539
1540 return( ret );
1541}
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544{
1545 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001546#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547 int w_ret;
1548 WCHAR szDir[MAX_PATH];
1549 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001550 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001551 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552
Paul Bakker9af723c2014-05-01 13:03:14 +02001553 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554 HANDLE hFind;
1555
1556 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Paul Bakker9af723c2014-05-01 13:03:14 +02001559 memset( szDir, 0, sizeof(szDir) );
1560 memset( filename, 0, MAX_PATH );
1561 memcpy( filename, path, len );
1562 filename[len++] = '\\';
1563 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564 filename[len++] = '*';
1565
Simon B3c6b18d2016-11-03 01:11:37 +00001566 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001567 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001568 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570
1571 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001572 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
1575 len = MAX_PATH - len;
1576 do
1577 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001578 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579
1580 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1581 continue;
1582
Paul Bakker9af723c2014-05-01 13:03:14 +02001583 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001584 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001585 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001586 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001587 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001588 {
1589 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1590 goto cleanup;
1591 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 if( w_ret < 0 )
1595 ret++;
1596 else
1597 ret += w_ret;
1598 }
1599 while( FindNextFileW( hFind, &file_data ) != 0 );
1600
Paul Bakker66d5d072014-06-17 16:39:18 +02001601 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603
Ron Eldor36d90422017-01-09 15:09:16 +02001604cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001606#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001607 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001608 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001610 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001611 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612 DIR *dir = opendir( path );
1613
Paul Bakker66d5d072014-06-17 16:39:18 +02001614 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616
Ron Eldor63140682017-01-09 19:27:59 +02001617#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001618 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001619 {
1620 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001621 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001622 }
Ron Eldor63140682017-01-09 19:27:59 +02001623#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001624
Paul Elliottfb91a482021-03-05 14:17:51 +00001625 memset( &sb, 0, sizeof( sb ) );
1626
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001627 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 {
Andres AGf9113192016-09-02 14:06:04 +01001629 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1630 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631
Andres AGf9113192016-09-02 14:06:04 +01001632 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633 {
Andres AGf9113192016-09-02 14:06:04 +01001634 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1635 goto cleanup;
1636 }
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001637 else if( stat( entry_name, &sb ) == -1 )
Andres AGf9113192016-09-02 14:06:04 +01001638 {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001639 if( errno == ENOENT )
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001640 {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001641 /* Broken symbolic link - ignore this entry.
1642 stat(2) will return this error for either (a) a dangling
1643 symlink or (b) a missing file.
1644 Given that we have just obtained the filename from readdir,
1645 assume that it does exist and therefore treat this as a
1646 dangling symlink. */
1647 continue;
1648 }
1649 else
1650 {
1651 /* Some other file error; report the error. */
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001652 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1653 goto cleanup;
1654 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655 }
1656
1657 if( !S_ISREG( sb.st_mode ) )
1658 continue;
1659
1660 // Ignore parse errors
1661 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663 if( t_ret < 0 )
1664 ret++;
1665 else
1666 ret += t_ret;
1667 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001668
1669cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001670 closedir( dir );
1671
Ron Eldor63140682017-01-09 19:27:59 +02001672#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001673 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001675#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001676
Paul Bakkerbe089b02013-10-14 15:51:50 +02001677#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678
1679 return( ret );
1680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001683/*
1684 * OtherName ::= SEQUENCE {
1685 * type-id OBJECT IDENTIFIER,
1686 * value [0] EXPLICIT ANY DEFINED BY type-id }
1687 *
1688 * HardwareModuleName ::= SEQUENCE {
1689 * hwType OBJECT IDENTIFIER,
1690 * hwSerialNum OCTET STRING }
1691 *
1692 * NOTE: we currently only parse and use otherName of type HwModuleName,
1693 * as defined in RFC 4108.
1694 */
1695static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1696 mbedtls_x509_san_other_name *other_name )
1697{
Janos Follathab23cd12019-05-09 13:53:57 +01001698 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001699 size_t len;
1700 unsigned char *p = subject_alt_name->p;
1701 const unsigned char *end = p + subject_alt_name->len;
1702 mbedtls_x509_buf cur_oid;
1703
1704 if( ( subject_alt_name->tag &
1705 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1706 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1707 {
1708 /*
1709 * The given subject alternative name is not of type "othername".
1710 */
1711 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1712 }
1713
1714 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1715 MBEDTLS_ASN1_OID ) ) != 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 cur_oid.tag = MBEDTLS_ASN1_OID;
1719 cur_oid.p = p;
1720 cur_oid.len = len;
1721
1722 /*
1723 * Only HwModuleName is currently supported.
1724 */
1725 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1726 {
1727 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1728 }
1729
Ron Eldor890819a2019-05-13 19:03:04 +03001730 if( p + len >= end )
1731 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001732 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001733 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1734 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001735 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001736 p += len;
1737 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1738 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001739 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001740
1741 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1742 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001743 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001744
1745 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001746 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001747
1748 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1749 other_name->value.hardware_module_name.oid.p = p;
1750 other_name->value.hardware_module_name.oid.len = len;
1751
Ron Eldor890819a2019-05-13 19:03:04 +03001752 if( p + len >= end )
1753 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001754 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001755 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1756 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001757 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001758 p += len;
1759 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1760 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001761 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001762
1763 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1764 other_name->value.hardware_module_name.val.p = p;
1765 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001766 p += len;
1767 if( p != end )
1768 {
Ron Eldor890819a2019-05-13 19:03:04 +03001769 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001770 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001771 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1772 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001773 }
1774 return( 0 );
1775}
1776
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001777static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001778 const mbedtls_x509_sequence
1779 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001780 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001781{
Janos Follath865b3eb2019-12-16 11:46:15 +00001782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001783 size_t n = *size;
1784 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001786 mbedtls_x509_subject_alternative_name san;
1787 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001788
1789 while( cur != NULL )
1790 {
Ron Eldor890819a2019-05-13 19:03:04 +03001791 memset( &san, 0, sizeof( san ) );
1792 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1793 if( parse_ret != 0 )
1794 {
1795 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1796 {
1797 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1798 MBEDTLS_X509_SAFE_SNPRINTF;
1799 }
1800 else
1801 {
1802 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1803 MBEDTLS_X509_SAFE_SNPRINTF;
1804 }
1805 cur = cur->next;
1806 continue;
1807 }
1808
1809 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001810 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001811 /*
1812 * otherName
1813 */
Ron Eldora2913912019-05-16 16:17:38 +03001814 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001815 {
1816 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1817
1818 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1819 MBEDTLS_X509_SAFE_SNPRINTF;
1820
1821 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1822 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001823 {
Ron Eldor890819a2019-05-13 19:03:04 +03001824 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1825 MBEDTLS_X509_SAFE_SNPRINTF;
1826 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001827 MBEDTLS_X509_SAFE_SNPRINTF;
1828
Ron Eldor890819a2019-05-13 19:03:04 +03001829 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1830 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001831
Ron Eldor890819a2019-05-13 19:03:04 +03001832 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1833 MBEDTLS_X509_SAFE_SNPRINTF;
1834
1835 if( other_name->value.hardware_module_name.val.len >= n )
1836 {
1837 *p = '\0';
1838 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001839 }
1840
Ron Eldora2913912019-05-16 16:17:38 +03001841 memcpy( p, other_name->value.hardware_module_name.val.p,
1842 other_name->value.hardware_module_name.val.len );
1843 p += other_name->value.hardware_module_name.val.len;
1844
Ron Eldor890819a2019-05-13 19:03:04 +03001845 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001846
Ron Eldor890819a2019-05-13 19:03:04 +03001847 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1848 }
1849 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001850
1851 /*
1852 * dNSName
1853 */
Ron Eldora2913912019-05-16 16:17:38 +03001854 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001855 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001856 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1857 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001858 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001859 {
1860 *p = '\0';
1861 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1862 }
Ron Eldora2913912019-05-16 16:17:38 +03001863
1864 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1865 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001866 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001867 }
1868 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001869
1870 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001871 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001872 */
1873 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001874 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1875 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001876 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001877 }
1878
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001879 cur = cur->next;
1880 }
1881
1882 *p = '\0';
1883
1884 *size = n;
1885 *buf = p;
1886
1887 return( 0 );
1888}
1889
Ron Eldor890819a2019-05-13 19:03:04 +03001890int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1891 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001892{
Janos Follath865b3eb2019-12-16 11:46:15 +00001893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001894 switch( san_buf->tag &
1895 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1896 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001897 {
Ron Eldor890819a2019-05-13 19:03:04 +03001898 /*
1899 * otherName
1900 */
1901 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001902 {
Ron Eldor890819a2019-05-13 19:03:04 +03001903 mbedtls_x509_san_other_name other_name;
1904
1905 ret = x509_get_other_name( san_buf, &other_name );
1906 if( ret != 0 )
1907 return( ret );
1908
1909 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1910 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1911 memcpy( &san->san.other_name,
1912 &other_name, sizeof( other_name ) );
1913
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001914 }
Ron Eldor890819a2019-05-13 19:03:04 +03001915 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001916
Ron Eldor890819a2019-05-13 19:03:04 +03001917 /*
1918 * dNSName
1919 */
1920 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001921 {
Ron Eldor890819a2019-05-13 19:03:04 +03001922 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1923 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001924
Ron Eldor890819a2019-05-13 19:03:04 +03001925 memcpy( &san->san.unstructured_name,
1926 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001927
Ron Eldor890819a2019-05-13 19:03:04 +03001928 }
1929 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001930
Ron Eldor890819a2019-05-13 19:03:04 +03001931 /*
1932 * Type not supported
1933 */
1934 default:
1935 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1936 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001937 return( 0 );
1938}
1939
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001940#define PRINT_ITEM(i) \
1941 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001943 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001944 sep = ", "; \
1945 }
1946
1947#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001948 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001949 PRINT_ITEM( name );
1950
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001951static int x509_info_cert_type( char **buf, size_t *size,
1952 unsigned char ns_cert_type )
1953{
Janos Follath865b3eb2019-12-16 11:46:15 +00001954 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001955 size_t n = *size;
1956 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001957 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001960 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1962 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1963 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001964 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1965 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1966 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001967
1968 *size = n;
1969 *buf = p;
1970
1971 return( 0 );
1972}
1973
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001974#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001975 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001976 PRINT_ITEM( name );
1977
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001978static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001979 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001980{
Janos Follath865b3eb2019-12-16 11:46:15 +00001981 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001982 size_t n = *size;
1983 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001984 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1987 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001988 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1989 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1990 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1992 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001993 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1994 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001995
1996 *size = n;
1997 *buf = p;
1998
1999 return( 0 );
2000}
2001
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002002static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002004{
Janos Follath865b3eb2019-12-16 11:46:15 +00002005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002006 const char *desc;
2007 size_t n = *size;
2008 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002010 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002011
2012 while( cur != NULL )
2013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002015 desc = "???";
2016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002018 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002019
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002020 sep = ", ";
2021
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002022 cur = cur->next;
2023 }
2024
2025 *size = n;
2026 *buf = p;
2027
2028 return( 0 );
2029}
2030
Ron Eldor74d9acc2019-03-21 14:00:03 +02002031static int x509_info_cert_policies( char **buf, size_t *size,
2032 const mbedtls_x509_sequence *certificate_policies )
2033{
Janos Follath865b3eb2019-12-16 11:46:15 +00002034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002035 const char *desc;
2036 size_t n = *size;
2037 char *p = *buf;
2038 const mbedtls_x509_sequence *cur = certificate_policies;
2039 const char *sep = "";
2040
2041 while( cur != NULL )
2042 {
2043 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2044 desc = "???";
2045
2046 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2047 MBEDTLS_X509_SAFE_SNPRINTF;
2048
2049 sep = ", ";
2050
2051 cur = cur->next;
2052 }
2053
2054 *size = n;
2055 *buf = p;
2056
2057 return( 0 );
2058}
2059
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002060/*
2061 * Return an informational string about the certificate.
2062 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002063#define BEFORE_COLON 18
2064#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2066 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067{
Janos Follath865b3eb2019-12-16 11:46:15 +00002068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002069 size_t n;
2070 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002071 char key_size_str[BEFORE_COLON];
2072
2073 p = buf;
2074 n = size;
2075
Janos Follath98e28a72016-05-31 14:03:54 +01002076 if( NULL == crt )
2077 {
2078 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2079 MBEDTLS_X509_SAFE_SNPRINTF;
2080
2081 return( (int) ( size - n ) );
2082 }
2083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002086 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002089 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002092 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002095 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002100 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002102 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2106 crt->valid_from.year, crt->valid_from.mon,
2107 crt->valid_from.day, crt->valid_from.hour,
2108 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002109 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2113 crt->valid_to.year, crt->valid_to.mon,
2114 crt->valid_to.day, crt->valid_to.hour,
2115 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002116 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002119 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002122 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002123 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002125 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2127 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128 {
2129 return( ret );
2130 }
2131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002133 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002134 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002135
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002136 /*
2137 * Optional extensions
2138 */
2139
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002140 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002143 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002144 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002145
2146 if( crt->max_pathlen > 0 )
2147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002149 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002150 }
2151 }
2152
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002153 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002154 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002155 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002156 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002157
2158 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002159 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002160 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002161 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002162 }
2163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002167 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002168
2169 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2170 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002171 }
2172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002176 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002177
2178 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2179 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002180 }
2181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002185 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002186
2187 if( ( ret = x509_info_ext_key_usage( &p, &n,
2188 &crt->ext_key_usage ) ) != 0 )
2189 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002190 }
2191
Ron Eldor74d9acc2019-03-21 14:00:03 +02002192 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2193 {
2194 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2195 MBEDTLS_X509_SAFE_SNPRINTF;
2196
2197 if( ( ret = x509_info_cert_policies( &p, &n,
2198 &crt->certificate_policies ) ) != 0 )
2199 return( ret );
2200 }
2201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002203 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002204
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002205 return( (int) ( size - n ) );
2206}
2207
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002208struct x509_crt_verify_string {
2209 int code;
2210 const char *string;
2211};
2212
2213static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002214 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002215 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2216 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2217 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2218 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2219 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002220 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2221 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2222 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002223 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002224 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2225 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2226 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2227 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002228 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2229 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2230 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2231 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2232 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2233 { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002234 { 0, NULL }
2235};
2236
2237int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002238 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002239{
Janos Follath865b3eb2019-12-16 11:46:15 +00002240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002241 const struct x509_crt_verify_string *cur;
2242 char *p = buf;
2243 size_t n = size;
2244
2245 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2246 {
2247 if( ( flags & cur->code ) == 0 )
2248 continue;
2249
2250 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002251 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002252 flags ^= cur->code;
2253 }
2254
2255 if( flags != 0 )
2256 {
2257 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2258 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002259 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002260 }
2261
2262 return( (int) ( size - n ) );
2263}
2264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002266int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2267 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002268{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002269 unsigned int usage_must, usage_may;
2270 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2271 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2272
2273 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2274 return( 0 );
2275
2276 usage_must = usage & ~may_mask;
2277
2278 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2279 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2280
2281 usage_may = usage & may_mask;
2282
2283 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002285
2286 return( 0 );
2287}
2288#endif
2289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2291int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002292 const char *usage_oid,
2293 size_t usage_len )
2294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002296
2297 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002299 return( 0 );
2300
2301 /*
2302 * Look for the requested usage (or wildcard ANY) in our list
2303 */
2304 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002307
2308 if( cur_oid->len == usage_len &&
2309 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2310 {
2311 return( 0 );
2312 }
2313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002315 return( 0 );
2316 }
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002319}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323/*
2324 * Return 1 if the certificate is revoked, or 0 otherwise.
2325 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002326int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329
2330 while( cur != NULL && cur->serial.len != 0 )
2331 {
2332 if( crt->serial.len == cur->serial.len &&
2333 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2334 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002335 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336 }
2337
2338 cur = cur->next;
2339 }
2340
2341 return( 0 );
2342}
2343
2344/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002345 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002346 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002349 mbedtls_x509_crl *crl_list,
2350 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351{
2352 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2354 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355
2356 if( ca == NULL )
2357 return( flags );
2358
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002359 while( crl_list != NULL )
2360 {
2361 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002362 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002363 {
2364 crl_list = crl_list->next;
2365 continue;
2366 }
2367
2368 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002369 * Check if the CA is configured to sign CRLs
2370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002372 if( mbedtls_x509_crt_check_key_usage( ca,
2373 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002376 break;
2377 }
2378#endif
2379
2380 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002381 * Check if CRL is correctly signed by the trusted CA
2382 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002383 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2384 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2385
2386 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2387 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002390 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002391 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002392 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002394 break;
2395 }
2396
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002397 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002398 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2401 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002402 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002405 break;
2406 }
2407
2408 /*
2409 * Check for validity of CRL (Do not drop out)
2410 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002411 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002414 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002415 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002416
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002417 /*
2418 * Check if certificate is revoked
2419 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002420 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002423 break;
2424 }
2425
2426 crl_list = crl_list->next;
2427 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002428
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002429 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002432
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002433/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002434 * Check the signature of a certificate by its parent
2435 */
2436static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002437 mbedtls_x509_crt *parent,
2438 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002439{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002440 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002441 size_t hash_len;
2442#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2443 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002444 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002445 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002446
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002447 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002448 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002449 return( -1 );
2450#else
Jaeden Amero34973232019-02-20 10:32:28 +00002451 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002452 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2453
2454 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2455 return( -1 );
2456
2457 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2458 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002459 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002460 return( -1 );
2461 }
2462
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002463 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2464 != PSA_SUCCESS )
2465 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002466 return( -1 );
2467 }
2468#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002469 /* Skip expensive computation on obvious mismatch */
2470 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002471 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002472
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002473#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002474 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2475 {
2476 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002477 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002478 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002479 }
2480#else
2481 (void) rs_ctx;
2482#endif
2483
2484 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002485 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002486 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002487}
2488
2489/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002490 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2491 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002492 *
2493 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2496 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002497 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002498{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002499 int need_ca_bit;
2500
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002501 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002502 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002503 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002504
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002505 /* Parent must have the basicConstraints CA bit set as a general rule */
2506 need_ca_bit = 1;
2507
2508 /* Exception: v1/v2 certificates that are locally trusted. */
2509 if( top && parent->version < 3 )
2510 need_ca_bit = 0;
2511
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002512 if( need_ca_bit && ! parent->ca_istrue )
2513 return( -1 );
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002516 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002518 {
2519 return( -1 );
2520 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002521#endif
2522
2523 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002524}
2525
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002526/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002527 * Find a suitable parent for child in candidates, or return NULL.
2528 *
2529 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002530 * 1. subject name matches child's issuer
2531 * 2. if necessary, the CA bit is set and key usage allows signing certs
2532 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002533 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002534 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002535 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002536 * If there's a suitable candidate which is also time-valid, return the first
2537 * such. Otherwise, return the first suitable candidate (or NULL if there is
2538 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002539 *
2540 * The rationale for this rule is that someone could have a list of trusted
2541 * roots with two versions on the same root with different validity periods.
2542 * (At least one user reported having such a list and wanted it to just work.)
2543 * The reason we don't just require time-validity is that generally there is
2544 * only one version, and if it's expired we want the flags to state that
2545 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002546 *
2547 * The rationale for rule 3 (signature for trusted roots) is that users might
2548 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002549 * way we select the correct one is by checking the signature (as we don't
2550 * rely on key identifier extensions). (This is one way users might choose to
2551 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002552 *
2553 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002554 * - [in] child: certificate for which we're looking for a parent
2555 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002556 * - [out] r_parent: parent found (or NULL)
2557 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002558 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2559 * of the chain, 0 otherwise
2560 * - [in] path_cnt: number of intermediates seen so far
2561 * - [in] self_cnt: number of self-signed intermediates seen so far
2562 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002563 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002564 *
2565 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002566 * - 0 on success
2567 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002568 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002569static int x509_crt_find_parent_in(
2570 mbedtls_x509_crt *child,
2571 mbedtls_x509_crt *candidates,
2572 mbedtls_x509_crt **r_parent,
2573 int *r_signature_is_good,
2574 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002575 unsigned path_cnt,
2576 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002577 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002578{
Janos Follath865b3eb2019-12-16 11:46:15 +00002579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002580 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002581 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582
2583#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002584 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002585 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2586 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002587 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002588 parent = rs_ctx->parent;
2589 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002590 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002591
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002592 /* clear saved state */
2593 rs_ctx->parent = NULL;
2594 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002595 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002596
2597 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002598 goto check_signature;
2599 }
2600#endif
2601
2602 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002603 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002604
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002605 for( parent = candidates; parent != NULL; parent = parent->next )
2606 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002607 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002608 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002609 continue;
2610
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002611 /* +1 because stored max_pathlen is 1 higher that the actual value */
2612 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002613 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002614 {
2615 continue;
2616 }
2617
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002618 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002619#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2620check_signature:
2621#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002622 ret = x509_crt_check_signature( child, parent, rs_ctx );
2623
2624#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002625 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2626 {
2627 /* save state */
2628 rs_ctx->parent = parent;
2629 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002630 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002631
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002632 return( ret );
2633 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002634#else
2635 (void) ret;
2636#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002637
2638 signature_is_good = ret == 0;
2639 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002640 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002641
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002642 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002643 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2644 mbedtls_x509_time_is_future( &parent->valid_from ) )
2645 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002646 if( fallback_parent == NULL )
2647 {
2648 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002649 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002650 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002651
2652 continue;
2653 }
2654
Andy Gross1f627142019-01-30 10:25:53 -06002655 *r_parent = parent;
2656 *r_signature_is_good = signature_is_good;
2657
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002658 break;
2659 }
2660
Andy Gross1f627142019-01-30 10:25:53 -06002661 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002662 {
2663 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002664 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002665 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002666
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002667 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002668}
2669
2670/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002671 * Find a parent in trusted CAs or the provided chain, or return NULL.
2672 *
2673 * Searches in trusted CAs first, and return the first suitable parent found
2674 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002675 *
2676 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002677 * - [in] child: certificate for which we're looking for a parent, followed
2678 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002679 * - [in] trust_ca: list of locally trusted certificates
2680 * - [out] parent: parent found (or NULL)
2681 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2682 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2683 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2684 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002685 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002686 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002687 *
2688 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002689 * - 0 on success
2690 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002691 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002692static int x509_crt_find_parent(
2693 mbedtls_x509_crt *child,
2694 mbedtls_x509_crt *trust_ca,
2695 mbedtls_x509_crt **parent,
2696 int *parent_is_trusted,
2697 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002698 unsigned path_cnt,
2699 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002700 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002701{
Janos Follath865b3eb2019-12-16 11:46:15 +00002702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002703 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002704
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002705 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002706
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002707#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002708 /* restore then clear saved state if we have some stored */
2709 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2710 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002711 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002712 rs_ctx->parent_is_trusted = -1;
2713 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002714#endif
2715
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002716 while( 1 ) {
2717 search_list = *parent_is_trusted ? trust_ca : child->next;
2718
2719 ret = x509_crt_find_parent_in( child, search_list,
2720 parent, signature_is_good,
2721 *parent_is_trusted,
2722 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002723
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002724#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002725 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2726 {
2727 /* save state */
2728 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002729 return( ret );
2730 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002731#else
2732 (void) ret;
2733#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002734
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002735 /* stop here if found or already in second iteration */
2736 if( *parent != NULL || *parent_is_trusted == 0 )
2737 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002738
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002739 /* prepare second iteration */
2740 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002741 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002742
2743 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002744 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002745 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002746 *parent_is_trusted = 0;
2747 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002748 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002749
2750 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002751}
2752
2753/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002754 * Check if an end-entity certificate is locally trusted
2755 *
2756 * Currently we require such certificates to be self-signed (actually only
2757 * check for self-issued as self-signatures are not checked)
2758 */
2759static int x509_crt_check_ee_locally_trusted(
2760 mbedtls_x509_crt *crt,
2761 mbedtls_x509_crt *trust_ca )
2762{
2763 mbedtls_x509_crt *cur;
2764
2765 /* must be self-issued */
2766 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2767 return( -1 );
2768
2769 /* look for an exact match with trusted cert */
2770 for( cur = trust_ca; cur != NULL; cur = cur->next )
2771 {
2772 if( crt->raw.len == cur->raw.len &&
2773 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2774 {
2775 return( 0 );
2776 }
2777 }
2778
2779 /* too bad */
2780 return( -1 );
2781}
2782
2783/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002784 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002785 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002786 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2787 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002788 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002789 * such that every cert in the chain is a child of the next one,
2790 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002791 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002792 * Verify that chain and return it with flags for all issues found.
2793 *
2794 * Special cases:
2795 * - EE == Rj -> return a one-element list containing it
2796 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2797 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002798 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002799 * Tests for (aspects of) this function should include at least:
2800 * - trusted EE
2801 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002802 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002803 * - if relevant: EE untrusted
2804 * - if relevant: EE -> intermediate, untrusted
2805 * with the aspect under test checked at each relevant level (EE, int, root).
2806 * For some aspects longer chains are required, but usually length 2 is
2807 * enough (but length 1 is not in general).
2808 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002809 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002810 * - [in] crt: the cert list EE, C1, ..., Cn
2811 * - [in] trust_ca: the trusted list R1, ..., Rp
2812 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002813 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002814 * Only valid when return value is 0, may contain garbage otherwise!
2815 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002816 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002817 *
2818 * Return value:
2819 * - non-zero if the chain could not be fully built and examined
2820 * - 0 is the chain was successfully built and examined,
2821 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002822 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002823static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002824 mbedtls_x509_crt *crt,
2825 mbedtls_x509_crt *trust_ca,
2826 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002827 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2828 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002829 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002830 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002831 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002832{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002833 /* Don't initialize any of those variables here, so that the compiler can
2834 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002836 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002837 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002838 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002839 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002840 int parent_is_trusted;
2841 int child_is_trusted;
2842 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002843 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002844 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002845
2846#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2847 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002848 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002849 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002850 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002851 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002852 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002853
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002854 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002855 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002856 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002857 flags = &cur->flags;
2858
2859 goto find_parent;
2860 }
2861#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002862
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002863 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002864 self_cnt = 0;
2865 parent_is_trusted = 0;
2866 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002867
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002868 while( 1 ) {
2869 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002870 cur = &ver_chain->items[ver_chain->len];
2871 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002872 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002873 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002874 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002875
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002876 /* Check time-validity (all certificates) */
2877 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2878 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002879
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002880 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2881 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002882
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002883 /* Stop here for trusted roots (but not for trusted EE certs) */
2884 if( child_is_trusted )
2885 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002886
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002887 /* Check signature algorithm: MD & PK algs */
2888 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2889 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002890
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002891 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2892 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002893
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002894 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002895 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002896 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2897 {
2898 return( 0 );
2899 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002900
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002901#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2902find_parent:
2903#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002904
2905 /* Obtain list of potential trusted signers from CA callback,
2906 * or use statically provided list. */
2907#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2908 if( f_ca_cb != NULL )
2909 {
2910 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2911 mbedtls_free( ver_chain->trust_ca_cb_result );
2912 ver_chain->trust_ca_cb_result = NULL;
2913
2914 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2915 if( ret != 0 )
2916 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2917
2918 cur_trust_ca = ver_chain->trust_ca_cb_result;
2919 }
2920 else
2921#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2922 {
2923 ((void) f_ca_cb);
2924 ((void) p_ca_cb);
2925 cur_trust_ca = trust_ca;
2926 }
2927
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002928 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002929 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002930 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002931 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002932
2933#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002934 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2935 {
2936 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002937 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002938 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002939 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002940
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002941 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002942 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002943#else
2944 (void) ret;
2945#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002946
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002947 /* No parent? We're done here */
2948 if( parent == NULL )
2949 {
2950 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2951 return( 0 );
2952 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002953
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002954 /* Count intermediate self-issued (not necessarily self-signed) certs.
2955 * These can occur with some strategies for key rollover, see [SIRO],
2956 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002957 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002958 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2959 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002961 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002962
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002963 /* path_cnt is 0 for the first intermediate CA,
2964 * and if parent is trusted it's not an intermediate CA */
2965 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002966 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002967 {
2968 /* return immediately to avoid overflow the chain array */
2969 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2970 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002971
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002972 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002973 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002974 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2975
2976 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002977 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002978 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002981 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002982 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002983#else
2984 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002985#endif
2986
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002987 /* prepare for next iteration */
2988 child = parent;
2989 parent = NULL;
2990 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002991 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002992 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002993}
2994
2995/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002996 * Check for CN match
2997 */
2998static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2999 const char *cn, size_t cn_len )
3000{
3001 /* try exact match */
3002 if( name->len == cn_len &&
3003 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3004 {
3005 return( 0 );
3006 }
3007
3008 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003009 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003010 {
3011 return( 0 );
3012 }
3013
3014 return( -1 );
3015}
3016
3017/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003018 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3019 */
3020static int x509_crt_check_san( const mbedtls_x509_buf *name,
3021 const char *cn, size_t cn_len )
3022{
3023 const unsigned char san_type = (unsigned char) name->tag &
3024 MBEDTLS_ASN1_TAG_VALUE_MASK;
3025
3026 /* dNSName */
3027 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3028 return( x509_crt_check_cn( name, cn, cn_len ) );
3029
3030 /* (We may handle other types here later.) */
3031
3032 /* Unrecognized type */
3033 return( -1 );
3034}
3035
3036/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003037 * Verify the requested CN - only call this if cn is not NULL!
3038 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003039static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003040 const char *cn,
3041 uint32_t *flags )
3042{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003043 const mbedtls_x509_name *name;
3044 const mbedtls_x509_sequence *cur;
3045 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003046
3047 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3048 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003049 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003051 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003052 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003053 }
3054
3055 if( cur == NULL )
3056 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3057 }
3058 else
3059 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003060 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003061 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003062 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3063 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003064 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003065 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003066 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003067 }
3068
3069 if( name == NULL )
3070 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3071 }
3072}
3073
3074/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003075 * Merge the flags for all certs in the chain, after calling callback
3076 */
3077static int x509_crt_merge_flags_with_cb(
3078 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003079 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003080 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3081 void *p_vrfy )
3082{
Janos Follath865b3eb2019-12-16 11:46:15 +00003083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003084 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003085 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003086 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003087
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003088 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003089 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003090 cur = &ver_chain->items[i-1];
3091 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003092
3093 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003094 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003095 return( ret );
3096
3097 *flags |= cur_flags;
3098 }
3099
3100 return( 0 );
3101}
3102
3103/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003104 * Verify the certificate validity, with profile, restartable version
3105 *
3106 * This function:
3107 * - checks the requested CN (if any)
3108 * - checks the type and size of the EE cert's key,
3109 * as that isn't done as part of chain building/verification currently
3110 * - builds and verifies the chain
3111 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003112 *
3113 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3114 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3115 * verification routine to search for trusted signers, and CRLs will
3116 * be disabled. Otherwise, `trust_ca` will be used as the static list
3117 * of trusted signers, and `ca_crl` will be use as the static list
3118 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003119 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003120static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003121 mbedtls_x509_crt *trust_ca,
3122 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003123 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3124 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003125 const mbedtls_x509_crt_profile *profile,
3126 const char *cn, uint32_t *flags,
3127 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3128 void *p_vrfy,
3129 mbedtls_x509_crt_restart_ctx *rs_ctx )
3130{
Janos Follath865b3eb2019-12-16 11:46:15 +00003131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003132 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003133 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003134 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003135
3136 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003137 ee_flags = 0;
3138 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003139
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003140 if( profile == NULL )
3141 {
3142 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3143 goto exit;
3144 }
3145
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003146 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003147 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003148 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003149
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003150 /* Check the type and size of the key */
3151 pk_type = mbedtls_pk_get_type( &crt->pk );
3152
3153 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003154 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003155
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003156 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003157 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003158
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003159 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003160 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3161 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003162 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003163
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003164 if( ret != 0 )
3165 goto exit;
3166
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003167 /* Merge end-entity flags */
3168 ver_chain.items[0].flags |= ee_flags;
3169
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003170 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003171 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003172
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003173exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003174
3175#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3176 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3177 mbedtls_free( ver_chain.trust_ca_cb_result );
3178 ver_chain.trust_ca_cb_result = NULL;
3179#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3180
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003181#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3182 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3183 mbedtls_x509_crt_restart_free( rs_ctx );
3184#endif
3185
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003186 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3187 * the SSL module for authmode optional, but non-zero return from the
3188 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003189 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3190 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3191
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003192 if( ret != 0 )
3193 {
3194 *flags = (uint32_t) -1;
3195 return( ret );
3196 }
3197
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003198 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003200
3201 return( 0 );
3202}
3203
Hanno Becker3116fb32019-03-28 13:34:42 +00003204
3205/*
3206 * Verify the certificate validity (default profile, not restartable)
3207 */
3208int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3209 mbedtls_x509_crt *trust_ca,
3210 mbedtls_x509_crl *ca_crl,
3211 const char *cn, uint32_t *flags,
3212 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3213 void *p_vrfy )
3214{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003215 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003216 NULL, NULL,
3217 &mbedtls_x509_crt_profile_default,
3218 cn, flags,
3219 f_vrfy, p_vrfy, NULL ) );
3220}
3221
3222/*
3223 * Verify the certificate validity (user-chosen profile, not restartable)
3224 */
3225int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3226 mbedtls_x509_crt *trust_ca,
3227 mbedtls_x509_crl *ca_crl,
3228 const mbedtls_x509_crt_profile *profile,
3229 const char *cn, uint32_t *flags,
3230 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3231 void *p_vrfy )
3232{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003233 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003234 NULL, NULL,
3235 profile, cn, flags,
3236 f_vrfy, p_vrfy, NULL ) );
3237}
3238
3239#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3240/*
3241 * Verify the certificate validity (user-chosen profile, CA callback,
3242 * not restartable).
3243 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003244int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003245 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3246 void *p_ca_cb,
3247 const mbedtls_x509_crt_profile *profile,
3248 const char *cn, uint32_t *flags,
3249 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3250 void *p_vrfy )
3251{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003252 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003253 f_ca_cb, p_ca_cb,
3254 profile, cn, flags,
3255 f_vrfy, p_vrfy, NULL ) );
3256}
3257#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3258
3259int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3260 mbedtls_x509_crt *trust_ca,
3261 mbedtls_x509_crl *ca_crl,
3262 const mbedtls_x509_crt_profile *profile,
3263 const char *cn, uint32_t *flags,
3264 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3265 void *p_vrfy,
3266 mbedtls_x509_crt_restart_ctx *rs_ctx )
3267{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003268 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003269 NULL, NULL,
3270 profile, cn, flags,
3271 f_vrfy, p_vrfy, rs_ctx ) );
3272}
3273
3274
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003275/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003276 * Initialize a certificate chain
3277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003279{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003281}
3282
3283/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003284 * Unallocate all certificate data
3285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 mbedtls_x509_crt *cert_cur = crt;
3289 mbedtls_x509_crt *cert_prv;
3290 mbedtls_x509_name *name_cur;
3291 mbedtls_x509_name *name_prv;
3292 mbedtls_x509_sequence *seq_cur;
3293 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003294
3295 if( crt == NULL )
3296 return;
3297
3298 do
3299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3303 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003304#endif
3305
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003306 name_cur = cert_cur->issuer.next;
3307 while( name_cur != NULL )
3308 {
3309 name_prv = name_cur;
3310 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003311 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003313 }
3314
3315 name_cur = cert_cur->subject.next;
3316 while( name_cur != NULL )
3317 {
3318 name_prv = name_cur;
3319 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003320 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003322 }
3323
3324 seq_cur = cert_cur->ext_key_usage.next;
3325 while( seq_cur != NULL )
3326 {
3327 seq_prv = seq_cur;
3328 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003329 mbedtls_platform_zeroize( seq_prv,
3330 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003332 }
3333
3334 seq_cur = cert_cur->subject_alt_names.next;
3335 while( seq_cur != NULL )
3336 {
3337 seq_prv = seq_cur;
3338 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003339 mbedtls_platform_zeroize( seq_prv,
3340 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003342 }
3343
Ron Eldor74d9acc2019-03-21 14:00:03 +02003344 seq_cur = cert_cur->certificate_policies.next;
3345 while( seq_cur != NULL )
3346 {
3347 seq_prv = seq_cur;
3348 seq_cur = seq_cur->next;
3349 mbedtls_platform_zeroize( seq_prv,
3350 sizeof( mbedtls_x509_sequence ) );
3351 mbedtls_free( seq_prv );
3352 }
3353
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003354 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003355 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003356 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003358 }
3359
3360 cert_cur = cert_cur->next;
3361 }
3362 while( cert_cur != NULL );
3363
3364 cert_cur = crt;
3365 do
3366 {
3367 cert_prv = cert_cur;
3368 cert_cur = cert_cur->next;
3369
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003370 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003371 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003373 }
3374 while( cert_cur != NULL );
3375}
3376
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003377#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3378/*
3379 * Initialize a restart context
3380 */
3381void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3382{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003383 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003384
3385 ctx->parent = NULL;
3386 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003387 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003388
3389 ctx->parent_is_trusted = -1;
3390
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003391 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003392 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003393 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003394}
3395
3396/*
3397 * Free the components of a restart context
3398 */
3399void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3400{
3401 if( ctx == NULL )
3402 return;
3403
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003404 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003405 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003406}
3407#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409#endif /* MBEDTLS_X509_CRT_PARSE_C */