blob: 8696a7e8ac7113d3b4d2d34bee749942215ce9f7 [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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020057#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_printf printf
59#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Paul Bakkerfa6a6202013-10-28 18:48:30 +010062#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063#include <windows.h>
64#else
65#include <time.h>
66#endif
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_FS_IO)
Rich Evans36796df2015-02-12 18:27:14 +000069#include <stdio.h>
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#if !defined(_WIN32)
71#include <sys/types.h>
72#include <sys/stat.h>
73#include <dirent.h>
74#endif
75#endif
76
Rich Evans7d5a55a2015-02-13 11:48:02 +000077#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
78
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079/*
80 * CertificateSerialNumber ::= INTEGER
81 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
83 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084{
85 int ret;
86
87 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
89 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
92 **p != MBEDTLS_ASN1_INTEGER )
93 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
94 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095
96 serial->tag = *(*p)++;
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
99 return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100
101 serial->p = *p;
102 *p += serial->len;
103
104 return( 0 );
105}
106
107/* Get an algorithm identifier without parameters (eg for signatures)
108 *
109 * AlgorithmIdentifier ::= SEQUENCE {
110 * algorithm OBJECT IDENTIFIER,
111 * parameters ANY DEFINED BY algorithm OPTIONAL }
112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
114 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115{
116 int ret;
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
119 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120
121 return( 0 );
122}
123
124/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100125 * Parse an algorithm identifier with (optional) paramaters
126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
128 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100129{
130 int ret;
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
133 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100134
135 return( 0 );
136}
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100139/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100140 * HashAlgorithm ::= AlgorithmIdentifier
141 *
142 * AlgorithmIdentifier ::= SEQUENCE {
143 * algorithm OBJECT IDENTIFIER,
144 * parameters ANY DEFINED BY algorithm OPTIONAL }
145 *
146 * For HashAlgorithm, parameters MUST be NULL or absent.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static 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 +0100149{
150 int ret;
151 unsigned char *p;
152 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100154 size_t len;
155
156 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
158 return( MBEDTLS_ERR_X509_INVALID_ALG +
159 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100160
161 p = (unsigned char *) alg->p;
162 end = p + alg->len;
163
164 if( p >= end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 return( MBEDTLS_ERR_X509_INVALID_ALG +
166 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100167
168 /* Parse md_oid */
169 md_oid.tag = *p;
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
172 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100173
174 md_oid.p = p;
175 p += md_oid.len;
176
177 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
179 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100180
181 /* Make sure params is absent of NULL */
182 if( p == end )
183 return( 0 );
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
186 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100187
188 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return( MBEDTLS_ERR_X509_INVALID_ALG +
190 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100191
192 return( 0 );
193}
194
195/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100196 * RSASSA-PSS-params ::= SEQUENCE {
197 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
198 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
199 * saltLength [2] INTEGER DEFAULT 20,
200 * trailerField [3] INTEGER DEFAULT 1 }
201 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200202 *
203 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
204 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
205 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
208 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200209 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100210{
211 int ret;
212 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100213 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100214 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100216
217 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 *md_alg = MBEDTLS_MD_SHA1;
219 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100220 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100221
222 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
224 return( MBEDTLS_ERR_X509_INVALID_ALG +
225 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100226
227 p = (unsigned char *) params->p;
228 end = p + params->len;
229
230 if( p == end )
231 return( 0 );
232
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100233 /*
234 * HashAlgorithm
235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
237 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100238 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100239 end2 = p + len;
240
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100241 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100243 return( ret );
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
246 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100247
248 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return( MBEDTLS_ERR_X509_INVALID_ALG +
250 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100251 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
253 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100254
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100255 if( p == end )
256 return( 0 );
257
258 /*
259 * MaskGenAlgorithm
260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
262 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100263 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100264 end2 = p + len;
265
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100266 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100268 return( ret );
269
270 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
272 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
273 MBEDTLS_ERR_OID_NOT_FOUND );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100274
275 /* Parse HashAlgorithm */
276 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
277 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100278
279 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 return( MBEDTLS_ERR_X509_INVALID_ALG +
281 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100282 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
284 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100285
286 if( p == end )
287 return( 0 );
288
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100289 /*
290 * salt_len
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
293 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100294 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100295 end2 = p + len;
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
298 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100299
300 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 return( MBEDTLS_ERR_X509_INVALID_ALG +
302 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
305 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100306
307 if( p == end )
308 return( 0 );
309
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100310 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200311 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
314 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100315 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200316 int trailer_field;
317
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100318 end2 = p + len;
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
321 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100322
323 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_X509_INVALID_ALG +
325 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200326
327 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
331 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100332
333 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 return( MBEDTLS_ERR_X509_INVALID_ALG +
335 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100336
337 return( 0 );
338}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100340
341/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200342 * AttributeTypeAndValue ::= SEQUENCE {
343 * type AttributeType,
344 * value AttributeValue }
345 *
346 * AttributeType ::= OBJECT IDENTIFIER
347 *
348 * AttributeValue ::= ANY DEFINED BY AttributeType
349 */
350static int x509_get_attr_type_value( unsigned char **p,
351 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353{
354 int ret;
355 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_x509_buf *oid;
357 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
360 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
361 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
363 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 return( MBEDTLS_ERR_X509_INVALID_NAME +
365 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366
367 oid = &cur->oid;
368 oid->tag = **p;
369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
371 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
373 oid->p = *p;
374 *p += oid->len;
375
376 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 return( MBEDTLS_ERR_X509_INVALID_NAME +
378 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
381 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
382 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
383 **p != MBEDTLS_ASN1_BIT_STRING )
384 return( MBEDTLS_ERR_X509_INVALID_NAME +
385 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
387 val = &cur->val;
388 val->tag = *(*p)++;
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
391 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392
393 val->p = *p;
394 *p += val->len;
395
396 cur->next = NULL;
397
398 return( 0 );
399}
400
401/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000402 * Name ::= CHOICE { -- only one possibility for now --
403 * rdnSequence RDNSequence }
404 *
405 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
406 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 * RelativeDistinguishedName ::=
408 * SET OF AttributeTypeAndValue
409 *
410 * AttributeTypeAndValue ::= SEQUENCE {
411 * type AttributeType,
412 * value AttributeValue }
413 *
414 * AttributeType ::= OBJECT IDENTIFIER
415 *
416 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200417 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000418 * The data structure is optimized for the common case where each RDN has only
419 * one element, which is represented as a list of AttributeTypeAndValue.
420 * For the general case we still use a flat list, but we mark elements of the
421 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
425 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426{
427 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200428 size_t set_len;
429 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100431 /* don't use recursion, we'd risk stack overflow if not optimized */
432 while( 1 )
433 {
434 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000435 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
438 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
439 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100441 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000443 while( 1 )
444 {
445 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
446 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000448 if( *p == end_set )
449 break;
450
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200451 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000452 cur->next_merged = 1;
453
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200454 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000455
456 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200457 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000458
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000459 cur = cur->next;
460 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100462 /*
463 * continue until end of SEQUENCE is reached
464 */
465 if( *p == end )
466 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200468 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100470 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200471 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100473 cur = cur->next;
474 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475}
476
Rich Evans7d5a55a2015-02-13 11:48:02 +0000477static int x509_parse_int(unsigned char **p, unsigned n, int *res){
478 *res = 0;
479 for( ; n > 0; --n ){
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( ( **p < '0') || ( **p > '9' ) ) return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000481 *res *= 10;
482 *res += (*(*p)++ - '0');
483 }
484 return 0;
485}
486
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487/*
488 * Time ::= CHOICE {
489 * utcTime UTCTime,
490 * generalTime GeneralizedTime }
491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
493 mbedtls_x509_time *time )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494{
495 int ret;
496 size_t len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 unsigned char tag;
498
499 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 return( MBEDTLS_ERR_X509_INVALID_DATE +
501 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
503 tag = **p;
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 if( tag == MBEDTLS_ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506 {
507 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 ret = mbedtls_asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200509
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
Rich Evans7d5a55a2015-02-13 11:48:02 +0000513 CHECK( x509_parse_int( p, 2, &time->year ) );
514 CHECK( x509_parse_int( p, 2, &time->mon ) );
515 CHECK( x509_parse_int( p, 2, &time->day ) );
516 CHECK( x509_parse_int( p, 2, &time->hour ) );
517 CHECK( x509_parse_int( p, 2, &time->min ) );
518 if( len > 10 )
519 CHECK( x509_parse_int( p, 2, &time->sec ) );
520 if( len > 12 && *(*p)++ != 'Z' )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 return( MBEDTLS_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
523 time->year += 100 * ( time->year < 50 );
524 time->year += 1900;
525
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526 return( 0 );
527 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529 {
530 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 ret = mbedtls_asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200532
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
Rich Evans7d5a55a2015-02-13 11:48:02 +0000536 CHECK( x509_parse_int( p, 4, &time->year ) );
537 CHECK( x509_parse_int( p, 2, &time->mon ) );
538 CHECK( x509_parse_int( p, 2, &time->day ) );
539 CHECK( x509_parse_int( p, 2, &time->hour ) );
540 CHECK( x509_parse_int( p, 2, &time->min ) );
541 if( len > 12 )
542 CHECK( x509_parse_int( p, 2, &time->sec ) );
543 if( len > 14 && *(*p)++ != 'Z' )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 return( MBEDTLS_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546 return( 0 );
547 }
548 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 return( MBEDTLS_ERR_X509_INVALID_DATE +
550 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551}
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554{
555 int ret;
556 size_t len;
Andres AG6a3fa212016-09-19 16:58:45 +0100557 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
559 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
561 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
Andres AG6a3fa212016-09-19 16:58:45 +0100563 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
566 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
Andres AG6a3fa212016-09-19 16:58:45 +0100568 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569 sig->len = len;
570 sig->p = *p;
571
572 *p += len;
573
574 return( 0 );
575}
576
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100577/*
578 * Get signature algorithm from alg OID and optional parameters
579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
581 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200582 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100584 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200586 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
590 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
593 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100596
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200597 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200598 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200599 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200602 md_alg,
603 &pss_opts->mgf1_hash_id,
604 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100605 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100608 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200609 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100610
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200611 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100612 }
613 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100615 {
616 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100618 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100620 }
621
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 return( 0 );
623}
624
625/*
626 * X.509 Extensions (No parsing of extensions, pointer should
627 * be either manually updated or extensions should be parsed!
628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
630 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631{
632 int ret;
633 size_t len;
634
635 if( *p == end )
636 return( 0 );
637
638 ext->tag = **p;
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
641 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 return( ret );
643
644 ext->p = *p;
645 end = *p + ext->len;
646
647 /*
648 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
649 *
650 * Extension ::= SEQUENCE {
651 * extnID OBJECT IDENTIFIER,
652 * critical BOOLEAN DEFAULT FALSE,
653 * extnValue OCTET STRING }
654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
656 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
657 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
659 if( end != *p + len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
661 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662
663 return( 0 );
664}
665
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666/*
667 * Store the name in printable form into buf; no more
668 * than size characters will be written
669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671{
672 int ret;
673 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000674 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678
679 memset( s, 0, sizeof( s ) );
680
681 name = dn;
682 p = buf;
683 n = size;
684
685 while( name != NULL )
686 {
687 if( !name->oid.p )
688 {
689 name = name->next;
690 continue;
691 }
692
693 if( name != dn )
694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200696 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697 }
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
701 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200705 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
707 for( i = 0; i < name->val.len; i++ )
708 {
709 if( i >= sizeof( s ) - 1 )
710 break;
711
712 c = name->val.p[i];
713 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
714 s[i] = '?';
715 else s[i] = c;
716 }
717 s[i] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200719 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000720
721 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 name = name->next;
723 }
724
725 return( (int) ( size - n ) );
726}
727
728/*
729 * Store the serial in printable form into buf; no more
730 * than size characters will be written
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733{
734 int ret;
735 size_t i, n, nr;
736 char *p;
737
738 p = buf;
739 n = size;
740
741 nr = ( serial->len <= 32 )
742 ? serial->len : 28;
743
744 for( i = 0; i < nr; i++ )
745 {
746 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
747 continue;
748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200751 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 }
753
754 if( nr != serial->len )
755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200757 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758 }
759
760 return( (int) ( size - n ) );
761}
762
763/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200764 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
767 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200768 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100769{
770 int ret;
771 char *p = buf;
772 size_t n = size;
773 const char *desc = NULL;
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100776 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100778 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200780 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
783 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 const mbedtls_pk_rsassa_pss_options *pss_opts;
786 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 md_info = mbedtls_md_info_from_type( md_alg );
791 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
794 md_info ? mbedtls_md_get_name( md_info ) : "???",
795 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200796 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200797 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100798 }
799#else
800 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200801 ((void) md_alg);
802 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100804
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200805 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100806}
807
808/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809 * Helper for writing "RSA key size", "EC key size", etc
810 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200811int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812{
813 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200814 size_t n = buf_size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 int ret;
816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200818 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819
820 return( 0 );
821}
822
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200823#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200824/*
825 * Set the time structure to the current time.
826 * Return 0 on success, non-zero on failure.
827 */
828#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200829static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100830{
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831 SYSTEMTIME st;
832
Paul Bakker66d5d072014-06-17 16:39:18 +0200833 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100835 now->year = st.wYear;
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200836 now->mon = st.wMonth;
837 now->day = st.wDay;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100838 now->hour = st.wHour;
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200839 now->min = st.wMinute;
840 now->sec = st.wSecond;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200841
842 return( 0 );
843}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844#else
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200845static int x509_get_current_time( mbedtls_x509_time *now )
846{
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200847 struct tm *lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848 time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200849 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200850
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200851#if defined(MBEDTLS_THREADING_C)
852 if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
853 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
854#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200856 tt = time( NULL );
857 lt = gmtime( &tt );
858
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200859 if( lt == NULL )
860 ret = -1;
861 else
862 {
863 now->year = lt->tm_year + 1900;
864 now->mon = lt->tm_mon + 1;
865 now->day = lt->tm_mday;
866 now->hour = lt->tm_hour;
867 now->min = lt->tm_min;
868 now->sec = lt->tm_sec;
869 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200870
871#if defined(MBEDTLS_THREADING_C)
872 if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
873 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
874#endif
875
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200876 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100877}
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200878#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100880/*
881 * Return 0 if before <= after, 1 otherwise
882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100884{
885 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 return( 1 );
887
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100888 if( before->year == after->year &&
889 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 return( 1 );
891
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100892 if( before->year == after->year &&
893 before->mon == after->mon &&
894 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 return( 1 );
896
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100897 if( before->year == after->year &&
898 before->mon == after->mon &&
899 before->day == after->day &&
900 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901 return( 1 );
902
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100903 if( before->year == after->year &&
904 before->mon == after->mon &&
905 before->day == after->day &&
906 before->hour == after->hour &&
907 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908 return( 1 );
909
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100910 if( before->year == after->year &&
911 before->mon == after->mon &&
912 before->day == after->day &&
913 before->hour == after->hour &&
914 before->min == after->min &&
915 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 return( 1 );
917
918 return( 0 );
919}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100920
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100921int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100922{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100924
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200925 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +0200926 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100927
928 return( x509_check_time( &now, to ) );
929}
930
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100931int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100934
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200935 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +0200936 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100937
938 return( x509_check_time( from, &now ) );
939}
940
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200941#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100942
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100943int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944{
945 ((void) to);
946 return( 0 );
947}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100948
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100949int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100950{
951 ((void) from);
952 return( 0 );
953}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200954#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200957
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000958#include "mbedtls/x509_crt.h"
959#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200960
961/*
962 * Checkup routine
963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964int mbedtls_x509_self_test( int verbose )
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200967 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200968 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_x509_crt cacert;
970 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200971
972 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200978 mbedtls_test_cli_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200979 if( ret != 0 )
980 {
981 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200983
984 return( ret );
985 }
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 mbedtls_x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200990 mbedtls_test_ca_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200991 if( ret != 0 )
992 {
993 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200995
996 return( ret );
997 }
998
999 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001003 if( ret != 0 )
1004 {
1005 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001007
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001008 return( ret );
1009 }
1010
1011 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_x509_crt_free( &cacert );
1015 mbedtls_x509_crt_free( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001016
1017 return( 0 );
1018#else
1019 ((void) verbose);
Manuel Pégourié-Gonnard620ee192015-08-07 10:56:09 +02001020 return( 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001022}
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#endif /* MBEDTLS_X509_USE_C */