blob: 3b6cd1bb12466a5dbd3f2ac04dc1597ac0155031 [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 Bakker7c6b2c32013-09-16 13:49:26 +020056#define polarssl_free free
Rich Evansfac657f2015-01-30 11:00:01 +000057#define polarssl_malloc malloc
58#define polarssl_printf printf
59#define polarssl_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Paul Bakkerfa6a6202013-10-28 18:48:30 +010062#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063#include <windows.h>
64#else
65#include <time.h>
66#endif
67
68#if defined(POLARSSL_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#if !defined(_WIN32)
70#include <sys/types.h>
71#include <sys/stat.h>
72#include <dirent.h>
73#endif
74#endif
75
76/*
77 * CertificateSerialNumber ::= INTEGER
78 */
79int x509_get_serial( unsigned char **p, const unsigned char *end,
80 x509_buf *serial )
81{
82 int ret;
83
84 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +020085 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086 POLARSSL_ERR_ASN1_OUT_OF_DATA );
87
88 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
89 **p != ASN1_INTEGER )
Paul Bakker51876562013-09-17 14:36:05 +020090 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
92
93 serial->tag = *(*p)++;
94
95 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +020096 return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097
98 serial->p = *p;
99 *p += serial->len;
100
101 return( 0 );
102}
103
104/* Get an algorithm identifier without parameters (eg for signatures)
105 *
106 * AlgorithmIdentifier ::= SEQUENCE {
107 * algorithm OBJECT IDENTIFIER,
108 * parameters ANY DEFINED BY algorithm OPTIONAL }
109 */
110int x509_get_alg_null( unsigned char **p, const unsigned char *end,
111 x509_buf *alg )
112{
113 int ret;
114
115 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200116 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117
118 return( 0 );
119}
120
121/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100122 * Parse an algorithm identifier with (optional) paramaters
123 */
124int x509_get_alg( unsigned char **p, const unsigned char *end,
125 x509_buf *alg, x509_buf *params )
126{
127 int ret;
128
129 if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
130 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
131
132 return( 0 );
133}
134
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200135#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100136/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100137 * HashAlgorithm ::= AlgorithmIdentifier
138 *
139 * AlgorithmIdentifier ::= SEQUENCE {
140 * algorithm OBJECT IDENTIFIER,
141 * parameters ANY DEFINED BY algorithm OPTIONAL }
142 *
143 * For HashAlgorithm, parameters MUST be NULL or absent.
144 */
145static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
146{
147 int ret;
148 unsigned char *p;
149 const unsigned char *end;
150 x509_buf md_oid;
151 size_t len;
152
153 /* Make sure we got a SEQUENCE and setup bounds */
154 if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
155 return( POLARSSL_ERR_X509_INVALID_ALG +
156 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
157
158 p = (unsigned char *) alg->p;
159 end = p + alg->len;
160
161 if( p >= end )
162 return( POLARSSL_ERR_X509_INVALID_ALG +
163 POLARSSL_ERR_ASN1_OUT_OF_DATA );
164
165 /* Parse md_oid */
166 md_oid.tag = *p;
167
168 if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
169 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
170
171 md_oid.p = p;
172 p += md_oid.len;
173
174 /* Get md_alg from md_oid */
175 if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
176 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
177
178 /* Make sure params is absent of NULL */
179 if( p == end )
180 return( 0 );
181
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100182 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100183 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
184
185 if( p != end )
186 return( POLARSSL_ERR_X509_INVALID_ALG +
187 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
188
189 return( 0 );
190}
191
192/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100193 * RSASSA-PSS-params ::= SEQUENCE {
194 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
195 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
196 * saltLength [2] INTEGER DEFAULT 20,
197 * trailerField [3] INTEGER DEFAULT 1 }
198 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200199 *
200 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
201 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
202 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100203 */
204int x509_get_rsassa_pss_params( const x509_buf *params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100205 md_type_t *md_alg, md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200206 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100207{
208 int ret;
209 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100210 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100211 size_t len;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100212 x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100213
214 /* First set everything to defaults */
215 *md_alg = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100216 *mgf_md = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100217 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100218
219 /* Make sure params is a SEQUENCE and setup bounds */
220 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
221 return( POLARSSL_ERR_X509_INVALID_ALG +
222 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
223
224 p = (unsigned char *) params->p;
225 end = p + params->len;
226
227 if( p == end )
228 return( 0 );
229
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100230 /*
231 * HashAlgorithm
232 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100233 if( ( ret = asn1_get_tag( &p, end, &len,
234 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
235 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100236 end2 = p + len;
237
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100238 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100239 if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100240 return( ret );
241
242 if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
243 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100244
245 if( p != end2 )
246 return( POLARSSL_ERR_X509_INVALID_ALG +
247 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100248 }
249 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
250 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
251
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100252 if( p == end )
253 return( 0 );
254
255 /*
256 * MaskGenAlgorithm
257 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258 if( ( ret = asn1_get_tag( &p, end, &len,
259 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
260 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100261 end2 = p + len;
262
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100263 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100264 if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100265 return( ret );
266
267 /* Only MFG1 is recognised for now */
268 if( ! OID_CMP( OID_MGF1, &alg_id ) )
269 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
270 POLARSSL_ERR_OID_NOT_FOUND );
271
272 /* Parse HashAlgorithm */
273 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
274 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100275
276 if( p != end2 )
277 return( POLARSSL_ERR_X509_INVALID_ALG +
278 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100279 }
280 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
281 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
282
283 if( p == end )
284 return( 0 );
285
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100286 /*
287 * salt_len
288 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100289 if( ( ret = asn1_get_tag( &p, end, &len,
290 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
291 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100292 end2 = p + len;
293
294 if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100295 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100296
297 if( p != end2 )
298 return( POLARSSL_ERR_X509_INVALID_ALG +
299 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100300 }
301 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
302 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
303
304 if( p == end )
305 return( 0 );
306
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100307 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200308 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100309 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100310 if( ( ret = asn1_get_tag( &p, end, &len,
311 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
312 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200313 int trailer_field;
314
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100315 end2 = p + len;
316
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200317 if( ( ret = asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100318 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100319
320 if( p != end2 )
321 return( POLARSSL_ERR_X509_INVALID_ALG +
322 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200323
324 if( trailer_field != 1 )
325 return( POLARSSL_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100326 }
327 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
328 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
329
330 if( p != end )
331 return( POLARSSL_ERR_X509_INVALID_ALG +
332 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
333
334 return( 0 );
335}
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200336#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100337
338/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339 * AttributeTypeAndValue ::= SEQUENCE {
340 * type AttributeType,
341 * value AttributeValue }
342 *
343 * AttributeType ::= OBJECT IDENTIFIER
344 *
345 * AttributeValue ::= ANY DEFINED BY AttributeType
346 */
347static int x509_get_attr_type_value( unsigned char **p,
348 const unsigned char *end,
349 x509_name *cur )
350{
351 int ret;
352 size_t len;
353 x509_buf *oid;
354 x509_buf *val;
355
356 if( ( ret = asn1_get_tag( p, end, &len,
357 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200358 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
360 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200361 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 POLARSSL_ERR_ASN1_OUT_OF_DATA );
363
364 oid = &cur->oid;
365 oid->tag = **p;
366
367 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200368 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369
370 oid->p = *p;
371 *p += oid->len;
372
373 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200374 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375 POLARSSL_ERR_ASN1_OUT_OF_DATA );
376
377 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
378 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
379 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200380 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
382
383 val = &cur->val;
384 val->tag = *(*p)++;
385
386 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200387 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388
389 val->p = *p;
390 *p += val->len;
391
392 cur->next = NULL;
393
394 return( 0 );
395}
396
397/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000398 * Name ::= CHOICE { -- only one possibility for now --
399 * rdnSequence RDNSequence }
400 *
401 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
402 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 * RelativeDistinguishedName ::=
404 * SET OF AttributeTypeAndValue
405 *
406 * AttributeTypeAndValue ::= SEQUENCE {
407 * type AttributeType,
408 * value AttributeValue }
409 *
410 * AttributeType ::= OBJECT IDENTIFIER
411 *
412 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200413 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000414 * The data structure is optimized for the common case where each RDN has only
415 * one element, which is represented as a list of AttributeTypeAndValue.
416 * For the general case we still use a flat list, but we mark elements of the
417 * same set so that they are "merged" together in the functions that consume
418 * this list, eg x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 */
420int x509_get_name( unsigned char **p, const unsigned char *end,
421 x509_name *cur )
422{
423 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200424 size_t set_len;
425 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100427 /* don't use recursion, we'd risk stack overflow if not optimized */
428 while( 1 )
429 {
430 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000431 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100432 */
433 if( ( ret = asn1_get_tag( p, end, &set_len,
434 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
435 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100437 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000439 while( 1 )
440 {
441 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
442 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000444 if( *p == end_set )
445 break;
446
447 /* Mark this item as being only one in a set */
448 cur->next_merged = 1;
449
450 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
451
452 if( cur->next == NULL )
453 return( POLARSSL_ERR_X509_MALLOC_FAILED );
454
455 memset( cur->next, 0, sizeof( x509_name ) );
456
457 cur = cur->next;
458 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100460 /*
461 * continue until end of SEQUENCE is reached
462 */
463 if( *p == end )
464 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100466 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100468 if( cur->next == NULL )
469 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100471 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100473 cur = cur->next;
474 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475}
476
477/*
478 * Time ::= CHOICE {
479 * utcTime UTCTime,
480 * generalTime GeneralizedTime }
481 */
482int x509_get_time( unsigned char **p, const unsigned char *end,
483 x509_time *time )
484{
485 int ret;
486 size_t len;
487 char date[64];
488 unsigned char tag;
489
490 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200491 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492 POLARSSL_ERR_ASN1_OUT_OF_DATA );
493
494 tag = **p;
495
Paul Bakker66d5d072014-06-17 16:39:18 +0200496 if( tag == ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 {
498 (*p)++;
499 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200500
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200502 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
504 memset( date, 0, sizeof( date ) );
505 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
506 len : sizeof( date ) - 1 );
507
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200508 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509 &time->year, &time->mon, &time->day,
510 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200511 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 time->year += 100 * ( time->year < 50 );
514 time->year += 1900;
515
516 *p += len;
517
518 return( 0 );
519 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200520 else if( tag == ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521 {
522 (*p)++;
523 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200524
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200526 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
528 memset( date, 0, sizeof( date ) );
529 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
530 len : sizeof( date ) - 1 );
531
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200532 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 &time->year, &time->mon, &time->day,
534 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200535 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
537 *p += len;
538
539 return( 0 );
540 }
541 else
Paul Bakker51876562013-09-17 14:36:05 +0200542 return( POLARSSL_ERR_X509_INVALID_DATE +
543 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544}
545
546int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
547{
548 int ret;
549 size_t len;
550
551 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200552 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553 POLARSSL_ERR_ASN1_OUT_OF_DATA );
554
555 sig->tag = **p;
556
557 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200558 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
560 sig->len = len;
561 sig->p = *p;
562
563 *p += len;
564
565 return( 0 );
566}
567
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100568/*
569 * Get signature algorithm from alg OID and optional parameters
570 */
571int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200572 md_type_t *md_alg, pk_type_t *pk_alg,
573 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100575 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200577 if( *sig_opts != NULL )
578 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
579
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100580 if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200581 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200583#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100584 if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
585 {
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200586 pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100587
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200588 pss_opts = polarssl_malloc( sizeof( pk_rsassa_pss_options ) );
589 if( pss_opts == NULL )
590 return( POLARSSL_ERR_X509_MALLOC_FAILED );
591
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100592 ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200593 md_alg,
594 &pss_opts->mgf1_hash_id,
595 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100596 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200597 {
598 polarssl_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100599 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200600 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100601
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200602 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100603 }
604 else
Paul Bakkerdb20c102014-06-17 14:34:44 +0200605#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100606 {
607 /* Make sure parameters are absent or NULL */
608 if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
609 sig_params->len != 0 )
610 return( POLARSSL_ERR_X509_INVALID_ALG );
611 }
612
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613 return( 0 );
614}
615
616/*
617 * X.509 Extensions (No parsing of extensions, pointer should
618 * be either manually updated or extensions should be parsed!
619 */
620int x509_get_ext( unsigned char **p, const unsigned char *end,
621 x509_buf *ext, int tag )
622{
623 int ret;
624 size_t len;
625
626 if( *p == end )
627 return( 0 );
628
629 ext->tag = **p;
630
631 if( ( ret = asn1_get_tag( p, end, &ext->len,
632 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
633 return( ret );
634
635 ext->p = *p;
636 end = *p + ext->len;
637
638 /*
639 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
640 *
641 * Extension ::= SEQUENCE {
642 * extnID OBJECT IDENTIFIER,
643 * critical BOOLEAN DEFAULT FALSE,
644 * extnValue OCTET STRING }
645 */
646 if( ( ret = asn1_get_tag( p, end, &len,
647 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200648 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649
650 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200651 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
653
654 return( 0 );
655}
656
Paul Bakker6edcd412013-10-29 15:22:54 +0100657#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
658 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659#include <stdarg.h>
660
661#if !defined vsnprintf
662#define vsnprintf _vsnprintf
663#endif // vsnprintf
664
665/*
666 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
667 * Result value is not size of buffer needed, but -1 if no fit is possible.
668 *
669 * This fuction tries to 'fix' this by at least suggesting enlarging the
670 * size by 20.
671 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200672static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673{
674 va_list ap;
675 int res = -1;
676
677 va_start( ap, format );
678
679 res = vsnprintf( str, size, format, ap );
680
681 va_end( ap );
682
683 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200684 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685 return( (int) size + 20 );
686
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200687 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688}
689
690#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200691#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692
693#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
694
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200695#define SAFE_SNPRINTF() \
696{ \
697 if( ret == -1 ) \
698 return( -1 ); \
699 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200700 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200701 p[n - 1] = '\0'; \
702 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
703 } \
704 \
705 n -= (unsigned int) ret; \
706 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707}
708
709/*
710 * Store the name in printable form into buf; no more
711 * than size characters will be written
712 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200713int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714{
715 int ret;
716 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000717 unsigned char c, merge = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200718 const x509_name *name;
719 const char *short_name = NULL;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200720 char s[X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721
722 memset( s, 0, sizeof( s ) );
723
724 name = dn;
725 p = buf;
726 n = size;
727
728 while( name != NULL )
729 {
730 if( !name->oid.p )
731 {
732 name = name->next;
733 continue;
734 }
735
736 if( name != dn )
737 {
Rich Evansfac657f2015-01-30 11:00:01 +0000738 ret = polarssl_snprintf( p, n, merge ? " + " : ", " );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 SAFE_SNPRINTF();
740 }
741
742 ret = oid_get_attr_short_name( &name->oid, &short_name );
743
744 if( ret == 0 )
Rich Evansfac657f2015-01-30 11:00:01 +0000745 ret = polarssl_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 else
Rich Evansfac657f2015-01-30 11:00:01 +0000747 ret = polarssl_snprintf( p, n, "\?\?=" );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 SAFE_SNPRINTF();
749
750 for( i = 0; i < name->val.len; i++ )
751 {
752 if( i >= sizeof( s ) - 1 )
753 break;
754
755 c = name->val.p[i];
756 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
757 s[i] = '?';
758 else s[i] = c;
759 }
760 s[i] = '\0';
Rich Evansfac657f2015-01-30 11:00:01 +0000761 ret = polarssl_snprintf( p, n, "%s", s );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000763
764 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 name = name->next;
766 }
767
768 return( (int) ( size - n ) );
769}
770
771/*
772 * Store the serial in printable form into buf; no more
773 * than size characters will be written
774 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200775int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776{
777 int ret;
778 size_t i, n, nr;
779 char *p;
780
781 p = buf;
782 n = size;
783
784 nr = ( serial->len <= 32 )
785 ? serial->len : 28;
786
787 for( i = 0; i < nr; i++ )
788 {
789 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
790 continue;
791
Rich Evansfac657f2015-01-30 11:00:01 +0000792 ret = polarssl_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
794 SAFE_SNPRINTF();
795 }
796
797 if( nr != serial->len )
798 {
Rich Evansfac657f2015-01-30 11:00:01 +0000799 ret = polarssl_snprintf( p, n, "...." );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800 SAFE_SNPRINTF();
801 }
802
803 return( (int) ( size - n ) );
804}
805
806/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200807 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100808 */
809int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200810 pk_type_t pk_alg, md_type_t md_alg,
811 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100812{
813 int ret;
814 char *p = buf;
815 size_t n = size;
816 const char *desc = NULL;
817
818 ret = oid_get_sig_alg_desc( sig_oid, &desc );
819 if( ret != 0 )
Rich Evansfac657f2015-01-30 11:00:01 +0000820 ret = polarssl_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100821 else
Rich Evansfac657f2015-01-30 11:00:01 +0000822 ret = polarssl_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100823 SAFE_SNPRINTF();
824
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200825#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100826 if( pk_alg == POLARSSL_PK_RSASSA_PSS )
827 {
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200828 const pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100829 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100830
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200831 pss_opts = (const pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100832
833 md_info = md_info_from_type( md_alg );
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200834 mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100835
Rich Evansfac657f2015-01-30 11:00:01 +0000836 ret = polarssl_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100837 md_info ? md_info->name : "???",
838 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200839 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100840 SAFE_SNPRINTF();
841 }
842#else
843 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200844 ((void) md_alg);
845 ((void) sig_opts);
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200846#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100847
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200848 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100849}
850
851/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852 * Helper for writing "RSA key size", "EC key size", etc
853 */
854int x509_key_size_helper( char *buf, size_t size, const char *name )
855{
856 char *p = buf;
857 size_t n = size;
858 int ret;
859
860 if( strlen( name ) + sizeof( " key size" ) > size )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200861 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862
Rich Evansfac657f2015-01-30 11:00:01 +0000863 ret = polarssl_snprintf( p, n, "%s key size", name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864 SAFE_SNPRINTF();
865
866 return( 0 );
867}
868
869/*
870 * Return an informational string describing the given OID
871 */
872const char *x509_oid_get_description( x509_buf *oid )
873{
874 const char *desc = NULL;
875 int ret;
876
877 ret = oid_get_extended_key_usage( oid, &desc );
878
879 if( ret != 0 )
880 return( NULL );
881
882 return( desc );
883}
884
885/* Return the x.y.z.... style numeric string for the given OID */
886int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
887{
888 return oid_get_numeric_string( buf, size, oid );
889}
890
891/*
892 * Return 0 if the x509_time is still valid, or 1 otherwise.
893 */
894#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100896static void x509_get_current_time( x509_time *now )
897{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100898#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 SYSTEMTIME st;
900
Paul Bakker66d5d072014-06-17 16:39:18 +0200901 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100903 now->year = st.wYear;
904 now->mon = st.wMonth;
905 now->day = st.wDay;
906 now->hour = st.wHour;
907 now->min = st.wMinute;
908 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100910 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 time_t tt;
912
913 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200914 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915
Paul Bakker5fff23b2014-03-26 15:34:54 +0100916 now->year = lt.tm_year + 1900;
917 now->mon = lt.tm_mon + 1;
918 now->day = lt.tm_mday;
919 now->hour = lt.tm_hour;
920 now->min = lt.tm_min;
921 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200922#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100923}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100925/*
926 * Return 0 if before <= after, 1 otherwise
927 */
928static int x509_check_time( const x509_time *before, const x509_time *after )
929{
930 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 return( 1 );
932
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100933 if( before->year == after->year &&
934 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 return( 1 );
936
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100937 if( before->year == after->year &&
938 before->mon == after->mon &&
939 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940 return( 1 );
941
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100942 if( before->year == after->year &&
943 before->mon == after->mon &&
944 before->day == after->day &&
945 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946 return( 1 );
947
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100948 if( before->year == after->year &&
949 before->mon == after->mon &&
950 before->day == after->day &&
951 before->hour == after->hour &&
952 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953 return( 1 );
954
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100955 if( before->year == after->year &&
956 before->mon == after->mon &&
957 before->day == after->day &&
958 before->hour == after->hour &&
959 before->min == after->min &&
960 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 return( 1 );
962
963 return( 0 );
964}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100965
966int x509_time_expired( const x509_time *to )
967{
968 x509_time now;
969
970 x509_get_current_time( &now );
971
972 return( x509_check_time( &now, to ) );
973}
974
975int x509_time_future( const x509_time *from )
976{
977 x509_time now;
978
979 x509_get_current_time( &now );
980
981 return( x509_check_time( from, &now ) );
982}
983
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100985
Paul Bakker86d0c192013-09-18 11:11:02 +0200986int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987{
988 ((void) to);
989 return( 0 );
990}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100991
992int x509_time_future( const x509_time *from )
993{
994 ((void) from);
995 return( 0 );
996}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997#endif /* POLARSSL_HAVE_TIME */
998
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200999#if defined(POLARSSL_SELF_TEST)
1000
1001#include "polarssl/x509_crt.h"
1002#include "polarssl/certs.h"
1003
1004/*
1005 * Checkup routine
1006 */
1007int x509_self_test( int verbose )
1008{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +02001009#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001010 int ret;
1011 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001012 x509_crt cacert;
1013 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001014
1015 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001016 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001017
Paul Bakkerb6b09562013-09-18 14:17:41 +02001018 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001019
Paul Bakkerddf26b42013-09-18 13:46:23 +02001020 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
1021 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001022 if( ret != 0 )
1023 {
1024 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001025 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001026
1027 return( ret );
1028 }
1029
Paul Bakkerb6b09562013-09-18 14:17:41 +02001030 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001031
Paul Bakkerddf26b42013-09-18 13:46:23 +02001032 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
1033 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001034 if( ret != 0 )
1035 {
1036 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001037 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001038
1039 return( ret );
1040 }
1041
1042 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001043 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001044
Paul Bakkerddf26b42013-09-18 13:46:23 +02001045 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001046 if( ret != 0 )
1047 {
1048 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001049 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001050
Paul Bakker66d5d072014-06-17 16:39:18 +02001051 polarssl_printf( "ret = %d, &flags = %04x\n", ret, flags );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001052
1053 return( ret );
1054 }
1055
1056 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001057 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001058
1059 x509_crt_free( &cacert );
1060 x509_crt_free( &clicert );
1061
1062 return( 0 );
1063#else
1064 ((void) verbose);
1065 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001066#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001067}
1068
Paul Bakker9af723c2014-05-01 13:03:14 +02001069#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001070
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071#endif /* POLARSSL_X509_USE_C */