blob: 89ba7633dd598159a31917c762715ad212925b70 [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 *
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 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020028 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
29 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
30 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031 *
32 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
34 */
35
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#else
39#include POLARSSL_CONFIG_FILE
40#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
42#if defined(POLARSSL_X509_USE_C)
43
44#include "polarssl/x509.h"
45#include "polarssl/asn1.h"
46#include "polarssl/oid.h"
47#if defined(POLARSSL_PEM_PARSE_C)
48#include "polarssl/pem.h"
49#endif
50
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#if defined(POLARSSL_PLATFORM_C)
52#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010054#define polarssl_printf printf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
59#include <string.h>
60#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010061#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <windows.h>
63#else
64#include <time.h>
65#endif
66
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(EFIX64) || defined(EFI32)
68#include <stdio.h>
69#endif
70
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071#if defined(POLARSSL_FS_IO)
72#include <stdio.h>
73#if !defined(_WIN32)
74#include <sys/types.h>
75#include <sys/stat.h>
76#include <dirent.h>
77#endif
78#endif
79
80/*
81 * CertificateSerialNumber ::= INTEGER
82 */
83int x509_get_serial( unsigned char **p, const unsigned char *end,
84 x509_buf *serial )
85{
86 int ret;
87
88 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +020089 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090 POLARSSL_ERR_ASN1_OUT_OF_DATA );
91
92 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
93 **p != ASN1_INTEGER )
Paul Bakker51876562013-09-17 14:36:05 +020094 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
96
97 serial->tag = *(*p)++;
98
99 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200100 return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101
102 serial->p = *p;
103 *p += serial->len;
104
105 return( 0 );
106}
107
108/* Get an algorithm identifier without parameters (eg for signatures)
109 *
110 * AlgorithmIdentifier ::= SEQUENCE {
111 * algorithm OBJECT IDENTIFIER,
112 * parameters ANY DEFINED BY algorithm OPTIONAL }
113 */
114int x509_get_alg_null( unsigned char **p, const unsigned char *end,
115 x509_buf *alg )
116{
117 int ret;
118
119 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200120 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121
122 return( 0 );
123}
124
125/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100126 * Parse an algorithm identifier with (optional) paramaters
127 */
128int x509_get_alg( unsigned char **p, const unsigned char *end,
129 x509_buf *alg, x509_buf *params )
130{
131 int ret;
132
133 if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
134 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
135
136 return( 0 );
137}
138
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200139#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100140/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100141 * HashAlgorithm ::= AlgorithmIdentifier
142 *
143 * AlgorithmIdentifier ::= SEQUENCE {
144 * algorithm OBJECT IDENTIFIER,
145 * parameters ANY DEFINED BY algorithm OPTIONAL }
146 *
147 * For HashAlgorithm, parameters MUST be NULL or absent.
148 */
149static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
150{
151 int ret;
152 unsigned char *p;
153 const unsigned char *end;
154 x509_buf md_oid;
155 size_t len;
156
157 /* Make sure we got a SEQUENCE and setup bounds */
158 if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
159 return( POLARSSL_ERR_X509_INVALID_ALG +
160 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
161
162 p = (unsigned char *) alg->p;
163 end = p + alg->len;
164
165 if( p >= end )
166 return( POLARSSL_ERR_X509_INVALID_ALG +
167 POLARSSL_ERR_ASN1_OUT_OF_DATA );
168
169 /* Parse md_oid */
170 md_oid.tag = *p;
171
172 if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
173 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
174
175 md_oid.p = p;
176 p += md_oid.len;
177
178 /* Get md_alg from md_oid */
179 if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
180 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
181
182 /* Make sure params is absent of NULL */
183 if( p == end )
184 return( 0 );
185
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100186 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100187 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
188
189 if( p != end )
190 return( POLARSSL_ERR_X509_INVALID_ALG +
191 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
192
193 return( 0 );
194}
195
196/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100197 * RSASSA-PSS-params ::= SEQUENCE {
198 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
199 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
200 * saltLength [2] INTEGER DEFAULT 20,
201 * trailerField [3] INTEGER DEFAULT 1 }
202 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200203 *
204 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
205 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
206 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100207 */
208int x509_get_rsassa_pss_params( const x509_buf *params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100209 md_type_t *md_alg, md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200210 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100211{
212 int ret;
213 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100214 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100215 size_t len;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100216 x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100217
218 /* First set everything to defaults */
219 *md_alg = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100220 *mgf_md = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100221 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100222
223 /* Make sure params is a SEQUENCE and setup bounds */
224 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
225 return( POLARSSL_ERR_X509_INVALID_ALG +
226 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
227
228 p = (unsigned char *) params->p;
229 end = p + params->len;
230
231 if( p == end )
232 return( 0 );
233
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100234 /*
235 * HashAlgorithm
236 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100237 if( ( ret = asn1_get_tag( &p, end, &len,
238 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
239 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100240 end2 = p + len;
241
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100242 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100243 if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100244 return( ret );
245
246 if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
247 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100248
249 if( p != end2 )
250 return( POLARSSL_ERR_X509_INVALID_ALG +
251 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100252 }
253 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
254 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
255
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100256 if( p == end )
257 return( 0 );
258
259 /*
260 * MaskGenAlgorithm
261 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100262 if( ( ret = asn1_get_tag( &p, end, &len,
263 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
264 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100265 end2 = p + len;
266
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100267 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100268 if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100269 return( ret );
270
271 /* Only MFG1 is recognised for now */
272 if( ! OID_CMP( OID_MGF1, &alg_id ) )
273 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
274 POLARSSL_ERR_OID_NOT_FOUND );
275
276 /* Parse HashAlgorithm */
277 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
278 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100279
280 if( p != end2 )
281 return( POLARSSL_ERR_X509_INVALID_ALG +
282 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100283 }
284 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
285 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
286
287 if( p == end )
288 return( 0 );
289
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100290 /*
291 * salt_len
292 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100293 if( ( ret = asn1_get_tag( &p, end, &len,
294 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
295 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100296 end2 = p + len;
297
298 if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100299 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100300
301 if( p != end2 )
302 return( POLARSSL_ERR_X509_INVALID_ALG +
303 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100304 }
305 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
306 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
307
308 if( p == end )
309 return( 0 );
310
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100311 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200312 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100313 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100314 if( ( ret = asn1_get_tag( &p, end, &len,
315 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
316 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200317 int trailer_field;
318
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100319 end2 = p + len;
320
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200321 if( ( ret = asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100322 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100323
324 if( p != end2 )
325 return( POLARSSL_ERR_X509_INVALID_ALG +
326 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200327
328 if( trailer_field != 1 )
329 return( POLARSSL_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100330 }
331 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
332 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
333
334 if( p != end )
335 return( POLARSSL_ERR_X509_INVALID_ALG +
336 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
337
338 return( 0 );
339}
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200340#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100341
342/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343 * AttributeTypeAndValue ::= SEQUENCE {
344 * type AttributeType,
345 * value AttributeValue }
346 *
347 * AttributeType ::= OBJECT IDENTIFIER
348 *
349 * AttributeValue ::= ANY DEFINED BY AttributeType
350 */
351static int x509_get_attr_type_value( unsigned char **p,
352 const unsigned char *end,
353 x509_name *cur )
354{
355 int ret;
356 size_t len;
357 x509_buf *oid;
358 x509_buf *val;
359
360 if( ( ret = asn1_get_tag( p, end, &len,
361 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200362 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363
364 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200365 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 POLARSSL_ERR_ASN1_OUT_OF_DATA );
367
368 oid = &cur->oid;
369 oid->tag = **p;
370
371 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200372 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373
374 oid->p = *p;
375 *p += oid->len;
376
377 if( ( end - *p ) < 1 )
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_OUT_OF_DATA );
380
381 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
382 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
383 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200384 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
386
387 val = &cur->val;
388 val->tag = *(*p)++;
389
390 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200391 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392
393 val->p = *p;
394 *p += val->len;
395
396 cur->next = NULL;
397
398 return( 0 );
399}
400
401/*
402 * RelativeDistinguishedName ::=
403 * SET OF AttributeTypeAndValue
404 *
405 * AttributeTypeAndValue ::= SEQUENCE {
406 * type AttributeType,
407 * value AttributeValue }
408 *
409 * AttributeType ::= OBJECT IDENTIFIER
410 *
411 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200412 *
413 * We restrict RelativeDistinguishedName to be a set of 1 element. This is
414 * the most common case, and our x509_name structure currently can't handle
415 * more than that.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416 */
417int x509_get_name( unsigned char **p, const unsigned char *end,
418 x509_name *cur )
419{
420 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200421 size_t set_len;
422 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100424 /* don't use recursion, we'd risk stack overflow if not optimized */
425 while( 1 )
426 {
427 /*
428 * parse first SET, restricted to 1 element
429 */
430 if( ( ret = asn1_get_tag( p, end, &set_len,
431 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
432 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100434 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100436 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
437 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100439 if( *p != end_set )
440 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100442 /*
443 * continue until end of SEQUENCE is reached
444 */
445 if( *p == end )
446 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100448 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100450 if( cur->next == NULL )
451 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100453 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100455 cur = cur->next;
456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457}
458
459/*
460 * Time ::= CHOICE {
461 * utcTime UTCTime,
462 * generalTime GeneralizedTime }
463 */
464int x509_get_time( unsigned char **p, const unsigned char *end,
465 x509_time *time )
466{
467 int ret;
468 size_t len;
469 char date[64];
470 unsigned char tag;
471
472 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200473 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 POLARSSL_ERR_ASN1_OUT_OF_DATA );
475
476 tag = **p;
477
Paul Bakker66d5d072014-06-17 16:39:18 +0200478 if( tag == ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479 {
480 (*p)++;
481 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200482
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200484 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485
486 memset( date, 0, sizeof( date ) );
487 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
488 len : sizeof( date ) - 1 );
489
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200490 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 &time->year, &time->mon, &time->day,
492 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200493 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
495 time->year += 100 * ( time->year < 50 );
496 time->year += 1900;
497
498 *p += len;
499
500 return( 0 );
501 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200502 else if( tag == ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503 {
504 (*p)++;
505 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200506
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200508 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
510 memset( date, 0, sizeof( date ) );
511 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
512 len : sizeof( date ) - 1 );
513
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200514 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515 &time->year, &time->mon, &time->day,
516 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200517 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
519 *p += len;
520
521 return( 0 );
522 }
523 else
Paul Bakker51876562013-09-17 14:36:05 +0200524 return( POLARSSL_ERR_X509_INVALID_DATE +
525 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526}
527
528int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
529{
530 int ret;
531 size_t len;
532
533 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200534 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 POLARSSL_ERR_ASN1_OUT_OF_DATA );
536
537 sig->tag = **p;
538
539 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200540 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 sig->len = len;
543 sig->p = *p;
544
545 *p += len;
546
547 return( 0 );
548}
549
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100550/*
551 * Get signature algorithm from alg OID and optional parameters
552 */
553int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200554 md_type_t *md_alg, pk_type_t *pk_alg,
555 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100557 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200559 if( *sig_opts != NULL )
560 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
561
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100562 if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200563 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200565#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100566 if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
567 {
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200568 pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100569
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200570 pss_opts = polarssl_malloc( sizeof( pk_rsassa_pss_options ) );
571 if( pss_opts == NULL )
572 return( POLARSSL_ERR_X509_MALLOC_FAILED );
573
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100574 ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200575 md_alg,
576 &pss_opts->mgf1_hash_id,
577 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100578 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200579 {
580 polarssl_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100581 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200582 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100583
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200584 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100585 }
586 else
Paul Bakkerdb20c102014-06-17 14:34:44 +0200587#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100588 {
589 /* Make sure parameters are absent or NULL */
590 if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
591 sig_params->len != 0 )
592 return( POLARSSL_ERR_X509_INVALID_ALG );
593 }
594
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595 return( 0 );
596}
597
598/*
599 * X.509 Extensions (No parsing of extensions, pointer should
600 * be either manually updated or extensions should be parsed!
601 */
602int x509_get_ext( unsigned char **p, const unsigned char *end,
603 x509_buf *ext, int tag )
604{
605 int ret;
606 size_t len;
607
608 if( *p == end )
609 return( 0 );
610
611 ext->tag = **p;
612
613 if( ( ret = asn1_get_tag( p, end, &ext->len,
614 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
615 return( ret );
616
617 ext->p = *p;
618 end = *p + ext->len;
619
620 /*
621 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
622 *
623 * Extension ::= SEQUENCE {
624 * extnID OBJECT IDENTIFIER,
625 * critical BOOLEAN DEFAULT FALSE,
626 * extnValue OCTET STRING }
627 */
628 if( ( ret = asn1_get_tag( p, end, &len,
629 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200630 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631
632 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200633 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
635
636 return( 0 );
637}
638
639#if defined(POLARSSL_FS_IO)
640/*
641 * Load all data from a file into a given buffer.
642 */
643int x509_load_file( const char *path, unsigned char **buf, size_t *n )
644{
645 FILE *f;
646 long size;
647
648 if( ( f = fopen( path, "rb" ) ) == NULL )
649 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
650
651 fseek( f, 0, SEEK_END );
652 if( ( size = ftell( f ) ) == -1 )
653 {
654 fclose( f );
655 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
656 }
657 fseek( f, 0, SEEK_SET );
658
659 *n = (size_t) size;
660
661 if( *n + 1 == 0 ||
662 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
663 {
664 fclose( f );
665 return( POLARSSL_ERR_X509_MALLOC_FAILED );
666 }
667
668 if( fread( *buf, 1, *n, f ) != *n )
669 {
670 fclose( f );
671 polarssl_free( *buf );
672 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
673 }
674
675 fclose( f );
676
677 (*buf)[*n] = '\0';
678
679 return( 0 );
680}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681#endif /* POLARSSL_FS_IO */
682
Paul Bakker6edcd412013-10-29 15:22:54 +0100683#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
684 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685#include <stdarg.h>
686
687#if !defined vsnprintf
688#define vsnprintf _vsnprintf
689#endif // vsnprintf
690
691/*
692 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
693 * Result value is not size of buffer needed, but -1 if no fit is possible.
694 *
695 * This fuction tries to 'fix' this by at least suggesting enlarging the
696 * size by 20.
697 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200698static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699{
700 va_list ap;
701 int res = -1;
702
703 va_start( ap, format );
704
705 res = vsnprintf( str, size, format, ap );
706
707 va_end( ap );
708
709 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200710 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 return( (int) size + 20 );
712
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200713 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714}
715
716#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200717#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718
719#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
720
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200721#define SAFE_SNPRINTF() \
722{ \
723 if( ret == -1 ) \
724 return( -1 ); \
725 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200726 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200727 p[n - 1] = '\0'; \
728 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
729 } \
730 \
731 n -= (unsigned int) ret; \
732 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733}
734
735/*
736 * Store the name in printable form into buf; no more
737 * than size characters will be written
738 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200739int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740{
741 int ret;
742 size_t i, n;
743 unsigned char c;
744 const x509_name *name;
745 const char *short_name = NULL;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200746 char s[X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747
748 memset( s, 0, sizeof( s ) );
749
750 name = dn;
751 p = buf;
752 n = size;
753
754 while( name != NULL )
755 {
756 if( !name->oid.p )
757 {
758 name = name->next;
759 continue;
760 }
761
762 if( name != dn )
763 {
764 ret = snprintf( p, n, ", " );
765 SAFE_SNPRINTF();
766 }
767
768 ret = oid_get_attr_short_name( &name->oid, &short_name );
769
770 if( ret == 0 )
771 ret = snprintf( p, n, "%s=", short_name );
772 else
773 ret = snprintf( p, n, "\?\?=" );
774 SAFE_SNPRINTF();
775
776 for( i = 0; i < name->val.len; i++ )
777 {
778 if( i >= sizeof( s ) - 1 )
779 break;
780
781 c = name->val.p[i];
782 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
783 s[i] = '?';
784 else s[i] = c;
785 }
786 s[i] = '\0';
787 ret = snprintf( p, n, "%s", s );
788 SAFE_SNPRINTF();
789 name = name->next;
790 }
791
792 return( (int) ( size - n ) );
793}
794
795/*
796 * Store the serial in printable form into buf; no more
797 * than size characters will be written
798 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200799int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800{
801 int ret;
802 size_t i, n, nr;
803 char *p;
804
805 p = buf;
806 n = size;
807
808 nr = ( serial->len <= 32 )
809 ? serial->len : 28;
810
811 for( i = 0; i < nr; i++ )
812 {
813 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
814 continue;
815
816 ret = snprintf( p, n, "%02X%s",
817 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
818 SAFE_SNPRINTF();
819 }
820
821 if( nr != serial->len )
822 {
823 ret = snprintf( p, n, "...." );
824 SAFE_SNPRINTF();
825 }
826
827 return( (int) ( size - n ) );
828}
829
830/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200831 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100832 */
833int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200834 pk_type_t pk_alg, md_type_t md_alg,
835 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100836{
837 int ret;
838 char *p = buf;
839 size_t n = size;
840 const char *desc = NULL;
841
842 ret = oid_get_sig_alg_desc( sig_oid, &desc );
843 if( ret != 0 )
844 ret = snprintf( p, n, "???" );
845 else
846 ret = snprintf( p, n, "%s", desc );
847 SAFE_SNPRINTF();
848
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200849#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100850 if( pk_alg == POLARSSL_PK_RSASSA_PSS )
851 {
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200852 const pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100853 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100854
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200855 pss_opts = (const pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100856
857 md_info = md_info_from_type( md_alg );
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200858 mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100859
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200860 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100861 md_info ? md_info->name : "???",
862 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200863 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100864 SAFE_SNPRINTF();
865 }
866#else
867 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200868 ((void) md_alg);
869 ((void) sig_opts);
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200870#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100871
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200872 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100873}
874
875/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876 * Helper for writing "RSA key size", "EC key size", etc
877 */
878int x509_key_size_helper( char *buf, size_t size, const char *name )
879{
880 char *p = buf;
881 size_t n = size;
882 int ret;
883
884 if( strlen( name ) + sizeof( " key size" ) > size )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200885 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886
887 ret = snprintf( p, n, "%s key size", name );
888 SAFE_SNPRINTF();
889
890 return( 0 );
891}
892
893/*
894 * Return an informational string describing the given OID
895 */
896const char *x509_oid_get_description( x509_buf *oid )
897{
898 const char *desc = NULL;
899 int ret;
900
901 ret = oid_get_extended_key_usage( oid, &desc );
902
903 if( ret != 0 )
904 return( NULL );
905
906 return( desc );
907}
908
909/* Return the x.y.z.... style numeric string for the given OID */
910int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
911{
912 return oid_get_numeric_string( buf, size, oid );
913}
914
915/*
916 * Return 0 if the x509_time is still valid, or 1 otherwise.
917 */
918#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100920static void x509_get_current_time( x509_time *now )
921{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100922#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923 SYSTEMTIME st;
924
Paul Bakker66d5d072014-06-17 16:39:18 +0200925 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100927 now->year = st.wYear;
928 now->mon = st.wMonth;
929 now->day = st.wDay;
930 now->hour = st.wHour;
931 now->min = st.wMinute;
932 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100934 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 time_t tt;
936
937 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200938 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
Paul Bakker5fff23b2014-03-26 15:34:54 +0100940 now->year = lt.tm_year + 1900;
941 now->mon = lt.tm_mon + 1;
942 now->day = lt.tm_mday;
943 now->hour = lt.tm_hour;
944 now->min = lt.tm_min;
945 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200946#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100947}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100949/*
950 * Return 0 if before <= after, 1 otherwise
951 */
952static int x509_check_time( const x509_time *before, const x509_time *after )
953{
954 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 return( 1 );
956
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100957 if( before->year == after->year &&
958 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 return( 1 );
960
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100961 if( before->year == after->year &&
962 before->mon == after->mon &&
963 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 return( 1 );
965
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100966 if( before->year == after->year &&
967 before->mon == after->mon &&
968 before->day == after->day &&
969 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 return( 1 );
971
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100972 if( before->year == after->year &&
973 before->mon == after->mon &&
974 before->day == after->day &&
975 before->hour == after->hour &&
976 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 return( 1 );
978
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100979 if( before->year == after->year &&
980 before->mon == after->mon &&
981 before->day == after->day &&
982 before->hour == after->hour &&
983 before->min == after->min &&
984 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 return( 1 );
986
987 return( 0 );
988}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100989
990int x509_time_expired( const x509_time *to )
991{
992 x509_time now;
993
994 x509_get_current_time( &now );
995
996 return( x509_check_time( &now, to ) );
997}
998
999int x509_time_future( const x509_time *from )
1000{
1001 x509_time now;
1002
1003 x509_get_current_time( &now );
1004
1005 return( x509_check_time( from, &now ) );
1006}
1007
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001009
Paul Bakker86d0c192013-09-18 11:11:02 +02001010int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011{
1012 ((void) to);
1013 return( 0 );
1014}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001015
1016int x509_time_future( const x509_time *from )
1017{
1018 ((void) from);
1019 return( 0 );
1020}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021#endif /* POLARSSL_HAVE_TIME */
1022
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001023#if defined(POLARSSL_SELF_TEST)
1024
1025#include "polarssl/x509_crt.h"
1026#include "polarssl/certs.h"
1027
1028/*
1029 * Checkup routine
1030 */
1031int x509_self_test( int verbose )
1032{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +02001033#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001034 int ret;
1035 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001036 x509_crt cacert;
1037 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001038
1039 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001040 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001041
Paul Bakkerb6b09562013-09-18 14:17:41 +02001042 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001043
Paul Bakkerddf26b42013-09-18 13:46:23 +02001044 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
1045 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001046 if( ret != 0 )
1047 {
1048 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001049 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001050
1051 return( ret );
1052 }
1053
Paul Bakkerb6b09562013-09-18 14:17:41 +02001054 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001055
Paul Bakkerddf26b42013-09-18 13:46:23 +02001056 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
1057 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001058 if( ret != 0 )
1059 {
1060 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001061 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001062
1063 return( ret );
1064 }
1065
1066 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001067 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001068
Paul Bakkerddf26b42013-09-18 13:46:23 +02001069 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001070 if( ret != 0 )
1071 {
1072 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001073 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001074
Paul Bakker66d5d072014-06-17 16:39:18 +02001075 polarssl_printf( "ret = %d, &flags = %04x\n", ret, flags );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001076
1077 return( ret );
1078 }
1079
1080 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001081 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001082
1083 x509_crt_free( &cacert );
1084 x509_crt_free( &clicert );
1085
1086 return( 0 );
1087#else
1088 ((void) verbose);
1089 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001090#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001091}
1092
Paul Bakker9af723c2014-05-01 13:03:14 +02001093#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001094
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095#endif /* POLARSSL_X509_USE_C */