blob: bc3bfe018f51bb542009e3ca52ffd9cac14cc2ae [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
63#if defined(MBEDTLS_HAVE_TIME)
64#include "mbedtls/platform_time.h"
65#endif
66
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <windows.h>
69#else
70#include <time.h>
71#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_FS_IO)
Rich Evans36796df2015-02-12 18:27:14 +000074#include <stdio.h>
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#if !defined(_WIN32)
76#include <sys/types.h>
77#include <sys/stat.h>
78#include <dirent.h>
79#endif
80#endif
81
Rich Evans7d5a55a2015-02-13 11:48:02 +000082#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
83
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084/*
85 * CertificateSerialNumber ::= INTEGER
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
88 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089{
90 int ret;
91
92 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
94 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
97 **p != MBEDTLS_ASN1_INTEGER )
98 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
99 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100
101 serial->tag = *(*p)++;
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
104 return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105
106 serial->p = *p;
107 *p += serial->len;
108
109 return( 0 );
110}
111
112/* Get an algorithm identifier without parameters (eg for signatures)
113 *
114 * AlgorithmIdentifier ::= SEQUENCE {
115 * algorithm OBJECT IDENTIFIER,
116 * parameters ANY DEFINED BY algorithm OPTIONAL }
117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
119 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120{
121 int ret;
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
124 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125
126 return( 0 );
127}
128
129/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100130 * Parse an algorithm identifier with (optional) paramaters
131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
133 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100134{
135 int ret;
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
138 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100139
140 return( 0 );
141}
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100144/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100145 * HashAlgorithm ::= AlgorithmIdentifier
146 *
147 * AlgorithmIdentifier ::= SEQUENCE {
148 * algorithm OBJECT IDENTIFIER,
149 * parameters ANY DEFINED BY algorithm OPTIONAL }
150 *
151 * For HashAlgorithm, parameters MUST be NULL or absent.
152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153static 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 +0100154{
155 int ret;
156 unsigned char *p;
157 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100159 size_t len;
160
161 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
163 return( MBEDTLS_ERR_X509_INVALID_ALG +
164 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100165
166 p = (unsigned char *) alg->p;
167 end = p + alg->len;
168
169 if( p >= end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 return( MBEDTLS_ERR_X509_INVALID_ALG +
171 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100172
173 /* Parse md_oid */
174 md_oid.tag = *p;
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
177 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100178
179 md_oid.p = p;
180 p += md_oid.len;
181
182 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
184 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100185
186 /* Make sure params is absent of NULL */
187 if( p == end )
188 return( 0 );
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
191 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100192
193 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 return( MBEDTLS_ERR_X509_INVALID_ALG +
195 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100196
197 return( 0 );
198}
199
200/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100201 * RSASSA-PSS-params ::= SEQUENCE {
202 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
203 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
204 * saltLength [2] INTEGER DEFAULT 20,
205 * trailerField [3] INTEGER DEFAULT 1 }
206 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200207 *
208 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
209 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
210 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
213 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200214 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100215{
216 int ret;
217 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100218 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100219 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100221
222 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 *md_alg = MBEDTLS_MD_SHA1;
224 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100225 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100226
227 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
229 return( MBEDTLS_ERR_X509_INVALID_ALG +
230 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100231
232 p = (unsigned char *) params->p;
233 end = p + params->len;
234
235 if( p == end )
236 return( 0 );
237
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100238 /*
239 * HashAlgorithm
240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
242 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100243 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100244 end2 = p + len;
245
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100246 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100248 return( ret );
249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
251 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100252
253 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return( MBEDTLS_ERR_X509_INVALID_ALG +
255 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
258 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100259
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100260 if( p == end )
261 return( 0 );
262
263 /*
264 * MaskGenAlgorithm
265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
267 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100268 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100269 end2 = p + len;
270
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100271 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100273 return( ret );
274
275 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
277 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
278 MBEDTLS_ERR_OID_NOT_FOUND );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100279
280 /* Parse HashAlgorithm */
281 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
282 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100283
284 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return( MBEDTLS_ERR_X509_INVALID_ALG +
286 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100287 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
289 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100290
291 if( p == end )
292 return( 0 );
293
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100294 /*
295 * salt_len
296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
298 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100299 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100300 end2 = p + len;
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
303 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100304
305 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 return( MBEDTLS_ERR_X509_INVALID_ALG +
307 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100308 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
310 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100311
312 if( p == end )
313 return( 0 );
314
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100315 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200316 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
319 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100320 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200321 int trailer_field;
322
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100323 end2 = p + len;
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
326 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100327
328 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 return( MBEDTLS_ERR_X509_INVALID_ALG +
330 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200331
332 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
336 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100337
338 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 return( MBEDTLS_ERR_X509_INVALID_ALG +
340 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100341
342 return( 0 );
343}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100345
346/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 * AttributeTypeAndValue ::= SEQUENCE {
348 * type AttributeType,
349 * value AttributeValue }
350 *
351 * AttributeType ::= OBJECT IDENTIFIER
352 *
353 * AttributeValue ::= ANY DEFINED BY AttributeType
354 */
355static int x509_get_attr_type_value( unsigned char **p,
356 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358{
359 int ret;
360 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_x509_buf *oid;
362 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
365 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
366 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
368 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_X509_INVALID_NAME +
370 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371
372 oid = &cur->oid;
373 oid->tag = **p;
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
376 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
378 oid->p = *p;
379 *p += oid->len;
380
381 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 return( MBEDTLS_ERR_X509_INVALID_NAME +
383 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
386 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
387 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
388 **p != MBEDTLS_ASN1_BIT_STRING )
389 return( MBEDTLS_ERR_X509_INVALID_NAME +
390 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391
392 val = &cur->val;
393 val->tag = *(*p)++;
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
396 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397
398 val->p = *p;
399 *p += val->len;
400
401 cur->next = NULL;
402
403 return( 0 );
404}
405
406/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000407 * Name ::= CHOICE { -- only one possibility for now --
408 * rdnSequence RDNSequence }
409 *
410 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
411 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 * RelativeDistinguishedName ::=
413 * SET OF AttributeTypeAndValue
414 *
415 * AttributeTypeAndValue ::= SEQUENCE {
416 * type AttributeType,
417 * value AttributeValue }
418 *
419 * AttributeType ::= OBJECT IDENTIFIER
420 *
421 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200422 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000423 * The data structure is optimized for the common case where each RDN has only
424 * one element, which is represented as a list of AttributeTypeAndValue.
425 * For the general case we still use a flat list, but we mark elements of the
426 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
430 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431{
432 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200433 size_t set_len;
434 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100436 /* don't use recursion, we'd risk stack overflow if not optimized */
437 while( 1 )
438 {
439 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000440 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
443 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
444 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100446 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000448 while( 1 )
449 {
450 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
451 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000453 if( *p == end_set )
454 break;
455
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200456 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000457 cur->next_merged = 1;
458
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200459 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000460
461 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200462 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000463
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000464 cur = cur->next;
465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100467 /*
468 * continue until end of SEQUENCE is reached
469 */
470 if( *p == end )
471 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200473 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100475 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200476 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100478 cur = cur->next;
479 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480}
481
Rich Evans7d5a55a2015-02-13 11:48:02 +0000482static int x509_parse_int(unsigned char **p, unsigned n, int *res){
483 *res = 0;
484 for( ; n > 0; --n ){
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( **p < '0') || ( **p > '9' ) ) return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000486 *res *= 10;
487 *res += (*(*p)++ - '0');
488 }
489 return 0;
490}
491
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492/*
493 * Time ::= CHOICE {
494 * utcTime UTCTime,
495 * generalTime GeneralizedTime }
496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
498 mbedtls_x509_time *time )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499{
500 int ret;
501 size_t len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 unsigned char tag;
503
504 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 return( MBEDTLS_ERR_X509_INVALID_DATE +
506 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507
508 tag = **p;
509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if( tag == MBEDTLS_ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 {
512 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 ret = mbedtls_asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200514
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Rich Evans7d5a55a2015-02-13 11:48:02 +0000518 CHECK( x509_parse_int( p, 2, &time->year ) );
519 CHECK( x509_parse_int( p, 2, &time->mon ) );
520 CHECK( x509_parse_int( p, 2, &time->day ) );
521 CHECK( x509_parse_int( p, 2, &time->hour ) );
522 CHECK( x509_parse_int( p, 2, &time->min ) );
523 if( len > 10 )
524 CHECK( x509_parse_int( p, 2, &time->sec ) );
525 if( len > 12 && *(*p)++ != 'Z' )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
528 time->year += 100 * ( time->year < 50 );
529 time->year += 1900;
530
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531 return( 0 );
532 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 {
535 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 ret = mbedtls_asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200537
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
Rich Evans7d5a55a2015-02-13 11:48:02 +0000541 CHECK( x509_parse_int( p, 4, &time->year ) );
542 CHECK( x509_parse_int( p, 2, &time->mon ) );
543 CHECK( x509_parse_int( p, 2, &time->day ) );
544 CHECK( x509_parse_int( p, 2, &time->hour ) );
545 CHECK( x509_parse_int( p, 2, &time->min ) );
546 if( len > 12 )
547 CHECK( x509_parse_int( p, 2, &time->sec ) );
548 if( len > 14 && *(*p)++ != 'Z' )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 return( MBEDTLS_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551 return( 0 );
552 }
553 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 return( MBEDTLS_ERR_X509_INVALID_DATE +
555 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556}
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559{
560 int ret;
561 size_t len;
562
563 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
565 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
567 sig->tag = **p;
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
570 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571
572 sig->len = len;
573 sig->p = *p;
574
575 *p += len;
576
577 return( 0 );
578}
579
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100580/*
581 * Get signature algorithm from alg OID and optional parameters
582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
584 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200585 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100587 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200589 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
593 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
596 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100599
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200600 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200601 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200602 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200605 md_alg,
606 &pss_opts->mgf1_hash_id,
607 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100608 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100611 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200612 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100613
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200614 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100615 }
616 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100618 {
619 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100621 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100623 }
624
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 return( 0 );
626}
627
628/*
629 * X.509 Extensions (No parsing of extensions, pointer should
630 * be either manually updated or extensions should be parsed!
631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
633 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634{
635 int ret;
636 size_t len;
637
638 if( *p == end )
639 return( 0 );
640
641 ext->tag = **p;
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
644 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 return( ret );
646
647 ext->p = *p;
648 end = *p + ext->len;
649
650 /*
651 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
652 *
653 * Extension ::= SEQUENCE {
654 * extnID OBJECT IDENTIFIER,
655 * critical BOOLEAN DEFAULT FALSE,
656 * extnValue OCTET STRING }
657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
659 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
660 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661
662 if( end != *p + len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
664 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665
666 return( 0 );
667}
668
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200669/*
670 * Store the name in printable form into buf; no more
671 * than size characters will be written
672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674{
675 int ret;
676 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000677 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682 memset( s, 0, sizeof( s ) );
683
684 name = dn;
685 p = buf;
686 n = size;
687
688 while( name != NULL )
689 {
690 if( !name->oid.p )
691 {
692 name = name->next;
693 continue;
694 }
695
696 if( name != dn )
697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200699 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 }
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703
704 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200708 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
710 for( i = 0; i < name->val.len; i++ )
711 {
712 if( i >= sizeof( s ) - 1 )
713 break;
714
715 c = name->val.p[i];
716 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
717 s[i] = '?';
718 else s[i] = c;
719 }
720 s[i] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200722 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000723
724 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 name = name->next;
726 }
727
728 return( (int) ( size - n ) );
729}
730
731/*
732 * Store the serial in printable form into buf; no more
733 * than size characters will be written
734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736{
737 int ret;
738 size_t i, n, nr;
739 char *p;
740
741 p = buf;
742 n = size;
743
744 nr = ( serial->len <= 32 )
745 ? serial->len : 28;
746
747 for( i = 0; i < nr; i++ )
748 {
749 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
750 continue;
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200754 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 }
756
757 if( nr != serial->len )
758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200760 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761 }
762
763 return( (int) ( size - n ) );
764}
765
766/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200767 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
770 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200771 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100772{
773 int ret;
774 char *p = buf;
775 size_t n = size;
776 const char *desc = NULL;
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100779 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100781 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200783 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
786 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 const mbedtls_pk_rsassa_pss_options *pss_opts;
789 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 md_info = mbedtls_md_info_from_type( md_alg );
794 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
797 md_info ? mbedtls_md_get_name( md_info ) : "???",
798 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200799 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200800 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100801 }
802#else
803 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200804 ((void) md_alg);
805 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100807
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200808 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100809}
810
811/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812 * Helper for writing "RSA key size", "EC key size", etc
813 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200814int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815{
816 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200817 size_t n = buf_size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 int ret;
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200821 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822
823 return( 0 );
824}
825
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200826#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200827/*
828 * Set the time structure to the current time.
829 * Return 0 on success, non-zero on failure.
830 */
831#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200832static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100833{
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834 SYSTEMTIME st;
835
Paul Bakker66d5d072014-06-17 16:39:18 +0200836 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100838 now->year = st.wYear;
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200839 now->mon = st.wMonth;
840 now->day = st.wDay;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100841 now->hour = st.wHour;
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200842 now->min = st.wMinute;
843 now->sec = st.wSecond;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200844
845 return( 0 );
846}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847#else
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200848static int x509_get_current_time( mbedtls_x509_time *now )
849{
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200850 struct tm *lt;
SimonBd5800b72016-04-26 07:43:27 +0100851 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200852 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200854#if defined(MBEDTLS_THREADING_C)
855 if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
856 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
857#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200858
SimonBd5800b72016-04-26 07:43:27 +0100859 tt = mbedtls_time( NULL );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200860 lt = gmtime( &tt );
861
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200862 if( lt == NULL )
863 ret = -1;
864 else
865 {
866 now->year = lt->tm_year + 1900;
867 now->mon = lt->tm_mon + 1;
868 now->day = lt->tm_mday;
869 now->hour = lt->tm_hour;
870 now->min = lt->tm_min;
871 now->sec = lt->tm_sec;
872 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200873
874#if defined(MBEDTLS_THREADING_C)
875 if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
876 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
877#endif
878
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200879 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100880}
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200881#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100883/*
884 * Return 0 if before <= after, 1 otherwise
885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100887{
888 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 return( 1 );
890
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100891 if( before->year == after->year &&
892 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893 return( 1 );
894
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100895 if( before->year == after->year &&
896 before->mon == after->mon &&
897 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 return( 1 );
899
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100900 if( before->year == after->year &&
901 before->mon == after->mon &&
902 before->day == after->day &&
903 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 return( 1 );
905
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100906 if( before->year == after->year &&
907 before->mon == after->mon &&
908 before->day == after->day &&
909 before->hour == after->hour &&
910 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 return( 1 );
912
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100913 if( before->year == after->year &&
914 before->mon == after->mon &&
915 before->day == after->day &&
916 before->hour == after->hour &&
917 before->min == after->min &&
918 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919 return( 1 );
920
921 return( 0 );
922}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100923
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100924int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100925{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100927
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200928 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +0200929 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100930
931 return( x509_check_time( &now, to ) );
932}
933
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100934int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100935{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100937
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200938 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +0200939 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100940
941 return( x509_check_time( from, &now ) );
942}
943
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200944#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100945
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100946int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947{
948 ((void) to);
949 return( 0 );
950}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100951
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100952int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100953{
954 ((void) from);
955 return( 0 );
956}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200957#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200960
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000961#include "mbedtls/x509_crt.h"
962#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200963
964/*
965 * Checkup routine
966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967int mbedtls_x509_self_test( int verbose )
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200970 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200971 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_x509_crt cacert;
973 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200974
975 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200981 mbedtls_test_cli_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200982 if( ret != 0 )
983 {
984 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200986
987 return( ret );
988 }
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200993 mbedtls_test_ca_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200994 if( ret != 0 )
995 {
996 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200998
999 return( ret );
1000 }
1001
1002 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001006 if( ret != 0 )
1007 {
1008 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001010
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001011 return( ret );
1012 }
1013
1014 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_x509_crt_free( &cacert );
1018 mbedtls_x509_crt_free( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001019
1020 return( 0 );
1021#else
1022 ((void) verbose);
Manuel Pégourié-Gonnard620ee192015-08-07 10:56:09 +02001023 return( 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001025}
1026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#endif /* MBEDTLS_X509_USE_C */