blob: 7cc813ec64924718d5cba0080cd6ca46c903c54d [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
30 */
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/x509.h"
41#include "mbedtls/asn1.h"
42#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000043
Rich Evans36796df2015-02-12 18:27:14 +000044#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#else
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <stdio.h>
55#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010056#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020057#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010058#define mbedtls_printf printf
59#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Simon Butcherb5b6af22016-07-13 14:46:18 +010062#if defined(MBEDTLS_HAVE_TIME)
63#include "mbedtls/platform_time.h"
64#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000065#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010066#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <time.h>
68#endif
69
Rich Evans7d5a55a2015-02-13 11:48:02 +000070#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
Andres AG4b76aec2016-09-23 13:16:02 +010071#define CHECK_RANGE(min, max, val) if( val < min || val > max ){ return( ret ); }
Rich Evans7d5a55a2015-02-13 11:48:02 +000072
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073/*
74 * CertificateSerialNumber ::= INTEGER
75 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
77 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078{
79 int ret;
80
81 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
83 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
86 **p != MBEDTLS_ASN1_INTEGER )
87 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
88 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089
90 serial->tag = *(*p)++;
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
93 return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094
95 serial->p = *p;
96 *p += serial->len;
97
98 return( 0 );
99}
100
101/* Get an algorithm identifier without parameters (eg for signatures)
102 *
103 * AlgorithmIdentifier ::= SEQUENCE {
104 * algorithm OBJECT IDENTIFIER,
105 * parameters ANY DEFINED BY algorithm OPTIONAL }
106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
108 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109{
110 int ret;
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
113 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114
115 return( 0 );
116}
117
118/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100119 * Parse an algorithm identifier with (optional) paramaters
120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
122 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100123{
124 int ret;
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
127 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100128
129 return( 0 );
130}
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100133/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100134 * HashAlgorithm ::= AlgorithmIdentifier
135 *
136 * AlgorithmIdentifier ::= SEQUENCE {
137 * algorithm OBJECT IDENTIFIER,
138 * parameters ANY DEFINED BY algorithm OPTIONAL }
139 *
140 * For HashAlgorithm, parameters MUST be NULL or absent.
141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142static 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 +0100143{
144 int ret;
145 unsigned char *p;
146 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100148 size_t len;
149
150 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
152 return( MBEDTLS_ERR_X509_INVALID_ALG +
153 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100154
155 p = (unsigned char *) alg->p;
156 end = p + alg->len;
157
158 if( p >= end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 return( MBEDTLS_ERR_X509_INVALID_ALG +
160 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100161
162 /* Parse md_oid */
163 md_oid.tag = *p;
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
166 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100167
168 md_oid.p = p;
169 p += md_oid.len;
170
171 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
173 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100174
175 /* Make sure params is absent of NULL */
176 if( p == end )
177 return( 0 );
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
180 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100181
182 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 return( MBEDTLS_ERR_X509_INVALID_ALG +
184 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100185
186 return( 0 );
187}
188
189/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100190 * RSASSA-PSS-params ::= SEQUENCE {
191 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
192 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
193 * saltLength [2] INTEGER DEFAULT 20,
194 * trailerField [3] INTEGER DEFAULT 1 }
195 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200196 *
197 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
198 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
199 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
202 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200203 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100204{
205 int ret;
206 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100207 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100208 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100210
211 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 *md_alg = MBEDTLS_MD_SHA1;
213 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100214 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100215
216 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
218 return( MBEDTLS_ERR_X509_INVALID_ALG +
219 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100220
221 p = (unsigned char *) params->p;
222 end = p + params->len;
223
224 if( p == end )
225 return( 0 );
226
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100227 /*
228 * HashAlgorithm
229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
231 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100232 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100233 end2 = p + len;
234
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100235 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100237 return( ret );
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
240 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100241
242 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_X509_INVALID_ALG +
244 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100245 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
247 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100248
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100249 if( p == end )
250 return( 0 );
251
252 /*
253 * MaskGenAlgorithm
254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
256 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100257 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100258 end2 = p + len;
259
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100260 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100262 return( ret );
263
264 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
266 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
267 MBEDTLS_ERR_OID_NOT_FOUND );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100268
269 /* Parse HashAlgorithm */
270 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
271 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100272
273 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 return( MBEDTLS_ERR_X509_INVALID_ALG +
275 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100276 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
278 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100279
280 if( p == end )
281 return( 0 );
282
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100283 /*
284 * salt_len
285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
287 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100288 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100289 end2 = p + len;
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
292 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100293
294 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 return( MBEDTLS_ERR_X509_INVALID_ALG +
296 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100297 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
299 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100300
301 if( p == end )
302 return( 0 );
303
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100304 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200305 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
308 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100309 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200310 int trailer_field;
311
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100312 end2 = p + len;
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
315 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100316
317 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_X509_INVALID_ALG +
319 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200320
321 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
325 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100326
327 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 return( MBEDTLS_ERR_X509_INVALID_ALG +
329 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100330
331 return( 0 );
332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334
335/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336 * AttributeTypeAndValue ::= SEQUENCE {
337 * type AttributeType,
338 * value AttributeValue }
339 *
340 * AttributeType ::= OBJECT IDENTIFIER
341 *
342 * AttributeValue ::= ANY DEFINED BY AttributeType
343 */
344static int x509_get_attr_type_value( unsigned char **p,
345 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347{
348 int ret;
349 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_x509_buf *oid;
351 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
354 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
355 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356
357 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 return( MBEDTLS_ERR_X509_INVALID_NAME +
359 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361 oid = &cur->oid;
362 oid->tag = **p;
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
365 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366
367 oid->p = *p;
368 *p += oid->len;
369
370 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( MBEDTLS_ERR_X509_INVALID_NAME +
372 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
375 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
376 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
377 **p != MBEDTLS_ASN1_BIT_STRING )
378 return( MBEDTLS_ERR_X509_INVALID_NAME +
379 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380
381 val = &cur->val;
382 val->tag = *(*p)++;
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
385 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
387 val->p = *p;
388 *p += val->len;
389
390 cur->next = NULL;
391
392 return( 0 );
393}
394
395/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000396 * Name ::= CHOICE { -- only one possibility for now --
397 * rdnSequence RDNSequence }
398 *
399 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
400 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 * RelativeDistinguishedName ::=
402 * SET OF AttributeTypeAndValue
403 *
404 * AttributeTypeAndValue ::= SEQUENCE {
405 * type AttributeType,
406 * value AttributeValue }
407 *
408 * AttributeType ::= OBJECT IDENTIFIER
409 *
410 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200411 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000412 * The data structure is optimized for the common case where each RDN has only
413 * one element, which is represented as a list of AttributeTypeAndValue.
414 * For the general case we still use a flat list, but we mark elements of the
415 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
419 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420{
421 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200422 size_t set_len;
423 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100425 /* don't use recursion, we'd risk stack overflow if not optimized */
426 while( 1 )
427 {
428 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000429 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
432 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
433 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100435 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000437 while( 1 )
438 {
439 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
440 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000442 if( *p == end_set )
443 break;
444
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200445 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000446 cur->next_merged = 1;
447
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200448 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000449
450 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200451 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000452
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000453 cur = cur->next;
454 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100456 /*
457 * continue until end of SEQUENCE is reached
458 */
459 if( *p == end )
460 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200462 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100464 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200465 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100467 cur = cur->next;
468 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469}
470
Janos Follath87c98072017-02-03 12:36:59 +0000471static int x509_parse_int( unsigned char **p, size_t n, int *res )
472{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000473 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000474
475 for( ; n > 0; --n )
476 {
477 if( ( **p < '0') || ( **p > '9' ) )
478 return ( MBEDTLS_ERR_X509_INVALID_DATE );
479
Rich Evans7d5a55a2015-02-13 11:48:02 +0000480 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000481 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000482 }
Janos Follath87c98072017-02-03 12:36:59 +0000483
484 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000485}
486
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000487static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100488{
489 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000490 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100491
Hanno Becker61937d42017-04-26 15:01:23 +0100492 CHECK_RANGE( 0, 9999, t->year );
493 CHECK_RANGE( 0, 23, t->hour );
494 CHECK_RANGE( 0, 59, t->min );
495 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100496
Hanno Becker61937d42017-04-26 15:01:23 +0100497 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100498 {
499 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000500 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100501 break;
502 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000503 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100504 break;
505 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000506 if( ( !( t->year % 4 ) && t->year % 100 ) ||
507 !( t->year % 400 ) )
508 month_len = 29;
509 else
510 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100511 break;
512 default:
513 return( ret );
514 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000515 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100516
517 return( 0 );
518}
519
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520/*
Janos Follath87c98072017-02-03 12:36:59 +0000521 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
522 * field.
523 */
524static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100525 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000526{
527 int ret;
528
529 /*
530 * Minimum length is 10 or 12 depending on yearlen
531 */
532 if ( len < yearlen + 8 )
533 return ( MBEDTLS_ERR_X509_INVALID_DATE );
534 len -= yearlen + 8;
535
536 /*
537 * Parse year, month, day, hour, minute
538 */
Hanno Becker61937d42017-04-26 15:01:23 +0100539 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000540 if ( 2 == yearlen )
541 {
Hanno Becker61937d42017-04-26 15:01:23 +0100542 if ( tm->year < 50 )
543 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000544
Hanno Becker61937d42017-04-26 15:01:23 +0100545 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000546 }
547
Hanno Becker61937d42017-04-26 15:01:23 +0100548 CHECK( x509_parse_int( p, 2, &tm->mon ) );
549 CHECK( x509_parse_int( p, 2, &tm->day ) );
550 CHECK( x509_parse_int( p, 2, &tm->hour ) );
551 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000552
553 /*
554 * Parse seconds if present
555 */
556 if ( len >= 2 )
557 {
Hanno Becker61937d42017-04-26 15:01:23 +0100558 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000559 len -= 2;
560 }
561 else
562 return ( MBEDTLS_ERR_X509_INVALID_DATE );
563
564 /*
565 * Parse trailing 'Z' if present
566 */
567 if ( 1 == len && 'Z' == **p )
568 {
569 (*p)++;
570 len--;
571 }
572
573 /*
574 * We should have parsed all characters at this point
575 */
576 if ( 0 != len )
577 return ( MBEDTLS_ERR_X509_INVALID_DATE );
578
Hanno Becker61937d42017-04-26 15:01:23 +0100579 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000580
581 return ( 0 );
582}
583
584/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585 * Time ::= CHOICE {
586 * utcTime UTCTime,
587 * generalTime GeneralizedTime }
588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100590 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591{
592 int ret;
Janos Follath87c98072017-02-03 12:36:59 +0000593 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594 unsigned char tag;
595
596 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_X509_INVALID_DATE +
598 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599
600 tag = **p;
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000603 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000605 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_X509_INVALID_DATE +
608 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Janos Follath87c98072017-02-03 12:36:59 +0000609
610 (*p)++;
611 ret = mbedtls_asn1_get_len( p, end, &len );
612
613 if( ret != 0 )
614 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
615
Hanno Becker61937d42017-04-26 15:01:23 +0100616 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617}
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620{
621 int ret;
622 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100623 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624
625 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
627 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628
Andres AG4bdbe092016-09-19 16:58:45 +0100629 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
632 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633
Andres AG4bdbe092016-09-19 16:58:45 +0100634 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 sig->len = len;
636 sig->p = *p;
637
638 *p += len;
639
640 return( 0 );
641}
642
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100643/*
644 * Get signature algorithm from alg OID and optional parameters
645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
647 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200648 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100650 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200652 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
656 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
659 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100662
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200663 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200664 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200665 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200668 md_alg,
669 &pss_opts->mgf1_hash_id,
670 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100671 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100674 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200675 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100676
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200677 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100678 }
679 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100681 {
682 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100684 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100686 }
687
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688 return( 0 );
689}
690
691/*
692 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800693 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
696 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697{
698 int ret;
699 size_t len;
700
701 if( *p == end )
702 return( 0 );
703
704 ext->tag = **p;
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
707 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 return( ret );
709
710 ext->p = *p;
711 end = *p + ext->len;
712
713 /*
714 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
715 *
716 * Extension ::= SEQUENCE {
717 * extnID OBJECT IDENTIFIER,
718 * critical BOOLEAN DEFAULT FALSE,
719 * extnValue OCTET STRING }
720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
722 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
723 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724
725 if( end != *p + len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
727 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728
729 return( 0 );
730}
731
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732/*
733 * Store the name in printable form into buf; no more
734 * than size characters will be written
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737{
738 int ret;
739 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000740 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744
745 memset( s, 0, sizeof( s ) );
746
747 name = dn;
748 p = buf;
749 n = size;
750
751 while( name != NULL )
752 {
753 if( !name->oid.p )
754 {
755 name = name->next;
756 continue;
757 }
758
759 if( name != dn )
760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200762 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 }
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766
767 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200771 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772
773 for( i = 0; i < name->val.len; i++ )
774 {
775 if( i >= sizeof( s ) - 1 )
776 break;
777
778 c = name->val.p[i];
779 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
780 s[i] = '?';
781 else s[i] = c;
782 }
783 s[i] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200785 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000786
787 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788 name = name->next;
789 }
790
791 return( (int) ( size - n ) );
792}
793
794/*
795 * Store the serial in printable form into buf; no more
796 * than size characters will be written
797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799{
800 int ret;
801 size_t i, n, nr;
802 char *p;
803
804 p = buf;
805 n = size;
806
807 nr = ( serial->len <= 32 )
808 ? serial->len : 28;
809
810 for( i = 0; i < nr; i++ )
811 {
812 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
813 continue;
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200817 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 }
819
820 if( nr != serial->len )
821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200823 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824 }
825
826 return( (int) ( size - n ) );
827}
828
829/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200830 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
833 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200834 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100835{
836 int ret;
837 char *p = buf;
838 size_t n = size;
839 const char *desc = NULL;
840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100842 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100844 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200846 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
849 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 const mbedtls_pk_rsassa_pss_options *pss_opts;
852 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 md_info = mbedtls_md_info_from_type( md_alg );
857 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
860 md_info ? mbedtls_md_get_name( md_info ) : "???",
861 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200862 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200863 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100864 }
865#else
866 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200867 ((void) md_alg);
868 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100870
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200871 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100872}
873
874/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875 * Helper for writing "RSA key size", "EC key size", etc
876 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200877int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878{
879 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200880 size_t n = buf_size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 int ret;
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200884 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885
886 return( 0 );
887}
888
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200889#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200890/*
891 * Set the time structure to the current time.
892 * Return 0 on success, non-zero on failure.
893 */
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200894static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100895{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000896 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100897 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200898 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899
SimonBd5800b72016-04-26 07:43:27 +0100900 tt = mbedtls_time( NULL );
Hanno Becker6a739782018-09-05 15:06:19 +0100901 lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200902
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200903 if( lt == NULL )
904 ret = -1;
905 else
906 {
907 now->year = lt->tm_year + 1900;
908 now->mon = lt->tm_mon + 1;
909 now->day = lt->tm_mday;
910 now->hour = lt->tm_hour;
911 now->min = lt->tm_min;
912 now->sec = lt->tm_sec;
913 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200914
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200915 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100916}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100918/*
919 * Return 0 if before <= after, 1 otherwise
920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100922{
923 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924 return( 1 );
925
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100926 if( before->year == after->year &&
927 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 return( 1 );
929
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100930 if( before->year == after->year &&
931 before->mon == after->mon &&
932 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 return( 1 );
934
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100935 if( before->year == after->year &&
936 before->mon == after->mon &&
937 before->day == after->day &&
938 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939 return( 1 );
940
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100941 if( before->year == after->year &&
942 before->mon == after->mon &&
943 before->day == after->day &&
944 before->hour == after->hour &&
945 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946 return( 1 );
947
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100948 if( before->year == after->year &&
949 before->mon == after->mon &&
950 before->day == after->day &&
951 before->hour == after->hour &&
952 before->min == after->min &&
953 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954 return( 1 );
955
956 return( 0 );
957}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100958
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100959int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100962
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200963 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +0200964 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100965
966 return( x509_check_time( &now, to ) );
967}
968
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100969int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100972
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200973 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +0200974 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100975
976 return( x509_check_time( from, &now ) );
977}
978
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200979#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100980
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100981int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982{
983 ((void) to);
984 return( 0 );
985}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100986
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100987int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100988{
989 ((void) from);
990 return( 0 );
991}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200992#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200995
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000996#include "mbedtls/x509_crt.h"
997#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200998
999/*
1000 * Checkup routine
1001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002int mbedtls_x509_self_test( int verbose )
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001003{
Junhwan Park39bdab72018-10-17 21:01:08 +09001004 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001005#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001006 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_x509_crt cacert;
1008 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001009
1010 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001012
Junhwan Park39bdab72018-10-17 21:01:08 +09001013 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001017 mbedtls_test_cli_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001018 if( ret != 0 )
1019 {
1020 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001022
Junhwan Park39bdab72018-10-17 21:01:08 +09001023 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001024 }
1025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001027 mbedtls_test_ca_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001028 if( ret != 0 )
1029 {
1030 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001032
Junhwan Park39bdab72018-10-17 21:01:08 +09001033 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001034 }
1035
1036 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 mbedtls_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001040 if( ret != 0 )
1041 {
1042 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001044
Junhwan Park39bdab72018-10-17 21:01:08 +09001045 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001046 }
1047
1048 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001050
Junhwan Park39bdab72018-10-17 21:01:08 +09001051cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_x509_crt_free( &cacert );
1053 mbedtls_x509_crt_free( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001054#else
1055 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA1_C */
Junhwan Park39bdab72018-10-17 21:01:08 +09001057 return( ret );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001058}
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#endif /* MBEDTLS_X509_USE_C */