blob: a3cb669432cf2d635c7455240f4749e5df6bbd19 [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"
44#if defined(POLARSSL_PEM_PARSE_C)
45#include "polarssl/pem.h"
46#endif
47
Paul Bakker7dc4c442014-02-01 22:50:26 +010048#if defined(POLARSSL_PLATFORM_C)
49#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#define polarssl_printf printf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#define polarssl_malloc malloc
53#define polarssl_free free
54#endif
55
56#include <string.h>
57#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010058#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059#include <windows.h>
60#else
61#include <time.h>
62#endif
63
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#include <stdio.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010065
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#if defined(POLARSSL_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#if !defined(_WIN32)
68#include <sys/types.h>
69#include <sys/stat.h>
70#include <dirent.h>
71#endif
72#endif
73
74/*
75 * CertificateSerialNumber ::= INTEGER
76 */
77int x509_get_serial( unsigned char **p, const unsigned char *end,
78 x509_buf *serial )
79{
80 int ret;
81
82 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +020083 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084 POLARSSL_ERR_ASN1_OUT_OF_DATA );
85
86 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
87 **p != ASN1_INTEGER )
Paul Bakker51876562013-09-17 14:36:05 +020088 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
90
91 serial->tag = *(*p)++;
92
93 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +020094 return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095
96 serial->p = *p;
97 *p += serial->len;
98
99 return( 0 );
100}
101
102/* Get an algorithm identifier without parameters (eg for signatures)
103 *
104 * AlgorithmIdentifier ::= SEQUENCE {
105 * algorithm OBJECT IDENTIFIER,
106 * parameters ANY DEFINED BY algorithm OPTIONAL }
107 */
108int x509_get_alg_null( unsigned char **p, const unsigned char *end,
109 x509_buf *alg )
110{
111 int ret;
112
113 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200114 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115
116 return( 0 );
117}
118
119/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100120 * Parse an algorithm identifier with (optional) paramaters
121 */
122int x509_get_alg( unsigned char **p, const unsigned char *end,
123 x509_buf *alg, x509_buf *params )
124{
125 int ret;
126
127 if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
128 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
129
130 return( 0 );
131}
132
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200133#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100134/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100135 * HashAlgorithm ::= AlgorithmIdentifier
136 *
137 * AlgorithmIdentifier ::= SEQUENCE {
138 * algorithm OBJECT IDENTIFIER,
139 * parameters ANY DEFINED BY algorithm OPTIONAL }
140 *
141 * For HashAlgorithm, parameters MUST be NULL or absent.
142 */
143static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
144{
145 int ret;
146 unsigned char *p;
147 const unsigned char *end;
148 x509_buf md_oid;
149 size_t len;
150
151 /* Make sure we got a SEQUENCE and setup bounds */
152 if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
153 return( POLARSSL_ERR_X509_INVALID_ALG +
154 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
155
156 p = (unsigned char *) alg->p;
157 end = p + alg->len;
158
159 if( p >= end )
160 return( POLARSSL_ERR_X509_INVALID_ALG +
161 POLARSSL_ERR_ASN1_OUT_OF_DATA );
162
163 /* Parse md_oid */
164 md_oid.tag = *p;
165
166 if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
167 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
168
169 md_oid.p = p;
170 p += md_oid.len;
171
172 /* Get md_alg from md_oid */
173 if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
174 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
175
176 /* Make sure params is absent of NULL */
177 if( p == end )
178 return( 0 );
179
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100180 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100181 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
182
183 if( p != end )
184 return( POLARSSL_ERR_X509_INVALID_ALG +
185 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
186
187 return( 0 );
188}
189
190/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100191 * RSASSA-PSS-params ::= SEQUENCE {
192 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
193 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
194 * saltLength [2] INTEGER DEFAULT 20,
195 * trailerField [3] INTEGER DEFAULT 1 }
196 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200197 *
198 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
199 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
200 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100201 */
202int x509_get_rsassa_pss_params( const x509_buf *params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100203 md_type_t *md_alg, md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200204 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100205{
206 int ret;
207 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100208 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100209 size_t len;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100210 x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100211
212 /* First set everything to defaults */
213 *md_alg = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100214 *mgf_md = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100215 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100216
217 /* Make sure params is a SEQUENCE and setup bounds */
218 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
219 return( POLARSSL_ERR_X509_INVALID_ALG +
220 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
221
222 p = (unsigned char *) params->p;
223 end = p + params->len;
224
225 if( p == end )
226 return( 0 );
227
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100228 /*
229 * HashAlgorithm
230 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100231 if( ( ret = asn1_get_tag( &p, end, &len,
232 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
233 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100234 end2 = p + len;
235
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100236 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100237 if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100238 return( ret );
239
240 if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
241 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100242
243 if( p != end2 )
244 return( POLARSSL_ERR_X509_INVALID_ALG +
245 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100246 }
247 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
248 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
249
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100250 if( p == end )
251 return( 0 );
252
253 /*
254 * MaskGenAlgorithm
255 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256 if( ( ret = asn1_get_tag( &p, end, &len,
257 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
258 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100259 end2 = p + len;
260
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100261 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100262 if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100263 return( ret );
264
265 /* Only MFG1 is recognised for now */
266 if( ! OID_CMP( OID_MGF1, &alg_id ) )
267 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
268 POLARSSL_ERR_OID_NOT_FOUND );
269
270 /* Parse HashAlgorithm */
271 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
272 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100273
274 if( p != end2 )
275 return( POLARSSL_ERR_X509_INVALID_ALG +
276 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100277 }
278 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
279 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
280
281 if( p == end )
282 return( 0 );
283
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100284 /*
285 * salt_len
286 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100287 if( ( ret = asn1_get_tag( &p, end, &len,
288 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
289 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100290 end2 = p + len;
291
292 if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100293 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100294
295 if( p != end2 )
296 return( POLARSSL_ERR_X509_INVALID_ALG +
297 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100298 }
299 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
300 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
301
302 if( p == end )
303 return( 0 );
304
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100305 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200306 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100307 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100308 if( ( ret = asn1_get_tag( &p, end, &len,
309 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
310 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200311 int trailer_field;
312
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100313 end2 = p + len;
314
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200315 if( ( ret = asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100316 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100317
318 if( p != end2 )
319 return( POLARSSL_ERR_X509_INVALID_ALG +
320 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200321
322 if( trailer_field != 1 )
323 return( POLARSSL_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100324 }
325 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
326 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
327
328 if( p != end )
329 return( POLARSSL_ERR_X509_INVALID_ALG +
330 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
331
332 return( 0 );
333}
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200334#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100335
336/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337 * AttributeTypeAndValue ::= SEQUENCE {
338 * type AttributeType,
339 * value AttributeValue }
340 *
341 * AttributeType ::= OBJECT IDENTIFIER
342 *
343 * AttributeValue ::= ANY DEFINED BY AttributeType
344 */
345static int x509_get_attr_type_value( unsigned char **p,
346 const unsigned char *end,
347 x509_name *cur )
348{
349 int ret;
350 size_t len;
351 x509_buf *oid;
352 x509_buf *val;
353
354 if( ( ret = asn1_get_tag( p, end, &len,
355 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200356 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
358 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200359 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 POLARSSL_ERR_ASN1_OUT_OF_DATA );
361
362 oid = &cur->oid;
363 oid->tag = **p;
364
365 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200366 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
368 oid->p = *p;
369 *p += oid->len;
370
371 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200372 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373 POLARSSL_ERR_ASN1_OUT_OF_DATA );
374
375 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
376 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
377 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200378 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
380
381 val = &cur->val;
382 val->tag = *(*p)++;
383
384 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200385 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
387 val->p = *p;
388 *p += val->len;
389
390 cur->next = NULL;
391
392 return( 0 );
393}
394
395/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000396 * Name ::= CHOICE { -- only one possibility for now --
397 * rdnSequence RDNSequence }
398 *
399 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
400 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 * RelativeDistinguishedName ::=
402 * SET OF AttributeTypeAndValue
403 *
404 * AttributeTypeAndValue ::= SEQUENCE {
405 * type AttributeType,
406 * value AttributeValue }
407 *
408 * AttributeType ::= OBJECT IDENTIFIER
409 *
410 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200411 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000412 * The data structure is optimized for the common case where each RDN has only
413 * one element, which is represented as a list of AttributeTypeAndValue.
414 * For the general case we still use a flat list, but we mark elements of the
415 * same set so that they are "merged" together in the functions that consume
416 * this list, eg x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 */
418int x509_get_name( unsigned char **p, const unsigned char *end,
419 x509_name *cur )
420{
421 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200422 size_t set_len;
423 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100425 /* don't use recursion, we'd risk stack overflow if not optimized */
426 while( 1 )
427 {
428 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000429 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100430 */
431 if( ( ret = asn1_get_tag( p, end, &set_len,
432 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
433 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100435 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000437 while( 1 )
438 {
439 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
440 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000442 if( *p == end_set )
443 break;
444
445 /* Mark this item as being only one in a set */
446 cur->next_merged = 1;
447
448 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
449
450 if( cur->next == NULL )
451 return( POLARSSL_ERR_X509_MALLOC_FAILED );
452
453 memset( cur->next, 0, sizeof( x509_name ) );
454
455 cur = cur->next;
456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100458 /*
459 * continue until end of SEQUENCE is reached
460 */
461 if( *p == end )
462 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100464 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100466 if( cur->next == NULL )
467 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100469 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100471 cur = cur->next;
472 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473}
474
475/*
476 * Time ::= CHOICE {
477 * utcTime UTCTime,
478 * generalTime GeneralizedTime }
479 */
480int x509_get_time( unsigned char **p, const unsigned char *end,
481 x509_time *time )
482{
483 int ret;
484 size_t len;
485 char date[64];
486 unsigned char tag;
487
488 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200489 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 POLARSSL_ERR_ASN1_OUT_OF_DATA );
491
492 tag = **p;
493
Paul Bakker66d5d072014-06-17 16:39:18 +0200494 if( tag == ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 {
496 (*p)++;
497 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200498
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200500 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
502 memset( date, 0, sizeof( date ) );
503 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
504 len : sizeof( date ) - 1 );
505
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200506 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 &time->year, &time->mon, &time->day,
508 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200509 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510
511 time->year += 100 * ( time->year < 50 );
512 time->year += 1900;
513
514 *p += len;
515
516 return( 0 );
517 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200518 else if( tag == ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519 {
520 (*p)++;
521 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200522
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200524 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525
526 memset( date, 0, sizeof( date ) );
527 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
528 len : sizeof( date ) - 1 );
529
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200530 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531 &time->year, &time->mon, &time->day,
532 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200533 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534
535 *p += len;
536
537 return( 0 );
538 }
539 else
Paul Bakker51876562013-09-17 14:36:05 +0200540 return( POLARSSL_ERR_X509_INVALID_DATE +
541 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542}
543
544int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
545{
546 int ret;
547 size_t len;
548
549 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200550 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551 POLARSSL_ERR_ASN1_OUT_OF_DATA );
552
553 sig->tag = **p;
554
555 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200556 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
558 sig->len = len;
559 sig->p = *p;
560
561 *p += len;
562
563 return( 0 );
564}
565
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100566/*
567 * Get signature algorithm from alg OID and optional parameters
568 */
569int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200570 md_type_t *md_alg, pk_type_t *pk_alg,
571 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100573 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200575 if( *sig_opts != NULL )
576 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
577
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100578 if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200579 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200581#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100582 if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
583 {
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200584 pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100585
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200586 pss_opts = polarssl_malloc( sizeof( pk_rsassa_pss_options ) );
587 if( pss_opts == NULL )
588 return( POLARSSL_ERR_X509_MALLOC_FAILED );
589
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100590 ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200591 md_alg,
592 &pss_opts->mgf1_hash_id,
593 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100594 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200595 {
596 polarssl_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100597 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200598 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100599
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200600 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100601 }
602 else
Paul Bakkerdb20c102014-06-17 14:34:44 +0200603#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100604 {
605 /* Make sure parameters are absent or NULL */
606 if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
607 sig_params->len != 0 )
608 return( POLARSSL_ERR_X509_INVALID_ALG );
609 }
610
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 return( 0 );
612}
613
614/*
615 * X.509 Extensions (No parsing of extensions, pointer should
616 * be either manually updated or extensions should be parsed!
617 */
618int x509_get_ext( unsigned char **p, const unsigned char *end,
619 x509_buf *ext, int tag )
620{
621 int ret;
622 size_t len;
623
624 if( *p == end )
625 return( 0 );
626
627 ext->tag = **p;
628
629 if( ( ret = asn1_get_tag( p, end, &ext->len,
630 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
631 return( ret );
632
633 ext->p = *p;
634 end = *p + ext->len;
635
636 /*
637 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
638 *
639 * Extension ::= SEQUENCE {
640 * extnID OBJECT IDENTIFIER,
641 * critical BOOLEAN DEFAULT FALSE,
642 * extnValue OCTET STRING }
643 */
644 if( ( ret = asn1_get_tag( p, end, &len,
645 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200646 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647
648 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200649 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
651
652 return( 0 );
653}
654
Paul Bakker6edcd412013-10-29 15:22:54 +0100655#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
656 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657#include <stdarg.h>
658
659#if !defined vsnprintf
660#define vsnprintf _vsnprintf
661#endif // vsnprintf
662
663/*
664 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
665 * Result value is not size of buffer needed, but -1 if no fit is possible.
666 *
667 * This fuction tries to 'fix' this by at least suggesting enlarging the
668 * size by 20.
669 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200670static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671{
672 va_list ap;
673 int res = -1;
674
675 va_start( ap, format );
676
677 res = vsnprintf( str, size, format, ap );
678
679 va_end( ap );
680
681 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200682 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 return( (int) size + 20 );
684
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200685 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686}
687
688#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200689#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
691#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
692
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200693#define SAFE_SNPRINTF() \
694{ \
695 if( ret == -1 ) \
696 return( -1 ); \
697 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200698 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200699 p[n - 1] = '\0'; \
700 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
701 } \
702 \
703 n -= (unsigned int) ret; \
704 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705}
706
707/*
708 * Store the name in printable form into buf; no more
709 * than size characters will be written
710 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200711int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712{
713 int ret;
714 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000715 unsigned char c, merge = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716 const x509_name *name;
717 const char *short_name = NULL;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200718 char s[X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719
720 memset( s, 0, sizeof( s ) );
721
722 name = dn;
723 p = buf;
724 n = size;
725
726 while( name != NULL )
727 {
728 if( !name->oid.p )
729 {
730 name = name->next;
731 continue;
732 }
733
734 if( name != dn )
735 {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000736 ret = snprintf( p, n, merge ? " + " : ", " );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 SAFE_SNPRINTF();
738 }
739
740 ret = oid_get_attr_short_name( &name->oid, &short_name );
741
742 if( ret == 0 )
743 ret = snprintf( p, n, "%s=", short_name );
744 else
745 ret = snprintf( p, n, "\?\?=" );
746 SAFE_SNPRINTF();
747
748 for( i = 0; i < name->val.len; i++ )
749 {
750 if( i >= sizeof( s ) - 1 )
751 break;
752
753 c = name->val.p[i];
754 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
755 s[i] = '?';
756 else s[i] = c;
757 }
758 s[i] = '\0';
759 ret = snprintf( p, n, "%s", s );
760 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000761
762 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 name = name->next;
764 }
765
766 return( (int) ( size - n ) );
767}
768
769/*
770 * Store the serial in printable form into buf; no more
771 * than size characters will be written
772 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200773int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774{
775 int ret;
776 size_t i, n, nr;
777 char *p;
778
779 p = buf;
780 n = size;
781
782 nr = ( serial->len <= 32 )
783 ? serial->len : 28;
784
785 for( i = 0; i < nr; i++ )
786 {
787 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
788 continue;
789
790 ret = snprintf( p, n, "%02X%s",
791 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
792 SAFE_SNPRINTF();
793 }
794
795 if( nr != serial->len )
796 {
797 ret = snprintf( p, n, "...." );
798 SAFE_SNPRINTF();
799 }
800
801 return( (int) ( size - n ) );
802}
803
804/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200805 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100806 */
807int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200808 pk_type_t pk_alg, md_type_t md_alg,
809 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100810{
811 int ret;
812 char *p = buf;
813 size_t n = size;
814 const char *desc = NULL;
815
816 ret = oid_get_sig_alg_desc( sig_oid, &desc );
817 if( ret != 0 )
818 ret = snprintf( p, n, "???" );
819 else
820 ret = snprintf( p, n, "%s", desc );
821 SAFE_SNPRINTF();
822
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200823#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100824 if( pk_alg == POLARSSL_PK_RSASSA_PSS )
825 {
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200826 const pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100827 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100828
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200829 pss_opts = (const pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100830
831 md_info = md_info_from_type( md_alg );
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200832 mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100833
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200834 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100835 md_info ? md_info->name : "???",
836 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200837 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100838 SAFE_SNPRINTF();
839 }
840#else
841 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200842 ((void) md_alg);
843 ((void) sig_opts);
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200844#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100845
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200846 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100847}
848
849/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200850 * Helper for writing "RSA key size", "EC key size", etc
851 */
852int x509_key_size_helper( char *buf, size_t size, const char *name )
853{
854 char *p = buf;
855 size_t n = size;
856 int ret;
857
858 if( strlen( name ) + sizeof( " key size" ) > size )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200859 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860
861 ret = snprintf( p, n, "%s key size", name );
862 SAFE_SNPRINTF();
863
864 return( 0 );
865}
866
867/*
868 * Return an informational string describing the given OID
869 */
870const char *x509_oid_get_description( x509_buf *oid )
871{
872 const char *desc = NULL;
873 int ret;
874
875 ret = oid_get_extended_key_usage( oid, &desc );
876
877 if( ret != 0 )
878 return( NULL );
879
880 return( desc );
881}
882
883/* Return the x.y.z.... style numeric string for the given OID */
884int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
885{
886 return oid_get_numeric_string( buf, size, oid );
887}
888
889/*
890 * Return 0 if the x509_time is still valid, or 1 otherwise.
891 */
892#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100894static void x509_get_current_time( x509_time *now )
895{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100896#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 SYSTEMTIME st;
898
Paul Bakker66d5d072014-06-17 16:39:18 +0200899 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100901 now->year = st.wYear;
902 now->mon = st.wMonth;
903 now->day = st.wDay;
904 now->hour = st.wHour;
905 now->min = st.wMinute;
906 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100908 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 time_t tt;
910
911 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200912 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913
Paul Bakker5fff23b2014-03-26 15:34:54 +0100914 now->year = lt.tm_year + 1900;
915 now->mon = lt.tm_mon + 1;
916 now->day = lt.tm_mday;
917 now->hour = lt.tm_hour;
918 now->min = lt.tm_min;
919 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200920#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100921}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100923/*
924 * Return 0 if before <= after, 1 otherwise
925 */
926static int x509_check_time( const x509_time *before, const x509_time *after )
927{
928 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 return( 1 );
930
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100931 if( before->year == after->year &&
932 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 return( 1 );
934
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100935 if( before->year == after->year &&
936 before->mon == after->mon &&
937 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 return( 1 );
939
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100940 if( before->year == after->year &&
941 before->mon == after->mon &&
942 before->day == after->day &&
943 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 return( 1 );
945
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100946 if( before->year == after->year &&
947 before->mon == after->mon &&
948 before->day == after->day &&
949 before->hour == after->hour &&
950 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 return( 1 );
952
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100953 if( before->year == after->year &&
954 before->mon == after->mon &&
955 before->day == after->day &&
956 before->hour == after->hour &&
957 before->min == after->min &&
958 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 return( 1 );
960
961 return( 0 );
962}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100963
964int x509_time_expired( const x509_time *to )
965{
966 x509_time now;
967
968 x509_get_current_time( &now );
969
970 return( x509_check_time( &now, to ) );
971}
972
973int x509_time_future( const x509_time *from )
974{
975 x509_time now;
976
977 x509_get_current_time( &now );
978
979 return( x509_check_time( from, &now ) );
980}
981
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100983
Paul Bakker86d0c192013-09-18 11:11:02 +0200984int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985{
986 ((void) to);
987 return( 0 );
988}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100989
990int x509_time_future( const x509_time *from )
991{
992 ((void) from);
993 return( 0 );
994}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995#endif /* POLARSSL_HAVE_TIME */
996
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200997#if defined(POLARSSL_SELF_TEST)
998
999#include "polarssl/x509_crt.h"
1000#include "polarssl/certs.h"
1001
1002/*
1003 * Checkup routine
1004 */
1005int x509_self_test( int verbose )
1006{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +02001007#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001008 int ret;
1009 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001010 x509_crt cacert;
1011 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001012
1013 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001014 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001015
Paul Bakkerb6b09562013-09-18 14:17:41 +02001016 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001017
Paul Bakkerddf26b42013-09-18 13:46:23 +02001018 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
1019 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001020 if( ret != 0 )
1021 {
1022 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001023 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001024
1025 return( ret );
1026 }
1027
Paul Bakkerb6b09562013-09-18 14:17:41 +02001028 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001029
Paul Bakkerddf26b42013-09-18 13:46:23 +02001030 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
1031 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001032 if( ret != 0 )
1033 {
1034 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001035 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001036
1037 return( ret );
1038 }
1039
1040 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001041 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001042
Paul Bakkerddf26b42013-09-18 13:46:23 +02001043 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001044 if( ret != 0 )
1045 {
1046 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001047 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001048
Paul Bakker66d5d072014-06-17 16:39:18 +02001049 polarssl_printf( "ret = %d, &flags = %04x\n", ret, flags );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001050
1051 return( ret );
1052 }
1053
1054 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001055 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001056
1057 x509_crt_free( &cacert );
1058 x509_crt_free( &clicert );
1059
1060 return( 0 );
1061#else
1062 ((void) verbose);
1063 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001064#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001065}
1066
Paul Bakker9af723c2014-05-01 13:03:14 +02001067#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001068
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069#endif /* POLARSSL_X509_USE_C */