blob: 4946ce6baa34d3a0e639ebca7c790cc028add0b7 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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"
Rich Evans00ab4702015-02-06 13:43:58 +000044
Rich Evans36796df2015-02-12 18:27:14 +000045#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
47
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#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
Rich Evans00ab4702015-02-06 13:43:58 +000055#include <stdio.h>
56#include <stdlib.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010057#define polarssl_printf printf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#define polarssl_malloc malloc
59#define polarssl_free free
60#endif
61
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
68#if defined(POLARSSL_FS_IO)
Rich Evans36796df2015-02-12 18:27:14 +000069#include <stdio.h>
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#if !defined(_WIN32)
71#include <sys/types.h>
72#include <sys/stat.h>
73#include <dirent.h>
74#endif
75#endif
76
77/*
78 * CertificateSerialNumber ::= INTEGER
79 */
80int x509_get_serial( unsigned char **p, const unsigned char *end,
81 x509_buf *serial )
82{
83 int ret;
84
85 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +020086 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087 POLARSSL_ERR_ASN1_OUT_OF_DATA );
88
89 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
90 **p != ASN1_INTEGER )
Paul Bakker51876562013-09-17 14:36:05 +020091 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
93
94 serial->tag = *(*p)++;
95
96 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +020097 return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
99 serial->p = *p;
100 *p += serial->len;
101
102 return( 0 );
103}
104
105/* Get an algorithm identifier without parameters (eg for signatures)
106 *
107 * AlgorithmIdentifier ::= SEQUENCE {
108 * algorithm OBJECT IDENTIFIER,
109 * parameters ANY DEFINED BY algorithm OPTIONAL }
110 */
111int x509_get_alg_null( unsigned char **p, const unsigned char *end,
112 x509_buf *alg )
113{
114 int ret;
115
116 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200117 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118
119 return( 0 );
120}
121
122/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100123 * Parse an algorithm identifier with (optional) paramaters
124 */
125int x509_get_alg( unsigned char **p, const unsigned char *end,
126 x509_buf *alg, x509_buf *params )
127{
128 int ret;
129
130 if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
131 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
132
133 return( 0 );
134}
135
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200136#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100137/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100138 * HashAlgorithm ::= AlgorithmIdentifier
139 *
140 * AlgorithmIdentifier ::= SEQUENCE {
141 * algorithm OBJECT IDENTIFIER,
142 * parameters ANY DEFINED BY algorithm OPTIONAL }
143 *
144 * For HashAlgorithm, parameters MUST be NULL or absent.
145 */
146static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
147{
148 int ret;
149 unsigned char *p;
150 const unsigned char *end;
151 x509_buf md_oid;
152 size_t len;
153
154 /* Make sure we got a SEQUENCE and setup bounds */
155 if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
156 return( POLARSSL_ERR_X509_INVALID_ALG +
157 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
158
159 p = (unsigned char *) alg->p;
160 end = p + alg->len;
161
162 if( p >= end )
163 return( POLARSSL_ERR_X509_INVALID_ALG +
164 POLARSSL_ERR_ASN1_OUT_OF_DATA );
165
166 /* Parse md_oid */
167 md_oid.tag = *p;
168
169 if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
170 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
171
172 md_oid.p = p;
173 p += md_oid.len;
174
175 /* Get md_alg from md_oid */
176 if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
177 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
178
179 /* Make sure params is absent of NULL */
180 if( p == end )
181 return( 0 );
182
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100183 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100184 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
185
186 if( p != end )
187 return( POLARSSL_ERR_X509_INVALID_ALG +
188 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
189
190 return( 0 );
191}
192
193/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100194 * RSASSA-PSS-params ::= SEQUENCE {
195 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
196 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
197 * saltLength [2] INTEGER DEFAULT 20,
198 * trailerField [3] INTEGER DEFAULT 1 }
199 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200200 *
201 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
202 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
203 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100204 */
205int x509_get_rsassa_pss_params( const x509_buf *params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100206 md_type_t *md_alg, md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200207 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100208{
209 int ret;
210 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100211 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100212 size_t len;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100213 x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100214
215 /* First set everything to defaults */
216 *md_alg = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100217 *mgf_md = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100218 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100219
220 /* Make sure params is a SEQUENCE and setup bounds */
221 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
222 return( POLARSSL_ERR_X509_INVALID_ALG +
223 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
224
225 p = (unsigned char *) params->p;
226 end = p + params->len;
227
228 if( p == end )
229 return( 0 );
230
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100231 /*
232 * HashAlgorithm
233 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100234 if( ( ret = asn1_get_tag( &p, end, &len,
235 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
236 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100237 end2 = p + len;
238
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100239 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100240 if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100241 return( ret );
242
243 if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
244 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100245
246 if( p != end2 )
247 return( POLARSSL_ERR_X509_INVALID_ALG +
248 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100249 }
250 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
251 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
252
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100253 if( p == end )
254 return( 0 );
255
256 /*
257 * MaskGenAlgorithm
258 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100259 if( ( ret = asn1_get_tag( &p, end, &len,
260 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
261 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100262 end2 = p + len;
263
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100264 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100265 if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100266 return( ret );
267
268 /* Only MFG1 is recognised for now */
269 if( ! OID_CMP( OID_MGF1, &alg_id ) )
270 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
271 POLARSSL_ERR_OID_NOT_FOUND );
272
273 /* Parse HashAlgorithm */
274 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
275 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100276
277 if( p != end2 )
278 return( POLARSSL_ERR_X509_INVALID_ALG +
279 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100280 }
281 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
282 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
283
284 if( p == end )
285 return( 0 );
286
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100287 /*
288 * salt_len
289 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100290 if( ( ret = asn1_get_tag( &p, end, &len,
291 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
292 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100293 end2 = p + len;
294
295 if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100296 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100297
298 if( p != end2 )
299 return( POLARSSL_ERR_X509_INVALID_ALG +
300 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100301 }
302 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
303 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
304
305 if( p == end )
306 return( 0 );
307
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100308 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200309 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100310 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100311 if( ( ret = asn1_get_tag( &p, end, &len,
312 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
313 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200314 int trailer_field;
315
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100316 end2 = p + len;
317
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200318 if( ( ret = asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100319 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100320
321 if( p != end2 )
322 return( POLARSSL_ERR_X509_INVALID_ALG +
323 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200324
325 if( trailer_field != 1 )
326 return( POLARSSL_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100327 }
328 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
329 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
330
331 if( p != end )
332 return( POLARSSL_ERR_X509_INVALID_ALG +
333 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
334
335 return( 0 );
336}
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200337#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100338
339/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340 * AttributeTypeAndValue ::= SEQUENCE {
341 * type AttributeType,
342 * value AttributeValue }
343 *
344 * AttributeType ::= OBJECT IDENTIFIER
345 *
346 * AttributeValue ::= ANY DEFINED BY AttributeType
347 */
348static int x509_get_attr_type_value( unsigned char **p,
349 const unsigned char *end,
350 x509_name *cur )
351{
352 int ret;
353 size_t len;
354 x509_buf *oid;
355 x509_buf *val;
356
357 if( ( ret = asn1_get_tag( p, end, &len,
358 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200359 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200362 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 POLARSSL_ERR_ASN1_OUT_OF_DATA );
364
365 oid = &cur->oid;
366 oid->tag = **p;
367
368 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200369 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370
371 oid->p = *p;
372 *p += oid->len;
373
374 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200375 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376 POLARSSL_ERR_ASN1_OUT_OF_DATA );
377
378 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
379 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
380 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200381 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
383
384 val = &cur->val;
385 val->tag = *(*p)++;
386
387 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200388 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389
390 val->p = *p;
391 *p += val->len;
392
393 cur->next = NULL;
394
395 return( 0 );
396}
397
398/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000399 * Name ::= CHOICE { -- only one possibility for now --
400 * rdnSequence RDNSequence }
401 *
402 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
403 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 * RelativeDistinguishedName ::=
405 * SET OF AttributeTypeAndValue
406 *
407 * AttributeTypeAndValue ::= SEQUENCE {
408 * type AttributeType,
409 * value AttributeValue }
410 *
411 * AttributeType ::= OBJECT IDENTIFIER
412 *
413 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200414 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000415 * The data structure is optimized for the common case where each RDN has only
416 * one element, which is represented as a list of AttributeTypeAndValue.
417 * For the general case we still use a flat list, but we mark elements of the
418 * same set so that they are "merged" together in the functions that consume
419 * this list, eg x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420 */
421int x509_get_name( unsigned char **p, const unsigned char *end,
422 x509_name *cur )
423{
424 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200425 size_t set_len;
426 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100428 /* don't use recursion, we'd risk stack overflow if not optimized */
429 while( 1 )
430 {
431 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000432 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100433 */
434 if( ( ret = asn1_get_tag( p, end, &set_len,
435 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
436 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100438 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000440 while( 1 )
441 {
442 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
443 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000445 if( *p == end_set )
446 break;
447
448 /* Mark this item as being only one in a set */
449 cur->next_merged = 1;
450
451 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
452
453 if( cur->next == NULL )
454 return( POLARSSL_ERR_X509_MALLOC_FAILED );
455
456 memset( cur->next, 0, sizeof( x509_name ) );
457
458 cur = cur->next;
459 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100461 /*
462 * continue until end of SEQUENCE is reached
463 */
464 if( *p == end )
465 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100467 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100469 if( cur->next == NULL )
470 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100472 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100474 cur = cur->next;
475 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476}
477
478/*
479 * Time ::= CHOICE {
480 * utcTime UTCTime,
481 * generalTime GeneralizedTime }
482 */
483int x509_get_time( unsigned char **p, const unsigned char *end,
484 x509_time *time )
485{
486 int ret;
487 size_t len;
488 char date[64];
489 unsigned char tag;
490
491 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200492 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 POLARSSL_ERR_ASN1_OUT_OF_DATA );
494
495 tag = **p;
496
Paul Bakker66d5d072014-06-17 16:39:18 +0200497 if( tag == ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 {
499 (*p)++;
500 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200501
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200503 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
505 memset( date, 0, sizeof( date ) );
506 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
507 len : sizeof( date ) - 1 );
508
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200509 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 &time->year, &time->mon, &time->day,
511 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200512 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
514 time->year += 100 * ( time->year < 50 );
515 time->year += 1900;
516
517 *p += len;
518
519 return( 0 );
520 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200521 else if( tag == ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 {
523 (*p)++;
524 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200525
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200527 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
529 memset( date, 0, sizeof( date ) );
530 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
531 len : sizeof( date ) - 1 );
532
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200533 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 &time->year, &time->mon, &time->day,
535 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200536 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
538 *p += len;
539
540 return( 0 );
541 }
542 else
Paul Bakker51876562013-09-17 14:36:05 +0200543 return( POLARSSL_ERR_X509_INVALID_DATE +
544 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545}
546
547int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
548{
549 int ret;
550 size_t len;
551
552 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200553 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554 POLARSSL_ERR_ASN1_OUT_OF_DATA );
555
556 sig->tag = **p;
557
558 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200559 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
561 sig->len = len;
562 sig->p = *p;
563
564 *p += len;
565
566 return( 0 );
567}
568
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100569/*
570 * Get signature algorithm from alg OID and optional parameters
571 */
572int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200573 md_type_t *md_alg, pk_type_t *pk_alg,
574 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100576 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200578 if( *sig_opts != NULL )
579 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
580
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100581 if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200582 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200584#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100585 if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
586 {
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200587 pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100588
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200589 pss_opts = polarssl_malloc( sizeof( pk_rsassa_pss_options ) );
590 if( pss_opts == NULL )
591 return( POLARSSL_ERR_X509_MALLOC_FAILED );
592
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100593 ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200594 md_alg,
595 &pss_opts->mgf1_hash_id,
596 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100597 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200598 {
599 polarssl_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100600 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200601 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100602
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200603 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100604 }
605 else
Paul Bakkerdb20c102014-06-17 14:34:44 +0200606#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100607 {
608 /* Make sure parameters are absent or NULL */
609 if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
610 sig_params->len != 0 )
611 return( POLARSSL_ERR_X509_INVALID_ALG );
612 }
613
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614 return( 0 );
615}
616
617/*
618 * X.509 Extensions (No parsing of extensions, pointer should
619 * be either manually updated or extensions should be parsed!
620 */
621int x509_get_ext( unsigned char **p, const unsigned char *end,
622 x509_buf *ext, int tag )
623{
624 int ret;
625 size_t len;
626
627 if( *p == end )
628 return( 0 );
629
630 ext->tag = **p;
631
632 if( ( ret = asn1_get_tag( p, end, &ext->len,
633 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
634 return( ret );
635
636 ext->p = *p;
637 end = *p + ext->len;
638
639 /*
640 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
641 *
642 * Extension ::= SEQUENCE {
643 * extnID OBJECT IDENTIFIER,
644 * critical BOOLEAN DEFAULT FALSE,
645 * extnValue OCTET STRING }
646 */
647 if( ( ret = asn1_get_tag( p, end, &len,
648 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200649 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
651 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200652 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
654
655 return( 0 );
656}
657
Paul Bakker6edcd412013-10-29 15:22:54 +0100658#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
659 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660#include <stdarg.h>
661
662#if !defined vsnprintf
663#define vsnprintf _vsnprintf
664#endif // vsnprintf
665
666/*
667 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
668 * Result value is not size of buffer needed, but -1 if no fit is possible.
669 *
670 * This fuction tries to 'fix' this by at least suggesting enlarging the
671 * size by 20.
672 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200673static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674{
675 va_list ap;
676 int res = -1;
677
678 va_start( ap, format );
679
680 res = vsnprintf( str, size, format, ap );
681
682 va_end( ap );
683
684 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200685 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686 return( (int) size + 20 );
687
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200688 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689}
690
691#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200692#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
694#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
695
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200696#define SAFE_SNPRINTF() \
697{ \
698 if( ret == -1 ) \
699 return( -1 ); \
700 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200701 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200702 p[n - 1] = '\0'; \
703 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
704 } \
705 \
706 n -= (unsigned int) ret; \
707 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708}
709
710/*
711 * Store the name in printable form into buf; no more
712 * than size characters will be written
713 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200714int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715{
716 int ret;
717 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000718 unsigned char c, merge = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719 const x509_name *name;
720 const char *short_name = NULL;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200721 char s[X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722
723 memset( s, 0, sizeof( s ) );
724
725 name = dn;
726 p = buf;
727 n = size;
728
729 while( name != NULL )
730 {
731 if( !name->oid.p )
732 {
733 name = name->next;
734 continue;
735 }
736
737 if( name != dn )
738 {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000739 ret = snprintf( p, n, merge ? " + " : ", " );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 SAFE_SNPRINTF();
741 }
742
743 ret = oid_get_attr_short_name( &name->oid, &short_name );
744
745 if( ret == 0 )
746 ret = snprintf( p, n, "%s=", short_name );
747 else
748 ret = snprintf( p, n, "\?\?=" );
749 SAFE_SNPRINTF();
750
751 for( i = 0; i < name->val.len; i++ )
752 {
753 if( i >= sizeof( s ) - 1 )
754 break;
755
756 c = name->val.p[i];
757 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
758 s[i] = '?';
759 else s[i] = c;
760 }
761 s[i] = '\0';
762 ret = snprintf( p, n, "%s", s );
763 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000764
765 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 name = name->next;
767 }
768
769 return( (int) ( size - n ) );
770}
771
772/*
773 * Store the serial in printable form into buf; no more
774 * than size characters will be written
775 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200776int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777{
778 int ret;
779 size_t i, n, nr;
780 char *p;
781
782 p = buf;
783 n = size;
784
785 nr = ( serial->len <= 32 )
786 ? serial->len : 28;
787
788 for( i = 0; i < nr; i++ )
789 {
790 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
791 continue;
792
793 ret = snprintf( p, n, "%02X%s",
794 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
795 SAFE_SNPRINTF();
796 }
797
798 if( nr != serial->len )
799 {
800 ret = snprintf( p, n, "...." );
801 SAFE_SNPRINTF();
802 }
803
804 return( (int) ( size - n ) );
805}
806
807/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200808 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100809 */
810int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200811 pk_type_t pk_alg, md_type_t md_alg,
812 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100813{
814 int ret;
815 char *p = buf;
816 size_t n = size;
817 const char *desc = NULL;
818
819 ret = oid_get_sig_alg_desc( sig_oid, &desc );
820 if( ret != 0 )
821 ret = snprintf( p, n, "???" );
822 else
823 ret = snprintf( p, n, "%s", desc );
824 SAFE_SNPRINTF();
825
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200826#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100827 if( pk_alg == POLARSSL_PK_RSASSA_PSS )
828 {
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200829 const pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100830 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100831
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200832 pss_opts = (const pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100833
834 md_info = md_info_from_type( md_alg );
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200835 mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100836
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200837 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100838 md_info ? md_info->name : "???",
839 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200840 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100841 SAFE_SNPRINTF();
842 }
843#else
844 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200845 ((void) md_alg);
846 ((void) sig_opts);
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200847#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100848
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200849 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100850}
851
852/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853 * Helper for writing "RSA key size", "EC key size", etc
854 */
855int x509_key_size_helper( char *buf, size_t size, const char *name )
856{
857 char *p = buf;
858 size_t n = size;
859 int ret;
860
861 if( strlen( name ) + sizeof( " key size" ) > size )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200862 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863
864 ret = snprintf( p, n, "%s key size", name );
865 SAFE_SNPRINTF();
866
867 return( 0 );
868}
869
870/*
871 * Return an informational string describing the given OID
872 */
873const char *x509_oid_get_description( x509_buf *oid )
874{
875 const char *desc = NULL;
876 int ret;
877
878 ret = oid_get_extended_key_usage( oid, &desc );
879
880 if( ret != 0 )
881 return( NULL );
882
883 return( desc );
884}
885
886/* Return the x.y.z.... style numeric string for the given OID */
887int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
888{
889 return oid_get_numeric_string( buf, size, oid );
890}
891
892/*
893 * Return 0 if the x509_time is still valid, or 1 otherwise.
894 */
895#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100897static void x509_get_current_time( x509_time *now )
898{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100899#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900 SYSTEMTIME st;
901
Paul Bakker66d5d072014-06-17 16:39:18 +0200902 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100904 now->year = st.wYear;
905 now->mon = st.wMonth;
906 now->day = st.wDay;
907 now->hour = st.wHour;
908 now->min = st.wMinute;
909 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100911 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 time_t tt;
913
914 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200915 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916
Paul Bakker5fff23b2014-03-26 15:34:54 +0100917 now->year = lt.tm_year + 1900;
918 now->mon = lt.tm_mon + 1;
919 now->day = lt.tm_mday;
920 now->hour = lt.tm_hour;
921 now->min = lt.tm_min;
922 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200923#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100924}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100926/*
927 * Return 0 if before <= after, 1 otherwise
928 */
929static int x509_check_time( const x509_time *before, const x509_time *after )
930{
931 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932 return( 1 );
933
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100934 if( before->year == after->year &&
935 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936 return( 1 );
937
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100938 if( before->year == after->year &&
939 before->mon == after->mon &&
940 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941 return( 1 );
942
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100943 if( before->year == after->year &&
944 before->mon == after->mon &&
945 before->day == after->day &&
946 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 return( 1 );
948
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100949 if( before->year == after->year &&
950 before->mon == after->mon &&
951 before->day == after->day &&
952 before->hour == after->hour &&
953 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954 return( 1 );
955
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100956 if( before->year == after->year &&
957 before->mon == after->mon &&
958 before->day == after->day &&
959 before->hour == after->hour &&
960 before->min == after->min &&
961 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 return( 1 );
963
964 return( 0 );
965}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100966
967int x509_time_expired( const x509_time *to )
968{
969 x509_time now;
970
971 x509_get_current_time( &now );
972
973 return( x509_check_time( &now, to ) );
974}
975
976int x509_time_future( const x509_time *from )
977{
978 x509_time now;
979
980 x509_get_current_time( &now );
981
982 return( x509_check_time( from, &now ) );
983}
984
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100986
Paul Bakker86d0c192013-09-18 11:11:02 +0200987int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988{
989 ((void) to);
990 return( 0 );
991}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100992
993int x509_time_future( const x509_time *from )
994{
995 ((void) from);
996 return( 0 );
997}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998#endif /* POLARSSL_HAVE_TIME */
999
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001000#if defined(POLARSSL_SELF_TEST)
1001
1002#include "polarssl/x509_crt.h"
1003#include "polarssl/certs.h"
1004
1005/*
1006 * Checkup routine
1007 */
1008int x509_self_test( int verbose )
1009{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +02001010#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001011 int ret;
1012 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001013 x509_crt cacert;
1014 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001015
1016 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001017 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001018
Paul Bakkerb6b09562013-09-18 14:17:41 +02001019 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001020
Paul Bakkerddf26b42013-09-18 13:46:23 +02001021 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
1022 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001023 if( ret != 0 )
1024 {
1025 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001026 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001027
1028 return( ret );
1029 }
1030
Paul Bakkerb6b09562013-09-18 14:17:41 +02001031 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001032
Paul Bakkerddf26b42013-09-18 13:46:23 +02001033 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
1034 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001035 if( ret != 0 )
1036 {
1037 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001038 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001039
1040 return( ret );
1041 }
1042
1043 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001044 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001045
Paul Bakkerddf26b42013-09-18 13:46:23 +02001046 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001047 if( ret != 0 )
1048 {
1049 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001050 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001051
Paul Bakker66d5d072014-06-17 16:39:18 +02001052 polarssl_printf( "ret = %d, &flags = %04x\n", ret, flags );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001053
1054 return( ret );
1055 }
1056
1057 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001058 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001059
1060 x509_crt_free( &cacert );
1061 x509_crt_free( &clicert );
1062
1063 return( 0 );
1064#else
1065 ((void) verbose);
1066 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001067#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001068}
1069
Paul Bakker9af723c2014-05-01 13:03:14 +02001070#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001071
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072#endif /* POLARSSL_X509_USE_C */