blob: bf45d5b8f18d877413f302f35d92f2a440b96f29 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 common functions for parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The ITU-T X.509 standard defines a certificate format for PKI.
24 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020025 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
26 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
27 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028 *
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
31 */
32
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
36#include POLARSSL_CONFIG_FILE
37#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
39#if defined(POLARSSL_X509_USE_C)
40
41#include "polarssl/x509.h"
42#include "polarssl/asn1.h"
43#include "polarssl/oid.h"
44#if defined(POLARSSL_PEM_PARSE_C)
45#include "polarssl/pem.h"
46#endif
47
Paul Bakker7dc4c442014-02-01 22:50:26 +010048#if defined(POLARSSL_PLATFORM_C)
49#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#define polarssl_printf printf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#define polarssl_malloc malloc
53#define polarssl_free free
54#endif
55
56#include <string.h>
57#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010058#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059#include <windows.h>
60#else
61#include <time.h>
62#endif
63
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#if defined(EFIX64) || defined(EFI32)
65#include <stdio.h>
66#endif
67
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
70#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/*
399 * RelativeDistinguishedName ::=
400 * SET OF AttributeTypeAndValue
401 *
402 * AttributeTypeAndValue ::= SEQUENCE {
403 * type AttributeType,
404 * value AttributeValue }
405 *
406 * AttributeType ::= OBJECT IDENTIFIER
407 *
408 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200409 *
410 * We restrict RelativeDistinguishedName to be a set of 1 element. This is
411 * the most common case, and our x509_name structure currently can't handle
412 * more than that.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 */
414int x509_get_name( unsigned char **p, const unsigned char *end,
415 x509_name *cur )
416{
417 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200418 size_t set_len;
419 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100421 /* don't use recursion, we'd risk stack overflow if not optimized */
422 while( 1 )
423 {
424 /*
425 * parse first SET, restricted to 1 element
426 */
427 if( ( ret = asn1_get_tag( p, end, &set_len,
428 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
429 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100431 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100433 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
434 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100436 if( *p != end_set )
437 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100439 /*
440 * continue until end of SEQUENCE is reached
441 */
442 if( *p == end )
443 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100445 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100447 if( cur->next == NULL )
448 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100450 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100452 cur = cur->next;
453 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454}
455
456/*
457 * Time ::= CHOICE {
458 * utcTime UTCTime,
459 * generalTime GeneralizedTime }
460 */
461int x509_get_time( unsigned char **p, const unsigned char *end,
462 x509_time *time )
463{
464 int ret;
465 size_t len;
466 char date[64];
467 unsigned char tag;
468
469 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200470 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 POLARSSL_ERR_ASN1_OUT_OF_DATA );
472
473 tag = **p;
474
Paul Bakker66d5d072014-06-17 16:39:18 +0200475 if( tag == ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 {
477 (*p)++;
478 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200479
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200481 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
483 memset( date, 0, sizeof( date ) );
484 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
485 len : sizeof( date ) - 1 );
486
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200487 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 &time->year, &time->mon, &time->day,
489 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200490 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
492 time->year += 100 * ( time->year < 50 );
493 time->year += 1900;
494
495 *p += len;
496
497 return( 0 );
498 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200499 else if( tag == ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 {
501 (*p)++;
502 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200503
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200505 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507 memset( date, 0, sizeof( date ) );
508 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
509 len : sizeof( date ) - 1 );
510
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200511 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 &time->year, &time->mon, &time->day,
513 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200514 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
516 *p += len;
517
518 return( 0 );
519 }
520 else
Paul Bakker51876562013-09-17 14:36:05 +0200521 return( POLARSSL_ERR_X509_INVALID_DATE +
522 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523}
524
525int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
526{
527 int ret;
528 size_t len;
529
530 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200531 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 POLARSSL_ERR_ASN1_OUT_OF_DATA );
533
534 sig->tag = **p;
535
536 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200537 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538
539 sig->len = len;
540 sig->p = *p;
541
542 *p += len;
543
544 return( 0 );
545}
546
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100547/*
548 * Get signature algorithm from alg OID and optional parameters
549 */
550int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200551 md_type_t *md_alg, pk_type_t *pk_alg,
552 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100554 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200556 if( *sig_opts != NULL )
557 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
558
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100559 if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200560 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200562#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100563 if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
564 {
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200565 pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100566
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200567 pss_opts = polarssl_malloc( sizeof( pk_rsassa_pss_options ) );
568 if( pss_opts == NULL )
569 return( POLARSSL_ERR_X509_MALLOC_FAILED );
570
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100571 ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200572 md_alg,
573 &pss_opts->mgf1_hash_id,
574 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100575 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200576 {
577 polarssl_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100578 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200579 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100580
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200581 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100582 }
583 else
Paul Bakkerdb20c102014-06-17 14:34:44 +0200584#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100585 {
586 /* Make sure parameters are absent or NULL */
587 if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
588 sig_params->len != 0 )
589 return( POLARSSL_ERR_X509_INVALID_ALG );
590 }
591
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592 return( 0 );
593}
594
595/*
596 * X.509 Extensions (No parsing of extensions, pointer should
597 * be either manually updated or extensions should be parsed!
598 */
599int x509_get_ext( unsigned char **p, const unsigned char *end,
600 x509_buf *ext, int tag )
601{
602 int ret;
603 size_t len;
604
605 if( *p == end )
606 return( 0 );
607
608 ext->tag = **p;
609
610 if( ( ret = asn1_get_tag( p, end, &ext->len,
611 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
612 return( ret );
613
614 ext->p = *p;
615 end = *p + ext->len;
616
617 /*
618 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
619 *
620 * Extension ::= SEQUENCE {
621 * extnID OBJECT IDENTIFIER,
622 * critical BOOLEAN DEFAULT FALSE,
623 * extnValue OCTET STRING }
624 */
625 if( ( ret = asn1_get_tag( p, end, &len,
626 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200627 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628
629 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200630 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
632
633 return( 0 );
634}
635
Paul Bakker6edcd412013-10-29 15:22:54 +0100636#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
637 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638#include <stdarg.h>
639
640#if !defined vsnprintf
641#define vsnprintf _vsnprintf
642#endif // vsnprintf
643
644/*
645 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
646 * Result value is not size of buffer needed, but -1 if no fit is possible.
647 *
648 * This fuction tries to 'fix' this by at least suggesting enlarging the
649 * size by 20.
650 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200651static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652{
653 va_list ap;
654 int res = -1;
655
656 va_start( ap, format );
657
658 res = vsnprintf( str, size, format, ap );
659
660 va_end( ap );
661
662 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200663 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 return( (int) size + 20 );
665
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667}
668
669#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200670#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671
672#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
673
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200674#define SAFE_SNPRINTF() \
675{ \
676 if( ret == -1 ) \
677 return( -1 ); \
678 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200679 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200680 p[n - 1] = '\0'; \
681 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
682 } \
683 \
684 n -= (unsigned int) ret; \
685 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686}
687
688/*
689 * Store the name in printable form into buf; no more
690 * than size characters will be written
691 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200692int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693{
694 int ret;
695 size_t i, n;
696 unsigned char c;
697 const x509_name *name;
698 const char *short_name = NULL;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200699 char s[X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
701 memset( s, 0, sizeof( s ) );
702
703 name = dn;
704 p = buf;
705 n = size;
706
707 while( name != NULL )
708 {
709 if( !name->oid.p )
710 {
711 name = name->next;
712 continue;
713 }
714
715 if( name != dn )
716 {
717 ret = snprintf( p, n, ", " );
718 SAFE_SNPRINTF();
719 }
720
721 ret = oid_get_attr_short_name( &name->oid, &short_name );
722
723 if( ret == 0 )
724 ret = snprintf( p, n, "%s=", short_name );
725 else
726 ret = snprintf( p, n, "\?\?=" );
727 SAFE_SNPRINTF();
728
729 for( i = 0; i < name->val.len; i++ )
730 {
731 if( i >= sizeof( s ) - 1 )
732 break;
733
734 c = name->val.p[i];
735 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
736 s[i] = '?';
737 else s[i] = c;
738 }
739 s[i] = '\0';
740 ret = snprintf( p, n, "%s", s );
741 SAFE_SNPRINTF();
742 name = name->next;
743 }
744
745 return( (int) ( size - n ) );
746}
747
748/*
749 * Store the serial in printable form into buf; no more
750 * than size characters will be written
751 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200752int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753{
754 int ret;
755 size_t i, n, nr;
756 char *p;
757
758 p = buf;
759 n = size;
760
761 nr = ( serial->len <= 32 )
762 ? serial->len : 28;
763
764 for( i = 0; i < nr; i++ )
765 {
766 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
767 continue;
768
769 ret = snprintf( p, n, "%02X%s",
770 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
771 SAFE_SNPRINTF();
772 }
773
774 if( nr != serial->len )
775 {
776 ret = snprintf( p, n, "...." );
777 SAFE_SNPRINTF();
778 }
779
780 return( (int) ( size - n ) );
781}
782
783/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200784 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100785 */
786int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200787 pk_type_t pk_alg, md_type_t md_alg,
788 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100789{
790 int ret;
791 char *p = buf;
792 size_t n = size;
793 const char *desc = NULL;
794
795 ret = oid_get_sig_alg_desc( sig_oid, &desc );
796 if( ret != 0 )
797 ret = snprintf( p, n, "???" );
798 else
799 ret = snprintf( p, n, "%s", desc );
800 SAFE_SNPRINTF();
801
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200802#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100803 if( pk_alg == POLARSSL_PK_RSASSA_PSS )
804 {
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200805 const pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100806 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100807
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200808 pss_opts = (const pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100809
810 md_info = md_info_from_type( md_alg );
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200811 mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100812
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200813 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100814 md_info ? md_info->name : "???",
815 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200816 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100817 SAFE_SNPRINTF();
818 }
819#else
820 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200821 ((void) md_alg);
822 ((void) sig_opts);
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200823#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100824
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200825 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100826}
827
828/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200829 * Helper for writing "RSA key size", "EC key size", etc
830 */
831int x509_key_size_helper( char *buf, size_t size, const char *name )
832{
833 char *p = buf;
834 size_t n = size;
835 int ret;
836
837 if( strlen( name ) + sizeof( " key size" ) > size )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200838 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839
840 ret = snprintf( p, n, "%s key size", name );
841 SAFE_SNPRINTF();
842
843 return( 0 );
844}
845
846/*
847 * Return an informational string describing the given OID
848 */
849const char *x509_oid_get_description( x509_buf *oid )
850{
851 const char *desc = NULL;
852 int ret;
853
854 ret = oid_get_extended_key_usage( oid, &desc );
855
856 if( ret != 0 )
857 return( NULL );
858
859 return( desc );
860}
861
862/* Return the x.y.z.... style numeric string for the given OID */
863int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
864{
865 return oid_get_numeric_string( buf, size, oid );
866}
867
868/*
869 * Return 0 if the x509_time is still valid, or 1 otherwise.
870 */
871#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100873static void x509_get_current_time( x509_time *now )
874{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100875#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876 SYSTEMTIME st;
877
Paul Bakker66d5d072014-06-17 16:39:18 +0200878 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100880 now->year = st.wYear;
881 now->mon = st.wMonth;
882 now->day = st.wDay;
883 now->hour = st.wHour;
884 now->min = st.wMinute;
885 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100887 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 time_t tt;
889
890 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200891 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892
Paul Bakker5fff23b2014-03-26 15:34:54 +0100893 now->year = lt.tm_year + 1900;
894 now->mon = lt.tm_mon + 1;
895 now->day = lt.tm_mday;
896 now->hour = lt.tm_hour;
897 now->min = lt.tm_min;
898 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200899#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100900}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100902/*
903 * Return 0 if before <= after, 1 otherwise
904 */
905static int x509_check_time( const x509_time *before, const x509_time *after )
906{
907 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908 return( 1 );
909
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100910 if( before->year == after->year &&
911 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912 return( 1 );
913
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100914 if( before->year == after->year &&
915 before->mon == after->mon &&
916 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 return( 1 );
918
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100919 if( before->year == after->year &&
920 before->mon == after->mon &&
921 before->day == after->day &&
922 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923 return( 1 );
924
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100925 if( before->year == after->year &&
926 before->mon == after->mon &&
927 before->day == after->day &&
928 before->hour == after->hour &&
929 before->min > after->min )
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 &&
934 before->day == after->day &&
935 before->hour == after->hour &&
936 before->min == after->min &&
937 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 return( 1 );
939
940 return( 0 );
941}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100942
943int x509_time_expired( const x509_time *to )
944{
945 x509_time now;
946
947 x509_get_current_time( &now );
948
949 return( x509_check_time( &now, to ) );
950}
951
952int x509_time_future( const x509_time *from )
953{
954 x509_time now;
955
956 x509_get_current_time( &now );
957
958 return( x509_check_time( from, &now ) );
959}
960
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100962
Paul Bakker86d0c192013-09-18 11:11:02 +0200963int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964{
965 ((void) to);
966 return( 0 );
967}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100968
969int x509_time_future( const x509_time *from )
970{
971 ((void) from);
972 return( 0 );
973}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974#endif /* POLARSSL_HAVE_TIME */
975
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200976#if defined(POLARSSL_SELF_TEST)
977
978#include "polarssl/x509_crt.h"
979#include "polarssl/certs.h"
980
981/*
982 * Checkup routine
983 */
984int x509_self_test( int verbose )
985{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +0200986#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200987 int ret;
988 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200989 x509_crt cacert;
990 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200991
992 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100993 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200994
Paul Bakkerb6b09562013-09-18 14:17:41 +0200995 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200996
Paul Bakkerddf26b42013-09-18 13:46:23 +0200997 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
998 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200999 if( ret != 0 )
1000 {
1001 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001002 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001003
1004 return( ret );
1005 }
1006
Paul Bakkerb6b09562013-09-18 14:17:41 +02001007 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001008
Paul Bakkerddf26b42013-09-18 13:46:23 +02001009 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
1010 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001011 if( ret != 0 )
1012 {
1013 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001014 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001015
1016 return( ret );
1017 }
1018
1019 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001020 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001021
Paul Bakkerddf26b42013-09-18 13:46:23 +02001022 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
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
Paul Bakker66d5d072014-06-17 16:39:18 +02001028 polarssl_printf( "ret = %d, &flags = %04x\n", ret, flags );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001029
1030 return( ret );
1031 }
1032
1033 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001034 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001035
1036 x509_crt_free( &cacert );
1037 x509_crt_free( &clicert );
1038
1039 return( 0 );
1040#else
1041 ((void) verbose);
1042 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001043#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001044}
1045
Paul Bakker9af723c2014-05-01 13:03:14 +02001046#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001047
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048#endif /* POLARSSL_X509_USE_C */