blob: a0df817081ef62494320714152c447b300804624 [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_time time
59#define mbedtls_time_t time_t
60#define mbedtls_printf printf
61#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#endif
63
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#include <windows.h>
66#else
67#include <time.h>
68#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_FS_IO)
Rich Evans36796df2015-02-12 18:27:14 +000071#include <stdio.h>
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#if !defined(_WIN32)
73#include <sys/types.h>
74#include <sys/stat.h>
75#include <dirent.h>
76#endif
77#endif
78
Rich Evans7d5a55a2015-02-13 11:48:02 +000079#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
80
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081/*
82 * CertificateSerialNumber ::= INTEGER
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
85 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086{
87 int ret;
88
89 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
91 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
94 **p != MBEDTLS_ASN1_INTEGER )
95 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
96 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097
98 serial->tag = *(*p)++;
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
101 return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102
103 serial->p = *p;
104 *p += serial->len;
105
106 return( 0 );
107}
108
109/* Get an algorithm identifier without parameters (eg for signatures)
110 *
111 * AlgorithmIdentifier ::= SEQUENCE {
112 * algorithm OBJECT IDENTIFIER,
113 * parameters ANY DEFINED BY algorithm OPTIONAL }
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
116 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117{
118 int ret;
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
121 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
123 return( 0 );
124}
125
126/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100127 * Parse an algorithm identifier with (optional) paramaters
128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
130 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100131{
132 int ret;
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
135 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100136
137 return( 0 );
138}
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100141/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100142 * HashAlgorithm ::= AlgorithmIdentifier
143 *
144 * AlgorithmIdentifier ::= SEQUENCE {
145 * algorithm OBJECT IDENTIFIER,
146 * parameters ANY DEFINED BY algorithm OPTIONAL }
147 *
148 * For HashAlgorithm, parameters MUST be NULL or absent.
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150static 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 +0100151{
152 int ret;
153 unsigned char *p;
154 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100156 size_t len;
157
158 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
160 return( MBEDTLS_ERR_X509_INVALID_ALG +
161 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100162
163 p = (unsigned char *) alg->p;
164 end = p + alg->len;
165
166 if( p >= end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 return( MBEDTLS_ERR_X509_INVALID_ALG +
168 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100169
170 /* Parse md_oid */
171 md_oid.tag = *p;
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
174 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100175
176 md_oid.p = p;
177 p += md_oid.len;
178
179 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
181 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100182
183 /* Make sure params is absent of NULL */
184 if( p == end )
185 return( 0 );
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
188 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100189
190 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_X509_INVALID_ALG +
192 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100193
194 return( 0 );
195}
196
197/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100198 * RSASSA-PSS-params ::= SEQUENCE {
199 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
200 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
201 * saltLength [2] INTEGER DEFAULT 20,
202 * trailerField [3] INTEGER DEFAULT 1 }
203 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200204 *
205 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
206 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
207 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
210 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200211 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100212{
213 int ret;
214 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100215 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100216 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100218
219 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 *md_alg = MBEDTLS_MD_SHA1;
221 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100222 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100223
224 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
226 return( MBEDTLS_ERR_X509_INVALID_ALG +
227 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100228
229 p = (unsigned char *) params->p;
230 end = p + params->len;
231
232 if( p == end )
233 return( 0 );
234
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100235 /*
236 * HashAlgorithm
237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
239 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100240 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100241 end2 = p + len;
242
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100243 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100245 return( ret );
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
248 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100249
250 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 return( MBEDTLS_ERR_X509_INVALID_ALG +
252 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100253 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
255 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100257 if( p == end )
258 return( 0 );
259
260 /*
261 * MaskGenAlgorithm
262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
264 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100265 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100266 end2 = p + len;
267
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100268 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100270 return( ret );
271
272 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
274 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
275 MBEDTLS_ERR_OID_NOT_FOUND );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100276
277 /* Parse HashAlgorithm */
278 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
279 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100280
281 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 return( MBEDTLS_ERR_X509_INVALID_ALG +
283 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100284 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
286 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100287
288 if( p == end )
289 return( 0 );
290
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100291 /*
292 * salt_len
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
295 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100296 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100297 end2 = p + len;
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
300 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100301
302 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 return( MBEDTLS_ERR_X509_INVALID_ALG +
304 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
307 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100308
309 if( p == end )
310 return( 0 );
311
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100312 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200313 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
316 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100317 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200318 int trailer_field;
319
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100320 end2 = p + len;
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
323 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100324
325 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 return( MBEDTLS_ERR_X509_INVALID_ALG +
327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200328
329 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
333 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334
335 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 return( MBEDTLS_ERR_X509_INVALID_ALG +
337 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100338
339 return( 0 );
340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100342
343/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 * AttributeTypeAndValue ::= SEQUENCE {
345 * type AttributeType,
346 * value AttributeValue }
347 *
348 * AttributeType ::= OBJECT IDENTIFIER
349 *
350 * AttributeValue ::= ANY DEFINED BY AttributeType
351 */
352static int x509_get_attr_type_value( unsigned char **p,
353 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355{
356 int ret;
357 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_x509_buf *oid;
359 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
362 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
363 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364
365 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 return( MBEDTLS_ERR_X509_INVALID_NAME +
367 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
369 oid = &cur->oid;
370 oid->tag = **p;
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
373 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374
375 oid->p = *p;
376 *p += oid->len;
377
378 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 return( MBEDTLS_ERR_X509_INVALID_NAME +
380 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
383 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
384 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
385 **p != MBEDTLS_ASN1_BIT_STRING )
386 return( MBEDTLS_ERR_X509_INVALID_NAME +
387 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388
389 val = &cur->val;
390 val->tag = *(*p)++;
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
393 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394
395 val->p = *p;
396 *p += val->len;
397
398 cur->next = NULL;
399
400 return( 0 );
401}
402
403/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000404 * Name ::= CHOICE { -- only one possibility for now --
405 * rdnSequence RDNSequence }
406 *
407 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
408 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409 * RelativeDistinguishedName ::=
410 * SET OF AttributeTypeAndValue
411 *
412 * AttributeTypeAndValue ::= SEQUENCE {
413 * type AttributeType,
414 * value AttributeValue }
415 *
416 * AttributeType ::= OBJECT IDENTIFIER
417 *
418 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200419 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000420 * The data structure is optimized for the common case where each RDN has only
421 * one element, which is represented as a list of AttributeTypeAndValue.
422 * For the general case we still use a flat list, but we mark elements of the
423 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
427 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428{
429 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200430 size_t set_len;
431 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100433 /* don't use recursion, we'd risk stack overflow if not optimized */
434 while( 1 )
435 {
436 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000437 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
440 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
441 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100443 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000445 while( 1 )
446 {
447 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
448 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000450 if( *p == end_set )
451 break;
452
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200453 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000454 cur->next_merged = 1;
455
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200456 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000457
458 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200459 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000460
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000461 cur = cur->next;
462 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100464 /*
465 * continue until end of SEQUENCE is reached
466 */
467 if( *p == end )
468 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200470 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100472 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200473 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100475 cur = cur->next;
476 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477}
478
Rich Evans7d5a55a2015-02-13 11:48:02 +0000479static int x509_parse_int(unsigned char **p, unsigned n, int *res){
480 *res = 0;
481 for( ; n > 0; --n ){
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( **p < '0') || ( **p > '9' ) ) return MBEDTLS_ERR_X509_INVALID_DATE;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000483 *res *= 10;
484 *res += (*(*p)++ - '0');
485 }
486 return 0;
487}
488
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489/*
490 * Time ::= CHOICE {
491 * utcTime UTCTime,
492 * generalTime GeneralizedTime }
493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
495 mbedtls_x509_time *time )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496{
497 int ret;
498 size_t len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 unsigned char tag;
500
501 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 return( MBEDTLS_ERR_X509_INVALID_DATE +
503 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
505 tag = **p;
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( tag == MBEDTLS_ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508 {
509 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 ret = mbedtls_asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200511
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514
Rich Evans7d5a55a2015-02-13 11:48:02 +0000515 CHECK( x509_parse_int( p, 2, &time->year ) );
516 CHECK( x509_parse_int( p, 2, &time->mon ) );
517 CHECK( x509_parse_int( p, 2, &time->day ) );
518 CHECK( x509_parse_int( p, 2, &time->hour ) );
519 CHECK( x509_parse_int( p, 2, &time->min ) );
520 if( len > 10 )
521 CHECK( x509_parse_int( p, 2, &time->sec ) );
522 if( len > 12 && *(*p)++ != 'Z' )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 return( MBEDTLS_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
525 time->year += 100 * ( time->year < 50 );
526 time->year += 1900;
527
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528 return( 0 );
529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531 {
532 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 ret = mbedtls_asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200534
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
Rich Evans7d5a55a2015-02-13 11:48:02 +0000538 CHECK( x509_parse_int( p, 4, &time->year ) );
539 CHECK( x509_parse_int( p, 2, &time->mon ) );
540 CHECK( x509_parse_int( p, 2, &time->day ) );
541 CHECK( x509_parse_int( p, 2, &time->hour ) );
542 CHECK( x509_parse_int( p, 2, &time->min ) );
543 if( len > 12 )
544 CHECK( x509_parse_int( p, 2, &time->sec ) );
545 if( len > 14 && *(*p)++ != 'Z' )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 return( MBEDTLS_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 return( 0 );
549 }
550 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 return( MBEDTLS_ERR_X509_INVALID_DATE +
552 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553}
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556{
557 int ret;
558 size_t len;
559
560 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
562 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
564 sig->tag = **p;
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
567 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
569 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;
SimonBd5800b72016-04-26 07:43:27 +0100848 mbedtls_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
SimonBd5800b72016-04-26 07:43:27 +0100856 tt = mbedtls_time( NULL );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200857 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 */