blob: 63ceaf9f4dbc52cedfbdb0edd705fdd6207e7d0b [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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047 */
48/*
49 * The ITU-T X.509 standard defines a certificate format for PKI.
50 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020051 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
52 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
53 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054 *
55 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
56 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
57 */
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020061#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020063#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/x509.h"
68#include "mbedtls/asn1.h"
69#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000070
Rich Evans36796df2015-02-12 18:27:14 +000071#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000072#include <string.h>
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000079#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#else
Rich Evans00ab4702015-02-06 13:43:58 +000081#include <stdio.h>
82#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010083#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020084#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010085#define mbedtls_printf printf
86#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087#endif
88
Simon Butcherb5b6af22016-07-13 14:46:18 +010089#if defined(MBEDTLS_HAVE_TIME)
90#include "mbedtls/platform_time.h"
91#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000092#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010093#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094#include <time.h>
95#endif
96
Hanno Beckerd6028a12018-10-15 12:01:35 +010097#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
98#define CHECK_RANGE(min, max, val) \
99 do \
100 { \
101 if( ( val ) < ( min ) || ( val ) > ( max ) ) \
102 { \
103 return( ret ); \
104 } \
105 } while( 0 )
Rich Evans7d5a55a2015-02-13 11:48:02 +0000106
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107/*
108 * CertificateSerialNumber ::= INTEGER
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
111 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112{
113 int ret;
114
115 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
117 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
120 **p != MBEDTLS_ASN1_INTEGER )
121 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
122 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123
124 serial->tag = *(*p)++;
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
127 return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128
129 serial->p = *p;
130 *p += serial->len;
131
132 return( 0 );
133}
134
135/* Get an algorithm identifier without parameters (eg for signatures)
136 *
137 * AlgorithmIdentifier ::= SEQUENCE {
138 * algorithm OBJECT IDENTIFIER,
139 * parameters ANY DEFINED BY algorithm OPTIONAL }
140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
142 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200143{
144 int ret;
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
147 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200148
149 return( 0 );
150}
151
152/*
Antonin Décimod5f47592019-01-23 15:24:37 +0100153 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
156 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100157{
158 int ret;
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
161 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100162
163 return( 0 );
164}
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100167/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100168 * HashAlgorithm ::= AlgorithmIdentifier
169 *
170 * AlgorithmIdentifier ::= SEQUENCE {
171 * algorithm OBJECT IDENTIFIER,
172 * parameters ANY DEFINED BY algorithm OPTIONAL }
173 *
174 * For HashAlgorithm, parameters MUST be NULL or absent.
175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176static 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 +0100177{
178 int ret;
179 unsigned char *p;
180 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100182 size_t len;
183
184 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
186 return( MBEDTLS_ERR_X509_INVALID_ALG +
187 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100188
189 p = (unsigned char *) alg->p;
190 end = p + alg->len;
191
192 if( p >= end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_X509_INVALID_ALG +
194 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100195
196 /* Parse md_oid */
197 md_oid.tag = *p;
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
200 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100201
202 md_oid.p = p;
203 p += md_oid.len;
204
205 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
207 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100208
209 /* Make sure params is absent of NULL */
210 if( p == end )
211 return( 0 );
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
214 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100215
216 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 return( MBEDTLS_ERR_X509_INVALID_ALG +
218 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100219
220 return( 0 );
221}
222
223/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100224 * RSASSA-PSS-params ::= SEQUENCE {
225 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
226 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
227 * saltLength [2] INTEGER DEFAULT 20,
228 * trailerField [3] INTEGER DEFAULT 1 }
229 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200230 *
231 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
232 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
233 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
236 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200237 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100238{
239 int ret;
240 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100241 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100242 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100244
245 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 *md_alg = MBEDTLS_MD_SHA1;
247 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100248 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100249
250 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
252 return( MBEDTLS_ERR_X509_INVALID_ALG +
253 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100254
255 p = (unsigned char *) params->p;
256 end = p + params->len;
257
258 if( p == end )
259 return( 0 );
260
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100261 /*
262 * HashAlgorithm
263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
265 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100266 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100267 end2 = p + len;
268
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100269 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100271 return( ret );
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
274 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100275
276 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 return( MBEDTLS_ERR_X509_INVALID_ALG +
278 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100279 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
281 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100282
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100283 if( p == end )
284 return( 0 );
285
286 /*
287 * MaskGenAlgorithm
288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
290 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100291 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100292 end2 = p + len;
293
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100294 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100296 return( ret );
297
298 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
300 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
301 MBEDTLS_ERR_OID_NOT_FOUND );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100302
303 /* Parse HashAlgorithm */
304 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
305 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100306
307 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 return( MBEDTLS_ERR_X509_INVALID_ALG +
309 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100310 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
312 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100313
314 if( p == end )
315 return( 0 );
316
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100317 /*
318 * salt_len
319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
321 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100322 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100323 end2 = p + len;
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
326 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100327
328 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 return( MBEDTLS_ERR_X509_INVALID_ALG +
330 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
333 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334
335 if( p == end )
336 return( 0 );
337
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100338 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200339 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
342 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100343 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200344 int trailer_field;
345
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100346 end2 = p + len;
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
349 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100350
351 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 return( MBEDTLS_ERR_X509_INVALID_ALG +
353 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200354
355 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
359 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100360
361 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 return( MBEDTLS_ERR_X509_INVALID_ALG +
363 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100364
365 return( 0 );
366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100368
369/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 * AttributeTypeAndValue ::= SEQUENCE {
371 * type AttributeType,
372 * value AttributeValue }
373 *
374 * AttributeType ::= OBJECT IDENTIFIER
375 *
376 * AttributeValue ::= ANY DEFINED BY AttributeType
377 */
378static int x509_get_attr_type_value( unsigned char **p,
379 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381{
382 int ret;
383 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_x509_buf *oid;
385 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
388 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
389 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000391 end = *p + len;
392
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 return( MBEDTLS_ERR_X509_INVALID_NAME +
395 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396
397 oid = &cur->oid;
398 oid->tag = **p;
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
401 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
403 oid->p = *p;
404 *p += oid->len;
405
406 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 return( MBEDTLS_ERR_X509_INVALID_NAME +
408 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
411 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
412 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
413 **p != MBEDTLS_ASN1_BIT_STRING )
414 return( MBEDTLS_ERR_X509_INVALID_NAME +
415 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
417 val = &cur->val;
418 val->tag = *(*p)++;
419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
421 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422
423 val->p = *p;
424 *p += val->len;
425
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000426 if( *p != end )
427 {
428 return( MBEDTLS_ERR_X509_INVALID_NAME +
429 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
430 }
431
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432 cur->next = NULL;
433
434 return( 0 );
435}
436
437/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000438 * Name ::= CHOICE { -- only one possibility for now --
439 * rdnSequence RDNSequence }
440 *
441 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
442 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 * RelativeDistinguishedName ::=
444 * SET OF AttributeTypeAndValue
445 *
446 * AttributeTypeAndValue ::= SEQUENCE {
447 * type AttributeType,
448 * value AttributeValue }
449 *
450 * AttributeType ::= OBJECT IDENTIFIER
451 *
452 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200453 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000454 * The data structure is optimized for the common case where each RDN has only
455 * one element, which is represented as a list of AttributeTypeAndValue.
456 * For the general case we still use a flat list, but we mark elements of the
457 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
461 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462{
463 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200464 size_t set_len;
465 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100467 /* don't use recursion, we'd risk stack overflow if not optimized */
468 while( 1 )
469 {
470 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000471 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
474 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
475 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100477 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000479 while( 1 )
480 {
481 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
482 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000484 if( *p == end_set )
485 break;
486
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200487 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000488 cur->next_merged = 1;
489
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200490 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000491
492 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200493 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000494
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000495 cur = cur->next;
496 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100498 /*
499 * continue until end of SEQUENCE is reached
500 */
501 if( *p == end )
502 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200504 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100506 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200507 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100509 cur = cur->next;
510 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511}
512
Janos Follath87c98072017-02-03 12:36:59 +0000513static int x509_parse_int( unsigned char **p, size_t n, int *res )
514{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000515 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000516
517 for( ; n > 0; --n )
518 {
519 if( ( **p < '0') || ( **p > '9' ) )
520 return ( MBEDTLS_ERR_X509_INVALID_DATE );
521
Rich Evans7d5a55a2015-02-13 11:48:02 +0000522 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000523 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000524 }
Janos Follath87c98072017-02-03 12:36:59 +0000525
526 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000527}
528
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000529static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100530{
531 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000532 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100533
Hanno Becker61937d42017-04-26 15:01:23 +0100534 CHECK_RANGE( 0, 9999, t->year );
535 CHECK_RANGE( 0, 23, t->hour );
536 CHECK_RANGE( 0, 59, t->min );
537 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100538
Hanno Becker61937d42017-04-26 15:01:23 +0100539 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100540 {
541 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000542 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100543 break;
544 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000545 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100546 break;
547 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000548 if( ( !( t->year % 4 ) && t->year % 100 ) ||
549 !( t->year % 400 ) )
550 month_len = 29;
551 else
552 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100553 break;
554 default:
555 return( ret );
556 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000557 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100558
559 return( 0 );
560}
561
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562/*
Janos Follath87c98072017-02-03 12:36:59 +0000563 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
564 * field.
565 */
566static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100567 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000568{
569 int ret;
570
571 /*
572 * Minimum length is 10 or 12 depending on yearlen
573 */
574 if ( len < yearlen + 8 )
575 return ( MBEDTLS_ERR_X509_INVALID_DATE );
576 len -= yearlen + 8;
577
578 /*
579 * Parse year, month, day, hour, minute
580 */
Hanno Becker61937d42017-04-26 15:01:23 +0100581 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000582 if ( 2 == yearlen )
583 {
Hanno Becker61937d42017-04-26 15:01:23 +0100584 if ( tm->year < 50 )
585 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000586
Hanno Becker61937d42017-04-26 15:01:23 +0100587 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000588 }
589
Hanno Becker61937d42017-04-26 15:01:23 +0100590 CHECK( x509_parse_int( p, 2, &tm->mon ) );
591 CHECK( x509_parse_int( p, 2, &tm->day ) );
592 CHECK( x509_parse_int( p, 2, &tm->hour ) );
593 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000594
595 /*
596 * Parse seconds if present
597 */
598 if ( len >= 2 )
599 {
Hanno Becker61937d42017-04-26 15:01:23 +0100600 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000601 len -= 2;
602 }
603 else
604 return ( MBEDTLS_ERR_X509_INVALID_DATE );
605
606 /*
607 * Parse trailing 'Z' if present
608 */
609 if ( 1 == len && 'Z' == **p )
610 {
611 (*p)++;
612 len--;
613 }
614
615 /*
616 * We should have parsed all characters at this point
617 */
618 if ( 0 != len )
619 return ( MBEDTLS_ERR_X509_INVALID_DATE );
620
Hanno Becker61937d42017-04-26 15:01:23 +0100621 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000622
623 return ( 0 );
624}
625
626/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 * Time ::= CHOICE {
628 * utcTime UTCTime,
629 * generalTime GeneralizedTime }
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100632 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633{
634 int ret;
Janos Follath87c98072017-02-03 12:36:59 +0000635 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 unsigned char tag;
637
638 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 return( MBEDTLS_ERR_X509_INVALID_DATE +
640 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641
642 tag = **p;
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000645 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000647 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( MBEDTLS_ERR_X509_INVALID_DATE +
650 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Janos Follath87c98072017-02-03 12:36:59 +0000651
652 (*p)++;
653 ret = mbedtls_asn1_get_len( p, end, &len );
654
655 if( ret != 0 )
656 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
657
Hanno Becker61937d42017-04-26 15:01:23 +0100658 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659}
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662{
663 int ret;
664 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100665 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666
667 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
669 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
Andres AG4bdbe092016-09-19 16:58:45 +0100671 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
674 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675
Andres AG4bdbe092016-09-19 16:58:45 +0100676 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 sig->len = len;
678 sig->p = *p;
679
680 *p += len;
681
682 return( 0 );
683}
684
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100685/*
686 * Get signature algorithm from alg OID and optional parameters
687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
689 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200690 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100692 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200694 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
698 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
701 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100704
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200705 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200706 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200707 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200710 md_alg,
711 &pss_opts->mgf1_hash_id,
712 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100713 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100716 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200717 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100718
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200719 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100720 }
721 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100723 {
724 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100726 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100728 }
729
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730 return( 0 );
731}
732
733/*
734 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800735 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
Hanno Becker2f472142019-02-12 11:52:10 +0000738 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739{
740 int ret;
741 size_t len;
742
Hanno Beckerc74ce442019-02-11 14:33:36 +0000743 /* Extension structure use EXPLICIT tagging. That is, the actual
744 * `Extensions` structure is wrapped by a tag-length pair using
745 * the respective context-specific tag. */
Hanno Becker2f472142019-02-12 11:52:10 +0000746 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
747 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
748 if( ret != 0 )
749 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750
Hanno Becker2f472142019-02-12 11:52:10 +0000751 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
752 ext->p = *p;
753 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754
755 /*
756 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
759 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
760 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761
762 if( end != *p + len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
764 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765
766 return( 0 );
767}
768
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769/*
770 * Store the name in printable form into buf; no more
771 * than size characters will be written
772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774{
775 int ret;
776 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000777 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781
782 memset( s, 0, sizeof( s ) );
783
784 name = dn;
785 p = buf;
786 n = size;
787
788 while( name != NULL )
789 {
790 if( !name->oid.p )
791 {
792 name = name->next;
793 continue;
794 }
795
796 if( name != dn )
797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200799 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800 }
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803
804 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200808 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809
810 for( i = 0; i < name->val.len; i++ )
811 {
812 if( i >= sizeof( s ) - 1 )
813 break;
814
815 c = name->val.p[i];
816 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
817 s[i] = '?';
818 else s[i] = c;
819 }
820 s[i] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200822 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000823
824 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825 name = name->next;
826 }
827
828 return( (int) ( size - n ) );
829}
830
831/*
832 * Store the serial in printable form into buf; no more
833 * than size characters will be written
834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836{
837 int ret;
838 size_t i, n, nr;
839 char *p;
840
841 p = buf;
842 n = size;
843
844 nr = ( serial->len <= 32 )
845 ? serial->len : 28;
846
847 for( i = 0; i < nr; i++ )
848 {
849 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
850 continue;
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200854 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855 }
856
857 if( nr != serial->len )
858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200860 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861 }
862
863 return( (int) ( size - n ) );
864}
865
866/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200867 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
870 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200871 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100872{
873 int ret;
874 char *p = buf;
875 size_t n = size;
876 const char *desc = NULL;
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100879 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100881 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200883 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
886 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 const mbedtls_pk_rsassa_pss_options *pss_opts;
889 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 md_info = mbedtls_md_info_from_type( md_alg );
894 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
897 md_info ? mbedtls_md_get_name( md_info ) : "???",
898 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200899 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200900 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100901 }
902#else
903 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200904 ((void) md_alg);
905 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100907
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200908 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100909}
910
911/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 * Helper for writing "RSA key size", "EC key size", etc
913 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200914int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915{
916 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200917 size_t n = buf_size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 int ret;
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200921 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922
923 return( 0 );
924}
925
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200926#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200927/*
928 * Set the time structure to the current time.
929 * Return 0 on success, non-zero on failure.
930 */
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200931static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100932{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000933 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100934 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200935 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936
SimonBd5800b72016-04-26 07:43:27 +0100937 tt = mbedtls_time( NULL );
Hanno Becker6a739782018-09-05 15:06:19 +0100938 lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200939
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200940 if( lt == NULL )
941 ret = -1;
942 else
943 {
944 now->year = lt->tm_year + 1900;
945 now->mon = lt->tm_mon + 1;
946 now->day = lt->tm_mday;
947 now->hour = lt->tm_hour;
948 now->min = lt->tm_min;
949 now->sec = lt->tm_sec;
950 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200951
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200952 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100953}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100955/*
956 * Return 0 if before <= after, 1 otherwise
957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100959{
960 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 return( 1 );
962
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100963 if( before->year == after->year &&
964 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 return( 1 );
966
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100967 if( before->year == after->year &&
968 before->mon == after->mon &&
969 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 return( 1 );
971
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100972 if( before->year == after->year &&
973 before->mon == after->mon &&
974 before->day == after->day &&
975 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 return( 1 );
977
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100978 if( before->year == after->year &&
979 before->mon == after->mon &&
980 before->day == after->day &&
981 before->hour == after->hour &&
982 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 return( 1 );
984
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100985 if( before->year == after->year &&
986 before->mon == after->mon &&
987 before->day == after->day &&
988 before->hour == after->hour &&
989 before->min == after->min &&
990 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 return( 1 );
992
993 return( 0 );
994}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100995
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100996int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100997{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100999
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001000 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001001 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001002
1003 return( x509_check_time( &now, to ) );
1004}
1005
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001006int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001007{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001009
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001010 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001011 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001012
1013 return( x509_check_time( from, &now ) );
1014}
1015
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001016#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001017
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001018int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019{
1020 ((void) to);
1021 return( 0 );
1022}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001023
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001024int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001025{
1026 ((void) from);
1027 return( 0 );
1028}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001029#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001032
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001033#include "mbedtls/x509_crt.h"
1034#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001035
1036/*
1037 * Checkup routine
1038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039int mbedtls_x509_self_test( int verbose )
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001040{
Junhwan Park60ee28b2018-10-17 21:01:08 +09001041 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001042#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001043 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_x509_crt cacert;
1045 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001046
1047 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001049
Junhwan Park60ee28b2018-10-17 21:01:08 +09001050 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001054 mbedtls_test_cli_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001055 if( ret != 0 )
1056 {
1057 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001059
Junhwan Park60ee28b2018-10-17 21:01:08 +09001060 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001061 }
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001064 mbedtls_test_ca_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001065 if( ret != 0 )
1066 {
1067 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001069
Junhwan Park60ee28b2018-10-17 21:01:08 +09001070 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001071 }
1072
1073 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001077 if( ret != 0 )
1078 {
1079 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001081
Junhwan Park60ee28b2018-10-17 21:01:08 +09001082 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001083 }
1084
1085 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 mbedtls_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001087
Junhwan Park60ee28b2018-10-17 21:01:08 +09001088cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_x509_crt_free( &cacert );
1090 mbedtls_x509_crt_free( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001091#else
1092 ((void) verbose);
Simon Butcher5cc08792020-03-27 16:55:35 +00001093#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
Junhwan Park60ee28b2018-10-17 21:01:08 +09001094 return( ret );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001095}
1096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#endif /* MBEDTLS_X509_USE_C */