blob: 48c11ef71627178a252f5ad9e45cf5dcc8bb9ad7 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ITU-T X.509 standard defines a certificate format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc3279.txt
29 * http://www.ietf.org/rfc/rfc3280.txt
30 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020039#else
40#include POLARSSL_CONFIG_FILE
41#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
43#if defined(POLARSSL_X509_USE_C)
44
45#include "polarssl/x509.h"
46#include "polarssl/asn1.h"
47#include "polarssl/oid.h"
48#if defined(POLARSSL_PEM_PARSE_C)
49#include "polarssl/pem.h"
50#endif
51
Paul Bakker7dc4c442014-02-01 22:50:26 +010052#if defined(POLARSSL_PLATFORM_C)
53#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010055#define polarssl_printf printf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
60#include <string.h>
61#include <stdlib.h>
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
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#if defined(EFIX64) || defined(EFI32)
69#include <stdio.h>
70#endif
71
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#if defined(POLARSSL_FS_IO)
73#include <stdio.h>
74#if !defined(_WIN32)
75#include <sys/types.h>
76#include <sys/stat.h>
77#include <dirent.h>
78#endif
79#endif
80
81/*
82 * CertificateSerialNumber ::= INTEGER
83 */
84int x509_get_serial( unsigned char **p, const unsigned char *end,
85 x509_buf *serial )
86{
87 int ret;
88
89 if( ( end - *p ) < 1 )
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_OUT_OF_DATA );
92
93 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
94 **p != ASN1_INTEGER )
Paul Bakker51876562013-09-17 14:36:05 +020095 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
97
98 serial->tag = *(*p)++;
99
100 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200101 return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102
103 serial->p = *p;
104 *p += serial->len;
105
106 return( 0 );
107}
108
109/* Get an algorithm identifier without parameters (eg for signatures)
110 *
111 * AlgorithmIdentifier ::= SEQUENCE {
112 * algorithm OBJECT IDENTIFIER,
113 * parameters ANY DEFINED BY algorithm OPTIONAL }
114 */
115int x509_get_alg_null( unsigned char **p, const unsigned char *end,
116 x509_buf *alg )
117{
118 int ret;
119
120 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200121 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
123 return( 0 );
124}
125
126/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100127 * Parse an algorithm identifier with (optional) paramaters
128 */
129int x509_get_alg( unsigned char **p, const unsigned char *end,
130 x509_buf *alg, x509_buf *params )
131{
132 int ret;
133
134 if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
135 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
136
137 return( 0 );
138}
139
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100140#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100141/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100142 * HashAlgorithm ::= AlgorithmIdentifier
143 *
144 * AlgorithmIdentifier ::= SEQUENCE {
145 * algorithm OBJECT IDENTIFIER,
146 * parameters ANY DEFINED BY algorithm OPTIONAL }
147 *
148 * For HashAlgorithm, parameters MUST be NULL or absent.
149 */
150static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
151{
152 int ret;
153 unsigned char *p;
154 const unsigned char *end;
155 x509_buf md_oid;
156 size_t len;
157
158 /* Make sure we got a SEQUENCE and setup bounds */
159 if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
160 return( POLARSSL_ERR_X509_INVALID_ALG +
161 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
162
163 p = (unsigned char *) alg->p;
164 end = p + alg->len;
165
166 if( p >= end )
167 return( POLARSSL_ERR_X509_INVALID_ALG +
168 POLARSSL_ERR_ASN1_OUT_OF_DATA );
169
170 /* Parse md_oid */
171 md_oid.tag = *p;
172
173 if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
174 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
175
176 md_oid.p = p;
177 p += md_oid.len;
178
179 /* Get md_alg from md_oid */
180 if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
181 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
182
183 /* Make sure params is absent of NULL */
184 if( p == end )
185 return( 0 );
186
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100187 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100188 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
189
190 if( p != end )
191 return( POLARSSL_ERR_X509_INVALID_ALG +
192 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
193
194 return( 0 );
195}
196
197/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100198 * RSASSA-PSS-params ::= SEQUENCE {
199 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
200 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
201 * saltLength [2] INTEGER DEFAULT 20,
202 * trailerField [3] INTEGER DEFAULT 1 }
203 * -- Note that the tags in this Sequence are explicit.
204 */
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,
207 int *salt_len, int *trailer_field )
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;
219 *trailer_field = 1;
220
221 /* Make sure params is a SEQUENCE and setup bounds */
222 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
223 return( POLARSSL_ERR_X509_INVALID_ALG +
224 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
225
226 p = (unsigned char *) params->p;
227 end = p + params->len;
228
229 if( p == end )
230 return( 0 );
231
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100232 /*
233 * HashAlgorithm
234 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100235 if( ( ret = asn1_get_tag( &p, end, &len,
236 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
237 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100238 end2 = p + len;
239
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100240 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100241 if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100242 return( ret );
243
244 if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
245 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100246
247 if( p != end2 )
248 return( POLARSSL_ERR_X509_INVALID_ALG +
249 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250 }
251 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
252 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
253
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100254 if( p == end )
255 return( 0 );
256
257 /*
258 * MaskGenAlgorithm
259 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100260 if( ( ret = asn1_get_tag( &p, end, &len,
261 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
262 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100263 end2 = p + len;
264
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100265 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100266 if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100267 return( ret );
268
269 /* Only MFG1 is recognised for now */
270 if( ! OID_CMP( OID_MGF1, &alg_id ) )
271 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
272 POLARSSL_ERR_OID_NOT_FOUND );
273
274 /* Parse HashAlgorithm */
275 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
276 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100277
278 if( p != end2 )
279 return( POLARSSL_ERR_X509_INVALID_ALG +
280 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100281 }
282 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
283 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
284
285 if( p == end )
286 return( 0 );
287
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100288 /*
289 * salt_len
290 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100291 if( ( ret = asn1_get_tag( &p, end, &len,
292 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
293 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100294 end2 = p + len;
295
296 if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100297 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100298
299 if( p != end2 )
300 return( POLARSSL_ERR_X509_INVALID_ALG +
301 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100302 }
303 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
304 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
305
306 if( p == end )
307 return( 0 );
308
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100309 /*
310 * trailer_field
311 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100312 if( ( ret = asn1_get_tag( &p, end, &len,
313 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
314 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100315 end2 = p + len;
316
317 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é-Gonnardf346bab2014-01-23 16:24:44 +0100323 }
324 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
325 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
326
327 if( p != end )
328 return( POLARSSL_ERR_X509_INVALID_ALG +
329 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
330
331 return( 0 );
332}
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100333#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334
335/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336 * AttributeTypeAndValue ::= SEQUENCE {
337 * type AttributeType,
338 * value AttributeValue }
339 *
340 * AttributeType ::= OBJECT IDENTIFIER
341 *
342 * AttributeValue ::= ANY DEFINED BY AttributeType
343 */
344static int x509_get_attr_type_value( unsigned char **p,
345 const unsigned char *end,
346 x509_name *cur )
347{
348 int ret;
349 size_t len;
350 x509_buf *oid;
351 x509_buf *val;
352
353 if( ( ret = asn1_get_tag( p, end, &len,
354 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200355 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356
357 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200358 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 POLARSSL_ERR_ASN1_OUT_OF_DATA );
360
361 oid = &cur->oid;
362 oid->tag = **p;
363
364 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200365 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366
367 oid->p = *p;
368 *p += oid->len;
369
370 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200371 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372 POLARSSL_ERR_ASN1_OUT_OF_DATA );
373
374 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
375 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
376 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200377 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
379
380 val = &cur->val;
381 val->tag = *(*p)++;
382
383 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200384 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385
386 val->p = *p;
387 *p += val->len;
388
389 cur->next = NULL;
390
391 return( 0 );
392}
393
394/*
395 * RelativeDistinguishedName ::=
396 * SET OF AttributeTypeAndValue
397 *
398 * AttributeTypeAndValue ::= SEQUENCE {
399 * type AttributeType,
400 * value AttributeValue }
401 *
402 * AttributeType ::= OBJECT IDENTIFIER
403 *
404 * AttributeValue ::= ANY DEFINED BY AttributeType
405 */
406int x509_get_name( unsigned char **p, const unsigned char *end,
407 x509_name *cur )
408{
409 int ret;
410 size_t len;
411 const unsigned char *end2;
412 x509_name *use;
413
414 if( ( ret = asn1_get_tag( p, end, &len,
415 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200416 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
418 end2 = end;
419 end = *p + len;
420 use = cur;
421
422 do
423 {
424 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
425 return( ret );
426
427 if( *p != end )
428 {
429 use->next = (x509_name *) polarssl_malloc(
430 sizeof( x509_name ) );
431
432 if( use->next == NULL )
433 return( POLARSSL_ERR_X509_MALLOC_FAILED );
434
435 memset( use->next, 0, sizeof( x509_name ) );
436
437 use = use->next;
438 }
439 }
440 while( *p != end );
441
442 /*
443 * recurse until end of SEQUENCE is reached
444 */
445 if( *p == end2 )
446 return( 0 );
447
448 cur->next = (x509_name *) polarssl_malloc(
449 sizeof( x509_name ) );
450
451 if( cur->next == NULL )
452 return( POLARSSL_ERR_X509_MALLOC_FAILED );
453
454 memset( cur->next, 0, sizeof( x509_name ) );
455
456 return( x509_get_name( p, end2, cur->next ) );
457}
458
459/*
460 * Time ::= CHOICE {
461 * utcTime UTCTime,
462 * generalTime GeneralizedTime }
463 */
464int x509_get_time( unsigned char **p, const unsigned char *end,
465 x509_time *time )
466{
467 int ret;
468 size_t len;
469 char date[64];
470 unsigned char tag;
471
472 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200473 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 POLARSSL_ERR_ASN1_OUT_OF_DATA );
475
476 tag = **p;
477
478 if ( tag == ASN1_UTC_TIME )
479 {
480 (*p)++;
481 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200482
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200484 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485
486 memset( date, 0, sizeof( date ) );
487 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
488 len : sizeof( date ) - 1 );
489
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200490 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 &time->year, &time->mon, &time->day,
492 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200493 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
495 time->year += 100 * ( time->year < 50 );
496 time->year += 1900;
497
498 *p += len;
499
500 return( 0 );
501 }
502 else if ( tag == ASN1_GENERALIZED_TIME )
503 {
504 (*p)++;
505 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200506
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200508 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
510 memset( date, 0, sizeof( date ) );
511 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
512 len : sizeof( date ) - 1 );
513
Manuel Pégourié-Gonnard9655e452014-04-11 12:29:49 +0200514 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515 &time->year, &time->mon, &time->day,
516 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200517 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
519 *p += len;
520
521 return( 0 );
522 }
523 else
Paul Bakker51876562013-09-17 14:36:05 +0200524 return( POLARSSL_ERR_X509_INVALID_DATE +
525 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526}
527
528int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
529{
530 int ret;
531 size_t len;
532
533 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200534 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 POLARSSL_ERR_ASN1_OUT_OF_DATA );
536
537 sig->tag = **p;
538
539 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200540 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 sig->len = len;
543 sig->p = *p;
544
545 *p += len;
546
547 return( 0 );
548}
549
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100550int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
551 pk_type_t *pk_alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552{
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100553 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100555 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200556 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
558 return( 0 );
559}
560
561/*
562 * X.509 Extensions (No parsing of extensions, pointer should
563 * be either manually updated or extensions should be parsed!
564 */
565int x509_get_ext( unsigned char **p, const unsigned char *end,
566 x509_buf *ext, int tag )
567{
568 int ret;
569 size_t len;
570
571 if( *p == end )
572 return( 0 );
573
574 ext->tag = **p;
575
576 if( ( ret = asn1_get_tag( p, end, &ext->len,
577 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
578 return( ret );
579
580 ext->p = *p;
581 end = *p + ext->len;
582
583 /*
584 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
585 *
586 * Extension ::= SEQUENCE {
587 * extnID OBJECT IDENTIFIER,
588 * critical BOOLEAN DEFAULT FALSE,
589 * extnValue OCTET STRING }
590 */
591 if( ( ret = asn1_get_tag( p, end, &len,
592 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200593 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
595 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200596 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
598
599 return( 0 );
600}
601
602#if defined(POLARSSL_FS_IO)
603/*
604 * Load all data from a file into a given buffer.
605 */
606int x509_load_file( const char *path, unsigned char **buf, size_t *n )
607{
608 FILE *f;
609 long size;
610
611 if( ( f = fopen( path, "rb" ) ) == NULL )
612 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
613
614 fseek( f, 0, SEEK_END );
615 if( ( size = ftell( f ) ) == -1 )
616 {
617 fclose( f );
618 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
619 }
620 fseek( f, 0, SEEK_SET );
621
622 *n = (size_t) size;
623
624 if( *n + 1 == 0 ||
625 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
626 {
627 fclose( f );
628 return( POLARSSL_ERR_X509_MALLOC_FAILED );
629 }
630
631 if( fread( *buf, 1, *n, f ) != *n )
632 {
633 fclose( f );
634 polarssl_free( *buf );
635 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
636 }
637
638 fclose( f );
639
640 (*buf)[*n] = '\0';
641
642 return( 0 );
643}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644#endif /* POLARSSL_FS_IO */
645
Paul Bakker6edcd412013-10-29 15:22:54 +0100646#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
647 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648#include <stdarg.h>
649
650#if !defined vsnprintf
651#define vsnprintf _vsnprintf
652#endif // vsnprintf
653
654/*
655 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
656 * Result value is not size of buffer needed, but -1 if no fit is possible.
657 *
658 * This fuction tries to 'fix' this by at least suggesting enlarging the
659 * size by 20.
660 */
661static int compat_snprintf(char *str, size_t size, const char *format, ...)
662{
663 va_list ap;
664 int res = -1;
665
666 va_start( ap, format );
667
668 res = vsnprintf( str, size, format, ap );
669
670 va_end( ap );
671
672 // No quick fix possible
673 if ( res < 0 )
674 return( (int) size + 20 );
675
676 return res;
677}
678
679#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200680#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
683
684#define SAFE_SNPRINTF() \
685{ \
686 if( ret == -1 ) \
687 return( -1 ); \
688 \
689 if ( (unsigned int) ret > n ) { \
690 p[n - 1] = '\0'; \
691 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
692 } \
693 \
694 n -= (unsigned int) ret; \
695 p += (unsigned int) ret; \
696}
697
698/*
699 * Store the name in printable form into buf; no more
700 * than size characters will be written
701 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200702int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703{
704 int ret;
705 size_t i, n;
706 unsigned char c;
707 const x509_name *name;
708 const char *short_name = NULL;
709 char s[128], *p;
710
711 memset( s, 0, sizeof( s ) );
712
713 name = dn;
714 p = buf;
715 n = size;
716
717 while( name != NULL )
718 {
719 if( !name->oid.p )
720 {
721 name = name->next;
722 continue;
723 }
724
725 if( name != dn )
726 {
727 ret = snprintf( p, n, ", " );
728 SAFE_SNPRINTF();
729 }
730
731 ret = oid_get_attr_short_name( &name->oid, &short_name );
732
733 if( ret == 0 )
734 ret = snprintf( p, n, "%s=", short_name );
735 else
736 ret = snprintf( p, n, "\?\?=" );
737 SAFE_SNPRINTF();
738
739 for( i = 0; i < name->val.len; i++ )
740 {
741 if( i >= sizeof( s ) - 1 )
742 break;
743
744 c = name->val.p[i];
745 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
746 s[i] = '?';
747 else s[i] = c;
748 }
749 s[i] = '\0';
750 ret = snprintf( p, n, "%s", s );
751 SAFE_SNPRINTF();
752 name = name->next;
753 }
754
755 return( (int) ( size - n ) );
756}
757
758/*
759 * Store the serial in printable form into buf; no more
760 * than size characters will be written
761 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200762int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763{
764 int ret;
765 size_t i, n, nr;
766 char *p;
767
768 p = buf;
769 n = size;
770
771 nr = ( serial->len <= 32 )
772 ? serial->len : 28;
773
774 for( i = 0; i < nr; i++ )
775 {
776 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
777 continue;
778
779 ret = snprintf( p, n, "%02X%s",
780 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
781 SAFE_SNPRINTF();
782 }
783
784 if( nr != serial->len )
785 {
786 ret = snprintf( p, n, "...." );
787 SAFE_SNPRINTF();
788 }
789
790 return( (int) ( size - n ) );
791}
792
793/*
794 * Helper for writing "RSA key size", "EC key size", etc
795 */
796int x509_key_size_helper( char *buf, size_t size, const char *name )
797{
798 char *p = buf;
799 size_t n = size;
800 int ret;
801
802 if( strlen( name ) + sizeof( " key size" ) > size )
803 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
804
805 ret = snprintf( p, n, "%s key size", name );
806 SAFE_SNPRINTF();
807
808 return( 0 );
809}
810
811/*
812 * Return an informational string describing the given OID
813 */
814const char *x509_oid_get_description( x509_buf *oid )
815{
816 const char *desc = NULL;
817 int ret;
818
819 ret = oid_get_extended_key_usage( oid, &desc );
820
821 if( ret != 0 )
822 return( NULL );
823
824 return( desc );
825}
826
827/* Return the x.y.z.... style numeric string for the given OID */
828int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
829{
830 return oid_get_numeric_string( buf, size, oid );
831}
832
833/*
834 * Return 0 if the x509_time is still valid, or 1 otherwise.
835 */
836#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100838static void x509_get_current_time( x509_time *now )
839{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100840#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200841 SYSTEMTIME st;
842
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200843 GetSystemTime(&st);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100845 now->year = st.wYear;
846 now->mon = st.wMonth;
847 now->day = st.wDay;
848 now->hour = st.wHour;
849 now->min = st.wMinute;
850 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100852 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853 time_t tt;
854
855 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200856 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857
Paul Bakker5fff23b2014-03-26 15:34:54 +0100858 now->year = lt.tm_year + 1900;
859 now->mon = lt.tm_mon + 1;
860 now->day = lt.tm_mday;
861 now->hour = lt.tm_hour;
862 now->min = lt.tm_min;
863 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200864#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100865}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100867/*
868 * Return 0 if before <= after, 1 otherwise
869 */
870static int x509_check_time( const x509_time *before, const x509_time *after )
871{
872 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873 return( 1 );
874
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100875 if( before->year == after->year &&
876 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877 return( 1 );
878
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100879 if( before->year == after->year &&
880 before->mon == after->mon &&
881 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 return( 1 );
883
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100884 if( before->year == after->year &&
885 before->mon == after->mon &&
886 before->day == after->day &&
887 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 return( 1 );
889
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100890 if( before->year == after->year &&
891 before->mon == after->mon &&
892 before->day == after->day &&
893 before->hour == after->hour &&
894 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 return( 1 );
896
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100897 if( before->year == after->year &&
898 before->mon == after->mon &&
899 before->day == after->day &&
900 before->hour == after->hour &&
901 before->min == after->min &&
902 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 return( 1 );
904
905 return( 0 );
906}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100907
908int x509_time_expired( const x509_time *to )
909{
910 x509_time now;
911
912 x509_get_current_time( &now );
913
914 return( x509_check_time( &now, to ) );
915}
916
917int x509_time_future( const x509_time *from )
918{
919 x509_time now;
920
921 x509_get_current_time( &now );
922
923 return( x509_check_time( from, &now ) );
924}
925
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100927
Paul Bakker86d0c192013-09-18 11:11:02 +0200928int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929{
930 ((void) to);
931 return( 0 );
932}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100933
934int x509_time_future( const x509_time *from )
935{
936 ((void) from);
937 return( 0 );
938}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939#endif /* POLARSSL_HAVE_TIME */
940
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200941#if defined(POLARSSL_SELF_TEST)
942
943#include "polarssl/x509_crt.h"
944#include "polarssl/certs.h"
945
946/*
947 * Checkup routine
948 */
949int x509_self_test( int verbose )
950{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +0200951#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200952 int ret;
953 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200954 x509_crt cacert;
955 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200956
957 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100958 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200959
Paul Bakkerb6b09562013-09-18 14:17:41 +0200960 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200961
Paul Bakkerddf26b42013-09-18 13:46:23 +0200962 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
963 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200964 if( ret != 0 )
965 {
966 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100967 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200968
969 return( ret );
970 }
971
Paul Bakkerb6b09562013-09-18 14:17:41 +0200972 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200973
Paul Bakkerddf26b42013-09-18 13:46:23 +0200974 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
975 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200976 if( ret != 0 )
977 {
978 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100979 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200980
981 return( ret );
982 }
983
984 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100985 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200986
Paul Bakkerddf26b42013-09-18 13:46:23 +0200987 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200988 if( ret != 0 )
989 {
990 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100991 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200992
Paul Bakker7dc4c442014-02-01 22:50:26 +0100993 polarssl_printf("ret = %d, &flags = %04x\n", ret, flags);
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200994
995 return( ret );
996 }
997
998 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100999 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001000
1001 x509_crt_free( &cacert );
1002 x509_crt_free( &clicert );
1003
1004 return( 0 );
1005#else
1006 ((void) verbose);
1007 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001008#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001009}
1010
Paul Bakker9af723c2014-05-01 13:03:14 +02001011#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001012
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013#endif /* POLARSSL_X509_USE_C */