blob: 27040b93a1ae5a64e4ea2d2a52385082175e8c18 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate and private key decoding
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
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
37#include "polarssl/config.h"
38
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
48#if defined(POLARSSL_MEMORY_C)
49#include "polarssl/memory.h"
50#else
51#define polarssl_malloc malloc
52#define polarssl_free free
53#endif
54
55#include <string.h>
56#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010057#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#include <windows.h>
59#else
60#include <time.h>
61#endif
62
Paul Bakkerfa6a6202013-10-28 18:48:30 +010063#if defined(EFIX64) || defined(EFI32)
64#include <stdio.h>
65#endif
66
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#if defined(POLARSSL_FS_IO)
68#include <stdio.h>
69#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/*
122 * AttributeTypeAndValue ::= SEQUENCE {
123 * type AttributeType,
124 * value AttributeValue }
125 *
126 * AttributeType ::= OBJECT IDENTIFIER
127 *
128 * AttributeValue ::= ANY DEFINED BY AttributeType
129 */
130static int x509_get_attr_type_value( unsigned char **p,
131 const unsigned char *end,
132 x509_name *cur )
133{
134 int ret;
135 size_t len;
136 x509_buf *oid;
137 x509_buf *val;
138
139 if( ( ret = asn1_get_tag( p, end, &len,
140 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200141 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142
143 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200144 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
146
147 oid = &cur->oid;
148 oid->tag = **p;
149
150 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200151 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
153 oid->p = *p;
154 *p += oid->len;
155
156 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200157 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200158 POLARSSL_ERR_ASN1_OUT_OF_DATA );
159
160 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
161 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
162 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200163 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200164 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
165
166 val = &cur->val;
167 val->tag = *(*p)++;
168
169 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200170 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171
172 val->p = *p;
173 *p += val->len;
174
175 cur->next = NULL;
176
177 return( 0 );
178}
179
180/*
181 * RelativeDistinguishedName ::=
182 * SET OF AttributeTypeAndValue
183 *
184 * AttributeTypeAndValue ::= SEQUENCE {
185 * type AttributeType,
186 * value AttributeValue }
187 *
188 * AttributeType ::= OBJECT IDENTIFIER
189 *
190 * AttributeValue ::= ANY DEFINED BY AttributeType
191 */
192int x509_get_name( unsigned char **p, const unsigned char *end,
193 x509_name *cur )
194{
195 int ret;
196 size_t len;
197 const unsigned char *end2;
198 x509_name *use;
199
200 if( ( ret = asn1_get_tag( p, end, &len,
201 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200202 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203
204 end2 = end;
205 end = *p + len;
206 use = cur;
207
208 do
209 {
210 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
211 return( ret );
212
213 if( *p != end )
214 {
215 use->next = (x509_name *) polarssl_malloc(
216 sizeof( x509_name ) );
217
218 if( use->next == NULL )
219 return( POLARSSL_ERR_X509_MALLOC_FAILED );
220
221 memset( use->next, 0, sizeof( x509_name ) );
222
223 use = use->next;
224 }
225 }
226 while( *p != end );
227
228 /*
229 * recurse until end of SEQUENCE is reached
230 */
231 if( *p == end2 )
232 return( 0 );
233
234 cur->next = (x509_name *) polarssl_malloc(
235 sizeof( x509_name ) );
236
237 if( cur->next == NULL )
238 return( POLARSSL_ERR_X509_MALLOC_FAILED );
239
240 memset( cur->next, 0, sizeof( x509_name ) );
241
242 return( x509_get_name( p, end2, cur->next ) );
243}
244
245/*
246 * Time ::= CHOICE {
247 * utcTime UTCTime,
248 * generalTime GeneralizedTime }
249 */
250int x509_get_time( unsigned char **p, const unsigned char *end,
251 x509_time *time )
252{
253 int ret;
254 size_t len;
255 char date[64];
256 unsigned char tag;
257
258 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200259 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260 POLARSSL_ERR_ASN1_OUT_OF_DATA );
261
262 tag = **p;
263
264 if ( tag == ASN1_UTC_TIME )
265 {
266 (*p)++;
267 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200268
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200270 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
272 memset( date, 0, sizeof( date ) );
273 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
274 len : sizeof( date ) - 1 );
275
276 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
277 &time->year, &time->mon, &time->day,
278 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200279 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280
281 time->year += 100 * ( time->year < 50 );
282 time->year += 1900;
283
284 *p += len;
285
286 return( 0 );
287 }
288 else if ( tag == ASN1_GENERALIZED_TIME )
289 {
290 (*p)++;
291 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200292
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200294 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295
296 memset( date, 0, sizeof( date ) );
297 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
298 len : sizeof( date ) - 1 );
299
300 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
301 &time->year, &time->mon, &time->day,
302 &time->hour, &time->min, &time->sec ) < 5 )
Paul Bakker51876562013-09-17 14:36:05 +0200303 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304
305 *p += len;
306
307 return( 0 );
308 }
309 else
Paul Bakker51876562013-09-17 14:36:05 +0200310 return( POLARSSL_ERR_X509_INVALID_DATE +
311 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200312}
313
314int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
315{
316 int ret;
317 size_t len;
318
319 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200320 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200321 POLARSSL_ERR_ASN1_OUT_OF_DATA );
322
323 sig->tag = **p;
324
325 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200326 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327
328 sig->len = len;
329 sig->p = *p;
330
331 *p += len;
332
333 return( 0 );
334}
335
336int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
337 pk_type_t *pk_alg )
338{
339 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
340
341 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200342 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
344 return( 0 );
345}
346
347/*
348 * X.509 Extensions (No parsing of extensions, pointer should
349 * be either manually updated or extensions should be parsed!
350 */
351int x509_get_ext( unsigned char **p, const unsigned char *end,
352 x509_buf *ext, int tag )
353{
354 int ret;
355 size_t len;
356
357 if( *p == end )
358 return( 0 );
359
360 ext->tag = **p;
361
362 if( ( ret = asn1_get_tag( p, end, &ext->len,
363 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
364 return( ret );
365
366 ext->p = *p;
367 end = *p + ext->len;
368
369 /*
370 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
371 *
372 * Extension ::= SEQUENCE {
373 * extnID OBJECT IDENTIFIER,
374 * critical BOOLEAN DEFAULT FALSE,
375 * extnValue OCTET STRING }
376 */
377 if( ( ret = asn1_get_tag( p, end, &len,
378 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200379 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380
381 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200382 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
384
385 return( 0 );
386}
387
388#if defined(POLARSSL_FS_IO)
389/*
390 * Load all data from a file into a given buffer.
391 */
392int x509_load_file( const char *path, unsigned char **buf, size_t *n )
393{
394 FILE *f;
395 long size;
396
397 if( ( f = fopen( path, "rb" ) ) == NULL )
398 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
399
400 fseek( f, 0, SEEK_END );
401 if( ( size = ftell( f ) ) == -1 )
402 {
403 fclose( f );
404 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
405 }
406 fseek( f, 0, SEEK_SET );
407
408 *n = (size_t) size;
409
410 if( *n + 1 == 0 ||
411 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
412 {
413 fclose( f );
414 return( POLARSSL_ERR_X509_MALLOC_FAILED );
415 }
416
417 if( fread( *buf, 1, *n, f ) != *n )
418 {
419 fclose( f );
420 polarssl_free( *buf );
421 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
422 }
423
424 fclose( f );
425
426 (*buf)[*n] = '\0';
427
428 return( 0 );
429}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430#endif /* POLARSSL_FS_IO */
431
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100432#if defined(_MSC_VER) && !defined snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433#include <stdarg.h>
434
435#if !defined vsnprintf
436#define vsnprintf _vsnprintf
437#endif // vsnprintf
438
439/*
440 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
441 * Result value is not size of buffer needed, but -1 if no fit is possible.
442 *
443 * This fuction tries to 'fix' this by at least suggesting enlarging the
444 * size by 20.
445 */
446static int compat_snprintf(char *str, size_t size, const char *format, ...)
447{
448 va_list ap;
449 int res = -1;
450
451 va_start( ap, format );
452
453 res = vsnprintf( str, size, format, ap );
454
455 va_end( ap );
456
457 // No quick fix possible
458 if ( res < 0 )
459 return( (int) size + 20 );
460
461 return res;
462}
463
464#define snprintf compat_snprintf
465#endif
466
467#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
468
469#define SAFE_SNPRINTF() \
470{ \
471 if( ret == -1 ) \
472 return( -1 ); \
473 \
474 if ( (unsigned int) ret > n ) { \
475 p[n - 1] = '\0'; \
476 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
477 } \
478 \
479 n -= (unsigned int) ret; \
480 p += (unsigned int) ret; \
481}
482
483/*
484 * Store the name in printable form into buf; no more
485 * than size characters will be written
486 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200487int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488{
489 int ret;
490 size_t i, n;
491 unsigned char c;
492 const x509_name *name;
493 const char *short_name = NULL;
494 char s[128], *p;
495
496 memset( s, 0, sizeof( s ) );
497
498 name = dn;
499 p = buf;
500 n = size;
501
502 while( name != NULL )
503 {
504 if( !name->oid.p )
505 {
506 name = name->next;
507 continue;
508 }
509
510 if( name != dn )
511 {
512 ret = snprintf( p, n, ", " );
513 SAFE_SNPRINTF();
514 }
515
516 ret = oid_get_attr_short_name( &name->oid, &short_name );
517
518 if( ret == 0 )
519 ret = snprintf( p, n, "%s=", short_name );
520 else
521 ret = snprintf( p, n, "\?\?=" );
522 SAFE_SNPRINTF();
523
524 for( i = 0; i < name->val.len; i++ )
525 {
526 if( i >= sizeof( s ) - 1 )
527 break;
528
529 c = name->val.p[i];
530 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
531 s[i] = '?';
532 else s[i] = c;
533 }
534 s[i] = '\0';
535 ret = snprintf( p, n, "%s", s );
536 SAFE_SNPRINTF();
537 name = name->next;
538 }
539
540 return( (int) ( size - n ) );
541}
542
543/*
544 * Store the serial in printable form into buf; no more
545 * than size characters will be written
546 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200547int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548{
549 int ret;
550 size_t i, n, nr;
551 char *p;
552
553 p = buf;
554 n = size;
555
556 nr = ( serial->len <= 32 )
557 ? serial->len : 28;
558
559 for( i = 0; i < nr; i++ )
560 {
561 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
562 continue;
563
564 ret = snprintf( p, n, "%02X%s",
565 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
566 SAFE_SNPRINTF();
567 }
568
569 if( nr != serial->len )
570 {
571 ret = snprintf( p, n, "...." );
572 SAFE_SNPRINTF();
573 }
574
575 return( (int) ( size - n ) );
576}
577
578/*
579 * Helper for writing "RSA key size", "EC key size", etc
580 */
581int x509_key_size_helper( char *buf, size_t size, const char *name )
582{
583 char *p = buf;
584 size_t n = size;
585 int ret;
586
587 if( strlen( name ) + sizeof( " key size" ) > size )
588 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
589
590 ret = snprintf( p, n, "%s key size", name );
591 SAFE_SNPRINTF();
592
593 return( 0 );
594}
595
596/*
597 * Return an informational string describing the given OID
598 */
599const char *x509_oid_get_description( x509_buf *oid )
600{
601 const char *desc = NULL;
602 int ret;
603
604 ret = oid_get_extended_key_usage( oid, &desc );
605
606 if( ret != 0 )
607 return( NULL );
608
609 return( desc );
610}
611
612/* Return the x.y.z.... style numeric string for the given OID */
613int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
614{
615 return oid_get_numeric_string( buf, size, oid );
616}
617
618/*
619 * Return 0 if the x509_time is still valid, or 1 otherwise.
620 */
621#if defined(POLARSSL_HAVE_TIME)
Paul Bakker86d0c192013-09-18 11:11:02 +0200622int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623{
624 int year, mon, day;
625 int hour, min, sec;
626
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100627#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 SYSTEMTIME st;
629
630 GetLocalTime(&st);
631
632 year = st.wYear;
633 mon = st.wMonth;
634 day = st.wDay;
635 hour = st.wHour;
636 min = st.wMinute;
637 sec = st.wSecond;
638#else
639 struct tm *lt;
640 time_t tt;
641
642 tt = time( NULL );
643 lt = localtime( &tt );
644
645 year = lt->tm_year + 1900;
646 mon = lt->tm_mon + 1;
647 day = lt->tm_mday;
648 hour = lt->tm_hour;
649 min = lt->tm_min;
650 sec = lt->tm_sec;
651#endif
652
653 if( year > to->year )
654 return( 1 );
655
656 if( year == to->year &&
657 mon > to->mon )
658 return( 1 );
659
660 if( year == to->year &&
661 mon == to->mon &&
662 day > to->day )
663 return( 1 );
664
665 if( year == to->year &&
666 mon == to->mon &&
667 day == to->day &&
668 hour > to->hour )
669 return( 1 );
670
671 if( year == to->year &&
672 mon == to->mon &&
673 day == to->day &&
674 hour == to->hour &&
675 min > to->min )
676 return( 1 );
677
678 if( year == to->year &&
679 mon == to->mon &&
680 day == to->day &&
681 hour == to->hour &&
682 min == to->min &&
683 sec > to->sec )
684 return( 1 );
685
686 return( 0 );
687}
688#else /* POLARSSL_HAVE_TIME */
Paul Bakker86d0c192013-09-18 11:11:02 +0200689int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690{
691 ((void) to);
692 return( 0 );
693}
694#endif /* POLARSSL_HAVE_TIME */
695
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200696#if defined(POLARSSL_SELF_TEST)
697
698#include "polarssl/x509_crt.h"
699#include "polarssl/certs.h"
700
701/*
702 * Checkup routine
703 */
704int x509_self_test( int verbose )
705{
706#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
707 int ret;
708 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200709 x509_crt cacert;
710 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200711
712 if( verbose != 0 )
713 printf( " X.509 certificate load: " );
714
Paul Bakkerb6b09562013-09-18 14:17:41 +0200715 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200716
Paul Bakkerddf26b42013-09-18 13:46:23 +0200717 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
718 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200719 if( ret != 0 )
720 {
721 if( verbose != 0 )
722 printf( "failed\n" );
723
724 return( ret );
725 }
726
Paul Bakkerb6b09562013-09-18 14:17:41 +0200727 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200728
Paul Bakkerddf26b42013-09-18 13:46:23 +0200729 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
730 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200731 if( ret != 0 )
732 {
733 if( verbose != 0 )
734 printf( "failed\n" );
735
736 return( ret );
737 }
738
739 if( verbose != 0 )
740 printf( "passed\n X.509 signature verify: ");
741
Paul Bakkerddf26b42013-09-18 13:46:23 +0200742 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200743 if( ret != 0 )
744 {
745 if( verbose != 0 )
746 printf( "failed\n" );
747
748 printf("ret = %d, &flags = %04x\n", ret, flags);
749
750 return( ret );
751 }
752
753 if( verbose != 0 )
754 printf( "passed\n\n");
755
756 x509_crt_free( &cacert );
757 x509_crt_free( &clicert );
758
759 return( 0 );
760#else
761 ((void) verbose);
762 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
763#endif
764}
765
766#endif
767
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768#endif /* POLARSSL_X509_USE_C */