blob: d97c867c073ceb6b4187619b01961f226ebc120d [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001262 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001263#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001264 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001265 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 if( ret != 0 )
1267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 return( ret );
1270 }
1271 }
1272
1273 if( p != end )
1274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001276 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1277 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 }
1279
1280 end = crt_end;
1281
1282 /*
1283 * }
1284 * -- end of TBSCertificate
1285 *
1286 * signatureAlgorithm AlgorithmIdentifier,
1287 * signatureValue BIT STRING
1288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 return( ret );
1293 }
1294
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001295 if( crt->sig_oid.len != sig_oid2.len ||
1296 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001297 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001298 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001299 ( sig_params1.len != 0 &&
1300 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 mbedtls_x509_crt_free( crt );
1303 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 }
1305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 return( ret );
1310 }
1311
1312 if( p != end )
1313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001315 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1316 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 }
1318
1319 return( 0 );
1320}
1321
1322/*
1323 * Parse one X.509 certificate in DER format from a buffer and add them to a
1324 * chained list
1325 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001326static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1327 const unsigned char *buf,
1328 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001329 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001330 mbedtls_x509_crt_ext_cb_t cb,
1331 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001332{
Janos Follath865b3eb2019-12-16 11:46:15 +00001333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335
1336 /*
1337 * Check for valid input
1338 */
1339 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341
1342 while( crt->version != 0 && crt->next != NULL )
1343 {
1344 prev = crt;
1345 crt = crt->next;
1346 }
1347
1348 /*
1349 * Add new certificate on the end of the chain if needed.
1350 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001351 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001353 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354
1355 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001356 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357
1358 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361 }
1362
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001363 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001364 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365 {
1366 if( prev )
1367 prev->next = NULL;
1368
1369 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371
1372 return( ret );
1373 }
1374
1375 return( 0 );
1376}
1377
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001378int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1379 const unsigned char *buf,
1380 size_t buflen )
1381{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001382 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001383}
1384
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001385int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1386 const unsigned char *buf,
1387 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001388 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001389 mbedtls_x509_crt_ext_cb_t cb,
1390 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001391{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001392 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001393}
1394
1395int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1396 const unsigned char *buf,
1397 size_t buflen )
1398{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001399 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001400}
1401
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001403 * Parse one or more PEM certificates from a buffer and add them to the chained
1404 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001406int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1407 const unsigned char *buf,
1408 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409{
Janos Follath98e28a72016-05-31 14:03:54 +01001410#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001411 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001413#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414
1415 /*
1416 * Check for valid input
1417 */
1418 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420
1421 /*
1422 * Determine buffer content. Buffer contains either one DER certificate or
1423 * one or more PEM certificates.
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001426 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001427 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001430 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1433 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001434#else
1435 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1436#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_PEM_PARSE_C)
1439 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001444 /* 1 rather than 0 since the terminating NULL byte is counted in */
1445 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446 {
1447 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452 "-----BEGIN CERTIFICATE-----",
1453 "-----END CERTIFICATE-----",
1454 buf, NULL, 0, &use_len );
1455
1456 if( ret == 0 )
1457 {
1458 /*
1459 * Was PEM encoded
1460 */
1461 buflen -= use_len;
1462 buf += use_len;
1463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465 {
1466 return( ret );
1467 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471
1472 /*
1473 * PEM header and footer were found
1474 */
1475 buflen -= use_len;
1476 buf += use_len;
1477
1478 if( first_error == 0 )
1479 first_error = ret;
1480
Paul Bakker5a5fa922014-09-26 14:53:04 +02001481 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482 continue;
1483 }
1484 else
1485 break;
1486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490
1491 if( ret != 0 )
1492 {
1493 /*
1494 * Quit parsing on a memory error
1495 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001496 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497 return( ret );
1498
1499 if( first_error == 0 )
1500 first_error = ret;
1501
1502 total_failed++;
1503 continue;
1504 }
1505
1506 success = 1;
1507 }
1508 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509
1510 if( success )
1511 return( total_failed );
1512 else if( first_error )
1513 return( first_error );
1514 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001516#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517}
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520/*
1521 * Load one or more certificates and add them to the chained list
1522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524{
Janos Follath865b3eb2019-12-16 11:46:15 +00001525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 size_t n;
1527 unsigned char *buf;
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530 return( ret );
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001534 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536
1537 return( ret );
1538}
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541{
1542 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001543#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 int w_ret;
1545 WCHAR szDir[MAX_PATH];
1546 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001547 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001548 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549
Paul Bakker9af723c2014-05-01 13:03:14 +02001550 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 HANDLE hFind;
1552
1553 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555
Paul Bakker9af723c2014-05-01 13:03:14 +02001556 memset( szDir, 0, sizeof(szDir) );
1557 memset( filename, 0, MAX_PATH );
1558 memcpy( filename, path, len );
1559 filename[len++] = '\\';
1560 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561 filename[len++] = '*';
1562
Simon B3c6b18d2016-11-03 01:11:37 +00001563 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001564 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001565 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567
1568 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001569 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571
1572 len = MAX_PATH - len;
1573 do
1574 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001575 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
1577 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1578 continue;
1579
Paul Bakker9af723c2014-05-01 13:03:14 +02001580 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001581 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001582 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001583 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001584 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001585 {
1586 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1587 goto cleanup;
1588 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591 if( w_ret < 0 )
1592 ret++;
1593 else
1594 ret += w_ret;
1595 }
1596 while( FindNextFileW( hFind, &file_data ) != 0 );
1597
Paul Bakker66d5d072014-06-17 16:39:18 +02001598 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Ron Eldor36d90422017-01-09 15:09:16 +02001601cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001603#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001604 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001605 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001607 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001608 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 DIR *dir = opendir( path );
1610
Paul Bakker66d5d072014-06-17 16:39:18 +02001611 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613
Ron Eldor63140682017-01-09 19:27:59 +02001614#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001615 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001616 {
1617 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001618 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001619 }
Ron Eldor63140682017-01-09 19:27:59 +02001620#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001621
Paul Elliottfb91a482021-03-05 14:17:51 +00001622 memset( &sb, 0, sizeof( sb ) );
1623
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001624 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625 {
Andres AGf9113192016-09-02 14:06:04 +01001626 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1627 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628
Andres AGf9113192016-09-02 14:06:04 +01001629 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630 {
Andres AGf9113192016-09-02 14:06:04 +01001631 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1632 goto cleanup;
1633 }
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001634 else if( stat( entry_name, &sb ) == -1 )
Andres AGf9113192016-09-02 14:06:04 +01001635 {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001636 if( errno == ENOENT )
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001637 {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001638 /* Broken symbolic link - ignore this entry.
1639 stat(2) will return this error for either (a) a dangling
1640 symlink or (b) a missing file.
1641 Given that we have just obtained the filename from readdir,
1642 assume that it does exist and therefore treat this as a
1643 dangling symlink. */
1644 continue;
1645 }
1646 else
1647 {
1648 /* Some other file error; report the error. */
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001649 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1650 goto cleanup;
1651 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652 }
1653
1654 if( !S_ISREG( sb.st_mode ) )
1655 continue;
1656
1657 // Ignore parse errors
1658 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660 if( t_ret < 0 )
1661 ret++;
1662 else
1663 ret += t_ret;
1664 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001665
1666cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001667 closedir( dir );
1668
Ron Eldor63140682017-01-09 19:27:59 +02001669#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001670 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001672#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001673
Paul Bakkerbe089b02013-10-14 15:51:50 +02001674#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675
1676 return( ret );
1677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001680/*
1681 * OtherName ::= SEQUENCE {
1682 * type-id OBJECT IDENTIFIER,
1683 * value [0] EXPLICIT ANY DEFINED BY type-id }
1684 *
1685 * HardwareModuleName ::= SEQUENCE {
1686 * hwType OBJECT IDENTIFIER,
1687 * hwSerialNum OCTET STRING }
1688 *
1689 * NOTE: we currently only parse and use otherName of type HwModuleName,
1690 * as defined in RFC 4108.
1691 */
1692static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1693 mbedtls_x509_san_other_name *other_name )
1694{
Janos Follathab23cd12019-05-09 13:53:57 +01001695 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001696 size_t len;
1697 unsigned char *p = subject_alt_name->p;
1698 const unsigned char *end = p + subject_alt_name->len;
1699 mbedtls_x509_buf cur_oid;
1700
1701 if( ( subject_alt_name->tag &
1702 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1703 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1704 {
1705 /*
1706 * The given subject alternative name is not of type "othername".
1707 */
1708 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1709 }
1710
1711 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1712 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001713 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001714
1715 cur_oid.tag = MBEDTLS_ASN1_OID;
1716 cur_oid.p = p;
1717 cur_oid.len = len;
1718
1719 /*
1720 * Only HwModuleName is currently supported.
1721 */
1722 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1723 {
1724 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1725 }
1726
Ron Eldor890819a2019-05-13 19:03:04 +03001727 if( p + len >= end )
1728 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001729 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001730 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1731 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001732 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001733 p += len;
1734 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1735 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001736 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001737
1738 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1739 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001740 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001741
1742 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 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 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1746 other_name->value.hardware_module_name.oid.p = p;
1747 other_name->value.hardware_module_name.oid.len = len;
1748
Ron Eldor890819a2019-05-13 19:03:04 +03001749 if( p + len >= end )
1750 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001751 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001752 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1753 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001754 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001755 p += len;
1756 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1757 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001758 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001759
1760 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1761 other_name->value.hardware_module_name.val.p = p;
1762 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001763 p += len;
1764 if( p != end )
1765 {
Ron Eldor890819a2019-05-13 19:03:04 +03001766 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001767 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001768 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1769 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001770 }
1771 return( 0 );
1772}
1773
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001774static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001775 const mbedtls_x509_sequence
1776 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001777 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001778{
Janos Follath865b3eb2019-12-16 11:46:15 +00001779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001780 size_t n = *size;
1781 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001783 mbedtls_x509_subject_alternative_name san;
1784 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001785
1786 while( cur != NULL )
1787 {
Ron Eldor890819a2019-05-13 19:03:04 +03001788 memset( &san, 0, sizeof( san ) );
1789 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1790 if( parse_ret != 0 )
1791 {
1792 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1793 {
1794 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1795 MBEDTLS_X509_SAFE_SNPRINTF;
1796 }
1797 else
1798 {
1799 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1800 MBEDTLS_X509_SAFE_SNPRINTF;
1801 }
1802 cur = cur->next;
1803 continue;
1804 }
1805
1806 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001807 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001808 /*
1809 * otherName
1810 */
Ron Eldora2913912019-05-16 16:17:38 +03001811 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001812 {
1813 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1814
1815 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1816 MBEDTLS_X509_SAFE_SNPRINTF;
1817
1818 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1819 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001820 {
Ron Eldor890819a2019-05-13 19:03:04 +03001821 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1822 MBEDTLS_X509_SAFE_SNPRINTF;
1823 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001824 MBEDTLS_X509_SAFE_SNPRINTF;
1825
Ron Eldor890819a2019-05-13 19:03:04 +03001826 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1827 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001828
Ron Eldor890819a2019-05-13 19:03:04 +03001829 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1830 MBEDTLS_X509_SAFE_SNPRINTF;
1831
1832 if( other_name->value.hardware_module_name.val.len >= n )
1833 {
1834 *p = '\0';
1835 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001836 }
1837
Ron Eldora2913912019-05-16 16:17:38 +03001838 memcpy( p, other_name->value.hardware_module_name.val.p,
1839 other_name->value.hardware_module_name.val.len );
1840 p += other_name->value.hardware_module_name.val.len;
1841
Ron Eldor890819a2019-05-13 19:03:04 +03001842 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001843
Ron Eldor890819a2019-05-13 19:03:04 +03001844 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1845 }
1846 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001847
1848 /*
1849 * dNSName
1850 */
Ron Eldora2913912019-05-16 16:17:38 +03001851 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001852 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001853 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1854 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001855 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001856 {
1857 *p = '\0';
1858 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1859 }
Ron Eldora2913912019-05-16 16:17:38 +03001860
1861 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1862 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001863 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001864 }
1865 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001866
1867 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001868 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001869 */
1870 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001871 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1872 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001873 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001874 }
1875
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001876 cur = cur->next;
1877 }
1878
1879 *p = '\0';
1880
1881 *size = n;
1882 *buf = p;
1883
1884 return( 0 );
1885}
1886
Ron Eldor890819a2019-05-13 19:03:04 +03001887int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1888 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001889{
Janos Follath865b3eb2019-12-16 11:46:15 +00001890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001891 switch( san_buf->tag &
1892 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1893 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001894 {
Ron Eldor890819a2019-05-13 19:03:04 +03001895 /*
1896 * otherName
1897 */
1898 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001899 {
Ron Eldor890819a2019-05-13 19:03:04 +03001900 mbedtls_x509_san_other_name other_name;
1901
1902 ret = x509_get_other_name( san_buf, &other_name );
1903 if( ret != 0 )
1904 return( ret );
1905
1906 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1907 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1908 memcpy( &san->san.other_name,
1909 &other_name, sizeof( other_name ) );
1910
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001911 }
Ron Eldor890819a2019-05-13 19:03:04 +03001912 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001913
Ron Eldor890819a2019-05-13 19:03:04 +03001914 /*
1915 * dNSName
1916 */
1917 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001918 {
Ron Eldor890819a2019-05-13 19:03:04 +03001919 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1920 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001921
Ron Eldor890819a2019-05-13 19:03:04 +03001922 memcpy( &san->san.unstructured_name,
1923 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001924
Ron Eldor890819a2019-05-13 19:03:04 +03001925 }
1926 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001927
Ron Eldor890819a2019-05-13 19:03:04 +03001928 /*
1929 * Type not supported
1930 */
1931 default:
1932 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1933 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001934 return( 0 );
1935}
1936
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001937#define PRINT_ITEM(i) \
1938 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001940 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001941 sep = ", "; \
1942 }
1943
1944#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001945 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001946 PRINT_ITEM( name );
1947
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001948static int x509_info_cert_type( char **buf, size_t *size,
1949 unsigned char ns_cert_type )
1950{
Janos Follath865b3eb2019-12-16 11:46:15 +00001951 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001952 size_t n = *size;
1953 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001954 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001957 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1959 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1960 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001961 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1962 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1963 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001964
1965 *size = n;
1966 *buf = p;
1967
1968 return( 0 );
1969}
1970
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001971#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001972 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001973 PRINT_ITEM( name );
1974
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001975static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001976 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001977{
Janos Follath865b3eb2019-12-16 11:46:15 +00001978 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001979 size_t n = *size;
1980 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001981 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1984 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001985 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1986 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1987 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1989 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001990 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1991 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001992
1993 *size = n;
1994 *buf = p;
1995
1996 return( 0 );
1997}
1998
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001999static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002001{
Janos Follath865b3eb2019-12-16 11:46:15 +00002002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002003 const char *desc;
2004 size_t n = *size;
2005 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002007 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002008
2009 while( cur != NULL )
2010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002012 desc = "???";
2013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002015 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002016
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002017 sep = ", ";
2018
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002019 cur = cur->next;
2020 }
2021
2022 *size = n;
2023 *buf = p;
2024
2025 return( 0 );
2026}
2027
Ron Eldor74d9acc2019-03-21 14:00:03 +02002028static int x509_info_cert_policies( char **buf, size_t *size,
2029 const mbedtls_x509_sequence *certificate_policies )
2030{
Janos Follath865b3eb2019-12-16 11:46:15 +00002031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002032 const char *desc;
2033 size_t n = *size;
2034 char *p = *buf;
2035 const mbedtls_x509_sequence *cur = certificate_policies;
2036 const char *sep = "";
2037
2038 while( cur != NULL )
2039 {
2040 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2041 desc = "???";
2042
2043 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2044 MBEDTLS_X509_SAFE_SNPRINTF;
2045
2046 sep = ", ";
2047
2048 cur = cur->next;
2049 }
2050
2051 *size = n;
2052 *buf = p;
2053
2054 return( 0 );
2055}
2056
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057/*
2058 * Return an informational string about the certificate.
2059 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002060#define BEFORE_COLON 18
2061#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2063 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002064{
Janos Follath865b3eb2019-12-16 11:46:15 +00002065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002066 size_t n;
2067 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002068 char key_size_str[BEFORE_COLON];
2069
2070 p = buf;
2071 n = size;
2072
Janos Follath98e28a72016-05-31 14:03:54 +01002073 if( NULL == crt )
2074 {
2075 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2076 MBEDTLS_X509_SAFE_SNPRINTF;
2077
2078 return( (int) ( size - n ) );
2079 }
2080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002082 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002083 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002086 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
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_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002092 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002094 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002097 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002099 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002102 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2103 crt->valid_from.year, crt->valid_from.mon,
2104 crt->valid_from.day, crt->valid_from.hour,
2105 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002106 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2110 crt->valid_to.year, crt->valid_to.mon,
2111 crt->valid_to.day, crt->valid_to.hour,
2112 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002113 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
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_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002119 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002120 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002121
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002122 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2124 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125 {
2126 return( ret );
2127 }
2128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002130 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002131 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002133 /*
2134 * Optional extensions
2135 */
2136
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002137 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002140 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002141 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002142
2143 if( crt->max_pathlen > 0 )
2144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002146 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002147 }
2148 }
2149
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002150 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002151 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002152 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002153 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002154
2155 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002156 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002157 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002158 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002159 }
2160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002164 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002165
2166 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2167 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002168 }
2169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002173 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002174
2175 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2176 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002177 }
2178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002182 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002183
2184 if( ( ret = x509_info_ext_key_usage( &p, &n,
2185 &crt->ext_key_usage ) ) != 0 )
2186 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002187 }
2188
Ron Eldor74d9acc2019-03-21 14:00:03 +02002189 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2190 {
2191 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2192 MBEDTLS_X509_SAFE_SNPRINTF;
2193
2194 if( ( ret = x509_info_cert_policies( &p, &n,
2195 &crt->certificate_policies ) ) != 0 )
2196 return( ret );
2197 }
2198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002200 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002201
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002202 return( (int) ( size - n ) );
2203}
2204
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002205struct x509_crt_verify_string {
2206 int code;
2207 const char *string;
2208};
2209
2210static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002211 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002212 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2213 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2214 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2215 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2216 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002217 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2218 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2219 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002220 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002221 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2222 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2223 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2224 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002225 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2226 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2227 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2228 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2229 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2230 { 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 +01002231 { 0, NULL }
2232};
2233
2234int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002235 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002236{
Janos Follath865b3eb2019-12-16 11:46:15 +00002237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002238 const struct x509_crt_verify_string *cur;
2239 char *p = buf;
2240 size_t n = size;
2241
2242 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2243 {
2244 if( ( flags & cur->code ) == 0 )
2245 continue;
2246
2247 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002248 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002249 flags ^= cur->code;
2250 }
2251
2252 if( flags != 0 )
2253 {
2254 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2255 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002256 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002257 }
2258
2259 return( (int) ( size - n ) );
2260}
2261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002263int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2264 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002265{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002266 unsigned int usage_must, usage_may;
2267 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2268 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2269
2270 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2271 return( 0 );
2272
2273 usage_must = usage & ~may_mask;
2274
2275 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2276 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2277
2278 usage_may = usage & may_mask;
2279
2280 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002282
2283 return( 0 );
2284}
2285#endif
2286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2288int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002289 const char *usage_oid,
2290 size_t usage_len )
2291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002293
2294 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002296 return( 0 );
2297
2298 /*
2299 * Look for the requested usage (or wildcard ANY) in our list
2300 */
2301 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002304
2305 if( cur_oid->len == usage_len &&
2306 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2307 {
2308 return( 0 );
2309 }
2310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002312 return( 0 );
2313 }
2314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002320/*
2321 * Return 1 if the certificate is revoked, or 0 otherwise.
2322 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002323int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002326
2327 while( cur != NULL && cur->serial.len != 0 )
2328 {
2329 if( crt->serial.len == cur->serial.len &&
2330 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2331 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002332 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333 }
2334
2335 cur = cur->next;
2336 }
2337
2338 return( 0 );
2339}
2340
2341/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002342 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002343 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002346 mbedtls_x509_crl *crl_list,
2347 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002348{
2349 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2351 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002352
2353 if( ca == NULL )
2354 return( flags );
2355
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002356 while( crl_list != NULL )
2357 {
2358 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002359 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002360 {
2361 crl_list = crl_list->next;
2362 continue;
2363 }
2364
2365 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002366 * Check if the CA is configured to sign CRLs
2367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002369 if( mbedtls_x509_crt_check_key_usage( ca,
2370 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002373 break;
2374 }
2375#endif
2376
2377 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002378 * Check if CRL is correctly signed by the trusted CA
2379 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002380 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2381 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2382
2383 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2384 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002387 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002388 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002389 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002391 break;
2392 }
2393
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002394 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002395 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2398 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002399 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002402 break;
2403 }
2404
2405 /*
2406 * Check for validity of CRL (Do not drop out)
2407 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002408 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002411 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002412 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002413
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002414 /*
2415 * Check if certificate is revoked
2416 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002417 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002420 break;
2421 }
2422
2423 crl_list = crl_list->next;
2424 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002425
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002426 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002429
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002430/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002431 * Check the signature of a certificate by its parent
2432 */
2433static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002434 mbedtls_x509_crt *parent,
2435 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002436{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002437 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002438 size_t hash_len;
2439#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2440 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002441 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002442 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002443
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002444 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002445 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002446 return( -1 );
2447#else
Jaeden Amero34973232019-02-20 10:32:28 +00002448 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002449 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2450
2451 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2452 return( -1 );
2453
2454 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2455 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002456 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002457 return( -1 );
2458 }
2459
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002460 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2461 != PSA_SUCCESS )
2462 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002463 return( -1 );
2464 }
2465#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002466 /* Skip expensive computation on obvious mismatch */
2467 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002468 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002469
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002470#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002471 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2472 {
2473 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002474 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002475 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002476 }
2477#else
2478 (void) rs_ctx;
2479#endif
2480
2481 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002482 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002483 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002484}
2485
2486/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002487 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2488 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002489 *
2490 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2493 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002494 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002495{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002496 int need_ca_bit;
2497
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002498 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002499 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002500 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002501
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002502 /* Parent must have the basicConstraints CA bit set as a general rule */
2503 need_ca_bit = 1;
2504
2505 /* Exception: v1/v2 certificates that are locally trusted. */
2506 if( top && parent->version < 3 )
2507 need_ca_bit = 0;
2508
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002509 if( need_ca_bit && ! parent->ca_istrue )
2510 return( -1 );
2511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002513 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002515 {
2516 return( -1 );
2517 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002518#endif
2519
2520 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002521}
2522
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002523/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002524 * Find a suitable parent for child in candidates, or return NULL.
2525 *
2526 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002527 * 1. subject name matches child's issuer
2528 * 2. if necessary, the CA bit is set and key usage allows signing certs
2529 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002530 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002531 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002532 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002533 * If there's a suitable candidate which is also time-valid, return the first
2534 * such. Otherwise, return the first suitable candidate (or NULL if there is
2535 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002536 *
2537 * The rationale for this rule is that someone could have a list of trusted
2538 * roots with two versions on the same root with different validity periods.
2539 * (At least one user reported having such a list and wanted it to just work.)
2540 * The reason we don't just require time-validity is that generally there is
2541 * only one version, and if it's expired we want the flags to state that
2542 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002543 *
2544 * The rationale for rule 3 (signature for trusted roots) is that users might
2545 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002546 * way we select the correct one is by checking the signature (as we don't
2547 * rely on key identifier extensions). (This is one way users might choose to
2548 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002549 *
2550 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002551 * - [in] child: certificate for which we're looking for a parent
2552 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002553 * - [out] r_parent: parent found (or NULL)
2554 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002555 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2556 * of the chain, 0 otherwise
2557 * - [in] path_cnt: number of intermediates seen so far
2558 * - [in] self_cnt: number of self-signed intermediates seen so far
2559 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002560 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002561 *
2562 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002563 * - 0 on success
2564 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002565 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002566static int x509_crt_find_parent_in(
2567 mbedtls_x509_crt *child,
2568 mbedtls_x509_crt *candidates,
2569 mbedtls_x509_crt **r_parent,
2570 int *r_signature_is_good,
2571 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002572 unsigned path_cnt,
2573 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002574 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002575{
Janos Follath865b3eb2019-12-16 11:46:15 +00002576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002577 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002578 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002579
2580#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002581 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2583 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002584 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002585 parent = rs_ctx->parent;
2586 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002587 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002588
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002589 /* clear saved state */
2590 rs_ctx->parent = NULL;
2591 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002592 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002593
2594 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002595 goto check_signature;
2596 }
2597#endif
2598
2599 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002600 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002601
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002602 for( parent = candidates; parent != NULL; parent = parent->next )
2603 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002604 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002605 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002606 continue;
2607
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002608 /* +1 because stored max_pathlen is 1 higher that the actual value */
2609 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002610 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002611 {
2612 continue;
2613 }
2614
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002615 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002616#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2617check_signature:
2618#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002619 ret = x509_crt_check_signature( child, parent, rs_ctx );
2620
2621#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002622 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2623 {
2624 /* save state */
2625 rs_ctx->parent = parent;
2626 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002627 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002628
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002629 return( ret );
2630 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002631#else
2632 (void) ret;
2633#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002634
2635 signature_is_good = ret == 0;
2636 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002637 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002638
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002639 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002640 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2641 mbedtls_x509_time_is_future( &parent->valid_from ) )
2642 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002643 if( fallback_parent == NULL )
2644 {
2645 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002646 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002647 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002648
2649 continue;
2650 }
2651
Andy Gross1f627142019-01-30 10:25:53 -06002652 *r_parent = parent;
2653 *r_signature_is_good = signature_is_good;
2654
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002655 break;
2656 }
2657
Andy Gross1f627142019-01-30 10:25:53 -06002658 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002659 {
2660 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002661 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002662 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002663
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002664 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002665}
2666
2667/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002668 * Find a parent in trusted CAs or the provided chain, or return NULL.
2669 *
2670 * Searches in trusted CAs first, and return the first suitable parent found
2671 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002672 *
2673 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002674 * - [in] child: certificate for which we're looking for a parent, followed
2675 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002676 * - [in] trust_ca: list of locally trusted certificates
2677 * - [out] parent: parent found (or NULL)
2678 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2679 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2680 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2681 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002682 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002683 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002684 *
2685 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002686 * - 0 on success
2687 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002688 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002689static int x509_crt_find_parent(
2690 mbedtls_x509_crt *child,
2691 mbedtls_x509_crt *trust_ca,
2692 mbedtls_x509_crt **parent,
2693 int *parent_is_trusted,
2694 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002695 unsigned path_cnt,
2696 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002697 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002698{
Janos Follath865b3eb2019-12-16 11:46:15 +00002699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002700 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002701
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002702 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002703
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002704#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002705 /* restore then clear saved state if we have some stored */
2706 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2707 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002708 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002709 rs_ctx->parent_is_trusted = -1;
2710 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002711#endif
2712
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002713 while( 1 ) {
2714 search_list = *parent_is_trusted ? trust_ca : child->next;
2715
2716 ret = x509_crt_find_parent_in( child, search_list,
2717 parent, signature_is_good,
2718 *parent_is_trusted,
2719 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002720
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002721#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002722 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2723 {
2724 /* save state */
2725 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002726 return( ret );
2727 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002728#else
2729 (void) ret;
2730#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002731
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002732 /* stop here if found or already in second iteration */
2733 if( *parent != NULL || *parent_is_trusted == 0 )
2734 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002735
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002736 /* prepare second iteration */
2737 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002738 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002739
2740 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002741 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002742 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002743 *parent_is_trusted = 0;
2744 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002745 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002746
2747 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002748}
2749
2750/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002751 * Check if an end-entity certificate is locally trusted
2752 *
2753 * Currently we require such certificates to be self-signed (actually only
2754 * check for self-issued as self-signatures are not checked)
2755 */
2756static int x509_crt_check_ee_locally_trusted(
2757 mbedtls_x509_crt *crt,
2758 mbedtls_x509_crt *trust_ca )
2759{
2760 mbedtls_x509_crt *cur;
2761
2762 /* must be self-issued */
2763 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2764 return( -1 );
2765
2766 /* look for an exact match with trusted cert */
2767 for( cur = trust_ca; cur != NULL; cur = cur->next )
2768 {
2769 if( crt->raw.len == cur->raw.len &&
2770 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2771 {
2772 return( 0 );
2773 }
2774 }
2775
2776 /* too bad */
2777 return( -1 );
2778}
2779
2780/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002781 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002782 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002783 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2784 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002785 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002786 * such that every cert in the chain is a child of the next one,
2787 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002788 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002789 * Verify that chain and return it with flags for all issues found.
2790 *
2791 * Special cases:
2792 * - EE == Rj -> return a one-element list containing it
2793 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2794 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002795 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002796 * Tests for (aspects of) this function should include at least:
2797 * - trusted EE
2798 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002799 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002800 * - if relevant: EE untrusted
2801 * - if relevant: EE -> intermediate, untrusted
2802 * with the aspect under test checked at each relevant level (EE, int, root).
2803 * For some aspects longer chains are required, but usually length 2 is
2804 * enough (but length 1 is not in general).
2805 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002806 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002807 * - [in] crt: the cert list EE, C1, ..., Cn
2808 * - [in] trust_ca: the trusted list R1, ..., Rp
2809 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002810 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002811 * Only valid when return value is 0, may contain garbage otherwise!
2812 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002813 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002814 *
2815 * Return value:
2816 * - non-zero if the chain could not be fully built and examined
2817 * - 0 is the chain was successfully built and examined,
2818 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002819 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002820static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002821 mbedtls_x509_crt *crt,
2822 mbedtls_x509_crt *trust_ca,
2823 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002824 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2825 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002826 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002827 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002828 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002829{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002830 /* Don't initialize any of those variables here, so that the compiler can
2831 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002833 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002834 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002835 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002836 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002837 int parent_is_trusted;
2838 int child_is_trusted;
2839 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002840 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002841 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002842
2843#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2844 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002845 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002846 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002847 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002848 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002849 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002850
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002851 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002852 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002853 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002854 flags = &cur->flags;
2855
2856 goto find_parent;
2857 }
2858#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002859
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002860 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002861 self_cnt = 0;
2862 parent_is_trusted = 0;
2863 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002864
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002865 while( 1 ) {
2866 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002867 cur = &ver_chain->items[ver_chain->len];
2868 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002869 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002870 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002871 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002872
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002873 /* Check time-validity (all certificates) */
2874 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2875 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002876
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002877 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2878 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002879
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002880 /* Stop here for trusted roots (but not for trusted EE certs) */
2881 if( child_is_trusted )
2882 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002883
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002884 /* Check signature algorithm: MD & PK algs */
2885 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2886 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002887
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002888 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2889 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002890
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002891 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002892 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002893 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2894 {
2895 return( 0 );
2896 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002897
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002898#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2899find_parent:
2900#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002901
2902 /* Obtain list of potential trusted signers from CA callback,
2903 * or use statically provided list. */
2904#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2905 if( f_ca_cb != NULL )
2906 {
2907 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2908 mbedtls_free( ver_chain->trust_ca_cb_result );
2909 ver_chain->trust_ca_cb_result = NULL;
2910
2911 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2912 if( ret != 0 )
2913 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2914
2915 cur_trust_ca = ver_chain->trust_ca_cb_result;
2916 }
2917 else
2918#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2919 {
2920 ((void) f_ca_cb);
2921 ((void) p_ca_cb);
2922 cur_trust_ca = trust_ca;
2923 }
2924
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002925 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002926 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002927 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002928 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002929
2930#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002931 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2932 {
2933 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002934 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002935 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002936 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002937
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002938 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002939 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002940#else
2941 (void) ret;
2942#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002943
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002944 /* No parent? We're done here */
2945 if( parent == NULL )
2946 {
2947 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2948 return( 0 );
2949 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002950
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002951 /* Count intermediate self-issued (not necessarily self-signed) certs.
2952 * These can occur with some strategies for key rollover, see [SIRO],
2953 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002954 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002955 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2956 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002957 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002958 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002959
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 /* path_cnt is 0 for the first intermediate CA,
2961 * and if parent is trusted it's not an intermediate CA */
2962 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002963 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002964 {
2965 /* return immediately to avoid overflow the chain array */
2966 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2967 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002968
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002969 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002970 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002971 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2972
2973 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002974 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002978 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002979 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002980#else
2981 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002982#endif
2983
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002984 /* prepare for next iteration */
2985 child = parent;
2986 parent = NULL;
2987 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002988 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002989 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002990}
2991
2992/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002993 * Check for CN match
2994 */
2995static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2996 const char *cn, size_t cn_len )
2997{
2998 /* try exact match */
2999 if( name->len == cn_len &&
3000 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3001 {
3002 return( 0 );
3003 }
3004
3005 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003006 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003007 {
3008 return( 0 );
3009 }
3010
3011 return( -1 );
3012}
3013
3014/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003015 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3016 */
3017static int x509_crt_check_san( const mbedtls_x509_buf *name,
3018 const char *cn, size_t cn_len )
3019{
3020 const unsigned char san_type = (unsigned char) name->tag &
3021 MBEDTLS_ASN1_TAG_VALUE_MASK;
3022
3023 /* dNSName */
3024 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3025 return( x509_crt_check_cn( name, cn, cn_len ) );
3026
3027 /* (We may handle other types here later.) */
3028
3029 /* Unrecognized type */
3030 return( -1 );
3031}
3032
3033/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003034 * Verify the requested CN - only call this if cn is not NULL!
3035 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003036static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003037 const char *cn,
3038 uint32_t *flags )
3039{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003040 const mbedtls_x509_name *name;
3041 const mbedtls_x509_sequence *cur;
3042 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003043
3044 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3045 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003046 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003047 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003048 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003049 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050 }
3051
3052 if( cur == NULL )
3053 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3054 }
3055 else
3056 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003057 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003058 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003059 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3060 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003061 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003062 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003063 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003064 }
3065
3066 if( name == NULL )
3067 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3068 }
3069}
3070
3071/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003072 * Merge the flags for all certs in the chain, after calling callback
3073 */
3074static int x509_crt_merge_flags_with_cb(
3075 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003076 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003077 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3078 void *p_vrfy )
3079{
Janos Follath865b3eb2019-12-16 11:46:15 +00003080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003081 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003082 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003083 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003084
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003085 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003086 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003087 cur = &ver_chain->items[i-1];
3088 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003089
3090 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003091 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003092 return( ret );
3093
3094 *flags |= cur_flags;
3095 }
3096
3097 return( 0 );
3098}
3099
3100/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003101 * Verify the certificate validity, with profile, restartable version
3102 *
3103 * This function:
3104 * - checks the requested CN (if any)
3105 * - checks the type and size of the EE cert's key,
3106 * as that isn't done as part of chain building/verification currently
3107 * - builds and verifies the chain
3108 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003109 *
3110 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3111 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3112 * verification routine to search for trusted signers, and CRLs will
3113 * be disabled. Otherwise, `trust_ca` will be used as the static list
3114 * of trusted signers, and `ca_crl` will be use as the static list
3115 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003116 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003117static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003118 mbedtls_x509_crt *trust_ca,
3119 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003120 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3121 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003122 const mbedtls_x509_crt_profile *profile,
3123 const char *cn, uint32_t *flags,
3124 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3125 void *p_vrfy,
3126 mbedtls_x509_crt_restart_ctx *rs_ctx )
3127{
Janos Follath865b3eb2019-12-16 11:46:15 +00003128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003129 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003130 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003131 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003132
3133 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003134 ee_flags = 0;
3135 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003136
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003137 if( profile == NULL )
3138 {
3139 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3140 goto exit;
3141 }
3142
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003143 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003144 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003145 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003146
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003147 /* Check the type and size of the key */
3148 pk_type = mbedtls_pk_get_type( &crt->pk );
3149
3150 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003151 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003152
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003153 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003154 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003155
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003156 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003157 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3158 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003159 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003160
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003161 if( ret != 0 )
3162 goto exit;
3163
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003164 /* Merge end-entity flags */
3165 ver_chain.items[0].flags |= ee_flags;
3166
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003167 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003168 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003169
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003170exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003171
3172#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3173 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3174 mbedtls_free( ver_chain.trust_ca_cb_result );
3175 ver_chain.trust_ca_cb_result = NULL;
3176#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3177
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003178#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3179 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3180 mbedtls_x509_crt_restart_free( rs_ctx );
3181#endif
3182
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003183 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3184 * the SSL module for authmode optional, but non-zero return from the
3185 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003186 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3187 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3188
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003189 if( ret != 0 )
3190 {
3191 *flags = (uint32_t) -1;
3192 return( ret );
3193 }
3194
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003195 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003197
3198 return( 0 );
3199}
3200
Hanno Becker3116fb32019-03-28 13:34:42 +00003201
3202/*
3203 * Verify the certificate validity (default profile, not restartable)
3204 */
3205int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3206 mbedtls_x509_crt *trust_ca,
3207 mbedtls_x509_crl *ca_crl,
3208 const char *cn, uint32_t *flags,
3209 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3210 void *p_vrfy )
3211{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003212 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003213 NULL, NULL,
3214 &mbedtls_x509_crt_profile_default,
3215 cn, flags,
3216 f_vrfy, p_vrfy, NULL ) );
3217}
3218
3219/*
3220 * Verify the certificate validity (user-chosen profile, not restartable)
3221 */
3222int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3223 mbedtls_x509_crt *trust_ca,
3224 mbedtls_x509_crl *ca_crl,
3225 const mbedtls_x509_crt_profile *profile,
3226 const char *cn, uint32_t *flags,
3227 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3228 void *p_vrfy )
3229{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003230 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003231 NULL, NULL,
3232 profile, cn, flags,
3233 f_vrfy, p_vrfy, NULL ) );
3234}
3235
3236#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3237/*
3238 * Verify the certificate validity (user-chosen profile, CA callback,
3239 * not restartable).
3240 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003241int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003242 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3243 void *p_ca_cb,
3244 const mbedtls_x509_crt_profile *profile,
3245 const char *cn, uint32_t *flags,
3246 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3247 void *p_vrfy )
3248{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003249 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003250 f_ca_cb, p_ca_cb,
3251 profile, cn, flags,
3252 f_vrfy, p_vrfy, NULL ) );
3253}
3254#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3255
3256int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3257 mbedtls_x509_crt *trust_ca,
3258 mbedtls_x509_crl *ca_crl,
3259 const mbedtls_x509_crt_profile *profile,
3260 const char *cn, uint32_t *flags,
3261 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3262 void *p_vrfy,
3263 mbedtls_x509_crt_restart_ctx *rs_ctx )
3264{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003265 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003266 NULL, NULL,
3267 profile, cn, flags,
3268 f_vrfy, p_vrfy, rs_ctx ) );
3269}
3270
3271
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003272/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003273 * Initialize a certificate chain
3274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003276{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003278}
3279
3280/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003281 * Unallocate all certificate data
3282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 mbedtls_x509_crt *cert_cur = crt;
3286 mbedtls_x509_crt *cert_prv;
3287 mbedtls_x509_name *name_cur;
3288 mbedtls_x509_name *name_prv;
3289 mbedtls_x509_sequence *seq_cur;
3290 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003291
3292 if( crt == NULL )
3293 return;
3294
3295 do
3296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3300 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003301#endif
3302
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003303 name_cur = cert_cur->issuer.next;
3304 while( name_cur != NULL )
3305 {
3306 name_prv = name_cur;
3307 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003308 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003310 }
3311
3312 name_cur = cert_cur->subject.next;
3313 while( name_cur != NULL )
3314 {
3315 name_prv = name_cur;
3316 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003317 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003319 }
3320
3321 seq_cur = cert_cur->ext_key_usage.next;
3322 while( seq_cur != NULL )
3323 {
3324 seq_prv = seq_cur;
3325 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003326 mbedtls_platform_zeroize( seq_prv,
3327 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003329 }
3330
3331 seq_cur = cert_cur->subject_alt_names.next;
3332 while( seq_cur != NULL )
3333 {
3334 seq_prv = seq_cur;
3335 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003336 mbedtls_platform_zeroize( seq_prv,
3337 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003339 }
3340
Ron Eldor74d9acc2019-03-21 14:00:03 +02003341 seq_cur = cert_cur->certificate_policies.next;
3342 while( seq_cur != NULL )
3343 {
3344 seq_prv = seq_cur;
3345 seq_cur = seq_cur->next;
3346 mbedtls_platform_zeroize( seq_prv,
3347 sizeof( mbedtls_x509_sequence ) );
3348 mbedtls_free( seq_prv );
3349 }
3350
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003351 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003352 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003353 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003355 }
3356
3357 cert_cur = cert_cur->next;
3358 }
3359 while( cert_cur != NULL );
3360
3361 cert_cur = crt;
3362 do
3363 {
3364 cert_prv = cert_cur;
3365 cert_cur = cert_cur->next;
3366
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003367 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003368 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003370 }
3371 while( cert_cur != NULL );
3372}
3373
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003374#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3375/*
3376 * Initialize a restart context
3377 */
3378void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3379{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003380 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003381
3382 ctx->parent = NULL;
3383 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003384 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003385
3386 ctx->parent_is_trusted = -1;
3387
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003388 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003389 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003390 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003391}
3392
3393/*
3394 * Free the components of a restart context
3395 */
3396void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3397{
3398 if( ctx == NULL )
3399 return;
3400
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003401 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003402 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003403}
3404#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406#endif /* MBEDTLS_X509_CRT_PARSE_C */