blob: def1414eca0f589a48b52b6676ab3f1c56d037af [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
50#endif
51
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010056#endif
57
Daniel Axtens301db662020-05-28 11:43:41 +100058#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010059#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#include <windows.h>
61#else
62#include <time.h>
63#endif
Daniel Axtens301db662020-05-28 11:43:41 +100064#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000067#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020068#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#include <sys/types.h>
70#include <sys/stat.h>
71#include <dirent.h>
Eduardo Silva32ffb2b2019-04-25 10:43:26 -060072#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000073#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif
75
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020076/*
77 * Item in a verification chain: cert and flags for it
78 */
79typedef struct {
80 mbedtls_x509_crt *crt;
81 uint32_t flags;
82} x509_crt_verify_chain_item;
83
84/*
85 * Max size of verification chain: end-entity + intermediates + trusted root
86 */
87#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020088
Gilles Peskine0ecd7192021-06-07 21:24:26 +020089/* Default profile. Do not remove items unless there are serious security
90 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020091const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
92{
Gilles Peskine5e79cb32017-05-04 16:17:21 +020093 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020094 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
95 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
96 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
97 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
98 0xFFFFFFF, /* Any PK alg */
99 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200100 2048,
101};
102
103/*
104 * Next-default profile
105 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200106const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
107{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 /* Hashes from SHA-256 and above */
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
112 0xFFFFFFF, /* Any PK alg */
113#if defined(MBEDTLS_ECP_C)
114 /* Curves at or above 128-bit security level */
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
122#else
123 0,
124#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200125 2048,
126};
127
128/*
129 * NSA Suite B Profile
130 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200131const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
132{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200133 /* Only SHA-256 and 384 */
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
136 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200137 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200139#if defined(MBEDTLS_ECP_C)
140 /* Only NIST P-256 and P-384 */
141 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
143#else
144 0,
145#endif
146 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147};
148
149/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200150 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200151 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200152 */
153static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
154 mbedtls_md_type_t md_alg )
155{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200156 if( md_alg == MBEDTLS_MD_NONE )
157 return( -1 );
158
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200159 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
160 return( 0 );
161
162 return( -1 );
163}
164
165/*
166 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200167 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200168 */
169static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
170 mbedtls_pk_type_t pk_alg )
171{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200172 if( pk_alg == MBEDTLS_PK_NONE )
173 return( -1 );
174
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200175 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
176 return( 0 );
177
178 return( -1 );
179}
180
181/*
182 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200183 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184 */
185static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200186 const mbedtls_pk_context *pk )
187{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200188 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200189
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190#if defined(MBEDTLS_RSA_C)
191 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
192 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200193 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194 return( 0 );
195
196 return( -1 );
197 }
198#endif
199
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200200#if defined(MBEDTLS_ECP_C)
201 if( pk_alg == MBEDTLS_PK_ECDSA ||
202 pk_alg == MBEDTLS_PK_ECKEY ||
203 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200204 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200205 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206
Philippe Antoineb5b25432018-05-11 11:06:29 +0200207 if( gid == MBEDTLS_ECP_DP_NONE )
208 return( -1 );
209
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
211 return( 0 );
212
213 return( -1 );
214 }
215#endif
216
217 return( -1 );
218}
219
220/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000221 * Like memcmp, but case-insensitive and always returns -1 if different
222 */
223static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
224{
225 size_t i;
226 unsigned char diff;
227 const unsigned char *n1 = s1, *n2 = s2;
228
229 for( i = 0; i < len; i++ )
230 {
231 diff = n1[i] ^ n2[i];
232
233 if( diff == 0 )
234 continue;
235
236 if( diff == 32 &&
237 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
238 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
239 {
240 continue;
241 }
242
243 return( -1 );
244 }
245
246 return( 0 );
247}
248
249/*
250 * Return 0 if name matches wildcard, -1 otherwise
251 */
252static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
253{
254 size_t i;
255 size_t cn_idx = 0, cn_len = strlen( cn );
256
257 /* We can't have a match if there is no wildcard to match */
258 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
259 return( -1 );
260
261 for( i = 0; i < cn_len; ++i )
262 {
263 if( cn[i] == '.' )
264 {
265 cn_idx = i;
266 break;
267 }
268 }
269
270 if( cn_idx == 0 )
271 return( -1 );
272
273 if( cn_len - cn_idx == name->len - 1 &&
274 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
275 {
276 return( 0 );
277 }
278
279 return( -1 );
280}
281
282/*
283 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
284 * variations (but not all).
285 *
286 * Return 0 if equal, -1 otherwise.
287 */
288static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
289{
290 if( a->tag == b->tag &&
291 a->len == b->len &&
292 memcmp( a->p, b->p, b->len ) == 0 )
293 {
294 return( 0 );
295 }
296
297 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
298 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
299 a->len == b->len &&
300 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
301 {
302 return( 0 );
303 }
304
305 return( -1 );
306}
307
308/*
309 * Compare two X.509 Names (aka rdnSequence).
310 *
311 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
312 * we sometimes return unequal when the full algorithm would return equal,
313 * but never the other way. (In particular, we don't do Unicode normalisation
314 * or space folding.)
315 *
316 * Return 0 if equal, -1 otherwise.
317 */
318static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
319{
320 /* Avoid recursion, it might not be optimised by the compiler */
321 while( a != NULL || b != NULL )
322 {
323 if( a == NULL || b == NULL )
324 return( -1 );
325
326 /* type */
327 if( a->oid.tag != b->oid.tag ||
328 a->oid.len != b->oid.len ||
329 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
330 {
331 return( -1 );
332 }
333
334 /* value */
335 if( x509_string_cmp( &a->val, &b->val ) != 0 )
336 return( -1 );
337
338 /* structure of the list of sets */
339 if( a->next_merged != b->next_merged )
340 return( -1 );
341
342 a = a->next;
343 b = b->next;
344 }
345
346 /* a == NULL == b */
347 return( 0 );
348}
349
350/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200351 * Reset (init or clear) a verify_chain
352 */
353static void x509_crt_verify_chain_reset(
354 mbedtls_x509_crt_verify_chain *ver_chain )
355{
356 size_t i;
357
358 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
359 {
360 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000361 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200362 }
363
364 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000365
366#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
367 ver_chain->trust_ca_cb_result = NULL;
368#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200369}
370
371/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
373 */
374static int x509_get_version( unsigned char **p,
375 const unsigned char *end,
376 int *ver )
377{
Janos Follath865b3eb2019-12-16 11:46:15 +0000378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 size_t len;
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
382 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385 {
386 *ver = 0;
387 return( 0 );
388 }
389
Chris Jones9f7a6932021-04-14 12:12:09 +0100390 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 }
392
393 end = *p + len;
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100396 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397
398 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100399 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
400 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401
402 return( 0 );
403}
404
405/*
406 * Validity ::= SEQUENCE {
407 * notBefore Time,
408 * notAfter Time }
409 */
410static int x509_get_dates( unsigned char **p,
411 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_x509_time *from,
413 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414{
Janos Follath865b3eb2019-12-16 11:46:15 +0000415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416 size_t len;
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
419 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100420 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
422 end = *p + len;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425 return( ret );
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428 return( ret );
429
430 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100431 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
432 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
434 return( 0 );
435}
436
437/*
438 * X.509 v2/v3 unique identifier (not parsed)
439 */
440static int x509_get_uid( unsigned char **p,
441 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443{
Janos Follath865b3eb2019-12-16 11:46:15 +0000444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
446 if( *p == end )
447 return( 0 );
448
449 uid->tag = **p;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
452 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 return( 0 );
456
Chris Jones9f7a6932021-04-14 12:12:09 +0100457 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 }
459
460 uid->p = *p;
461 *p += uid->len;
462
463 return( 0 );
464}
465
466static int x509_get_basic_constraints( unsigned char **p,
467 const unsigned char *end,
468 int *ca_istrue,
469 int *max_pathlen )
470{
Janos Follath865b3eb2019-12-16 11:46:15 +0000471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472 size_t len;
473
474 /*
475 * BasicConstraints ::= SEQUENCE {
476 * cA BOOLEAN DEFAULT FALSE,
477 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
478 */
479 *ca_istrue = 0; /* DEFAULT FALSE */
480 *max_pathlen = 0; /* endless */
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
483 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100484 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485
486 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200487 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
492 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
494 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100495 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
497 if( *ca_istrue != 0 )
498 *ca_istrue = 1;
499 }
500
501 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200502 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100505 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100508 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
509 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510
Andrzej Kurek16050742020-04-14 09:49:52 -0400511 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
512 * overflow, which is an undefined behavior. */
513 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100514 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
515 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400516
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517 (*max_pathlen)++;
518
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200519 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520}
521
522static int x509_get_ns_cert_type( unsigned char **p,
523 const unsigned char *end,
524 unsigned char *ns_cert_type)
525{
Janos Follath865b3eb2019-12-16 11:46:15 +0000526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100530 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531
532 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100533 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
534 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
536 /* Get actual bitstring */
537 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200538 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539}
540
541static int x509_get_key_usage( unsigned char **p,
542 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100543 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544{
Janos Follath865b3eb2019-12-16 11:46:15 +0000545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200546 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100550 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
552 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100553 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
554 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
556 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200557 *key_usage = 0;
558 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
559 {
560 *key_usage |= (unsigned int) bs.p[i] << (8*i);
561 }
562
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200563 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564}
565
566/*
567 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
568 *
569 * KeyPurposeId ::= OBJECT IDENTIFIER
570 */
571static int x509_get_ext_key_usage( unsigned char **p,
572 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574{
Janos Follath865b3eb2019-12-16 11:46:15 +0000575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100578 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
580 /* Sequence length must be >= 1 */
581 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100582 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
583 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200585 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586}
587
588/*
589 * SubjectAltName ::= GeneralNames
590 *
591 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
592 *
593 * GeneralName ::= CHOICE {
594 * otherName [0] OtherName,
595 * rfc822Name [1] IA5String,
596 * dNSName [2] IA5String,
597 * x400Address [3] ORAddress,
598 * directoryName [4] Name,
599 * ediPartyName [5] EDIPartyName,
600 * uniformResourceIdentifier [6] IA5String,
601 * iPAddress [7] OCTET STRING,
602 * registeredID [8] OBJECT IDENTIFIER }
603 *
604 * OtherName ::= SEQUENCE {
605 * type-id OBJECT IDENTIFIER,
606 * value [0] EXPLICIT ANY DEFINED BY type-id }
607 *
608 * EDIPartyName ::= SEQUENCE {
609 * nameAssigner [0] DirectoryString OPTIONAL,
610 * partyName [1] DirectoryString }
611 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300612 * NOTE: we list all types, but only use dNSName and otherName
613 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614 */
615static int x509_get_subject_alt_name( unsigned char **p,
616 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618{
Janos Follath865b3eb2019-12-16 11:46:15 +0000619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624
625 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
627 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100628 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629
630 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100631 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
632 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633
634 while( *p < end )
635 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300636 mbedtls_x509_subject_alternative_name dummy_san_buf;
637 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
638
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 tag = **p;
640 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100642 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100644 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
645 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
646 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100647 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
648 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100649 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
Ron Eldordbbd9662019-05-15 15:46:03 +0300651 /*
irwir74aee1c2019-09-21 18:21:48 +0300652 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300653 */
654 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
655 /*
656 * In case the extension is malformed, return an error,
657 * and clear the allocated sequences.
658 */
659 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
660 {
661 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
662 mbedtls_x509_sequence *seq_prv;
663 while( seq_cur != NULL )
664 {
665 seq_prv = seq_cur;
666 seq_cur = seq_cur->next;
667 mbedtls_platform_zeroize( seq_prv,
668 sizeof( mbedtls_x509_sequence ) );
669 mbedtls_free( seq_prv );
670 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300671 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300672 return( ret );
673 }
674
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200676 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100678 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100680
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200681 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
683 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100684 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
685 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 cur = cur->next;
688 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200689
690 buf = &(cur->buf);
691 buf->tag = tag;
692 buf->p = *p;
693 buf->len = tag_len;
694 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 }
696
697 /* Set final sequence entry's next pointer to NULL */
698 cur->next = NULL;
699
700 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100701 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
702 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703
704 return( 0 );
705}
706
707/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200708 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
709 *
710 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
711 *
712 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
713 *
714 * PolicyInformation ::= SEQUENCE {
715 * policyIdentifier CertPolicyId,
716 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
717 * PolicyQualifierInfo OPTIONAL }
718 *
719 * CertPolicyId ::= OBJECT IDENTIFIER
720 *
721 * PolicyQualifierInfo ::= SEQUENCE {
722 * policyQualifierId PolicyQualifierId,
723 * qualifier ANY DEFINED BY policyQualifierId }
724 *
725 * -- policyQualifierIds for Internet policy qualifiers
726 *
727 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
728 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
729 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
730 *
731 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
732 *
733 * Qualifier ::= CHOICE {
734 * cPSuri CPSuri,
735 * userNotice UserNotice }
736 *
737 * CPSuri ::= IA5String
738 *
739 * UserNotice ::= SEQUENCE {
740 * noticeRef NoticeReference OPTIONAL,
741 * explicitText DisplayText OPTIONAL }
742 *
743 * NoticeReference ::= SEQUENCE {
744 * organization DisplayText,
745 * noticeNumbers SEQUENCE OF INTEGER }
746 *
747 * DisplayText ::= CHOICE {
748 * ia5String IA5String (SIZE (1..200)),
749 * visibleString VisibleString (SIZE (1..200)),
750 * bmpString BMPString (SIZE (1..200)),
751 * utf8String UTF8String (SIZE (1..200)) }
752 *
753 * NOTE: we only parse and use anyPolicy without qualifiers at this point
754 * as defined in RFC 5280.
755 */
756static int x509_get_certificate_policies( unsigned char **p,
757 const unsigned char *end,
758 mbedtls_x509_sequence *certificate_policies )
759{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300760 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200761 size_t len;
762 mbedtls_asn1_buf *buf;
763 mbedtls_asn1_sequence *cur = certificate_policies;
764
765 /* Get main sequence tag */
766 ret = mbedtls_asn1_get_tag( p, end, &len,
767 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
768 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100769 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200770
771 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100772 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
773 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200774
775 /*
776 * Cannot be an empty sequence.
777 */
778 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100779 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
780 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200781
782 while( *p < end )
783 {
784 mbedtls_x509_buf policy_oid;
785 const unsigned char *policy_end;
786
787 /*
788 * Get the policy sequence
789 */
790 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
791 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100792 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200793
794 policy_end = *p + len;
795
Ron Eldor08063792019-05-13 16:38:39 +0300796 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200797 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100798 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200799
800 policy_oid.tag = MBEDTLS_ASN1_OID;
801 policy_oid.len = len;
802 policy_oid.p = *p;
803
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300804 /*
805 * Only AnyPolicy is currently supported when enforcing policy.
806 */
807 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
808 {
809 /*
810 * Set the parsing return code but continue parsing, in case this
811 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
812 * is configured.
813 */
814 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
815 }
816
Ron Eldor74d9acc2019-03-21 14:00:03 +0200817 /* Allocate and assign next pointer */
818 if( cur->buf.p != NULL )
819 {
820 if( cur->next != NULL )
821 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
822
823 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
824
825 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100826 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
827 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200828
829 cur = cur->next;
830 }
831
832 buf = &( cur->buf );
833 buf->tag = policy_oid.tag;
834 buf->p = policy_oid.p;
835 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300836
837 *p += len;
838
839 /*
840 * If there is an optional qualifier, then *p < policy_end
841 * Check the Qualifier len to verify it doesn't exceed policy_end.
842 */
843 if( *p < policy_end )
844 {
845 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
846 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100847 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300848 /*
849 * Skip the optional policy qualifiers.
850 */
851 *p += len;
852 }
853
854 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100855 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
856 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200857 }
858
859 /* Set final sequence entry's next pointer to NULL */
860 cur->next = NULL;
861
862 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100863 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
864 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200865
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300866 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200867}
868
869/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 * X.509 v3 extensions
871 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872 */
873static int x509_get_crt_ext( unsigned char **p,
874 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200875 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200876 mbedtls_x509_crt_ext_cb_t cb,
877 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878{
Janos Follath865b3eb2019-12-16 11:46:15 +0000879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200881 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882
Hanno Becker12f62fb2019-02-12 17:22:36 +0000883 if( *p == end )
884 return( 0 );
885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888
Hanno Becker12f62fb2019-02-12 17:22:36 +0000889 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 while( *p < end )
891 {
892 /*
893 * Extension ::= SEQUENCE {
894 * extnID OBJECT IDENTIFIER,
895 * critical BOOLEAN DEFAULT FALSE,
896 * extnValue OCTET STRING }
897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 int is_critical = 0; /* DEFAULT FALSE */
900 int ext_type = 0;
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
903 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100904 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905
906 end_ext_data = *p + len;
907
908 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200909 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200910 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100911 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
k-stachowiak463928a2018-07-24 12:50:59 +0200913 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 extn_oid.p = *p;
915 *p += extn_oid.len;
916
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
919 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100920 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921
922 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
924 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100925 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200927 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 end_ext_octet = *p + len;
929
930 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100931 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
932 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
934 /*
935 * Detect supported extensions
936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939 if( ret != 0 )
940 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200941 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200942 if( cb != NULL )
943 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200944 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200945 if( ret != 0 && is_critical )
946 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200947 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200948 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200949 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200950
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 /* No parser found, skip extension */
952 *p = end_ext_octet;
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 if( is_critical )
956 {
957 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100958 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
959 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 }
961#endif
962 continue;
963 }
964
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100965 /* Forbid repeated extensions */
966 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100968
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 crt->ext_types |= ext_type;
970
971 switch( ext_type )
972 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100973 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 /* Parse basic constraints */
975 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
976 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200977 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 break;
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 /* Parse key usage */
982 if( ( ret = x509_get_key_usage( p, end_ext_octet,
983 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200984 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 break;
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 /* Parse extended key usage */
989 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
990 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200991 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 break;
993
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100994 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 /* Parse subject alt name */
996 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
997 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200998 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 break;
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 /* Parse netscape certificate type */
1003 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1004 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001005 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006 break;
1007
Ron Eldor74d9acc2019-03-21 14:00:03 +02001008 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1009 /* Parse certificate policies type */
1010 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1011 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001012 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001013 /* Give the callback (if any) a chance to handle the extension
1014 * if it contains unsupported policies */
1015 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1016 cb( p_ctx, crt, &extn_oid, is_critical,
1017 start_ext_octet, end_ext_octet ) == 0 )
1018 break;
1019
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001020#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1021 if( is_critical )
1022 return( ret );
1023 else
1024#endif
1025 /*
Ron Eldora2913912019-05-16 16:17:38 +03001026 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1027 * cannot interpret or enforce the policy. However, it is up to
1028 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001029 * unless the extension is critical.
1030 */
1031 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1032 return( ret );
1033 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001034 break;
1035
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001037 /*
1038 * If this is a non-critical extension, which the oid layer
1039 * supports, but there isn't an x509 parser for it,
1040 * skip the extension.
1041 */
1042#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1043 if( is_critical )
1044 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1045 else
1046#endif
1047 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 }
1049 }
1050
1051 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001052 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1053 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001054
1055 return( 0 );
1056}
1057
1058/*
1059 * Parse and fill a single X.509 certificate in DER format
1060 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001061static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1062 const unsigned char *buf,
1063 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001064 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001065 mbedtls_x509_crt_ext_cb_t cb,
1066 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067{
Janos Follath865b3eb2019-12-16 11:46:15 +00001068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069 size_t len;
1070 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1074 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1075 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076
1077 /*
1078 * Check for valid input
1079 */
1080 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001082
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001083 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001084 p = (unsigned char*) buf;
1085 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 end = p + len;
1087
1088 /*
1089 * Certificate ::= SEQUENCE {
1090 * tbsCertificate TBSCertificate,
1091 * signatureAlgorithm AlgorithmIdentifier,
1092 * signatureValue BIT STRING }
1093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1095 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_x509_crt_free( crt );
1098 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099 }
1100
Janos Follathcc0e49d2016-02-17 14:34:12 +00001101 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001102 crt->raw.len = crt_end - buf;
1103 if( make_copy != 0 )
1104 {
1105 /* Create and populate a new buffer for the raw field. */
1106 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1107 if( crt->raw.p == NULL )
1108 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1109
1110 memcpy( crt->raw.p, buf, crt->raw.len );
1111 crt->own_buffer = 1;
1112
1113 p += crt->raw.len - len;
1114 end = crt_end = p + len;
1115 }
1116 else
1117 {
1118 crt->raw.p = (unsigned char*) buf;
1119 crt->own_buffer = 0;
1120 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001121
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 /*
1123 * TBSCertificate ::= SEQUENCE {
1124 */
1125 crt->tbs.p = p;
1126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1128 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001131 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 }
1133
1134 end = p + len;
1135 crt->tbs.len = end - crt->tbs.p;
1136
1137 /*
1138 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1139 *
1140 * CertificateSerialNumber ::= INTEGER
1141 *
1142 * signature AlgorithmIdentifier
1143 */
1144 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1146 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001147 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 return( ret );
1151 }
1152
Andres AG7ca4a032017-03-09 16:16:11 +00001153 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 mbedtls_x509_crt_free( crt );
1156 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 }
1158
Andres AG7ca4a032017-03-09 16:16:11 +00001159 crt->version++;
1160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001162 &crt->sig_md, &crt->sig_pk,
1163 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 return( ret );
1167 }
1168
1169 /*
1170 * issuer Name
1171 */
1172 crt->issuer_raw.p = p;
1173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1175 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001178 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 }
1180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 return( ret );
1185 }
1186
1187 crt->issuer_raw.len = p - crt->issuer_raw.p;
1188
1189 /*
1190 * Validity ::= SEQUENCE {
1191 * notBefore Time,
1192 * notAfter Time }
1193 *
1194 */
1195 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1196 &crt->valid_to ) ) != 0 )
1197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199 return( ret );
1200 }
1201
1202 /*
1203 * subject Name
1204 */
1205 crt->subject_raw.p = p;
1206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001211 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 }
1213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217 return( ret );
1218 }
1219
1220 crt->subject_raw.len = p - crt->subject_raw.p;
1221
1222 /*
1223 * SubjectPublicKeyInfo
1224 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001225 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 return( ret );
1230 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001231 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232
1233 /*
1234 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1235 * -- If present, version shall be v2 or v3
1236 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1237 * -- If present, version shall be v2 or v3
1238 * extensions [3] EXPLICIT Extensions OPTIONAL
1239 * -- If present, version shall be v3
1240 */
1241 if( crt->version == 2 || crt->version == 3 )
1242 {
1243 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1244 if( ret != 0 )
1245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 return( ret );
1248 }
1249 }
1250
1251 if( crt->version == 2 || crt->version == 3 )
1252 {
1253 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1254 if( ret != 0 )
1255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001257 return( ret );
1258 }
1259 }
1260
David Horstmannab617512022-10-27 11:45:01 +01001261 int extensions_allowed = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
David Horstmannab617512022-10-27 11:45:01 +01001263 if( crt->version != 3 )
1264 extensions_allowed = 0;
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001265#endif
David Horstmannab617512022-10-27 11:45:01 +01001266 if( extensions_allowed )
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001267 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001268 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 if( ret != 0 )
1270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 return( ret );
1273 }
1274 }
1275
1276 if( p != end )
1277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001279 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1280 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 }
1282
1283 end = crt_end;
1284
1285 /*
1286 * }
1287 * -- end of TBSCertificate
1288 *
1289 * signatureAlgorithm AlgorithmIdentifier,
1290 * signatureValue BIT STRING
1291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 return( ret );
1296 }
1297
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001298 if( crt->sig_oid.len != sig_oid2.len ||
1299 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001300 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001301 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001302 ( sig_params1.len != 0 &&
1303 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 mbedtls_x509_crt_free( crt );
1306 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 }
1308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 return( ret );
1313 }
1314
1315 if( p != end )
1316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001318 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1319 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 }
1321
1322 return( 0 );
1323}
1324
1325/*
1326 * Parse one X.509 certificate in DER format from a buffer and add them to a
1327 * chained list
1328 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001329static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1330 const unsigned char *buf,
1331 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001332 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001333 mbedtls_x509_crt_ext_cb_t cb,
1334 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335{
Janos Follath865b3eb2019-12-16 11:46:15 +00001336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001338
1339 /*
1340 * Check for valid input
1341 */
1342 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344
1345 while( crt->version != 0 && crt->next != NULL )
1346 {
1347 prev = crt;
1348 crt = crt->next;
1349 }
1350
1351 /*
1352 * Add new certificate on the end of the chain if needed.
1353 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001354 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001356 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357
1358 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001359 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360
1361 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364 }
1365
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001366 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001367 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368 {
1369 if( prev )
1370 prev->next = NULL;
1371
1372 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374
1375 return( ret );
1376 }
1377
1378 return( 0 );
1379}
1380
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001381int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1382 const unsigned char *buf,
1383 size_t buflen )
1384{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001385 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001386}
1387
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001388int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1389 const unsigned char *buf,
1390 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001391 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001392 mbedtls_x509_crt_ext_cb_t cb,
1393 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001394{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001395 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001396}
1397
1398int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1399 const unsigned char *buf,
1400 size_t buflen )
1401{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001402 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001403}
1404
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001406 * Parse one or more PEM certificates from a buffer and add them to the chained
1407 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001409int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1410 const unsigned char *buf,
1411 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412{
Janos Follath98e28a72016-05-31 14:03:54 +01001413#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001414 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001416#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417
1418 /*
1419 * Check for valid input
1420 */
1421 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423
1424 /*
1425 * Determine buffer content. Buffer contains either one DER certificate or
1426 * one or more PEM certificates.
1427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001429 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001430 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001433 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1436 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001437#else
1438 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1439#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_PEM_PARSE_C)
1442 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001447 /* 1 rather than 0 since the terminating NULL byte is counted in */
1448 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449 {
1450 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455 "-----BEGIN CERTIFICATE-----",
1456 "-----END CERTIFICATE-----",
1457 buf, NULL, 0, &use_len );
1458
1459 if( ret == 0 )
1460 {
1461 /*
1462 * Was PEM encoded
1463 */
1464 buflen -= use_len;
1465 buf += use_len;
1466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 {
1469 return( ret );
1470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474
1475 /*
1476 * PEM header and footer were found
1477 */
1478 buflen -= use_len;
1479 buf += use_len;
1480
1481 if( first_error == 0 )
1482 first_error = ret;
1483
Paul Bakker5a5fa922014-09-26 14:53:04 +02001484 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485 continue;
1486 }
1487 else
1488 break;
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
1494 if( ret != 0 )
1495 {
1496 /*
1497 * Quit parsing on a memory error
1498 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001499 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001500 return( ret );
1501
1502 if( first_error == 0 )
1503 first_error = ret;
1504
1505 total_failed++;
1506 continue;
1507 }
1508
1509 success = 1;
1510 }
1511 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512
1513 if( success )
1514 return( total_failed );
1515 else if( first_error )
1516 return( first_error );
1517 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001519#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520}
1521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523/*
1524 * Load one or more certificates and add them to the chained list
1525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527{
Janos Follath865b3eb2019-12-16 11:46:15 +00001528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529 size_t n;
1530 unsigned char *buf;
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533 return( ret );
1534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001537 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539
1540 return( ret );
1541}
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544{
1545 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001546#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547 int w_ret;
1548 WCHAR szDir[MAX_PATH];
1549 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001550 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001551 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552
Paul Bakker9af723c2014-05-01 13:03:14 +02001553 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554 HANDLE hFind;
1555
1556 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Paul Bakker9af723c2014-05-01 13:03:14 +02001559 memset( szDir, 0, sizeof(szDir) );
1560 memset( filename, 0, MAX_PATH );
1561 memcpy( filename, path, len );
1562 filename[len++] = '\\';
1563 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564 filename[len++] = '*';
1565
Simon B3c6b18d2016-11-03 01:11:37 +00001566 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001567 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001568 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570
1571 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001572 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
1575 len = MAX_PATH - len;
1576 do
1577 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001578 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579
1580 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1581 continue;
1582
Paul Bakker9af723c2014-05-01 13:03:14 +02001583 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001584 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001585 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001586 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001587 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001588 {
1589 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1590 goto cleanup;
1591 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 if( w_ret < 0 )
1595 ret++;
1596 else
1597 ret += w_ret;
1598 }
1599 while( FindNextFileW( hFind, &file_data ) != 0 );
1600
Paul Bakker66d5d072014-06-17 16:39:18 +02001601 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603
Ron Eldor36d90422017-01-09 15:09:16 +02001604cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001606#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001607 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001608 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001610 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001611 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612 DIR *dir = opendir( path );
1613
Paul Bakker66d5d072014-06-17 16:39:18 +02001614 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616
Ron Eldor63140682017-01-09 19:27:59 +02001617#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001618 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001619 {
1620 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001621 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001622 }
Ron Eldor63140682017-01-09 19:27:59 +02001623#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001624
Paul Elliottfb91a482021-03-05 14:17:51 +00001625 memset( &sb, 0, sizeof( sb ) );
1626
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001627 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628 {
Andres AGf9113192016-09-02 14:06:04 +01001629 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1630 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631
Andres AGf9113192016-09-02 14:06:04 +01001632 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633 {
Andres AGf9113192016-09-02 14:06:04 +01001634 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1635 goto cleanup;
1636 }
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001637 else if( stat( entry_name, &sb ) == -1 )
Andres AGf9113192016-09-02 14:06:04 +01001638 {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001639 if( errno == ENOENT )
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001640 {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001641 /* Broken symbolic link - ignore this entry.
1642 stat(2) will return this error for either (a) a dangling
1643 symlink or (b) a missing file.
1644 Given that we have just obtained the filename from readdir,
1645 assume that it does exist and therefore treat this as a
1646 dangling symlink. */
1647 continue;
1648 }
1649 else
1650 {
1651 /* Some other file error; report the error. */
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001652 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1653 goto cleanup;
1654 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655 }
1656
1657 if( !S_ISREG( sb.st_mode ) )
1658 continue;
1659
1660 // Ignore parse errors
1661 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663 if( t_ret < 0 )
1664 ret++;
1665 else
1666 ret += t_ret;
1667 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001668
1669cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001670 closedir( dir );
1671
Ron Eldor63140682017-01-09 19:27:59 +02001672#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001673 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001675#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001676
Paul Bakkerbe089b02013-10-14 15:51:50 +02001677#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678
1679 return( ret );
1680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001683/*
1684 * OtherName ::= SEQUENCE {
1685 * type-id OBJECT IDENTIFIER,
1686 * value [0] EXPLICIT ANY DEFINED BY type-id }
1687 *
1688 * HardwareModuleName ::= SEQUENCE {
1689 * hwType OBJECT IDENTIFIER,
1690 * hwSerialNum OCTET STRING }
1691 *
1692 * NOTE: we currently only parse and use otherName of type HwModuleName,
1693 * as defined in RFC 4108.
1694 */
1695static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1696 mbedtls_x509_san_other_name *other_name )
1697{
Janos Follathab23cd12019-05-09 13:53:57 +01001698 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001699 size_t len;
1700 unsigned char *p = subject_alt_name->p;
1701 const unsigned char *end = p + subject_alt_name->len;
1702 mbedtls_x509_buf cur_oid;
1703
1704 if( ( subject_alt_name->tag &
1705 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1706 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1707 {
1708 /*
1709 * The given subject alternative name is not of type "othername".
1710 */
1711 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1712 }
1713
1714 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1715 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001716 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001717
1718 cur_oid.tag = MBEDTLS_ASN1_OID;
1719 cur_oid.p = p;
1720 cur_oid.len = len;
1721
1722 /*
1723 * Only HwModuleName is currently supported.
1724 */
1725 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1726 {
1727 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1728 }
1729
Ron Eldor890819a2019-05-13 19:03:04 +03001730 if( p + len >= end )
1731 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001732 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001733 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1734 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001735 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001736 p += len;
1737 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1738 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001739 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001740
1741 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1742 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001743 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001744
1745 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001746 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001747
1748 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1749 other_name->value.hardware_module_name.oid.p = p;
1750 other_name->value.hardware_module_name.oid.len = len;
1751
Ron Eldor890819a2019-05-13 19:03:04 +03001752 if( p + len >= end )
1753 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001754 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001755 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1756 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001757 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001758 p += len;
1759 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1760 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001761 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001762
1763 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1764 other_name->value.hardware_module_name.val.p = p;
1765 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001766 p += len;
1767 if( p != end )
1768 {
Ron Eldor890819a2019-05-13 19:03:04 +03001769 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001770 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001771 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1772 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001773 }
1774 return( 0 );
1775}
1776
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001777static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001778 const mbedtls_x509_sequence
1779 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001780 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001781{
Janos Follath865b3eb2019-12-16 11:46:15 +00001782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -03001783 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001784 size_t n = *size;
1785 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001787 mbedtls_x509_subject_alternative_name san;
1788 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001789
1790 while( cur != NULL )
1791 {
Ron Eldor890819a2019-05-13 19:03:04 +03001792 memset( &san, 0, sizeof( san ) );
1793 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1794 if( parse_ret != 0 )
1795 {
1796 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1797 {
1798 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1799 MBEDTLS_X509_SAFE_SNPRINTF;
1800 }
1801 else
1802 {
1803 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1804 MBEDTLS_X509_SAFE_SNPRINTF;
1805 }
1806 cur = cur->next;
1807 continue;
1808 }
1809
1810 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001811 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001812 /*
1813 * otherName
1814 */
Ron Eldora2913912019-05-16 16:17:38 +03001815 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001816 {
1817 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1818
1819 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1820 MBEDTLS_X509_SAFE_SNPRINTF;
1821
1822 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1823 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001824 {
Ron Eldor890819a2019-05-13 19:03:04 +03001825 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1826 MBEDTLS_X509_SAFE_SNPRINTF;
1827 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001828 MBEDTLS_X509_SAFE_SNPRINTF;
1829
Ron Eldor890819a2019-05-13 19:03:04 +03001830 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1831 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001832
Ron Eldor890819a2019-05-13 19:03:04 +03001833 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1834 MBEDTLS_X509_SAFE_SNPRINTF;
1835
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -03001836 for( i = 0; i < other_name->value.hardware_module_name.val.len; i++ )
Ron Eldor890819a2019-05-13 19:03:04 +03001837 {
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -03001838 ret = mbedtls_snprintf( p, n, "%02X", other_name->value.hardware_module_name.val.p[i] );
1839 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01001840 }
Ron Eldor890819a2019-05-13 19:03:04 +03001841 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1842 }
1843 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001844
1845 /*
1846 * dNSName
1847 */
Ron Eldora2913912019-05-16 16:17:38 +03001848 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001849 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001850 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1851 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001852 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001853 {
1854 *p = '\0';
1855 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1856 }
Ron Eldora2913912019-05-16 16:17:38 +03001857
1858 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1859 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001860 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001861 }
1862 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001863
1864 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001865 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001866 */
1867 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001868 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1869 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001870 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001871 }
1872
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001873 cur = cur->next;
1874 }
1875
1876 *p = '\0';
1877
1878 *size = n;
1879 *buf = p;
1880
1881 return( 0 );
1882}
1883
Ron Eldor890819a2019-05-13 19:03:04 +03001884int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1885 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001886{
Janos Follath865b3eb2019-12-16 11:46:15 +00001887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001888 switch( san_buf->tag &
1889 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1890 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001891 {
Ron Eldor890819a2019-05-13 19:03:04 +03001892 /*
1893 * otherName
1894 */
1895 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001896 {
Ron Eldor890819a2019-05-13 19:03:04 +03001897 mbedtls_x509_san_other_name other_name;
1898
1899 ret = x509_get_other_name( san_buf, &other_name );
1900 if( ret != 0 )
1901 return( ret );
1902
1903 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1904 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1905 memcpy( &san->san.other_name,
1906 &other_name, sizeof( other_name ) );
1907
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001908 }
Ron Eldor890819a2019-05-13 19:03:04 +03001909 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001910
Ron Eldor890819a2019-05-13 19:03:04 +03001911 /*
1912 * dNSName
1913 */
1914 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001915 {
Ron Eldor890819a2019-05-13 19:03:04 +03001916 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1917 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001918
Ron Eldor890819a2019-05-13 19:03:04 +03001919 memcpy( &san->san.unstructured_name,
1920 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001921
Ron Eldor890819a2019-05-13 19:03:04 +03001922 }
1923 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001924
Ron Eldor890819a2019-05-13 19:03:04 +03001925 /*
1926 * Type not supported
1927 */
1928 default:
1929 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1930 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001931 return( 0 );
1932}
1933
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001934#define PRINT_ITEM(i) \
1935 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001937 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001938 sep = ", "; \
1939 }
1940
1941#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001942 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001943 PRINT_ITEM( name );
1944
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001945static int x509_info_cert_type( char **buf, size_t *size,
1946 unsigned char ns_cert_type )
1947{
Janos Follath865b3eb2019-12-16 11:46:15 +00001948 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001949 size_t n = *size;
1950 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001951 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001954 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1957 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001958 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1959 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1960 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001961
1962 *size = n;
1963 *buf = p;
1964
1965 return( 0 );
1966}
1967
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001968#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001969 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001970 PRINT_ITEM( name );
1971
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001972static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001973 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001974{
Janos Follath865b3eb2019-12-16 11:46:15 +00001975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001976 size_t n = *size;
1977 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001978 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1981 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001982 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1983 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1984 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1986 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001987 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1988 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001989
1990 *size = n;
1991 *buf = p;
1992
1993 return( 0 );
1994}
1995
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001996static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001998{
Janos Follath865b3eb2019-12-16 11:46:15 +00001999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002000 const char *desc;
2001 size_t n = *size;
2002 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002004 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002005
2006 while( cur != NULL )
2007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002009 desc = "???";
2010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002012 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002013
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002014 sep = ", ";
2015
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002016 cur = cur->next;
2017 }
2018
2019 *size = n;
2020 *buf = p;
2021
2022 return( 0 );
2023}
2024
Ron Eldor74d9acc2019-03-21 14:00:03 +02002025static int x509_info_cert_policies( char **buf, size_t *size,
2026 const mbedtls_x509_sequence *certificate_policies )
2027{
Janos Follath865b3eb2019-12-16 11:46:15 +00002028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002029 const char *desc;
2030 size_t n = *size;
2031 char *p = *buf;
2032 const mbedtls_x509_sequence *cur = certificate_policies;
2033 const char *sep = "";
2034
2035 while( cur != NULL )
2036 {
2037 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2038 desc = "???";
2039
2040 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2041 MBEDTLS_X509_SAFE_SNPRINTF;
2042
2043 sep = ", ";
2044
2045 cur = cur->next;
2046 }
2047
2048 *size = n;
2049 *buf = p;
2050
2051 return( 0 );
2052}
2053
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002054/*
2055 * Return an informational string about the certificate.
2056 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002057#define BEFORE_COLON 18
2058#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2060 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061{
Janos Follath865b3eb2019-12-16 11:46:15 +00002062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063 size_t n;
2064 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065 char key_size_str[BEFORE_COLON];
2066
2067 p = buf;
2068 n = size;
2069
Janos Follath98e28a72016-05-31 14:03:54 +01002070 if( NULL == crt )
2071 {
2072 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2073 MBEDTLS_X509_SAFE_SNPRINTF;
2074
2075 return( (int) ( size - n ) );
2076 }
2077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002080 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002082 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002083 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
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_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002089 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002091 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002094 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002096 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002099 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2100 crt->valid_from.year, crt->valid_from.mon,
2101 crt->valid_from.day, crt->valid_from.hour,
2102 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002103 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2107 crt->valid_to.year, crt->valid_to.mon,
2108 crt->valid_to.day, crt->valid_to.hour,
2109 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002110 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
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_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002116 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002117 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002119 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2121 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122 {
2123 return( ret );
2124 }
2125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002127 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002128 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002130 /*
2131 * Optional extensions
2132 */
2133
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002134 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002137 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002138 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002139
2140 if( crt->max_pathlen > 0 )
2141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002143 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002144 }
2145 }
2146
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002147 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002148 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002149 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002150 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002151
2152 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002153 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002154 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002155 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002156 }
2157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002161 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002162
2163 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2164 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002165 }
2166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002170 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002171
2172 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2173 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002174 }
2175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002179 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002180
2181 if( ( ret = x509_info_ext_key_usage( &p, &n,
2182 &crt->ext_key_usage ) ) != 0 )
2183 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002184 }
2185
Ron Eldor74d9acc2019-03-21 14:00:03 +02002186 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2187 {
2188 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2189 MBEDTLS_X509_SAFE_SNPRINTF;
2190
2191 if( ( ret = x509_info_cert_policies( &p, &n,
2192 &crt->certificate_policies ) ) != 0 )
2193 return( ret );
2194 }
2195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002197 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002198
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002199 return( (int) ( size - n ) );
2200}
2201
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002202struct x509_crt_verify_string {
2203 int code;
2204 const char *string;
2205};
2206
2207static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002208 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002209 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2210 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2211 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2212 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2213 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002214 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2215 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2216 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002217 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002218 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2219 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2220 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2221 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002222 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2223 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2224 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2225 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2226 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2227 { 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 +01002228 { 0, NULL }
2229};
2230
2231int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002232 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002233{
Janos Follath865b3eb2019-12-16 11:46:15 +00002234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002235 const struct x509_crt_verify_string *cur;
2236 char *p = buf;
2237 size_t n = size;
2238
2239 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2240 {
2241 if( ( flags & cur->code ) == 0 )
2242 continue;
2243
2244 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002245 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002246 flags ^= cur->code;
2247 }
2248
2249 if( flags != 0 )
2250 {
2251 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2252 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002253 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002254 }
2255
2256 return( (int) ( size - n ) );
2257}
2258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002260int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2261 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002262{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002263 unsigned int usage_must, usage_may;
2264 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2265 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2266
2267 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2268 return( 0 );
2269
2270 usage_must = usage & ~may_mask;
2271
2272 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2273 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2274
2275 usage_may = usage & may_mask;
2276
2277 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002279
2280 return( 0 );
2281}
2282#endif
2283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2285int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002286 const char *usage_oid,
2287 size_t usage_len )
2288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002290
2291 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002293 return( 0 );
2294
2295 /*
2296 * Look for the requested usage (or wildcard ANY) in our list
2297 */
2298 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002301
2302 if( cur_oid->len == usage_len &&
2303 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2304 {
2305 return( 0 );
2306 }
2307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002309 return( 0 );
2310 }
2311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317/*
2318 * Return 1 if the certificate is revoked, or 0 otherwise.
2319 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002320int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323
2324 while( cur != NULL && cur->serial.len != 0 )
2325 {
2326 if( crt->serial.len == cur->serial.len &&
2327 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2328 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002329 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002330 }
2331
2332 cur = cur->next;
2333 }
2334
2335 return( 0 );
2336}
2337
2338/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002339 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002340 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002343 mbedtls_x509_crl *crl_list,
2344 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345{
2346 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2348 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002349
2350 if( ca == NULL )
2351 return( flags );
2352
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002353 while( crl_list != NULL )
2354 {
2355 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002356 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002357 {
2358 crl_list = crl_list->next;
2359 continue;
2360 }
2361
2362 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002363 * Check if the CA is configured to sign CRLs
2364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002366 if( mbedtls_x509_crt_check_key_usage( ca,
2367 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002370 break;
2371 }
2372#endif
2373
2374 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002375 * Check if CRL is correctly signed by the trusted CA
2376 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002377 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2378 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2379
2380 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2381 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002384 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002385 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002386 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002388 break;
2389 }
2390
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002391 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002392 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2395 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002396 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002399 break;
2400 }
2401
2402 /*
2403 * Check for validity of CRL (Do not drop out)
2404 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002405 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002407
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002408 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002409 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002410
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002411 /*
2412 * Check if certificate is revoked
2413 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002414 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002417 break;
2418 }
2419
2420 crl_list = crl_list->next;
2421 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002422
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002423 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002426
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002427/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002428 * Check the signature of a certificate by its parent
2429 */
2430static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002431 mbedtls_x509_crt *parent,
2432 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002433{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002434 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002435 size_t hash_len;
2436#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2437 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002438 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002439 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002440
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002441 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002442 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002443 return( -1 );
2444#else
Jaeden Amero34973232019-02-20 10:32:28 +00002445 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002446 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2447
2448 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2449 return( -1 );
2450
2451 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2452 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002453 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002454 return( -1 );
2455 }
2456
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002457 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2458 != PSA_SUCCESS )
2459 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002460 return( -1 );
2461 }
2462#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002463 /* Skip expensive computation on obvious mismatch */
2464 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002465 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002466
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002467#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002468 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2469 {
2470 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002471 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002472 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002473 }
2474#else
2475 (void) rs_ctx;
2476#endif
2477
2478 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002479 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002480 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002481}
2482
2483/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002484 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2485 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002486 *
2487 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2490 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002491 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002492{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002493 int need_ca_bit;
2494
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002495 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002496 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002497 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002498
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002499 /* Parent must have the basicConstraints CA bit set as a general rule */
2500 need_ca_bit = 1;
2501
2502 /* Exception: v1/v2 certificates that are locally trusted. */
2503 if( top && parent->version < 3 )
2504 need_ca_bit = 0;
2505
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002506 if( need_ca_bit && ! parent->ca_istrue )
2507 return( -1 );
2508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002510 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002512 {
2513 return( -1 );
2514 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002515#endif
2516
2517 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002518}
2519
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002520/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002521 * Find a suitable parent for child in candidates, or return NULL.
2522 *
2523 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002524 * 1. subject name matches child's issuer
2525 * 2. if necessary, the CA bit is set and key usage allows signing certs
2526 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002527 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002528 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002529 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002530 * If there's a suitable candidate which is also time-valid, return the first
2531 * such. Otherwise, return the first suitable candidate (or NULL if there is
2532 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002533 *
2534 * The rationale for this rule is that someone could have a list of trusted
2535 * roots with two versions on the same root with different validity periods.
2536 * (At least one user reported having such a list and wanted it to just work.)
2537 * The reason we don't just require time-validity is that generally there is
2538 * only one version, and if it's expired we want the flags to state that
2539 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002540 *
2541 * The rationale for rule 3 (signature for trusted roots) is that users might
2542 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002543 * way we select the correct one is by checking the signature (as we don't
2544 * rely on key identifier extensions). (This is one way users might choose to
2545 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002546 *
2547 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002548 * - [in] child: certificate for which we're looking for a parent
2549 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002550 * - [out] r_parent: parent found (or NULL)
2551 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002552 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2553 * of the chain, 0 otherwise
2554 * - [in] path_cnt: number of intermediates seen so far
2555 * - [in] self_cnt: number of self-signed intermediates seen so far
2556 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002557 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002558 *
2559 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002560 * - 0 on success
2561 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002562 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002563static int x509_crt_find_parent_in(
2564 mbedtls_x509_crt *child,
2565 mbedtls_x509_crt *candidates,
2566 mbedtls_x509_crt **r_parent,
2567 int *r_signature_is_good,
2568 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002569 unsigned path_cnt,
2570 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002571 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002572{
Janos Follath865b3eb2019-12-16 11:46:15 +00002573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002574 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002575 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002576
2577#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002578 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002579 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2580 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002581 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582 parent = rs_ctx->parent;
2583 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002584 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002585
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002586 /* clear saved state */
2587 rs_ctx->parent = NULL;
2588 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002589 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002590
2591 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002592 goto check_signature;
2593 }
2594#endif
2595
2596 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002597 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002598
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002599 for( parent = candidates; parent != NULL; parent = parent->next )
2600 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002601 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002602 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002603 continue;
2604
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002605 /* +1 because stored max_pathlen is 1 higher that the actual value */
2606 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002607 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002608 {
2609 continue;
2610 }
2611
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002612 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002613#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2614check_signature:
2615#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002616 ret = x509_crt_check_signature( child, parent, rs_ctx );
2617
2618#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002619 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2620 {
2621 /* save state */
2622 rs_ctx->parent = parent;
2623 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002624 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002625
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002626 return( ret );
2627 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002628#else
2629 (void) ret;
2630#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002631
2632 signature_is_good = ret == 0;
2633 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002634 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002635
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002636 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002637 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2638 mbedtls_x509_time_is_future( &parent->valid_from ) )
2639 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002640 if( fallback_parent == NULL )
2641 {
2642 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002643 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002644 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002645
2646 continue;
2647 }
2648
Andy Gross1f627142019-01-30 10:25:53 -06002649 *r_parent = parent;
2650 *r_signature_is_good = signature_is_good;
2651
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002652 break;
2653 }
2654
Andy Gross1f627142019-01-30 10:25:53 -06002655 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002656 {
2657 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002658 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002659 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002660
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002661 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002662}
2663
2664/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002665 * Find a parent in trusted CAs or the provided chain, or return NULL.
2666 *
2667 * Searches in trusted CAs first, and return the first suitable parent found
2668 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002669 *
2670 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002671 * - [in] child: certificate for which we're looking for a parent, followed
2672 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002673 * - [in] trust_ca: list of locally trusted certificates
2674 * - [out] parent: parent found (or NULL)
2675 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2676 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2677 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2678 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002679 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002680 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002681 *
2682 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002683 * - 0 on success
2684 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002685 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002686static int x509_crt_find_parent(
2687 mbedtls_x509_crt *child,
2688 mbedtls_x509_crt *trust_ca,
2689 mbedtls_x509_crt **parent,
2690 int *parent_is_trusted,
2691 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002692 unsigned path_cnt,
2693 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002694 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002695{
Janos Follath865b3eb2019-12-16 11:46:15 +00002696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002697 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002698
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002699 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002700
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002701#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002702 /* restore then clear saved state if we have some stored */
2703 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2704 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002705 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002706 rs_ctx->parent_is_trusted = -1;
2707 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002708#endif
2709
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002710 while( 1 ) {
2711 search_list = *parent_is_trusted ? trust_ca : child->next;
2712
2713 ret = x509_crt_find_parent_in( child, search_list,
2714 parent, signature_is_good,
2715 *parent_is_trusted,
2716 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002717
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002718#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002719 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2720 {
2721 /* save state */
2722 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002723 return( ret );
2724 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002725#else
2726 (void) ret;
2727#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002728
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002729 /* stop here if found or already in second iteration */
2730 if( *parent != NULL || *parent_is_trusted == 0 )
2731 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002732
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002733 /* prepare second iteration */
2734 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002735 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002736
2737 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002738 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002739 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002740 *parent_is_trusted = 0;
2741 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002742 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002743
2744 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002745}
2746
2747/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002748 * Check if an end-entity certificate is locally trusted
2749 *
2750 * Currently we require such certificates to be self-signed (actually only
2751 * check for self-issued as self-signatures are not checked)
2752 */
2753static int x509_crt_check_ee_locally_trusted(
2754 mbedtls_x509_crt *crt,
2755 mbedtls_x509_crt *trust_ca )
2756{
2757 mbedtls_x509_crt *cur;
2758
2759 /* must be self-issued */
2760 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2761 return( -1 );
2762
2763 /* look for an exact match with trusted cert */
2764 for( cur = trust_ca; cur != NULL; cur = cur->next )
2765 {
2766 if( crt->raw.len == cur->raw.len &&
2767 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2768 {
2769 return( 0 );
2770 }
2771 }
2772
2773 /* too bad */
2774 return( -1 );
2775}
2776
2777/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002778 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002779 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002780 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2781 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002782 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002783 * such that every cert in the chain is a child of the next one,
2784 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002785 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002786 * Verify that chain and return it with flags for all issues found.
2787 *
2788 * Special cases:
2789 * - EE == Rj -> return a one-element list containing it
2790 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2791 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002792 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002793 * Tests for (aspects of) this function should include at least:
2794 * - trusted EE
2795 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002796 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002797 * - if relevant: EE untrusted
2798 * - if relevant: EE -> intermediate, untrusted
2799 * with the aspect under test checked at each relevant level (EE, int, root).
2800 * For some aspects longer chains are required, but usually length 2 is
2801 * enough (but length 1 is not in general).
2802 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002803 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002804 * - [in] crt: the cert list EE, C1, ..., Cn
2805 * - [in] trust_ca: the trusted list R1, ..., Rp
2806 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002807 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002808 * Only valid when return value is 0, may contain garbage otherwise!
2809 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002810 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002811 *
2812 * Return value:
2813 * - non-zero if the chain could not be fully built and examined
2814 * - 0 is the chain was successfully built and examined,
2815 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002816 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002817static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002818 mbedtls_x509_crt *crt,
2819 mbedtls_x509_crt *trust_ca,
2820 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002821 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2822 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002823 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002824 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002825 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002826{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002827 /* Don't initialize any of those variables here, so that the compiler can
2828 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002829 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002830 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002831 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002832 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002833 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002834 int parent_is_trusted;
2835 int child_is_trusted;
2836 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002837 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002838 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002839
2840#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2841 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002842 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002843 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002844 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002845 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002846 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002847
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002848 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002849 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002850 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002851 flags = &cur->flags;
2852
2853 goto find_parent;
2854 }
2855#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002856
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002857 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002858 self_cnt = 0;
2859 parent_is_trusted = 0;
2860 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002861
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002862 while( 1 ) {
2863 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002864 cur = &ver_chain->items[ver_chain->len];
2865 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002866 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002867 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002868 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002869
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002870 /* Check time-validity (all certificates) */
2871 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2872 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002873
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002874 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2875 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002876
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002877 /* Stop here for trusted roots (but not for trusted EE certs) */
2878 if( child_is_trusted )
2879 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002880
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002881 /* Check signature algorithm: MD & PK algs */
2882 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2883 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002884
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002885 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2886 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002887
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002888 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002889 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002890 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2891 {
2892 return( 0 );
2893 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002894
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002895#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2896find_parent:
2897#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002898
2899 /* Obtain list of potential trusted signers from CA callback,
2900 * or use statically provided list. */
2901#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2902 if( f_ca_cb != NULL )
2903 {
2904 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2905 mbedtls_free( ver_chain->trust_ca_cb_result );
2906 ver_chain->trust_ca_cb_result = NULL;
2907
2908 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2909 if( ret != 0 )
2910 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2911
2912 cur_trust_ca = ver_chain->trust_ca_cb_result;
2913 }
2914 else
2915#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2916 {
2917 ((void) f_ca_cb);
2918 ((void) p_ca_cb);
2919 cur_trust_ca = trust_ca;
2920 }
2921
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002922 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002923 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002924 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002925 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002926
2927#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002928 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2929 {
2930 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002931 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002932 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002933 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002934
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002935 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002936 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002937#else
2938 (void) ret;
2939#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002940
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002941 /* No parent? We're done here */
2942 if( parent == NULL )
2943 {
2944 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2945 return( 0 );
2946 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002947
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002948 /* Count intermediate self-issued (not necessarily self-signed) certs.
2949 * These can occur with some strategies for key rollover, see [SIRO],
2950 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002951 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002952 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2953 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002954 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002955 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002956
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002957 /* path_cnt is 0 for the first intermediate CA,
2958 * and if parent is trusted it's not an intermediate CA */
2959 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002960 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002961 {
2962 /* return immediately to avoid overflow the chain array */
2963 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2964 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002965
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002966 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002967 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002968 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2969
2970 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002971 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002972 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002976 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002977#else
2978 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002979#endif
2980
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002981 /* prepare for next iteration */
2982 child = parent;
2983 parent = NULL;
2984 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002985 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002986 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002987}
2988
2989/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002990 * Check for CN match
2991 */
2992static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2993 const char *cn, size_t cn_len )
2994{
2995 /* try exact match */
2996 if( name->len == cn_len &&
2997 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2998 {
2999 return( 0 );
3000 }
3001
3002 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003003 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003004 {
3005 return( 0 );
3006 }
3007
3008 return( -1 );
3009}
3010
3011/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003012 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3013 */
3014static int x509_crt_check_san( const mbedtls_x509_buf *name,
3015 const char *cn, size_t cn_len )
3016{
3017 const unsigned char san_type = (unsigned char) name->tag &
3018 MBEDTLS_ASN1_TAG_VALUE_MASK;
3019
3020 /* dNSName */
3021 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3022 return( x509_crt_check_cn( name, cn, cn_len ) );
3023
3024 /* (We may handle other types here later.) */
3025
3026 /* Unrecognized type */
3027 return( -1 );
3028}
3029
3030/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003031 * Verify the requested CN - only call this if cn is not NULL!
3032 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003033static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003034 const char *cn,
3035 uint32_t *flags )
3036{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003037 const mbedtls_x509_name *name;
3038 const mbedtls_x509_sequence *cur;
3039 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003040
3041 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3042 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003043 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003044 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003045 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003046 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003047 }
3048
3049 if( cur == NULL )
3050 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3051 }
3052 else
3053 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003054 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003055 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003056 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3057 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003058 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003059 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003060 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003061 }
3062
3063 if( name == NULL )
3064 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3065 }
3066}
3067
3068/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003069 * Merge the flags for all certs in the chain, after calling callback
3070 */
3071static int x509_crt_merge_flags_with_cb(
3072 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003073 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003074 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3075 void *p_vrfy )
3076{
Janos Follath865b3eb2019-12-16 11:46:15 +00003077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003078 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003079 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003080 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003081
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003082 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003083 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003084 cur = &ver_chain->items[i-1];
3085 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003086
3087 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003088 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003089 return( ret );
3090
3091 *flags |= cur_flags;
3092 }
3093
3094 return( 0 );
3095}
3096
3097/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003098 * Verify the certificate validity, with profile, restartable version
3099 *
3100 * This function:
3101 * - checks the requested CN (if any)
3102 * - checks the type and size of the EE cert's key,
3103 * as that isn't done as part of chain building/verification currently
3104 * - builds and verifies the chain
3105 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003106 *
3107 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3108 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3109 * verification routine to search for trusted signers, and CRLs will
3110 * be disabled. Otherwise, `trust_ca` will be used as the static list
3111 * of trusted signers, and `ca_crl` will be use as the static list
3112 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003113 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003114static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003115 mbedtls_x509_crt *trust_ca,
3116 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003117 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3118 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003119 const mbedtls_x509_crt_profile *profile,
3120 const char *cn, uint32_t *flags,
3121 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3122 void *p_vrfy,
3123 mbedtls_x509_crt_restart_ctx *rs_ctx )
3124{
Janos Follath865b3eb2019-12-16 11:46:15 +00003125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003126 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003127 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003128 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003129
3130 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003131 ee_flags = 0;
3132 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003133
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003134 if( profile == NULL )
3135 {
3136 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3137 goto exit;
3138 }
3139
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003140 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003141 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003142 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003143
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003144 /* Check the type and size of the key */
3145 pk_type = mbedtls_pk_get_type( &crt->pk );
3146
3147 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003148 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003149
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003150 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003151 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003152
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003153 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003154 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3155 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003156 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003157
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003158 if( ret != 0 )
3159 goto exit;
3160
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003161 /* Merge end-entity flags */
3162 ver_chain.items[0].flags |= ee_flags;
3163
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003164 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003165 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003166
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003167exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003168
3169#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3170 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3171 mbedtls_free( ver_chain.trust_ca_cb_result );
3172 ver_chain.trust_ca_cb_result = NULL;
3173#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3174
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003175#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3176 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3177 mbedtls_x509_crt_restart_free( rs_ctx );
3178#endif
3179
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003180 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3181 * the SSL module for authmode optional, but non-zero return from the
3182 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003183 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3184 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3185
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003186 if( ret != 0 )
3187 {
3188 *flags = (uint32_t) -1;
3189 return( ret );
3190 }
3191
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003192 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003194
3195 return( 0 );
3196}
3197
Hanno Becker3116fb32019-03-28 13:34:42 +00003198
3199/*
3200 * Verify the certificate validity (default profile, not restartable)
3201 */
3202int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3203 mbedtls_x509_crt *trust_ca,
3204 mbedtls_x509_crl *ca_crl,
3205 const char *cn, uint32_t *flags,
3206 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3207 void *p_vrfy )
3208{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003209 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003210 NULL, NULL,
3211 &mbedtls_x509_crt_profile_default,
3212 cn, flags,
3213 f_vrfy, p_vrfy, NULL ) );
3214}
3215
3216/*
3217 * Verify the certificate validity (user-chosen profile, not restartable)
3218 */
3219int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3220 mbedtls_x509_crt *trust_ca,
3221 mbedtls_x509_crl *ca_crl,
3222 const mbedtls_x509_crt_profile *profile,
3223 const char *cn, uint32_t *flags,
3224 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3225 void *p_vrfy )
3226{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003227 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003228 NULL, NULL,
3229 profile, cn, flags,
3230 f_vrfy, p_vrfy, NULL ) );
3231}
3232
3233#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3234/*
3235 * Verify the certificate validity (user-chosen profile, CA callback,
3236 * not restartable).
3237 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003238int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003239 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3240 void *p_ca_cb,
3241 const mbedtls_x509_crt_profile *profile,
3242 const char *cn, uint32_t *flags,
3243 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3244 void *p_vrfy )
3245{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003246 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003247 f_ca_cb, p_ca_cb,
3248 profile, cn, flags,
3249 f_vrfy, p_vrfy, NULL ) );
3250}
3251#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3252
3253int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3254 mbedtls_x509_crt *trust_ca,
3255 mbedtls_x509_crl *ca_crl,
3256 const mbedtls_x509_crt_profile *profile,
3257 const char *cn, uint32_t *flags,
3258 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3259 void *p_vrfy,
3260 mbedtls_x509_crt_restart_ctx *rs_ctx )
3261{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003262 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003263 NULL, NULL,
3264 profile, cn, flags,
3265 f_vrfy, p_vrfy, rs_ctx ) );
3266}
3267
3268
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003269/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003270 * Initialize a certificate chain
3271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003273{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003275}
3276
3277/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003278 * Unallocate all certificate data
3279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003281{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 mbedtls_x509_crt *cert_cur = crt;
3283 mbedtls_x509_crt *cert_prv;
3284 mbedtls_x509_name *name_cur;
3285 mbedtls_x509_name *name_prv;
3286 mbedtls_x509_sequence *seq_cur;
3287 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003288
3289 if( crt == NULL )
3290 return;
3291
3292 do
3293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3297 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003298#endif
3299
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003300 name_cur = cert_cur->issuer.next;
3301 while( name_cur != NULL )
3302 {
3303 name_prv = name_cur;
3304 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003305 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003307 }
3308
3309 name_cur = cert_cur->subject.next;
3310 while( name_cur != NULL )
3311 {
3312 name_prv = name_cur;
3313 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003314 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003316 }
3317
3318 seq_cur = cert_cur->ext_key_usage.next;
3319 while( seq_cur != NULL )
3320 {
3321 seq_prv = seq_cur;
3322 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003323 mbedtls_platform_zeroize( seq_prv,
3324 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003326 }
3327
3328 seq_cur = cert_cur->subject_alt_names.next;
3329 while( seq_cur != NULL )
3330 {
3331 seq_prv = seq_cur;
3332 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003333 mbedtls_platform_zeroize( seq_prv,
3334 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003336 }
3337
Ron Eldor74d9acc2019-03-21 14:00:03 +02003338 seq_cur = cert_cur->certificate_policies.next;
3339 while( seq_cur != NULL )
3340 {
3341 seq_prv = seq_cur;
3342 seq_cur = seq_cur->next;
3343 mbedtls_platform_zeroize( seq_prv,
3344 sizeof( mbedtls_x509_sequence ) );
3345 mbedtls_free( seq_prv );
3346 }
3347
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003348 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003349 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003350 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003352 }
3353
3354 cert_cur = cert_cur->next;
3355 }
3356 while( cert_cur != NULL );
3357
3358 cert_cur = crt;
3359 do
3360 {
3361 cert_prv = cert_cur;
3362 cert_cur = cert_cur->next;
3363
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003364 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003365 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003367 }
3368 while( cert_cur != NULL );
3369}
3370
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003371#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3372/*
3373 * Initialize a restart context
3374 */
3375void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3376{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003377 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003378
3379 ctx->parent = NULL;
3380 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003381 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003382
3383 ctx->parent_is_trusted = -1;
3384
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003385 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003386 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003387 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003388}
3389
3390/*
3391 * Free the components of a restart context
3392 */
3393void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3394{
3395 if( ctx == NULL )
3396 return;
3397
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003398 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003399 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003400}
3401#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403#endif /* MBEDTLS_X509_CRT_PARSE_C */