blob: 8e53eb798bdde586bfc1afae0efcbb55d73b66fe [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ITU-T X.509 standard defines a certificate format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc3279.txt
29 * http://www.ietf.org/rfc/rfc3280.txt
30 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020039#else
40#include POLARSSL_CONFIG_FILE
41#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
43#if defined(POLARSSL_X509_USE_C)
44
45#include "polarssl/x509.h"
46#include "polarssl/asn1.h"
47#include "polarssl/oid.h"
48#if defined(POLARSSL_PEM_PARSE_C)
49#include "polarssl/pem.h"
50#endif
51
Paul Bakker7dc4c442014-02-01 22:50:26 +010052#if defined(POLARSSL_PLATFORM_C)
53#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010055#define polarssl_printf printf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
60#include <string.h>
61#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010062#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063#include <windows.h>
64#else
65#include <time.h>
66#endif
67
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#if defined(EFIX64) || defined(EFI32)
69#include <stdio.h>
70#endif
71
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#if defined(POLARSSL_FS_IO)
73#include <stdio.h>
74#if !defined(_WIN32)
75#include <sys/types.h>
76#include <sys/stat.h>
77#include <dirent.h>
78#endif
79#endif
80
81/*
82 * CertificateSerialNumber ::= INTEGER
83 */
84int x509_get_serial( unsigned char **p, const unsigned char *end,
85 x509_buf *serial )
86{
87 int ret;
88
89 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +020090 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091 POLARSSL_ERR_ASN1_OUT_OF_DATA );
92
93 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
94 **p != ASN1_INTEGER )
Paul Bakker51876562013-09-17 14:36:05 +020095 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
97
98 serial->tag = *(*p)++;
99
100 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200101 return( POLARSSL_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 */
115int x509_get_alg_null( unsigned char **p, const unsigned char *end,
116 x509_buf *alg )
117{
118 int ret;
119
120 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200121 return( POLARSSL_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 */
129int x509_get_alg( unsigned char **p, const unsigned char *end,
130 x509_buf *alg, x509_buf *params )
131{
132 int ret;
133
134 if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
135 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
136
137 return( 0 );
138}
139
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100140#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
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 */
150static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
151{
152 int ret;
153 unsigned char *p;
154 const unsigned char *end;
155 x509_buf md_oid;
156 size_t len;
157
158 /* Make sure we got a SEQUENCE and setup bounds */
159 if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
160 return( POLARSSL_ERR_X509_INVALID_ALG +
161 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
162
163 p = (unsigned char *) alg->p;
164 end = p + alg->len;
165
166 if( p >= end )
167 return( POLARSSL_ERR_X509_INVALID_ALG +
168 POLARSSL_ERR_ASN1_OUT_OF_DATA );
169
170 /* Parse md_oid */
171 md_oid.tag = *p;
172
173 if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
174 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
175
176 md_oid.p = p;
177 p += md_oid.len;
178
179 /* Get md_alg from md_oid */
180 if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
181 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
182
183 /* Make sure params is absent of NULL */
184 if( p == end )
185 return( 0 );
186
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100187 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100188 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
189
190 if( p != end )
191 return( POLARSSL_ERR_X509_INVALID_ALG +
192 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
193
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 */
209int x509_get_rsassa_pss_params( const x509_buf *params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100210 md_type_t *md_alg, 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é-Gonnarde76b7502014-01-23 19:15:29 +0100217 x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100218
219 /* First set everything to defaults */
220 *md_alg = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100221 *mgf_md = POLARSSL_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 */
225 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
226 return( POLARSSL_ERR_X509_INVALID_ALG +
227 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
228
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é-Gonnardf346bab2014-01-23 16:24:44 +0100238 if( ( ret = asn1_get_tag( &p, end, &len,
239 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
240 {
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é-Gonnard9c9cf5b2014-01-24 14:15:20 +0100244 if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100245 return( ret );
246
247 if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
248 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100249
250 if( p != end2 )
251 return( POLARSSL_ERR_X509_INVALID_ALG +
252 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100253 }
254 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
255 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
256
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100257 if( p == end )
258 return( 0 );
259
260 /*
261 * MaskGenAlgorithm
262 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100263 if( ( ret = asn1_get_tag( &p, end, &len,
264 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
265 {
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é-Gonnard9c9cf5b2014-01-24 14:15:20 +0100269 if( ( ret = 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 */
273 if( ! OID_CMP( OID_MGF1, &alg_id ) )
274 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
275 POLARSSL_ERR_OID_NOT_FOUND );
276
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 )
282 return( POLARSSL_ERR_X509_INVALID_ALG +
283 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100284 }
285 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
286 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
287
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é-Gonnardf346bab2014-01-23 16:24:44 +0100294 if( ( ret = asn1_get_tag( &p, end, &len,
295 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
296 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100297 end2 = p + len;
298
299 if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100300 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100301
302 if( p != end2 )
303 return( POLARSSL_ERR_X509_INVALID_ALG +
304 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100305 }
306 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
307 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
308
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é-Gonnardf346bab2014-01-23 16:24:44 +0100315 if( ( ret = asn1_get_tag( &p, end, &len,
316 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
317 {
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é-Gonnard78117d52014-05-31 17:08:16 +0200322 if( ( ret = asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100323 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100324
325 if( p != end2 )
326 return( POLARSSL_ERR_X509_INVALID_ALG +
327 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200328
329 if( trailer_field != 1 )
330 return( POLARSSL_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100331 }
332 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
333 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
334
335 if( p != end )
336 return( POLARSSL_ERR_X509_INVALID_ALG +
337 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
338
339 return( 0 );
340}
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100341#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */
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,
354 x509_name *cur )
355{
356 int ret;
357 size_t len;
358 x509_buf *oid;
359 x509_buf *val;
360
361 if( ( ret = asn1_get_tag( p, end, &len,
362 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200363 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364
365 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200366 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367 POLARSSL_ERR_ASN1_OUT_OF_DATA );
368
369 oid = &cur->oid;
370 oid->tag = **p;
371
372 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200373 return( POLARSSL_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 )
Paul Bakker51876562013-09-17 14:36:05 +0200379 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 POLARSSL_ERR_ASN1_OUT_OF_DATA );
381
382 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
383 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
384 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200385 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
387
388 val = &cur->val;
389 val->tag = *(*p)++;
390
391 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200392 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393
394 val->p = *p;
395 *p += val->len;
396
397 cur->next = NULL;
398
399 return( 0 );
400}
401
402/*
403 * RelativeDistinguishedName ::=
404 * SET OF AttributeTypeAndValue
405 *
406 * AttributeTypeAndValue ::= SEQUENCE {
407 * type AttributeType,
408 * value AttributeValue }
409 *
410 * AttributeType ::= OBJECT IDENTIFIER
411 *
412 * AttributeValue ::= ANY DEFINED BY AttributeType
413 */
414int x509_get_name( unsigned char **p, const unsigned char *end,
415 x509_name *cur )
416{
417 int ret;
418 size_t len;
419 const unsigned char *end2;
420 x509_name *use;
421
422 if( ( ret = asn1_get_tag( p, end, &len,
423 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200424 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
426 end2 = end;
427 end = *p + len;
428 use = cur;
429
430 do
431 {
432 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
433 return( ret );
434
435 if( *p != end )
436 {
437 use->next = (x509_name *) polarssl_malloc(
438 sizeof( x509_name ) );
439
440 if( use->next == NULL )
441 return( POLARSSL_ERR_X509_MALLOC_FAILED );
442
443 memset( use->next, 0, sizeof( x509_name ) );
444
445 use = use->next;
446 }
447 }
448 while( *p != end );
449
450 /*
451 * recurse until end of SEQUENCE is reached
452 */
453 if( *p == end2 )
454 return( 0 );
455
456 cur->next = (x509_name *) polarssl_malloc(
457 sizeof( x509_name ) );
458
459 if( cur->next == NULL )
460 return( POLARSSL_ERR_X509_MALLOC_FAILED );
461
462 memset( cur->next, 0, sizeof( x509_name ) );
463
464 return( x509_get_name( p, end2, cur->next ) );
465}
466
467/*
468 * Time ::= CHOICE {
469 * utcTime UTCTime,
470 * generalTime GeneralizedTime }
471 */
472int x509_get_time( unsigned char **p, const unsigned char *end,
473 x509_time *time )
474{
475 int ret;
476 size_t len;
477 char date[64];
478 unsigned char tag;
479
480 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200481 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 POLARSSL_ERR_ASN1_OUT_OF_DATA );
483
484 tag = **p;
485
486 if ( tag == ASN1_UTC_TIME )
487 {
488 (*p)++;
489 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200490
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200492 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
494 memset( date, 0, sizeof( date ) );
495 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
496 len : sizeof( date ) - 1 );
497
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200498 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 &time->year, &time->mon, &time->day,
500 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200501 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
503 time->year += 100 * ( time->year < 50 );
504 time->year += 1900;
505
506 *p += len;
507
508 return( 0 );
509 }
510 else if ( tag == ASN1_GENERALIZED_TIME )
511 {
512 (*p)++;
513 ret = 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 )
Paul Bakker51876562013-09-17 14:36:05 +0200516 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
518 memset( date, 0, sizeof( date ) );
519 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
520 len : sizeof( date ) - 1 );
521
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200522 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 &time->year, &time->mon, &time->day,
524 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200525 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
527 *p += len;
528
529 return( 0 );
530 }
531 else
Paul Bakker51876562013-09-17 14:36:05 +0200532 return( POLARSSL_ERR_X509_INVALID_DATE +
533 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534}
535
536int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
537{
538 int ret;
539 size_t len;
540
541 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200542 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543 POLARSSL_ERR_ASN1_OUT_OF_DATA );
544
545 sig->tag = **p;
546
547 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200548 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549
550 sig->len = len;
551 sig->p = *p;
552
553 *p += len;
554
555 return( 0 );
556}
557
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100558/*
559 * Get signature algorithm from alg OID and optional parameters
560 */
561int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
562 md_type_t *md_alg, pk_type_t *pk_alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100564 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100566 if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200567 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100569#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
570 if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
571 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200572 int salt_len;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100573 md_type_t mgf_md;
574
575 /* Make sure params are valid */
576 ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200577 md_alg, &mgf_md, &salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100578 if( ret != 0 )
579 return( ret );
580
581 }
582 else
583#endif
584 {
585 /* Make sure parameters are absent or NULL */
586 if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
587 sig_params->len != 0 )
588 return( POLARSSL_ERR_X509_INVALID_ALG );
589 }
590
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591 return( 0 );
592}
593
594/*
595 * X.509 Extensions (No parsing of extensions, pointer should
596 * be either manually updated or extensions should be parsed!
597 */
598int x509_get_ext( unsigned char **p, const unsigned char *end,
599 x509_buf *ext, int tag )
600{
601 int ret;
602 size_t len;
603
604 if( *p == end )
605 return( 0 );
606
607 ext->tag = **p;
608
609 if( ( ret = asn1_get_tag( p, end, &ext->len,
610 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
611 return( ret );
612
613 ext->p = *p;
614 end = *p + ext->len;
615
616 /*
617 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
618 *
619 * Extension ::= SEQUENCE {
620 * extnID OBJECT IDENTIFIER,
621 * critical BOOLEAN DEFAULT FALSE,
622 * extnValue OCTET STRING }
623 */
624 if( ( ret = asn1_get_tag( p, end, &len,
625 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200626 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627
628 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200629 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
631
632 return( 0 );
633}
634
635#if defined(POLARSSL_FS_IO)
636/*
637 * Load all data from a file into a given buffer.
638 */
639int x509_load_file( const char *path, unsigned char **buf, size_t *n )
640{
641 FILE *f;
642 long size;
643
644 if( ( f = fopen( path, "rb" ) ) == NULL )
645 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
646
647 fseek( f, 0, SEEK_END );
648 if( ( size = ftell( f ) ) == -1 )
649 {
650 fclose( f );
651 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
652 }
653 fseek( f, 0, SEEK_SET );
654
655 *n = (size_t) size;
656
657 if( *n + 1 == 0 ||
658 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
659 {
660 fclose( f );
661 return( POLARSSL_ERR_X509_MALLOC_FAILED );
662 }
663
664 if( fread( *buf, 1, *n, f ) != *n )
665 {
666 fclose( f );
667 polarssl_free( *buf );
668 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
669 }
670
671 fclose( f );
672
673 (*buf)[*n] = '\0';
674
675 return( 0 );
676}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677#endif /* POLARSSL_FS_IO */
678
Paul Bakker6edcd412013-10-29 15:22:54 +0100679#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
680 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681#include <stdarg.h>
682
683#if !defined vsnprintf
684#define vsnprintf _vsnprintf
685#endif // vsnprintf
686
687/*
688 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
689 * Result value is not size of buffer needed, but -1 if no fit is possible.
690 *
691 * This fuction tries to 'fix' this by at least suggesting enlarging the
692 * size by 20.
693 */
694static int compat_snprintf(char *str, size_t size, const char *format, ...)
695{
696 va_list ap;
697 int res = -1;
698
699 va_start( ap, format );
700
701 res = vsnprintf( str, size, format, ap );
702
703 va_end( ap );
704
705 // No quick fix possible
706 if ( res < 0 )
707 return( (int) size + 20 );
708
709 return res;
710}
711
712#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200713#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714
715#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
716
717#define SAFE_SNPRINTF() \
718{ \
719 if( ret == -1 ) \
720 return( -1 ); \
721 \
722 if ( (unsigned int) ret > n ) { \
723 p[n - 1] = '\0'; \
724 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
725 } \
726 \
727 n -= (unsigned int) ret; \
728 p += (unsigned int) ret; \
729}
730
731/*
732 * Store the name in printable form into buf; no more
733 * than size characters will be written
734 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200735int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736{
737 int ret;
738 size_t i, n;
739 unsigned char c;
740 const x509_name *name;
741 const char *short_name = NULL;
742 char s[128], *p;
743
744 memset( s, 0, sizeof( s ) );
745
746 name = dn;
747 p = buf;
748 n = size;
749
750 while( name != NULL )
751 {
752 if( !name->oid.p )
753 {
754 name = name->next;
755 continue;
756 }
757
758 if( name != dn )
759 {
760 ret = snprintf( p, n, ", " );
761 SAFE_SNPRINTF();
762 }
763
764 ret = oid_get_attr_short_name( &name->oid, &short_name );
765
766 if( ret == 0 )
767 ret = snprintf( p, n, "%s=", short_name );
768 else
769 ret = snprintf( p, n, "\?\?=" );
770 SAFE_SNPRINTF();
771
772 for( i = 0; i < name->val.len; i++ )
773 {
774 if( i >= sizeof( s ) - 1 )
775 break;
776
777 c = name->val.p[i];
778 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
779 s[i] = '?';
780 else s[i] = c;
781 }
782 s[i] = '\0';
783 ret = snprintf( p, n, "%s", s );
784 SAFE_SNPRINTF();
785 name = name->next;
786 }
787
788 return( (int) ( size - n ) );
789}
790
791/*
792 * Store the serial in printable form into buf; no more
793 * than size characters will be written
794 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200795int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796{
797 int ret;
798 size_t i, n, nr;
799 char *p;
800
801 p = buf;
802 n = size;
803
804 nr = ( serial->len <= 32 )
805 ? serial->len : 28;
806
807 for( i = 0; i < nr; i++ )
808 {
809 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
810 continue;
811
812 ret = snprintf( p, n, "%02X%s",
813 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
814 SAFE_SNPRINTF();
815 }
816
817 if( nr != serial->len )
818 {
819 ret = snprintf( p, n, "...." );
820 SAFE_SNPRINTF();
821 }
822
823 return( (int) ( size - n ) );
824}
825
826/*
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100827 * Helper for writing signature alrogithms
828 */
829int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
830 pk_type_t pk_alg, const x509_buf *sig_params )
831{
832 int ret;
833 char *p = buf;
834 size_t n = size;
835 const char *desc = NULL;
836
837 ret = oid_get_sig_alg_desc( sig_oid, &desc );
838 if( ret != 0 )
839 ret = snprintf( p, n, "???" );
840 else
841 ret = snprintf( p, n, "%s", desc );
842 SAFE_SNPRINTF();
843
844#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
845 if( pk_alg == POLARSSL_PK_RSASSA_PSS )
846 {
847 md_type_t md_alg, mgf_md;
848 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200849 int salt_len;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100850
851 if( ( ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200852 &md_alg, &mgf_md, &salt_len ) ) != 0 )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100853 return( ret );
854
855 md_info = md_info_from_type( md_alg );
856 mgf_md_info = md_info_from_type( mgf_md );
857
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200858 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100859 md_info ? md_info->name : "???",
860 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200861 salt_len );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100862 SAFE_SNPRINTF();
863 }
864#else
865 ((void) pk_alg);
866 ((void) sig_params);
867#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */
868
869 return( (int) size - n );
870}
871
872/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873 * Helper for writing "RSA key size", "EC key size", etc
874 */
875int x509_key_size_helper( char *buf, size_t size, const char *name )
876{
877 char *p = buf;
878 size_t n = size;
879 int ret;
880
881 if( strlen( name ) + sizeof( " key size" ) > size )
882 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
883
884 ret = snprintf( p, n, "%s key size", name );
885 SAFE_SNPRINTF();
886
887 return( 0 );
888}
889
890/*
891 * Return an informational string describing the given OID
892 */
893const char *x509_oid_get_description( x509_buf *oid )
894{
895 const char *desc = NULL;
896 int ret;
897
898 ret = oid_get_extended_key_usage( oid, &desc );
899
900 if( ret != 0 )
901 return( NULL );
902
903 return( desc );
904}
905
906/* Return the x.y.z.... style numeric string for the given OID */
907int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
908{
909 return oid_get_numeric_string( buf, size, oid );
910}
911
912/*
913 * Return 0 if the x509_time is still valid, or 1 otherwise.
914 */
915#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100917static void x509_get_current_time( x509_time *now )
918{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100919#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 SYSTEMTIME st;
921
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200922 GetSystemTime(&st);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100924 now->year = st.wYear;
925 now->mon = st.wMonth;
926 now->day = st.wDay;
927 now->hour = st.wHour;
928 now->min = st.wMinute;
929 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100931 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932 time_t tt;
933
934 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200935 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936
Paul Bakker5fff23b2014-03-26 15:34:54 +0100937 now->year = lt.tm_year + 1900;
938 now->mon = lt.tm_mon + 1;
939 now->day = lt.tm_mday;
940 now->hour = lt.tm_hour;
941 now->min = lt.tm_min;
942 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200943#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100944}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100946/*
947 * Return 0 if before <= after, 1 otherwise
948 */
949static int x509_check_time( const x509_time *before, const x509_time *after )
950{
951 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 return( 1 );
953
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100954 if( before->year == after->year &&
955 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 return( 1 );
957
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100958 if( before->year == after->year &&
959 before->mon == after->mon &&
960 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 return( 1 );
962
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100963 if( before->year == after->year &&
964 before->mon == after->mon &&
965 before->day == after->day &&
966 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 return( 1 );
968
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100969 if( before->year == after->year &&
970 before->mon == after->mon &&
971 before->day == after->day &&
972 before->hour == after->hour &&
973 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 return( 1 );
975
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100976 if( before->year == after->year &&
977 before->mon == after->mon &&
978 before->day == after->day &&
979 before->hour == after->hour &&
980 before->min == after->min &&
981 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 return( 1 );
983
984 return( 0 );
985}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100986
987int x509_time_expired( const x509_time *to )
988{
989 x509_time now;
990
991 x509_get_current_time( &now );
992
993 return( x509_check_time( &now, to ) );
994}
995
996int x509_time_future( const x509_time *from )
997{
998 x509_time now;
999
1000 x509_get_current_time( &now );
1001
1002 return( x509_check_time( from, &now ) );
1003}
1004
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001006
Paul Bakker86d0c192013-09-18 11:11:02 +02001007int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008{
1009 ((void) to);
1010 return( 0 );
1011}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001012
1013int x509_time_future( const x509_time *from )
1014{
1015 ((void) from);
1016 return( 0 );
1017}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018#endif /* POLARSSL_HAVE_TIME */
1019
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001020#if defined(POLARSSL_SELF_TEST)
1021
1022#include "polarssl/x509_crt.h"
1023#include "polarssl/certs.h"
1024
1025/*
1026 * Checkup routine
1027 */
1028int x509_self_test( int verbose )
1029{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +02001030#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001031 int ret;
1032 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001033 x509_crt cacert;
1034 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001035
1036 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001037 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001038
Paul Bakkerb6b09562013-09-18 14:17:41 +02001039 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001040
Paul Bakkerddf26b42013-09-18 13:46:23 +02001041 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
1042 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001043 if( ret != 0 )
1044 {
1045 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001046 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001047
1048 return( ret );
1049 }
1050
Paul Bakkerb6b09562013-09-18 14:17:41 +02001051 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001052
Paul Bakkerddf26b42013-09-18 13:46:23 +02001053 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
1054 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001055 if( ret != 0 )
1056 {
1057 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001058 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001059
1060 return( ret );
1061 }
1062
1063 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001064 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001065
Paul Bakkerddf26b42013-09-18 13:46:23 +02001066 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001067 if( ret != 0 )
1068 {
1069 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001070 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001071
Paul Bakker7dc4c442014-02-01 22:50:26 +01001072 polarssl_printf("ret = %d, &flags = %04x\n", ret, flags);
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001073
1074 return( ret );
1075 }
1076
1077 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001078 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001079
1080 x509_crt_free( &cacert );
1081 x509_crt_free( &clicert );
1082
1083 return( 0 );
1084#else
1085 ((void) verbose);
1086 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001087#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001088}
1089
Paul Bakker9af723c2014-05-01 13:03:14 +02001090#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001091
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092#endif /* POLARSSL_X509_USE_C */