blob: 7ddeb2f3761356a88a9bb7b657ee3fac9a3de61a [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é-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The ITU-T X.509 standard defines a certificate format for PKI.
24 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020025 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
26 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
27 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028 *
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
31 */
32
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
36#include POLARSSL_CONFIG_FILE
37#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
39#if defined(POLARSSL_X509_USE_C)
40
41#include "polarssl/x509.h"
42#include "polarssl/asn1.h"
43#include "polarssl/oid.h"
44#if defined(POLARSSL_PEM_PARSE_C)
45#include "polarssl/pem.h"
46#endif
47
Paul Bakker7dc4c442014-02-01 22:50:26 +010048#if defined(POLARSSL_PLATFORM_C)
49#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#define polarssl_printf printf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#define polarssl_malloc malloc
53#define polarssl_free free
54#endif
55
56#include <string.h>
57#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010058#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059#include <windows.h>
60#else
61#include <time.h>
62#endif
63
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#include <stdio.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010065
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#if defined(POLARSSL_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#if !defined(_WIN32)
68#include <sys/types.h>
69#include <sys/stat.h>
70#include <dirent.h>
71#endif
72#endif
73
74/*
75 * CertificateSerialNumber ::= INTEGER
76 */
77int x509_get_serial( unsigned char **p, const unsigned char *end,
78 x509_buf *serial )
79{
80 int ret;
81
82 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +020083 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084 POLARSSL_ERR_ASN1_OUT_OF_DATA );
85
86 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
87 **p != ASN1_INTEGER )
Paul Bakker51876562013-09-17 14:36:05 +020088 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
90
91 serial->tag = *(*p)++;
92
93 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +020094 return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095
96 serial->p = *p;
97 *p += serial->len;
98
99 return( 0 );
100}
101
102/* Get an algorithm identifier without parameters (eg for signatures)
103 *
104 * AlgorithmIdentifier ::= SEQUENCE {
105 * algorithm OBJECT IDENTIFIER,
106 * parameters ANY DEFINED BY algorithm OPTIONAL }
107 */
108int x509_get_alg_null( unsigned char **p, const unsigned char *end,
109 x509_buf *alg )
110{
111 int ret;
112
113 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200114 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115
116 return( 0 );
117}
118
119/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100120 * Parse an algorithm identifier with (optional) paramaters
121 */
122int x509_get_alg( unsigned char **p, const unsigned char *end,
123 x509_buf *alg, x509_buf *params )
124{
125 int ret;
126
127 if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
128 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
129
130 return( 0 );
131}
132
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200133#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100134/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100135 * HashAlgorithm ::= AlgorithmIdentifier
136 *
137 * AlgorithmIdentifier ::= SEQUENCE {
138 * algorithm OBJECT IDENTIFIER,
139 * parameters ANY DEFINED BY algorithm OPTIONAL }
140 *
141 * For HashAlgorithm, parameters MUST be NULL or absent.
142 */
143static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
144{
145 int ret;
146 unsigned char *p;
147 const unsigned char *end;
148 x509_buf md_oid;
149 size_t len;
150
151 /* Make sure we got a SEQUENCE and setup bounds */
152 if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
153 return( POLARSSL_ERR_X509_INVALID_ALG +
154 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
155
156 p = (unsigned char *) alg->p;
157 end = p + alg->len;
158
159 if( p >= end )
160 return( POLARSSL_ERR_X509_INVALID_ALG +
161 POLARSSL_ERR_ASN1_OUT_OF_DATA );
162
163 /* Parse md_oid */
164 md_oid.tag = *p;
165
166 if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
167 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
168
169 md_oid.p = p;
170 p += md_oid.len;
171
172 /* Get md_alg from md_oid */
173 if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
174 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
175
176 /* Make sure params is absent of NULL */
177 if( p == end )
178 return( 0 );
179
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100180 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100181 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
182
183 if( p != end )
184 return( POLARSSL_ERR_X509_INVALID_ALG +
185 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
186
187 return( 0 );
188}
189
190/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100191 * RSASSA-PSS-params ::= SEQUENCE {
192 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
193 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
194 * saltLength [2] INTEGER DEFAULT 20,
195 * trailerField [3] INTEGER DEFAULT 1 }
196 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200197 *
198 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
199 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
200 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100201 */
202int x509_get_rsassa_pss_params( const x509_buf *params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100203 md_type_t *md_alg, md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200204 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100205{
206 int ret;
207 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100208 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100209 size_t len;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100210 x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100211
212 /* First set everything to defaults */
213 *md_alg = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100214 *mgf_md = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100215 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100216
217 /* Make sure params is a SEQUENCE and setup bounds */
218 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
219 return( POLARSSL_ERR_X509_INVALID_ALG +
220 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
221
222 p = (unsigned char *) params->p;
223 end = p + params->len;
224
225 if( p == end )
226 return( 0 );
227
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100228 /*
229 * HashAlgorithm
230 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100231 if( ( ret = asn1_get_tag( &p, end, &len,
232 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
233 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100234 end2 = p + len;
235
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100236 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100237 if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100238 return( ret );
239
240 if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
241 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100242
243 if( p != end2 )
244 return( POLARSSL_ERR_X509_INVALID_ALG +
245 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100246 }
247 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
248 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
249
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100250 if( p == end )
251 return( 0 );
252
253 /*
254 * MaskGenAlgorithm
255 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256 if( ( ret = asn1_get_tag( &p, end, &len,
257 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
258 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100259 end2 = p + len;
260
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100261 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100262 if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100263 return( ret );
264
265 /* Only MFG1 is recognised for now */
266 if( ! OID_CMP( OID_MGF1, &alg_id ) )
267 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
268 POLARSSL_ERR_OID_NOT_FOUND );
269
270 /* Parse HashAlgorithm */
271 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
272 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100273
274 if( p != end2 )
275 return( POLARSSL_ERR_X509_INVALID_ALG +
276 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100277 }
278 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
279 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
280
281 if( p == end )
282 return( 0 );
283
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100284 /*
285 * salt_len
286 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100287 if( ( ret = asn1_get_tag( &p, end, &len,
288 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
289 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100290 end2 = p + len;
291
292 if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100293 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100294
295 if( p != end2 )
296 return( POLARSSL_ERR_X509_INVALID_ALG +
297 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100298 }
299 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
300 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
301
302 if( p == end )
303 return( 0 );
304
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100305 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200306 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100307 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100308 if( ( ret = asn1_get_tag( &p, end, &len,
309 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
310 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200311 int trailer_field;
312
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100313 end2 = p + len;
314
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200315 if( ( ret = asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100316 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100317
318 if( p != end2 )
319 return( POLARSSL_ERR_X509_INVALID_ALG +
320 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200321
322 if( trailer_field != 1 )
323 return( POLARSSL_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100324 }
325 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
326 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
327
328 if( p != end )
329 return( POLARSSL_ERR_X509_INVALID_ALG +
330 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
331
332 return( 0 );
333}
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200334#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100335
336/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337 * AttributeTypeAndValue ::= SEQUENCE {
338 * type AttributeType,
339 * value AttributeValue }
340 *
341 * AttributeType ::= OBJECT IDENTIFIER
342 *
343 * AttributeValue ::= ANY DEFINED BY AttributeType
344 */
345static int x509_get_attr_type_value( unsigned char **p,
346 const unsigned char *end,
347 x509_name *cur )
348{
349 int ret;
350 size_t len;
351 x509_buf *oid;
352 x509_buf *val;
353
354 if( ( ret = asn1_get_tag( p, end, &len,
355 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200356 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
358 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200359 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 POLARSSL_ERR_ASN1_OUT_OF_DATA );
361
362 oid = &cur->oid;
363 oid->tag = **p;
364
365 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200366 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
368 oid->p = *p;
369 *p += oid->len;
370
371 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200372 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373 POLARSSL_ERR_ASN1_OUT_OF_DATA );
374
375 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
376 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
377 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200378 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
380
381 val = &cur->val;
382 val->tag = *(*p)++;
383
384 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200385 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
387 val->p = *p;
388 *p += val->len;
389
390 cur->next = NULL;
391
392 return( 0 );
393}
394
395/*
396 * RelativeDistinguishedName ::=
397 * SET OF AttributeTypeAndValue
398 *
399 * AttributeTypeAndValue ::= SEQUENCE {
400 * type AttributeType,
401 * value AttributeValue }
402 *
403 * AttributeType ::= OBJECT IDENTIFIER
404 *
405 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200406 *
407 * We restrict RelativeDistinguishedName to be a set of 1 element. This is
408 * the most common case, and our x509_name structure currently can't handle
409 * more than that.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 */
411int x509_get_name( unsigned char **p, const unsigned char *end,
412 x509_name *cur )
413{
414 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200415 size_t set_len;
416 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100418 /* don't use recursion, we'd risk stack overflow if not optimized */
419 while( 1 )
420 {
421 /*
422 * parse first SET, restricted to 1 element
423 */
424 if( ( ret = asn1_get_tag( p, end, &set_len,
425 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
426 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100428 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100430 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
431 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100433 if( *p != end_set )
434 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100436 /*
437 * continue until end of SEQUENCE is reached
438 */
439 if( *p == end )
440 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100442 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100444 if( cur->next == NULL )
445 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100447 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100449 cur = cur->next;
450 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451}
452
453/*
454 * Time ::= CHOICE {
455 * utcTime UTCTime,
456 * generalTime GeneralizedTime }
457 */
458int x509_get_time( unsigned char **p, const unsigned char *end,
459 x509_time *time )
460{
461 int ret;
462 size_t len;
463 char date[64];
464 unsigned char tag;
465
466 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200467 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 POLARSSL_ERR_ASN1_OUT_OF_DATA );
469
470 tag = **p;
471
Paul Bakker66d5d072014-06-17 16:39:18 +0200472 if( tag == ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473 {
474 (*p)++;
475 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200476
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200478 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479
480 memset( date, 0, sizeof( date ) );
481 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
482 len : sizeof( date ) - 1 );
483
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200484 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 &time->year, &time->mon, &time->day,
486 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200487 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
489 time->year += 100 * ( time->year < 50 );
490 time->year += 1900;
491
492 *p += len;
493
494 return( 0 );
495 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200496 else if( tag == ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 {
498 (*p)++;
499 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200500
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200502 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
504 memset( date, 0, sizeof( date ) );
505 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
506 len : sizeof( date ) - 1 );
507
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200508 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509 &time->year, &time->mon, &time->day,
510 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200511 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 *p += len;
514
515 return( 0 );
516 }
517 else
Paul Bakker51876562013-09-17 14:36:05 +0200518 return( POLARSSL_ERR_X509_INVALID_DATE +
519 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520}
521
522int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
523{
524 int ret;
525 size_t len;
526
527 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200528 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529 POLARSSL_ERR_ASN1_OUT_OF_DATA );
530
531 sig->tag = **p;
532
533 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200534 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
536 sig->len = len;
537 sig->p = *p;
538
539 *p += len;
540
541 return( 0 );
542}
543
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100544/*
545 * Get signature algorithm from alg OID and optional parameters
546 */
547int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200548 md_type_t *md_alg, pk_type_t *pk_alg,
549 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100551 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200553 if( *sig_opts != NULL )
554 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
555
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100556 if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200557 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200559#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100560 if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
561 {
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200562 pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100563
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200564 pss_opts = polarssl_malloc( sizeof( pk_rsassa_pss_options ) );
565 if( pss_opts == NULL )
566 return( POLARSSL_ERR_X509_MALLOC_FAILED );
567
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100568 ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200569 md_alg,
570 &pss_opts->mgf1_hash_id,
571 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100572 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200573 {
574 polarssl_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100575 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200576 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100577
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200578 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100579 }
580 else
Paul Bakkerdb20c102014-06-17 14:34:44 +0200581#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100582 {
583 /* Make sure parameters are absent or NULL */
584 if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
585 sig_params->len != 0 )
586 return( POLARSSL_ERR_X509_INVALID_ALG );
587 }
588
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589 return( 0 );
590}
591
592/*
593 * X.509 Extensions (No parsing of extensions, pointer should
594 * be either manually updated or extensions should be parsed!
595 */
596int x509_get_ext( unsigned char **p, const unsigned char *end,
597 x509_buf *ext, int tag )
598{
599 int ret;
600 size_t len;
601
602 if( *p == end )
603 return( 0 );
604
605 ext->tag = **p;
606
607 if( ( ret = asn1_get_tag( p, end, &ext->len,
608 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
609 return( ret );
610
611 ext->p = *p;
612 end = *p + ext->len;
613
614 /*
615 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
616 *
617 * Extension ::= SEQUENCE {
618 * extnID OBJECT IDENTIFIER,
619 * critical BOOLEAN DEFAULT FALSE,
620 * extnValue OCTET STRING }
621 */
622 if( ( ret = asn1_get_tag( p, end, &len,
623 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200624 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625
626 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200627 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
629
630 return( 0 );
631}
632
Paul Bakker6edcd412013-10-29 15:22:54 +0100633#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
634 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635#include <stdarg.h>
636
637#if !defined vsnprintf
638#define vsnprintf _vsnprintf
639#endif // vsnprintf
640
641/*
642 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
643 * Result value is not size of buffer needed, but -1 if no fit is possible.
644 *
645 * This fuction tries to 'fix' this by at least suggesting enlarging the
646 * size by 20.
647 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200648static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649{
650 va_list ap;
651 int res = -1;
652
653 va_start( ap, format );
654
655 res = vsnprintf( str, size, format, ap );
656
657 va_end( ap );
658
659 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200660 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 return( (int) size + 20 );
662
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200663 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664}
665
666#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200667#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668
669#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
670
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671#define SAFE_SNPRINTF() \
672{ \
673 if( ret == -1 ) \
674 return( -1 ); \
675 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200676 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200677 p[n - 1] = '\0'; \
678 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
679 } \
680 \
681 n -= (unsigned int) ret; \
682 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683}
684
685/*
686 * Store the name in printable form into buf; no more
687 * than size characters will be written
688 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200689int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690{
691 int ret;
692 size_t i, n;
693 unsigned char c;
694 const x509_name *name;
695 const char *short_name = NULL;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200696 char s[X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697
698 memset( s, 0, sizeof( s ) );
699
700 name = dn;
701 p = buf;
702 n = size;
703
704 while( name != NULL )
705 {
706 if( !name->oid.p )
707 {
708 name = name->next;
709 continue;
710 }
711
712 if( name != dn )
713 {
714 ret = snprintf( p, n, ", " );
715 SAFE_SNPRINTF();
716 }
717
718 ret = oid_get_attr_short_name( &name->oid, &short_name );
719
720 if( ret == 0 )
721 ret = snprintf( p, n, "%s=", short_name );
722 else
723 ret = snprintf( p, n, "\?\?=" );
724 SAFE_SNPRINTF();
725
726 for( i = 0; i < name->val.len; i++ )
727 {
728 if( i >= sizeof( s ) - 1 )
729 break;
730
731 c = name->val.p[i];
732 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
733 s[i] = '?';
734 else s[i] = c;
735 }
736 s[i] = '\0';
737 ret = snprintf( p, n, "%s", s );
738 SAFE_SNPRINTF();
739 name = name->next;
740 }
741
742 return( (int) ( size - n ) );
743}
744
745/*
746 * Store the serial in printable form into buf; no more
747 * than size characters will be written
748 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200749int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750{
751 int ret;
752 size_t i, n, nr;
753 char *p;
754
755 p = buf;
756 n = size;
757
758 nr = ( serial->len <= 32 )
759 ? serial->len : 28;
760
761 for( i = 0; i < nr; i++ )
762 {
763 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
764 continue;
765
766 ret = snprintf( p, n, "%02X%s",
767 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
768 SAFE_SNPRINTF();
769 }
770
771 if( nr != serial->len )
772 {
773 ret = snprintf( p, n, "...." );
774 SAFE_SNPRINTF();
775 }
776
777 return( (int) ( size - n ) );
778}
779
780/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200781 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100782 */
783int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200784 pk_type_t pk_alg, md_type_t md_alg,
785 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100786{
787 int ret;
788 char *p = buf;
789 size_t n = size;
790 const char *desc = NULL;
791
792 ret = oid_get_sig_alg_desc( sig_oid, &desc );
793 if( ret != 0 )
794 ret = snprintf( p, n, "???" );
795 else
796 ret = snprintf( p, n, "%s", desc );
797 SAFE_SNPRINTF();
798
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200799#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100800 if( pk_alg == POLARSSL_PK_RSASSA_PSS )
801 {
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200802 const pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100803 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100804
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200805 pss_opts = (const pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100806
807 md_info = md_info_from_type( md_alg );
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200808 mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100809
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200810 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100811 md_info ? md_info->name : "???",
812 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200813 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100814 SAFE_SNPRINTF();
815 }
816#else
817 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200818 ((void) md_alg);
819 ((void) sig_opts);
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200820#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100821
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200822 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100823}
824
825/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826 * Helper for writing "RSA key size", "EC key size", etc
827 */
828int x509_key_size_helper( char *buf, size_t size, const char *name )
829{
830 char *p = buf;
831 size_t n = size;
832 int ret;
833
834 if( strlen( name ) + sizeof( " key size" ) > size )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200835 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
837 ret = snprintf( p, n, "%s key size", name );
838 SAFE_SNPRINTF();
839
840 return( 0 );
841}
842
843/*
844 * Return an informational string describing the given OID
845 */
846const char *x509_oid_get_description( x509_buf *oid )
847{
848 const char *desc = NULL;
849 int ret;
850
851 ret = oid_get_extended_key_usage( oid, &desc );
852
853 if( ret != 0 )
854 return( NULL );
855
856 return( desc );
857}
858
859/* Return the x.y.z.... style numeric string for the given OID */
860int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
861{
862 return oid_get_numeric_string( buf, size, oid );
863}
864
865/*
866 * Return 0 if the x509_time is still valid, or 1 otherwise.
867 */
868#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100870static void x509_get_current_time( x509_time *now )
871{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100872#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873 SYSTEMTIME st;
874
Paul Bakker66d5d072014-06-17 16:39:18 +0200875 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100877 now->year = st.wYear;
878 now->mon = st.wMonth;
879 now->day = st.wDay;
880 now->hour = st.wHour;
881 now->min = st.wMinute;
882 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100884 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 time_t tt;
886
887 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200888 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889
Paul Bakker5fff23b2014-03-26 15:34:54 +0100890 now->year = lt.tm_year + 1900;
891 now->mon = lt.tm_mon + 1;
892 now->day = lt.tm_mday;
893 now->hour = lt.tm_hour;
894 now->min = lt.tm_min;
895 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200896#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100897}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100899/*
900 * Return 0 if before <= after, 1 otherwise
901 */
902static int x509_check_time( const x509_time *before, const x509_time *after )
903{
904 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 return( 1 );
906
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100907 if( before->year == after->year &&
908 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 return( 1 );
910
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100911 if( before->year == after->year &&
912 before->mon == after->mon &&
913 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 return( 1 );
915
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100916 if( before->year == after->year &&
917 before->mon == after->mon &&
918 before->day == after->day &&
919 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 return( 1 );
921
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100922 if( before->year == after->year &&
923 before->mon == after->mon &&
924 before->day == after->day &&
925 before->hour == after->hour &&
926 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 return( 1 );
928
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100929 if( before->year == after->year &&
930 before->mon == after->mon &&
931 before->day == after->day &&
932 before->hour == after->hour &&
933 before->min == after->min &&
934 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 return( 1 );
936
937 return( 0 );
938}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100939
940int x509_time_expired( const x509_time *to )
941{
942 x509_time now;
943
944 x509_get_current_time( &now );
945
946 return( x509_check_time( &now, to ) );
947}
948
949int x509_time_future( const x509_time *from )
950{
951 x509_time now;
952
953 x509_get_current_time( &now );
954
955 return( x509_check_time( from, &now ) );
956}
957
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100959
Paul Bakker86d0c192013-09-18 11:11:02 +0200960int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961{
962 ((void) to);
963 return( 0 );
964}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100965
966int x509_time_future( const x509_time *from )
967{
968 ((void) from);
969 return( 0 );
970}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971#endif /* POLARSSL_HAVE_TIME */
972
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200973#if defined(POLARSSL_SELF_TEST)
974
975#include "polarssl/x509_crt.h"
976#include "polarssl/certs.h"
977
978/*
979 * Checkup routine
980 */
981int x509_self_test( int verbose )
982{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +0200983#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200984 int ret;
985 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200986 x509_crt cacert;
987 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200988
989 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100990 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200991
Paul Bakkerb6b09562013-09-18 14:17:41 +0200992 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200993
Paul Bakkerddf26b42013-09-18 13:46:23 +0200994 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
995 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200996 if( ret != 0 )
997 {
998 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100999 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001000
1001 return( ret );
1002 }
1003
Paul Bakkerb6b09562013-09-18 14:17:41 +02001004 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001005
Paul Bakkerddf26b42013-09-18 13:46:23 +02001006 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
1007 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001008 if( ret != 0 )
1009 {
1010 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001011 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001012
1013 return( ret );
1014 }
1015
1016 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001017 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001018
Paul Bakkerddf26b42013-09-18 13:46:23 +02001019 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001020 if( ret != 0 )
1021 {
1022 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001023 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001024
Paul Bakker66d5d072014-06-17 16:39:18 +02001025 polarssl_printf( "ret = %d, &flags = %04x\n", ret, flags );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001026
1027 return( ret );
1028 }
1029
1030 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001031 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001032
1033 x509_crt_free( &cacert );
1034 x509_crt_free( &clicert );
1035
1036 return( 0 );
1037#else
1038 ((void) verbose);
1039 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001040#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001041}
1042
Paul Bakker9af723c2014-05-01 13:03:14 +02001043#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001044
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045#endif /* POLARSSL_X509_USE_C */