blob: 0c820eca90fa1fe4c67b1256ee933a04b7f2c61d [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 common functions for parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045 */
46/*
47 * The ITU-T X.509 standard defines a certificate format for PKI.
48 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020049 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
50 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
51 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052 *
53 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
54 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
55 */
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/x509.h"
66#include "mbedtls/asn1.h"
67#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000068
Rich Evans36796df2015-02-12 18:27:14 +000069#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <string.h>
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#else
Rich Evans00ab4702015-02-06 13:43:58 +000079#include <stdio.h>
80#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010081#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020082#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010083#define mbedtls_printf printf
84#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#endif
86
Simon Butcherb5b6af22016-07-13 14:46:18 +010087#if defined(MBEDTLS_HAVE_TIME)
88#include "mbedtls/platform_time.h"
89#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000090#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010091#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092#include <time.h>
93#endif
94
Hanno Beckerd6028a12018-10-15 12:01:35 +010095#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
96#define CHECK_RANGE(min, max, val) \
97 do \
98 { \
99 if( ( val ) < ( min ) || ( val ) > ( max ) ) \
100 { \
101 return( ret ); \
102 } \
103 } while( 0 )
Rich Evans7d5a55a2015-02-13 11:48:02 +0000104
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105/*
106 * CertificateSerialNumber ::= INTEGER
107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
109 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110{
111 int ret;
112
113 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
115 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
118 **p != MBEDTLS_ASN1_INTEGER )
119 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
120 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121
122 serial->tag = *(*p)++;
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
125 return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200126
127 serial->p = *p;
128 *p += serial->len;
129
130 return( 0 );
131}
132
133/* Get an algorithm identifier without parameters (eg for signatures)
134 *
135 * AlgorithmIdentifier ::= SEQUENCE {
136 * algorithm OBJECT IDENTIFIER,
137 * parameters ANY DEFINED BY algorithm OPTIONAL }
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
140 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141{
142 int ret;
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
145 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146
147 return( 0 );
148}
149
150/*
Antonin Décimod5f47592019-01-23 15:24:37 +0100151 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
154 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100155{
156 int ret;
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
159 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100160
161 return( 0 );
162}
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100165/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100166 * HashAlgorithm ::= AlgorithmIdentifier
167 *
168 * AlgorithmIdentifier ::= SEQUENCE {
169 * algorithm OBJECT IDENTIFIER,
170 * parameters ANY DEFINED BY algorithm OPTIONAL }
171 *
172 * For HashAlgorithm, parameters MUST be NULL or absent.
173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100175{
176 int ret;
177 unsigned char *p;
178 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100180 size_t len;
181
182 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
184 return( MBEDTLS_ERR_X509_INVALID_ALG +
185 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100186
187 p = (unsigned char *) alg->p;
188 end = p + alg->len;
189
190 if( p >= end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_X509_INVALID_ALG +
192 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100193
194 /* Parse md_oid */
195 md_oid.tag = *p;
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
198 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100199
200 md_oid.p = p;
201 p += md_oid.len;
202
203 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
205 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100206
207 /* Make sure params is absent of NULL */
208 if( p == end )
209 return( 0 );
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
212 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100213
214 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_X509_INVALID_ALG +
216 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100217
218 return( 0 );
219}
220
221/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100222 * RSASSA-PSS-params ::= SEQUENCE {
223 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
224 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
225 * saltLength [2] INTEGER DEFAULT 20,
226 * trailerField [3] INTEGER DEFAULT 1 }
227 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200228 *
229 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
230 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
231 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
234 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200235 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100236{
237 int ret;
238 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100239 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100240 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100242
243 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 *md_alg = MBEDTLS_MD_SHA1;
245 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100246 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100247
248 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
250 return( MBEDTLS_ERR_X509_INVALID_ALG +
251 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100252
253 p = (unsigned char *) params->p;
254 end = p + params->len;
255
256 if( p == end )
257 return( 0 );
258
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100259 /*
260 * HashAlgorithm
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
263 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100264 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100265 end2 = p + len;
266
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100267 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100269 return( ret );
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
272 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100273
274 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return( MBEDTLS_ERR_X509_INVALID_ALG +
276 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100277 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
279 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100280
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100281 if( p == end )
282 return( 0 );
283
284 /*
285 * MaskGenAlgorithm
286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
288 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100289 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100290 end2 = p + len;
291
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100292 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100294 return( ret );
295
296 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
298 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
299 MBEDTLS_ERR_OID_NOT_FOUND );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100300
301 /* Parse HashAlgorithm */
302 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
303 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100304
305 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 return( MBEDTLS_ERR_X509_INVALID_ALG +
307 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100308 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
310 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100311
312 if( p == end )
313 return( 0 );
314
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100315 /*
316 * salt_len
317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
319 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100320 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100321 end2 = p + len;
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
324 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100325
326 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 return( MBEDTLS_ERR_X509_INVALID_ALG +
328 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
331 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100332
333 if( p == end )
334 return( 0 );
335
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100336 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200337 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
340 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100341 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200342 int trailer_field;
343
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100344 end2 = p + len;
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
347 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100348
349 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 return( MBEDTLS_ERR_X509_INVALID_ALG +
351 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200352
353 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100355 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
357 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100358
359 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 return( MBEDTLS_ERR_X509_INVALID_ALG +
361 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100362
363 return( 0 );
364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100366
367/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368 * AttributeTypeAndValue ::= SEQUENCE {
369 * type AttributeType,
370 * value AttributeValue }
371 *
372 * AttributeType ::= OBJECT IDENTIFIER
373 *
374 * AttributeValue ::= ANY DEFINED BY AttributeType
375 */
376static int x509_get_attr_type_value( unsigned char **p,
377 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379{
380 int ret;
381 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_x509_buf *oid;
383 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
386 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
387 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000389 end = *p + len;
390
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 return( MBEDTLS_ERR_X509_INVALID_NAME +
393 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394
395 oid = &cur->oid;
396 oid->tag = **p;
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
399 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400
401 oid->p = *p;
402 *p += oid->len;
403
404 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 return( MBEDTLS_ERR_X509_INVALID_NAME +
406 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
409 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
410 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
411 **p != MBEDTLS_ASN1_BIT_STRING )
412 return( MBEDTLS_ERR_X509_INVALID_NAME +
413 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414
415 val = &cur->val;
416 val->tag = *(*p)++;
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
419 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
421 val->p = *p;
422 *p += val->len;
423
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000424 if( *p != end )
425 {
426 return( MBEDTLS_ERR_X509_INVALID_NAME +
427 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
428 }
429
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 cur->next = NULL;
431
432 return( 0 );
433}
434
435/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000436 * Name ::= CHOICE { -- only one possibility for now --
437 * rdnSequence RDNSequence }
438 *
439 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
440 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 * RelativeDistinguishedName ::=
442 * SET OF AttributeTypeAndValue
443 *
444 * AttributeTypeAndValue ::= SEQUENCE {
445 * type AttributeType,
446 * value AttributeValue }
447 *
448 * AttributeType ::= OBJECT IDENTIFIER
449 *
450 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200451 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000452 * The data structure is optimized for the common case where each RDN has only
453 * one element, which is represented as a list of AttributeTypeAndValue.
454 * For the general case we still use a flat list, but we mark elements of the
455 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
459 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460{
461 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200462 size_t set_len;
463 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100465 /* don't use recursion, we'd risk stack overflow if not optimized */
466 while( 1 )
467 {
468 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000469 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
472 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
473 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100475 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000477 while( 1 )
478 {
479 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
480 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000482 if( *p == end_set )
483 break;
484
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200485 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000486 cur->next_merged = 1;
487
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200488 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000489
490 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200491 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000492
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000493 cur = cur->next;
494 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100496 /*
497 * continue until end of SEQUENCE is reached
498 */
499 if( *p == end )
500 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200502 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100504 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200505 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100507 cur = cur->next;
508 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509}
510
Janos Follath87c98072017-02-03 12:36:59 +0000511static int x509_parse_int( unsigned char **p, size_t n, int *res )
512{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000513 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000514
515 for( ; n > 0; --n )
516 {
517 if( ( **p < '0') || ( **p > '9' ) )
518 return ( MBEDTLS_ERR_X509_INVALID_DATE );
519
Rich Evans7d5a55a2015-02-13 11:48:02 +0000520 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000521 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000522 }
Janos Follath87c98072017-02-03 12:36:59 +0000523
524 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000525}
526
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000527static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100528{
529 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000530 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100531
Hanno Becker61937d42017-04-26 15:01:23 +0100532 CHECK_RANGE( 0, 9999, t->year );
533 CHECK_RANGE( 0, 23, t->hour );
534 CHECK_RANGE( 0, 59, t->min );
535 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100536
Hanno Becker61937d42017-04-26 15:01:23 +0100537 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100538 {
539 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000540 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100541 break;
542 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000543 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100544 break;
545 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000546 if( ( !( t->year % 4 ) && t->year % 100 ) ||
547 !( t->year % 400 ) )
548 month_len = 29;
549 else
550 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100551 break;
552 default:
553 return( ret );
554 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000555 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100556
557 return( 0 );
558}
559
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560/*
Janos Follath87c98072017-02-03 12:36:59 +0000561 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
562 * field.
563 */
564static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100565 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000566{
567 int ret;
568
569 /*
570 * Minimum length is 10 or 12 depending on yearlen
571 */
572 if ( len < yearlen + 8 )
573 return ( MBEDTLS_ERR_X509_INVALID_DATE );
574 len -= yearlen + 8;
575
576 /*
577 * Parse year, month, day, hour, minute
578 */
Hanno Becker61937d42017-04-26 15:01:23 +0100579 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000580 if ( 2 == yearlen )
581 {
Hanno Becker61937d42017-04-26 15:01:23 +0100582 if ( tm->year < 50 )
583 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000584
Hanno Becker61937d42017-04-26 15:01:23 +0100585 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000586 }
587
Hanno Becker61937d42017-04-26 15:01:23 +0100588 CHECK( x509_parse_int( p, 2, &tm->mon ) );
589 CHECK( x509_parse_int( p, 2, &tm->day ) );
590 CHECK( x509_parse_int( p, 2, &tm->hour ) );
591 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000592
593 /*
594 * Parse seconds if present
595 */
596 if ( len >= 2 )
597 {
Hanno Becker61937d42017-04-26 15:01:23 +0100598 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000599 len -= 2;
600 }
601 else
602 return ( MBEDTLS_ERR_X509_INVALID_DATE );
603
604 /*
605 * Parse trailing 'Z' if present
606 */
607 if ( 1 == len && 'Z' == **p )
608 {
609 (*p)++;
610 len--;
611 }
612
613 /*
614 * We should have parsed all characters at this point
615 */
616 if ( 0 != len )
617 return ( MBEDTLS_ERR_X509_INVALID_DATE );
618
Hanno Becker61937d42017-04-26 15:01:23 +0100619 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000620
621 return ( 0 );
622}
623
624/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 * Time ::= CHOICE {
626 * utcTime UTCTime,
627 * generalTime GeneralizedTime }
628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100630 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631{
632 int ret;
Janos Follath87c98072017-02-03 12:36:59 +0000633 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 unsigned char tag;
635
636 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_X509_INVALID_DATE +
638 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639
640 tag = **p;
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000643 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000645 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_X509_INVALID_DATE +
648 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Janos Follath87c98072017-02-03 12:36:59 +0000649
650 (*p)++;
651 ret = mbedtls_asn1_get_len( p, end, &len );
652
653 if( ret != 0 )
654 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
655
Hanno Becker61937d42017-04-26 15:01:23 +0100656 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657}
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660{
661 int ret;
662 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100663 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
665 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
667 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668
Andres AG4bdbe092016-09-19 16:58:45 +0100669 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
672 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673
Andres AG4bdbe092016-09-19 16:58:45 +0100674 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 sig->len = len;
676 sig->p = *p;
677
678 *p += len;
679
680 return( 0 );
681}
682
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100683/*
684 * Get signature algorithm from alg OID and optional parameters
685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
687 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200688 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100690 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200692 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
696 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
699 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100702
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200703 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200704 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200705 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200708 md_alg,
709 &pss_opts->mgf1_hash_id,
710 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100711 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100714 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200715 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100716
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200717 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100718 }
719 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100721 {
722 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100724 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100726 }
727
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 return( 0 );
729}
730
731/*
732 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800733 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
Hanno Becker2f472142019-02-12 11:52:10 +0000736 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737{
738 int ret;
739 size_t len;
740
Hanno Beckerc74ce442019-02-11 14:33:36 +0000741 /* Extension structure use EXPLICIT tagging. That is, the actual
742 * `Extensions` structure is wrapped by a tag-length pair using
743 * the respective context-specific tag. */
Hanno Becker2f472142019-02-12 11:52:10 +0000744 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
745 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
746 if( ret != 0 )
747 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748
Hanno Becker2f472142019-02-12 11:52:10 +0000749 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
750 ext->p = *p;
751 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752
753 /*
754 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
757 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
758 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759
760 if( end != *p + len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
762 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763
764 return( 0 );
765}
766
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767/*
768 * Store the name in printable form into buf; no more
769 * than size characters will be written
770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772{
773 int ret;
774 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000775 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779
780 memset( s, 0, sizeof( s ) );
781
782 name = dn;
783 p = buf;
784 n = size;
785
786 while( name != NULL )
787 {
788 if( !name->oid.p )
789 {
790 name = name->next;
791 continue;
792 }
793
794 if( name != dn )
795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200797 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798 }
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801
802 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200806 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807
808 for( i = 0; i < name->val.len; i++ )
809 {
810 if( i >= sizeof( s ) - 1 )
811 break;
812
813 c = name->val.p[i];
814 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
815 s[i] = '?';
816 else s[i] = c;
817 }
818 s[i] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200820 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000821
822 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 name = name->next;
824 }
825
826 return( (int) ( size - n ) );
827}
828
829/*
830 * Store the serial in printable form into buf; no more
831 * than size characters will be written
832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834{
835 int ret;
836 size_t i, n, nr;
837 char *p;
838
839 p = buf;
840 n = size;
841
842 nr = ( serial->len <= 32 )
843 ? serial->len : 28;
844
845 for( i = 0; i < nr; i++ )
846 {
847 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
848 continue;
849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200852 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853 }
854
855 if( nr != serial->len )
856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200858 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 }
860
861 return( (int) ( size - n ) );
862}
863
864/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200865 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
868 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200869 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100870{
871 int ret;
872 char *p = buf;
873 size_t n = size;
874 const char *desc = NULL;
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100877 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100879 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200881 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
884 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 const mbedtls_pk_rsassa_pss_options *pss_opts;
887 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 md_info = mbedtls_md_info_from_type( md_alg );
892 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
895 md_info ? mbedtls_md_get_name( md_info ) : "???",
896 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200897 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200898 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100899 }
900#else
901 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200902 ((void) md_alg);
903 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100905
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200906 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100907}
908
909/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910 * Helper for writing "RSA key size", "EC key size", etc
911 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200912int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913{
914 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200915 size_t n = buf_size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 int ret;
917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200919 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920
921 return( 0 );
922}
923
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200924#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200925/*
926 * Set the time structure to the current time.
927 * Return 0 on success, non-zero on failure.
928 */
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200929static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100930{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000931 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100932 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200933 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934
SimonBd5800b72016-04-26 07:43:27 +0100935 tt = mbedtls_time( NULL );
Hanno Becker6a739782018-09-05 15:06:19 +0100936 lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200937
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200938 if( lt == NULL )
939 ret = -1;
940 else
941 {
942 now->year = lt->tm_year + 1900;
943 now->mon = lt->tm_mon + 1;
944 now->day = lt->tm_mday;
945 now->hour = lt->tm_hour;
946 now->min = lt->tm_min;
947 now->sec = lt->tm_sec;
948 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200949
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200950 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100951}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100953/*
954 * Return 0 if before <= after, 1 otherwise
955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100957{
958 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 return( 1 );
960
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100961 if( before->year == after->year &&
962 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 return( 1 );
964
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100965 if( before->year == after->year &&
966 before->mon == after->mon &&
967 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 return( 1 );
969
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100970 if( before->year == after->year &&
971 before->mon == after->mon &&
972 before->day == after->day &&
973 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 return( 1 );
975
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100976 if( before->year == after->year &&
977 before->mon == after->mon &&
978 before->day == after->day &&
979 before->hour == after->hour &&
980 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 return( 1 );
982
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100983 if( before->year == after->year &&
984 before->mon == after->mon &&
985 before->day == after->day &&
986 before->hour == after->hour &&
987 before->min == after->min &&
988 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 return( 1 );
990
991 return( 0 );
992}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100993
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100994int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100995{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100997
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200998 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +0200999 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001000
1001 return( x509_check_time( &now, to ) );
1002}
1003
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001004int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001007
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001008 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001009 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001010
1011 return( x509_check_time( from, &now ) );
1012}
1013
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001014#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001015
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001016int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017{
1018 ((void) to);
1019 return( 0 );
1020}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001021
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001022int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001023{
1024 ((void) from);
1025 return( 0 );
1026}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001027#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001031#include "mbedtls/x509_crt.h"
1032#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001033
1034/*
1035 * Checkup routine
1036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037int mbedtls_x509_self_test( int verbose )
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001038{
Junhwan Park60ee28b2018-10-17 21:01:08 +09001039 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001040#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001041 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_x509_crt cacert;
1043 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001044
1045 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001047
Junhwan Park60ee28b2018-10-17 21:01:08 +09001048 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001052 mbedtls_test_cli_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001053 if( ret != 0 )
1054 {
1055 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001057
Junhwan Park60ee28b2018-10-17 21:01:08 +09001058 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001059 }
1060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001062 mbedtls_test_ca_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001063 if( ret != 0 )
1064 {
1065 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001067
Junhwan Park60ee28b2018-10-17 21:01:08 +09001068 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001069 }
1070
1071 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001075 if( ret != 0 )
1076 {
1077 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001079
Junhwan Park60ee28b2018-10-17 21:01:08 +09001080 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001081 }
1082
1083 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001085
Junhwan Park60ee28b2018-10-17 21:01:08 +09001086cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_x509_crt_free( &cacert );
1088 mbedtls_x509_crt_free( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001089#else
1090 ((void) verbose);
Simon Butcher5cc08792020-03-27 16:55:35 +00001091#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
Junhwan Park60ee28b2018-10-17 21:01:08 +09001092 return( ret );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001093}
1094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#endif /* MBEDTLS_X509_USE_C */