2 * X.509 certificate and private key decoding
4 * Copyright (C) 2006-2014, Brainspark B.V.
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
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.
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.
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.
26 * The ITU-T X.509 standard defines a certificate format for PKI.
28 * http://www.ietf.org/rfc/rfc3279.txt
29 * http://www.ietf.org/rfc/rfc3280.txt
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
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
37 #if !defined(POLARSSL_CONFIG_FILE)
40 #include POLARSSL_CONFIG_FILE
43 #if defined(POLARSSL_X509_CRL_PARSE_C)
47 #if defined(POLARSSL_PEM_PARSE_C)
51 #if defined(POLARSSL_PLATFORM_C)
54 #define polarssl_malloc malloc
55 #define polarssl_free free
60 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
67 #if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32)
72 * Version ::= INTEGER { v1(0), v2(1) }
74 static int x509_crl_get_version( unsigned char **p
,
75 const unsigned char *end
,
80 if( ( ret
= asn1_get_int( p
, end
, ver
) ) != 0 )
82 if( ret
== POLARSSL_ERR_ASN1_UNEXPECTED_TAG
)
88 return( POLARSSL_ERR_X509_INVALID_VERSION
+ ret
);
95 * X.509 CRL v2 extensions (no extensions parsed yet.)
97 static int x509_get_crl_ext( unsigned char **p
,
98 const unsigned char *end
,
104 /* Get explicit tag */
105 if( ( ret
= x509_get_ext( p
, end
, ext
, 0) ) != 0 )
107 if( ret
== POLARSSL_ERR_ASN1_UNEXPECTED_TAG
)
115 if( ( ret
= asn1_get_tag( p
, end
, &len
,
116 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 )
117 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS
+ ret
);
123 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS
+
124 POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
130 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
132 static int x509_get_crl_entry_ext( unsigned char **p
,
133 const unsigned char *end
,
147 * Get CRL-entry extension sequence header
148 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
150 if( ( ret
= asn1_get_tag( p
, end
, &ext
->len
,
151 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 )
153 if( ret
== POLARSSL_ERR_ASN1_UNEXPECTED_TAG
)
158 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS
+ ret
);
163 if( end
!= *p
+ ext
->len
)
164 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS
+
165 POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
169 if( ( ret
= asn1_get_tag( p
, end
, &len
,
170 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 )
171 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS
+ ret
);
177 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS
+
178 POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
186 static int x509_get_entries( unsigned char **p
,
187 const unsigned char *end
,
188 x509_crl_entry
*entry
)
192 x509_crl_entry
*cur_entry
= entry
;
197 if( ( ret
= asn1_get_tag( p
, end
, &entry_len
,
198 ASN1_SEQUENCE
| ASN1_CONSTRUCTED
) ) != 0 )
200 if( ret
== POLARSSL_ERR_ASN1_UNEXPECTED_TAG
)
206 end
= *p
+ entry_len
;
211 const unsigned char *end2
;
213 if( ( ret
= asn1_get_tag( p
, end
, &len2
,
214 ASN1_SEQUENCE
| ASN1_CONSTRUCTED
) ) != 0 )
219 cur_entry
->raw
.tag
= **p
;
220 cur_entry
->raw
.p
= *p
;
221 cur_entry
->raw
.len
= len2
;
224 if( ( ret
= x509_get_serial( p
, end2
, &cur_entry
->serial
) ) != 0 )
227 if( ( ret
= x509_get_time( p
, end2
,
228 &cur_entry
->revocation_date
) ) != 0 )
231 if( ( ret
= x509_get_crl_entry_ext( p
, end2
,
232 &cur_entry
->entry_ext
) ) != 0 )
237 cur_entry
->next
= polarssl_malloc( sizeof( x509_crl_entry
) );
239 if( cur_entry
->next
== NULL
)
240 return( POLARSSL_ERR_X509_MALLOC_FAILED
);
242 cur_entry
= cur_entry
->next
;
243 memset( cur_entry
, 0, sizeof( x509_crl_entry
) );
251 * Parse one or more CRLs and add them to the chained list
253 int x509_crl_parse( x509_crl
*chain
, const unsigned char *buf
, size_t buflen
)
257 unsigned char *p
, *end
;
259 #if defined(POLARSSL_PEM_PARSE_C)
267 * Check for valid input
269 if( crl
== NULL
|| buf
== NULL
)
270 return( POLARSSL_ERR_X509_BAD_INPUT_DATA
);
272 while( crl
->version
!= 0 && crl
->next
!= NULL
)
276 * Add new CRL on the end of the chain if needed.
278 if ( crl
->version
!= 0 && crl
->next
== NULL
)
280 crl
->next
= (x509_crl
*) polarssl_malloc( sizeof( x509_crl
) );
282 if( crl
->next
== NULL
)
284 x509_crl_free( crl
);
285 return( POLARSSL_ERR_X509_MALLOC_FAILED
);
289 x509_crl_init( crl
);
292 #if defined(POLARSSL_PEM_PARSE_C)
294 ret
= pem_read_buffer( &pem
,
295 "-----BEGIN X509 CRL-----",
296 "-----END X509 CRL-----",
297 buf
, NULL
, 0, &use_len
);
315 else if( ret
!= POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT
)
321 #endif /* POLARSSL_PEM_PARSE_C */
324 * nope, copy the raw DER data
326 p
= (unsigned char *) polarssl_malloc( len
= buflen
);
329 return( POLARSSL_ERR_X509_MALLOC_FAILED
);
331 memcpy( p
, buf
, buflen
);
341 * CertificateList ::= SEQUENCE {
342 * tbsCertList TBSCertList,
343 * signatureAlgorithm AlgorithmIdentifier,
344 * signatureValue BIT STRING }
346 if( ( ret
= asn1_get_tag( &p
, end
, &len
,
347 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 )
349 x509_crl_free( crl
);
350 return( POLARSSL_ERR_X509_INVALID_FORMAT
);
353 if( len
!= (size_t) ( end
- p
) )
355 x509_crl_free( crl
);
356 return( POLARSSL_ERR_X509_INVALID_FORMAT
+
357 POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
361 * TBSCertList ::= SEQUENCE {
365 if( ( ret
= asn1_get_tag( &p
, end
, &len
,
366 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 )
368 x509_crl_free( crl
);
369 return( POLARSSL_ERR_X509_INVALID_FORMAT
+ ret
);
373 crl
->tbs
.len
= end
- crl
->tbs
.p
;
376 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
377 * -- if present, MUST be v2
379 * signature AlgorithmIdentifier
381 if( ( ret
= x509_crl_get_version( &p
, end
, &crl
->version
) ) != 0 ||
382 ( ret
= x509_get_alg_null( &p
, end
, &crl
->sig_oid1
) ) != 0 )
384 x509_crl_free( crl
);
390 if( crl
->version
> 2 )
392 x509_crl_free( crl
);
393 return( POLARSSL_ERR_X509_UNKNOWN_VERSION
);
396 if( ( ret
= x509_get_sig_alg( &crl
->sig_oid1
, &crl
->sig_md
,
397 &crl
->sig_pk
) ) != 0 )
399 x509_crl_free( crl
);
400 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG
);
406 crl
->issuer_raw
.p
= p
;
408 if( ( ret
= asn1_get_tag( &p
, end
, &len
,
409 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 )
411 x509_crl_free( crl
);
412 return( POLARSSL_ERR_X509_INVALID_FORMAT
+ ret
);
415 if( ( ret
= x509_get_name( &p
, p
+ len
, &crl
->issuer
) ) != 0 )
417 x509_crl_free( crl
);
421 crl
->issuer_raw
.len
= p
- crl
->issuer_raw
.p
;
425 * nextUpdate Time OPTIONAL
427 if( ( ret
= x509_get_time( &p
, end
, &crl
->this_update
) ) != 0 )
429 x509_crl_free( crl
);
433 if( ( ret
= x509_get_time( &p
, end
, &crl
->next_update
) ) != 0 )
435 if ( ret
!= ( POLARSSL_ERR_X509_INVALID_DATE
+
436 POLARSSL_ERR_ASN1_UNEXPECTED_TAG
) &&
437 ret
!= ( POLARSSL_ERR_X509_INVALID_DATE
+
438 POLARSSL_ERR_ASN1_OUT_OF_DATA
) )
440 x509_crl_free( crl
);
446 * revokedCertificates SEQUENCE OF SEQUENCE {
447 * userCertificate CertificateSerialNumber,
448 * revocationDate Time,
449 * crlEntryExtensions Extensions OPTIONAL
450 * -- if present, MUST be v2
453 if( ( ret
= x509_get_entries( &p
, end
, &crl
->entry
) ) != 0 )
455 x509_crl_free( crl
);
460 * crlExtensions EXPLICIT Extensions OPTIONAL
461 * -- if present, MUST be v2
463 if( crl
->version
== 2 )
465 ret
= x509_get_crl_ext( &p
, end
, &crl
->crl_ext
);
469 x509_crl_free( crl
);
476 x509_crl_free( crl
);
477 return( POLARSSL_ERR_X509_INVALID_FORMAT
+
478 POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
481 end
= crl
->raw
.p
+ crl
->raw
.len
;
484 * signatureAlgorithm AlgorithmIdentifier,
485 * signatureValue BIT STRING
487 if( ( ret
= x509_get_alg_null( &p
, end
, &crl
->sig_oid2
) ) != 0 )
489 x509_crl_free( crl
);
493 if( crl
->sig_oid1
.len
!= crl
->sig_oid2
.len
||
494 memcmp( crl
->sig_oid1
.p
, crl
->sig_oid2
.p
, crl
->sig_oid1
.len
) != 0 )
496 x509_crl_free( crl
);
497 return( POLARSSL_ERR_X509_SIG_MISMATCH
);
500 if( ( ret
= x509_get_sig( &p
, end
, &crl
->sig
) ) != 0 )
502 x509_crl_free( crl
);
508 x509_crl_free( crl
);
509 return( POLARSSL_ERR_X509_INVALID_FORMAT
+
510 POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
515 crl
->next
= (x509_crl
*) polarssl_malloc( sizeof( x509_crl
) );
517 if( crl
->next
== NULL
)
519 x509_crl_free( crl
);
520 return( POLARSSL_ERR_X509_MALLOC_FAILED
);
524 x509_crl_init( crl
);
526 return( x509_crl_parse( crl
, buf
, buflen
) );
532 #if defined(POLARSSL_FS_IO)
534 * Load one or more CRLs and add them to the chained list
536 int x509_crl_parse_file( x509_crl
*chain
, const char *path
)
542 if ( ( ret
= x509_load_file( path
, &buf
, &n
) ) != 0 )
545 ret
= x509_crl_parse( chain
, buf
, n
);
547 memset( buf
, 0, n
+ 1 );
548 polarssl_free( buf
);
552 #endif /* POLARSSL_FS_IO */
554 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
558 #if !defined vsnprintf
559 #define vsnprintf _vsnprintf
563 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
564 * Result value is not size of buffer needed, but -1 if no fit is possible.
566 * This fuction tries to 'fix' this by at least suggesting enlarging the
569 static int compat_snprintf(char *str
, size_t size
, const char *format
, ...)
574 va_start( ap
, format
);
576 res
= vsnprintf( str
, size
, format
, ap
);
580 // No quick fix possible
582 return( (int) size
+ 20 );
587 #define snprintf compat_snprintf
588 #endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
590 #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
592 #define SAFE_SNPRINTF() \
597 if ( (unsigned int) ret > n ) { \
599 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
602 n -= (unsigned int) ret; \
603 p += (unsigned int) ret; \
607 * Return an informational string about the certificate.
609 #define BEFORE_COLON 14
612 * Return an informational string about the CRL.
614 int x509_crl_info( char *buf
, size_t size
, const char *prefix
,
615 const x509_crl
*crl
)
621 const x509_crl_entry
*entry
;
626 ret
= snprintf( p
, n
, "%sCRL version : %d",
627 prefix
, crl
->version
);
630 ret
= snprintf( p
, n
, "\n%sissuer name : ", prefix
);
632 ret
= x509_dn_gets( p
, n
, &crl
->issuer
);
635 ret
= snprintf( p
, n
, "\n%sthis update : " \
636 "%04d-%02d-%02d %02d:%02d:%02d", prefix
,
637 crl
->this_update
.year
, crl
->this_update
.mon
,
638 crl
->this_update
.day
, crl
->this_update
.hour
,
639 crl
->this_update
.min
, crl
->this_update
.sec
);
642 ret
= snprintf( p
, n
, "\n%snext update : " \
643 "%04d-%02d-%02d %02d:%02d:%02d", prefix
,
644 crl
->next_update
.year
, crl
->next_update
.mon
,
645 crl
->next_update
.day
, crl
->next_update
.hour
,
646 crl
->next_update
.min
, crl
->next_update
.sec
);
651 ret
= snprintf( p
, n
, "\n%sRevoked certificates:",
655 while( entry
!= NULL
&& entry
->raw
.len
!= 0 )
657 ret
= snprintf( p
, n
, "\n%sserial number: ",
661 ret
= x509_serial_gets( p
, n
, &entry
->serial
);
664 ret
= snprintf( p
, n
, " revocation date: " \
665 "%04d-%02d-%02d %02d:%02d:%02d",
666 entry
->revocation_date
.year
, entry
->revocation_date
.mon
,
667 entry
->revocation_date
.day
, entry
->revocation_date
.hour
,
668 entry
->revocation_date
.min
, entry
->revocation_date
.sec
);
674 ret
= snprintf( p
, n
, "\n%ssigned using : ", prefix
);
677 ret
= oid_get_sig_alg_desc( &crl
->sig_oid1
, &desc
);
679 ret
= snprintf( p
, n
, "???" );
681 ret
= snprintf( p
, n
, "%s", desc
);
684 ret
= snprintf( p
, n
, "\n" );
687 return( (int) ( size
- n
) );
691 * Initialize a CRL chain
693 void x509_crl_init( x509_crl
*crl
)
695 memset( crl
, 0, sizeof(x509_crl
) );
699 * Unallocate all CRL data
701 void x509_crl_free( x509_crl
*crl
)
703 x509_crl
*crl_cur
= crl
;
707 x509_crl_entry
*entry_cur
;
708 x509_crl_entry
*entry_prv
;
715 name_cur
= crl_cur
->issuer
.next
;
716 while( name_cur
!= NULL
)
719 name_cur
= name_cur
->next
;
720 memset( name_prv
, 0, sizeof( x509_name
) );
721 polarssl_free( name_prv
);
724 entry_cur
= crl_cur
->entry
.next
;
725 while( entry_cur
!= NULL
)
727 entry_prv
= entry_cur
;
728 entry_cur
= entry_cur
->next
;
729 memset( entry_prv
, 0, sizeof( x509_crl_entry
) );
730 polarssl_free( entry_prv
);
733 if( crl_cur
->raw
.p
!= NULL
)
735 memset( crl_cur
->raw
.p
, 0, crl_cur
->raw
.len
);
736 polarssl_free( crl_cur
->raw
.p
);
739 crl_cur
= crl_cur
->next
;
741 while( crl_cur
!= NULL
);
747 crl_cur
= crl_cur
->next
;
749 memset( crl_prv
, 0, sizeof( x509_crl
) );
751 polarssl_free( crl_prv
);
753 while( crl_cur
!= NULL
);
756 #endif /* POLARSSL_X509_CRL_PARSE_C */