blob: c9b196f46095f0e66ec20a5b6fb9c25e20b5a07c [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
45#include <string.h>
46
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047#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
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <stdio.h>
55#include <stdlib.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010056#define polarssl_printf printf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#define polarssl_malloc malloc
58#define polarssl_free free
59#endif
60
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
67#if defined(POLARSSL_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#if !defined(_WIN32)
69#include <sys/types.h>
70#include <sys/stat.h>
71#include <dirent.h>
72#endif
73#endif
74
75/*
76 * CertificateSerialNumber ::= INTEGER
77 */
78int x509_get_serial( unsigned char **p, const unsigned char *end,
79 x509_buf *serial )
80{
81 int ret;
82
83 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +020084 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085 POLARSSL_ERR_ASN1_OUT_OF_DATA );
86
87 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
88 **p != ASN1_INTEGER )
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_UNEXPECTED_TAG );
91
92 serial->tag = *(*p)++;
93
94 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +020095 return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096
97 serial->p = *p;
98 *p += serial->len;
99
100 return( 0 );
101}
102
103/* Get an algorithm identifier without parameters (eg for signatures)
104 *
105 * AlgorithmIdentifier ::= SEQUENCE {
106 * algorithm OBJECT IDENTIFIER,
107 * parameters ANY DEFINED BY algorithm OPTIONAL }
108 */
109int x509_get_alg_null( unsigned char **p, const unsigned char *end,
110 x509_buf *alg )
111{
112 int ret;
113
114 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200115 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116
117 return( 0 );
118}
119
120/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100121 * Parse an algorithm identifier with (optional) paramaters
122 */
123int x509_get_alg( unsigned char **p, const unsigned char *end,
124 x509_buf *alg, x509_buf *params )
125{
126 int ret;
127
128 if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
129 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
130
131 return( 0 );
132}
133
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200134#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100135/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100136 * HashAlgorithm ::= AlgorithmIdentifier
137 *
138 * AlgorithmIdentifier ::= SEQUENCE {
139 * algorithm OBJECT IDENTIFIER,
140 * parameters ANY DEFINED BY algorithm OPTIONAL }
141 *
142 * For HashAlgorithm, parameters MUST be NULL or absent.
143 */
144static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
145{
146 int ret;
147 unsigned char *p;
148 const unsigned char *end;
149 x509_buf md_oid;
150 size_t len;
151
152 /* Make sure we got a SEQUENCE and setup bounds */
153 if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
154 return( POLARSSL_ERR_X509_INVALID_ALG +
155 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
156
157 p = (unsigned char *) alg->p;
158 end = p + alg->len;
159
160 if( p >= end )
161 return( POLARSSL_ERR_X509_INVALID_ALG +
162 POLARSSL_ERR_ASN1_OUT_OF_DATA );
163
164 /* Parse md_oid */
165 md_oid.tag = *p;
166
167 if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
168 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
169
170 md_oid.p = p;
171 p += md_oid.len;
172
173 /* Get md_alg from md_oid */
174 if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
175 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
176
177 /* Make sure params is absent of NULL */
178 if( p == end )
179 return( 0 );
180
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100181 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100182 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
183
184 if( p != end )
185 return( POLARSSL_ERR_X509_INVALID_ALG +
186 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
187
188 return( 0 );
189}
190
191/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100192 * RSASSA-PSS-params ::= SEQUENCE {
193 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
194 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
195 * saltLength [2] INTEGER DEFAULT 20,
196 * trailerField [3] INTEGER DEFAULT 1 }
197 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200198 *
199 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
200 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
201 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100202 */
203int x509_get_rsassa_pss_params( const x509_buf *params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100204 md_type_t *md_alg, md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200205 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100206{
207 int ret;
208 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100209 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100210 size_t len;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100211 x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100212
213 /* First set everything to defaults */
214 *md_alg = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100215 *mgf_md = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100216 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100217
218 /* Make sure params is a SEQUENCE and setup bounds */
219 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
220 return( POLARSSL_ERR_X509_INVALID_ALG +
221 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
222
223 p = (unsigned char *) params->p;
224 end = p + params->len;
225
226 if( p == end )
227 return( 0 );
228
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100229 /*
230 * HashAlgorithm
231 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100232 if( ( ret = asn1_get_tag( &p, end, &len,
233 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
234 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100235 end2 = p + len;
236
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100237 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100238 if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100239 return( ret );
240
241 if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
242 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100243
244 if( p != end2 )
245 return( POLARSSL_ERR_X509_INVALID_ALG +
246 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100247 }
248 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
249 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
250
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100251 if( p == end )
252 return( 0 );
253
254 /*
255 * MaskGenAlgorithm
256 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100257 if( ( ret = asn1_get_tag( &p, end, &len,
258 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
259 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100260 end2 = p + len;
261
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100262 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100263 if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100264 return( ret );
265
266 /* Only MFG1 is recognised for now */
267 if( ! OID_CMP( OID_MGF1, &alg_id ) )
268 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
269 POLARSSL_ERR_OID_NOT_FOUND );
270
271 /* Parse HashAlgorithm */
272 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
273 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100274
275 if( p != end2 )
276 return( POLARSSL_ERR_X509_INVALID_ALG +
277 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100278 }
279 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
280 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
281
282 if( p == end )
283 return( 0 );
284
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100285 /*
286 * salt_len
287 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100288 if( ( ret = asn1_get_tag( &p, end, &len,
289 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
290 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100291 end2 = p + len;
292
293 if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100294 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100295
296 if( p != end2 )
297 return( POLARSSL_ERR_X509_INVALID_ALG +
298 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100299 }
300 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
301 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
302
303 if( p == end )
304 return( 0 );
305
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100306 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200307 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100308 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100309 if( ( ret = asn1_get_tag( &p, end, &len,
310 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
311 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200312 int trailer_field;
313
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100314 end2 = p + len;
315
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200316 if( ( ret = asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100317 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100318
319 if( p != end2 )
320 return( POLARSSL_ERR_X509_INVALID_ALG +
321 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200322
323 if( trailer_field != 1 )
324 return( POLARSSL_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100325 }
326 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
327 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
328
329 if( p != end )
330 return( POLARSSL_ERR_X509_INVALID_ALG +
331 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
332
333 return( 0 );
334}
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200335#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100336
337/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338 * AttributeTypeAndValue ::= SEQUENCE {
339 * type AttributeType,
340 * value AttributeValue }
341 *
342 * AttributeType ::= OBJECT IDENTIFIER
343 *
344 * AttributeValue ::= ANY DEFINED BY AttributeType
345 */
346static int x509_get_attr_type_value( unsigned char **p,
347 const unsigned char *end,
348 x509_name *cur )
349{
350 int ret;
351 size_t len;
352 x509_buf *oid;
353 x509_buf *val;
354
355 if( ( ret = asn1_get_tag( p, end, &len,
356 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200357 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358
359 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200360 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 POLARSSL_ERR_ASN1_OUT_OF_DATA );
362
363 oid = &cur->oid;
364 oid->tag = **p;
365
366 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200367 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
369 oid->p = *p;
370 *p += oid->len;
371
372 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200373 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374 POLARSSL_ERR_ASN1_OUT_OF_DATA );
375
376 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
377 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
378 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200379 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
381
382 val = &cur->val;
383 val->tag = *(*p)++;
384
385 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200386 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387
388 val->p = *p;
389 *p += val->len;
390
391 cur->next = NULL;
392
393 return( 0 );
394}
395
396/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000397 * Name ::= CHOICE { -- only one possibility for now --
398 * rdnSequence RDNSequence }
399 *
400 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
401 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 * 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 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000413 * The data structure is optimized for the common case where each RDN has only
414 * one element, which is represented as a list of AttributeTypeAndValue.
415 * For the general case we still use a flat list, but we mark elements of the
416 * same set so that they are "merged" together in the functions that consume
417 * this list, eg x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 */
419int x509_get_name( unsigned char **p, const unsigned char *end,
420 x509_name *cur )
421{
422 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200423 size_t set_len;
424 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100426 /* don't use recursion, we'd risk stack overflow if not optimized */
427 while( 1 )
428 {
429 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000430 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100431 */
432 if( ( ret = asn1_get_tag( p, end, &set_len,
433 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
434 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100436 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000438 while( 1 )
439 {
440 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
441 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000443 if( *p == end_set )
444 break;
445
446 /* Mark this item as being only one in a set */
447 cur->next_merged = 1;
448
449 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
450
451 if( cur->next == NULL )
452 return( POLARSSL_ERR_X509_MALLOC_FAILED );
453
454 memset( cur->next, 0, sizeof( x509_name ) );
455
456 cur = cur->next;
457 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100459 /*
460 * continue until end of SEQUENCE is reached
461 */
462 if( *p == end )
463 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100465 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100467 if( cur->next == NULL )
468 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100470 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100472 cur = cur->next;
473 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474}
475
476/*
477 * Time ::= CHOICE {
478 * utcTime UTCTime,
479 * generalTime GeneralizedTime }
480 */
481int x509_get_time( unsigned char **p, const unsigned char *end,
482 x509_time *time )
483{
484 int ret;
485 size_t len;
486 char date[64];
487 unsigned char tag;
488
489 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200490 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 POLARSSL_ERR_ASN1_OUT_OF_DATA );
492
493 tag = **p;
494
Paul Bakker66d5d072014-06-17 16:39:18 +0200495 if( tag == ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 {
497 (*p)++;
498 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200499
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200501 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
503 memset( date, 0, sizeof( date ) );
504 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
505 len : sizeof( date ) - 1 );
506
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200507 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508 &time->year, &time->mon, &time->day,
509 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200510 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511
512 time->year += 100 * ( time->year < 50 );
513 time->year += 1900;
514
515 *p += len;
516
517 return( 0 );
518 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200519 else if( tag == ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520 {
521 (*p)++;
522 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200523
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200525 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
527 memset( date, 0, sizeof( date ) );
528 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
529 len : sizeof( date ) - 1 );
530
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200531 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 &time->year, &time->mon, &time->day,
533 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200534 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
536 *p += len;
537
538 return( 0 );
539 }
540 else
Paul Bakker51876562013-09-17 14:36:05 +0200541 return( POLARSSL_ERR_X509_INVALID_DATE +
542 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543}
544
545int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
546{
547 int ret;
548 size_t len;
549
550 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200551 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 POLARSSL_ERR_ASN1_OUT_OF_DATA );
553
554 sig->tag = **p;
555
556 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200557 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
559 sig->len = len;
560 sig->p = *p;
561
562 *p += len;
563
564 return( 0 );
565}
566
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100567/*
568 * Get signature algorithm from alg OID and optional parameters
569 */
570int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200571 md_type_t *md_alg, pk_type_t *pk_alg,
572 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100574 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200576 if( *sig_opts != NULL )
577 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
578
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100579 if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200580 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200582#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100583 if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
584 {
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200585 pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100586
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200587 pss_opts = polarssl_malloc( sizeof( pk_rsassa_pss_options ) );
588 if( pss_opts == NULL )
589 return( POLARSSL_ERR_X509_MALLOC_FAILED );
590
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100591 ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200592 md_alg,
593 &pss_opts->mgf1_hash_id,
594 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100595 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200596 {
597 polarssl_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100598 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200599 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100600
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200601 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100602 }
603 else
Paul Bakkerdb20c102014-06-17 14:34:44 +0200604#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100605 {
606 /* Make sure parameters are absent or NULL */
607 if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
608 sig_params->len != 0 )
609 return( POLARSSL_ERR_X509_INVALID_ALG );
610 }
611
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612 return( 0 );
613}
614
615/*
616 * X.509 Extensions (No parsing of extensions, pointer should
617 * be either manually updated or extensions should be parsed!
618 */
619int x509_get_ext( unsigned char **p, const unsigned char *end,
620 x509_buf *ext, int tag )
621{
622 int ret;
623 size_t len;
624
625 if( *p == end )
626 return( 0 );
627
628 ext->tag = **p;
629
630 if( ( ret = asn1_get_tag( p, end, &ext->len,
631 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
632 return( ret );
633
634 ext->p = *p;
635 end = *p + ext->len;
636
637 /*
638 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
639 *
640 * Extension ::= SEQUENCE {
641 * extnID OBJECT IDENTIFIER,
642 * critical BOOLEAN DEFAULT FALSE,
643 * extnValue OCTET STRING }
644 */
645 if( ( ret = asn1_get_tag( p, end, &len,
646 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200647 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648
649 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200650 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
652
653 return( 0 );
654}
655
Paul Bakker6edcd412013-10-29 15:22:54 +0100656#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
657 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658#include <stdarg.h>
659
660#if !defined vsnprintf
661#define vsnprintf _vsnprintf
662#endif // vsnprintf
663
664/*
665 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
666 * Result value is not size of buffer needed, but -1 if no fit is possible.
667 *
668 * This fuction tries to 'fix' this by at least suggesting enlarging the
669 * size by 20.
670 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200671static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672{
673 va_list ap;
674 int res = -1;
675
676 va_start( ap, format );
677
678 res = vsnprintf( str, size, format, ap );
679
680 va_end( ap );
681
682 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200683 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 return( (int) size + 20 );
685
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200686 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687}
688
689#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200690#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691
692#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
693
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200694#define SAFE_SNPRINTF() \
695{ \
696 if( ret == -1 ) \
697 return( -1 ); \
698 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200699 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200700 p[n - 1] = '\0'; \
701 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
702 } \
703 \
704 n -= (unsigned int) ret; \
705 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706}
707
708/*
709 * Store the name in printable form into buf; no more
710 * than size characters will be written
711 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200712int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713{
714 int ret;
715 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000716 unsigned char c, merge = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717 const x509_name *name;
718 const char *short_name = NULL;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200719 char s[X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720
721 memset( s, 0, sizeof( s ) );
722
723 name = dn;
724 p = buf;
725 n = size;
726
727 while( name != NULL )
728 {
729 if( !name->oid.p )
730 {
731 name = name->next;
732 continue;
733 }
734
735 if( name != dn )
736 {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000737 ret = snprintf( p, n, merge ? " + " : ", " );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 SAFE_SNPRINTF();
739 }
740
741 ret = oid_get_attr_short_name( &name->oid, &short_name );
742
743 if( ret == 0 )
744 ret = snprintf( p, n, "%s=", short_name );
745 else
746 ret = snprintf( p, n, "\?\?=" );
747 SAFE_SNPRINTF();
748
749 for( i = 0; i < name->val.len; i++ )
750 {
751 if( i >= sizeof( s ) - 1 )
752 break;
753
754 c = name->val.p[i];
755 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
756 s[i] = '?';
757 else s[i] = c;
758 }
759 s[i] = '\0';
760 ret = snprintf( p, n, "%s", s );
761 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000762
763 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 name = name->next;
765 }
766
767 return( (int) ( size - n ) );
768}
769
770/*
771 * Store the serial in printable form into buf; no more
772 * than size characters will be written
773 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200774int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775{
776 int ret;
777 size_t i, n, nr;
778 char *p;
779
780 p = buf;
781 n = size;
782
783 nr = ( serial->len <= 32 )
784 ? serial->len : 28;
785
786 for( i = 0; i < nr; i++ )
787 {
788 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
789 continue;
790
791 ret = snprintf( p, n, "%02X%s",
792 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
793 SAFE_SNPRINTF();
794 }
795
796 if( nr != serial->len )
797 {
798 ret = snprintf( p, n, "...." );
799 SAFE_SNPRINTF();
800 }
801
802 return( (int) ( size - n ) );
803}
804
805/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200806 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100807 */
808int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200809 pk_type_t pk_alg, md_type_t md_alg,
810 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100811{
812 int ret;
813 char *p = buf;
814 size_t n = size;
815 const char *desc = NULL;
816
817 ret = oid_get_sig_alg_desc( sig_oid, &desc );
818 if( ret != 0 )
819 ret = snprintf( p, n, "???" );
820 else
821 ret = snprintf( p, n, "%s", desc );
822 SAFE_SNPRINTF();
823
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200824#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100825 if( pk_alg == POLARSSL_PK_RSASSA_PSS )
826 {
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200827 const pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100828 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100829
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200830 pss_opts = (const pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100831
832 md_info = md_info_from_type( md_alg );
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200833 mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100834
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200835 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100836 md_info ? md_info->name : "???",
837 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200838 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100839 SAFE_SNPRINTF();
840 }
841#else
842 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200843 ((void) md_alg);
844 ((void) sig_opts);
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200845#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100846
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200847 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100848}
849
850/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 * Helper for writing "RSA key size", "EC key size", etc
852 */
853int x509_key_size_helper( char *buf, size_t size, const char *name )
854{
855 char *p = buf;
856 size_t n = size;
857 int ret;
858
859 if( strlen( name ) + sizeof( " key size" ) > size )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200860 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861
862 ret = snprintf( p, n, "%s key size", name );
863 SAFE_SNPRINTF();
864
865 return( 0 );
866}
867
868/*
869 * Return an informational string describing the given OID
870 */
871const char *x509_oid_get_description( x509_buf *oid )
872{
873 const char *desc = NULL;
874 int ret;
875
876 ret = oid_get_extended_key_usage( oid, &desc );
877
878 if( ret != 0 )
879 return( NULL );
880
881 return( desc );
882}
883
884/* Return the x.y.z.... style numeric string for the given OID */
885int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
886{
887 return oid_get_numeric_string( buf, size, oid );
888}
889
890/*
891 * Return 0 if the x509_time is still valid, or 1 otherwise.
892 */
893#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100895static void x509_get_current_time( x509_time *now )
896{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100897#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 SYSTEMTIME st;
899
Paul Bakker66d5d072014-06-17 16:39:18 +0200900 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100902 now->year = st.wYear;
903 now->mon = st.wMonth;
904 now->day = st.wDay;
905 now->hour = st.wHour;
906 now->min = st.wMinute;
907 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100909 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910 time_t tt;
911
912 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200913 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914
Paul Bakker5fff23b2014-03-26 15:34:54 +0100915 now->year = lt.tm_year + 1900;
916 now->mon = lt.tm_mon + 1;
917 now->day = lt.tm_mday;
918 now->hour = lt.tm_hour;
919 now->min = lt.tm_min;
920 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200921#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100922}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100924/*
925 * Return 0 if before <= after, 1 otherwise
926 */
927static int x509_check_time( const x509_time *before, const x509_time *after )
928{
929 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930 return( 1 );
931
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100932 if( before->year == after->year &&
933 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934 return( 1 );
935
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100936 if( before->year == after->year &&
937 before->mon == after->mon &&
938 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939 return( 1 );
940
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100941 if( before->year == after->year &&
942 before->mon == after->mon &&
943 before->day == after->day &&
944 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945 return( 1 );
946
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100947 if( before->year == after->year &&
948 before->mon == after->mon &&
949 before->day == after->day &&
950 before->hour == after->hour &&
951 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 return( 1 );
953
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100954 if( before->year == after->year &&
955 before->mon == after->mon &&
956 before->day == after->day &&
957 before->hour == after->hour &&
958 before->min == after->min &&
959 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 return( 1 );
961
962 return( 0 );
963}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100964
965int x509_time_expired( const x509_time *to )
966{
967 x509_time now;
968
969 x509_get_current_time( &now );
970
971 return( x509_check_time( &now, to ) );
972}
973
974int x509_time_future( const x509_time *from )
975{
976 x509_time now;
977
978 x509_get_current_time( &now );
979
980 return( x509_check_time( from, &now ) );
981}
982
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100984
Paul Bakker86d0c192013-09-18 11:11:02 +0200985int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986{
987 ((void) to);
988 return( 0 );
989}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100990
991int x509_time_future( const x509_time *from )
992{
993 ((void) from);
994 return( 0 );
995}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996#endif /* POLARSSL_HAVE_TIME */
997
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200998#if defined(POLARSSL_SELF_TEST)
999
1000#include "polarssl/x509_crt.h"
1001#include "polarssl/certs.h"
1002
1003/*
1004 * Checkup routine
1005 */
1006int x509_self_test( int verbose )
1007{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +02001008#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001009 int ret;
1010 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001011 x509_crt cacert;
1012 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001013
1014 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001015 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001016
Paul Bakkerb6b09562013-09-18 14:17:41 +02001017 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001018
Paul Bakkerddf26b42013-09-18 13:46:23 +02001019 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
1020 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001021 if( ret != 0 )
1022 {
1023 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001024 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001025
1026 return( ret );
1027 }
1028
Paul Bakkerb6b09562013-09-18 14:17:41 +02001029 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001030
Paul Bakkerddf26b42013-09-18 13:46:23 +02001031 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
1032 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001033 if( ret != 0 )
1034 {
1035 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001036 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001037
1038 return( ret );
1039 }
1040
1041 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001042 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001043
Paul Bakkerddf26b42013-09-18 13:46:23 +02001044 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001045 if( ret != 0 )
1046 {
1047 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001048 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001049
Paul Bakker66d5d072014-06-17 16:39:18 +02001050 polarssl_printf( "ret = %d, &flags = %04x\n", ret, flags );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001051
1052 return( ret );
1053 }
1054
1055 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001056 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001057
1058 x509_crt_free( &cacert );
1059 x509_crt_free( &clicert );
1060
1061 return( 0 );
1062#else
1063 ((void) verbose);
1064 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001065#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001066}
1067
Paul Bakker9af723c2014-05-01 13:03:14 +02001068#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001069
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070#endif /* POLARSSL_X509_USE_C */