blob: bdcc95144fcbeb9c23da7acb330e0ce15166343f [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>
57#if defined(_WIN32)
58#include <windows.h>
59#else
60#include <time.h>
61#endif
62
63#if defined(POLARSSL_FS_IO)
64#include <stdio.h>
65#if !defined(_WIN32)
66#include <sys/types.h>
67#include <sys/stat.h>
68#include <dirent.h>
69#endif
70#endif
71
72/*
73 * CertificateSerialNumber ::= INTEGER
74 */
75int x509_get_serial( unsigned char **p, const unsigned char *end,
76 x509_buf *serial )
77{
78 int ret;
79
80 if( ( end - *p ) < 1 )
81 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
82 POLARSSL_ERR_ASN1_OUT_OF_DATA );
83
84 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
85 **p != ASN1_INTEGER )
86 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
87 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
88
89 serial->tag = *(*p)++;
90
91 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
92 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
93
94 serial->p = *p;
95 *p += serial->len;
96
97 return( 0 );
98}
99
100/* Get an algorithm identifier without parameters (eg for signatures)
101 *
102 * AlgorithmIdentifier ::= SEQUENCE {
103 * algorithm OBJECT IDENTIFIER,
104 * parameters ANY DEFINED BY algorithm OPTIONAL }
105 */
106int x509_get_alg_null( unsigned char **p, const unsigned char *end,
107 x509_buf *alg )
108{
109 int ret;
110
111 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
112 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
113
114 return( 0 );
115}
116
117/*
118 * AttributeTypeAndValue ::= SEQUENCE {
119 * type AttributeType,
120 * value AttributeValue }
121 *
122 * AttributeType ::= OBJECT IDENTIFIER
123 *
124 * AttributeValue ::= ANY DEFINED BY AttributeType
125 */
126static int x509_get_attr_type_value( unsigned char **p,
127 const unsigned char *end,
128 x509_name *cur )
129{
130 int ret;
131 size_t len;
132 x509_buf *oid;
133 x509_buf *val;
134
135 if( ( ret = asn1_get_tag( p, end, &len,
136 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
137 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
138
139 if( ( end - *p ) < 1 )
140 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
141 POLARSSL_ERR_ASN1_OUT_OF_DATA );
142
143 oid = &cur->oid;
144 oid->tag = **p;
145
146 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
147 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
148
149 oid->p = *p;
150 *p += oid->len;
151
152 if( ( end - *p ) < 1 )
153 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
154 POLARSSL_ERR_ASN1_OUT_OF_DATA );
155
156 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
157 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
158 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
159 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
160 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
161
162 val = &cur->val;
163 val->tag = *(*p)++;
164
165 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
166 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
167
168 val->p = *p;
169 *p += val->len;
170
171 cur->next = NULL;
172
173 return( 0 );
174}
175
176/*
177 * RelativeDistinguishedName ::=
178 * SET OF AttributeTypeAndValue
179 *
180 * AttributeTypeAndValue ::= SEQUENCE {
181 * type AttributeType,
182 * value AttributeValue }
183 *
184 * AttributeType ::= OBJECT IDENTIFIER
185 *
186 * AttributeValue ::= ANY DEFINED BY AttributeType
187 */
188int x509_get_name( unsigned char **p, const unsigned char *end,
189 x509_name *cur )
190{
191 int ret;
192 size_t len;
193 const unsigned char *end2;
194 x509_name *use;
195
196 if( ( ret = asn1_get_tag( p, end, &len,
197 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
198 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
199
200 end2 = end;
201 end = *p + len;
202 use = cur;
203
204 do
205 {
206 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
207 return( ret );
208
209 if( *p != end )
210 {
211 use->next = (x509_name *) polarssl_malloc(
212 sizeof( x509_name ) );
213
214 if( use->next == NULL )
215 return( POLARSSL_ERR_X509_MALLOC_FAILED );
216
217 memset( use->next, 0, sizeof( x509_name ) );
218
219 use = use->next;
220 }
221 }
222 while( *p != end );
223
224 /*
225 * recurse until end of SEQUENCE is reached
226 */
227 if( *p == end2 )
228 return( 0 );
229
230 cur->next = (x509_name *) polarssl_malloc(
231 sizeof( x509_name ) );
232
233 if( cur->next == NULL )
234 return( POLARSSL_ERR_X509_MALLOC_FAILED );
235
236 memset( cur->next, 0, sizeof( x509_name ) );
237
238 return( x509_get_name( p, end2, cur->next ) );
239}
240
241/*
242 * Time ::= CHOICE {
243 * utcTime UTCTime,
244 * generalTime GeneralizedTime }
245 */
246int x509_get_time( unsigned char **p, const unsigned char *end,
247 x509_time *time )
248{
249 int ret;
250 size_t len;
251 char date[64];
252 unsigned char tag;
253
254 if( ( end - *p ) < 1 )
255 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
256 POLARSSL_ERR_ASN1_OUT_OF_DATA );
257
258 tag = **p;
259
260 if ( tag == ASN1_UTC_TIME )
261 {
262 (*p)++;
263 ret = asn1_get_len( p, end, &len );
264
265 if( ret != 0 )
266 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
267
268 memset( date, 0, sizeof( date ) );
269 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
270 len : sizeof( date ) - 1 );
271
272 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
273 &time->year, &time->mon, &time->day,
274 &time->hour, &time->min, &time->sec ) < 5 )
275 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
276
277 time->year += 100 * ( time->year < 50 );
278 time->year += 1900;
279
280 *p += len;
281
282 return( 0 );
283 }
284 else if ( tag == ASN1_GENERALIZED_TIME )
285 {
286 (*p)++;
287 ret = asn1_get_len( p, end, &len );
288
289 if( ret != 0 )
290 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
291
292 memset( date, 0, sizeof( date ) );
293 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
294 len : sizeof( date ) - 1 );
295
296 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
297 &time->year, &time->mon, &time->day,
298 &time->hour, &time->min, &time->sec ) < 5 )
299 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
300
301 *p += len;
302
303 return( 0 );
304 }
305 else
306 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
307}
308
309int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
310{
311 int ret;
312 size_t len;
313
314 if( ( end - *p ) < 1 )
315 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
316 POLARSSL_ERR_ASN1_OUT_OF_DATA );
317
318 sig->tag = **p;
319
320 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
321 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
322
323 sig->len = len;
324 sig->p = *p;
325
326 *p += len;
327
328 return( 0 );
329}
330
331int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
332 pk_type_t *pk_alg )
333{
334 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
335
336 if( ret != 0 )
337 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
338
339 return( 0 );
340}
341
342/*
343 * X.509 Extensions (No parsing of extensions, pointer should
344 * be either manually updated or extensions should be parsed!
345 */
346int x509_get_ext( unsigned char **p, const unsigned char *end,
347 x509_buf *ext, int tag )
348{
349 int ret;
350 size_t len;
351
352 if( *p == end )
353 return( 0 );
354
355 ext->tag = **p;
356
357 if( ( ret = asn1_get_tag( p, end, &ext->len,
358 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
359 return( ret );
360
361 ext->p = *p;
362 end = *p + ext->len;
363
364 /*
365 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
366 *
367 * Extension ::= SEQUENCE {
368 * extnID OBJECT IDENTIFIER,
369 * critical BOOLEAN DEFAULT FALSE,
370 * extnValue OCTET STRING }
371 */
372 if( ( ret = asn1_get_tag( p, end, &len,
373 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
374 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
375
376 if( end != *p + len )
377 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
378 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
379
380 return( 0 );
381}
382
383#if defined(POLARSSL_FS_IO)
384/*
385 * Load all data from a file into a given buffer.
386 */
387int x509_load_file( const char *path, unsigned char **buf, size_t *n )
388{
389 FILE *f;
390 long size;
391
392 if( ( f = fopen( path, "rb" ) ) == NULL )
393 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
394
395 fseek( f, 0, SEEK_END );
396 if( ( size = ftell( f ) ) == -1 )
397 {
398 fclose( f );
399 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
400 }
401 fseek( f, 0, SEEK_SET );
402
403 *n = (size_t) size;
404
405 if( *n + 1 == 0 ||
406 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
407 {
408 fclose( f );
409 return( POLARSSL_ERR_X509_MALLOC_FAILED );
410 }
411
412 if( fread( *buf, 1, *n, f ) != *n )
413 {
414 fclose( f );
415 polarssl_free( *buf );
416 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
417 }
418
419 fclose( f );
420
421 (*buf)[*n] = '\0';
422
423 return( 0 );
424}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425#endif /* POLARSSL_FS_IO */
426
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427#if defined _MSC_VER && !defined snprintf
428#include <stdarg.h>
429
430#if !defined vsnprintf
431#define vsnprintf _vsnprintf
432#endif // vsnprintf
433
434/*
435 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
436 * Result value is not size of buffer needed, but -1 if no fit is possible.
437 *
438 * This fuction tries to 'fix' this by at least suggesting enlarging the
439 * size by 20.
440 */
441static int compat_snprintf(char *str, size_t size, const char *format, ...)
442{
443 va_list ap;
444 int res = -1;
445
446 va_start( ap, format );
447
448 res = vsnprintf( str, size, format, ap );
449
450 va_end( ap );
451
452 // No quick fix possible
453 if ( res < 0 )
454 return( (int) size + 20 );
455
456 return res;
457}
458
459#define snprintf compat_snprintf
460#endif
461
462#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
463
464#define SAFE_SNPRINTF() \
465{ \
466 if( ret == -1 ) \
467 return( -1 ); \
468 \
469 if ( (unsigned int) ret > n ) { \
470 p[n - 1] = '\0'; \
471 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
472 } \
473 \
474 n -= (unsigned int) ret; \
475 p += (unsigned int) ret; \
476}
477
478/*
479 * Store the name in printable form into buf; no more
480 * than size characters will be written
481 */
482int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
483{
484 int ret;
485 size_t i, n;
486 unsigned char c;
487 const x509_name *name;
488 const char *short_name = NULL;
489 char s[128], *p;
490
491 memset( s, 0, sizeof( s ) );
492
493 name = dn;
494 p = buf;
495 n = size;
496
497 while( name != NULL )
498 {
499 if( !name->oid.p )
500 {
501 name = name->next;
502 continue;
503 }
504
505 if( name != dn )
506 {
507 ret = snprintf( p, n, ", " );
508 SAFE_SNPRINTF();
509 }
510
511 ret = oid_get_attr_short_name( &name->oid, &short_name );
512
513 if( ret == 0 )
514 ret = snprintf( p, n, "%s=", short_name );
515 else
516 ret = snprintf( p, n, "\?\?=" );
517 SAFE_SNPRINTF();
518
519 for( i = 0; i < name->val.len; i++ )
520 {
521 if( i >= sizeof( s ) - 1 )
522 break;
523
524 c = name->val.p[i];
525 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
526 s[i] = '?';
527 else s[i] = c;
528 }
529 s[i] = '\0';
530 ret = snprintf( p, n, "%s", s );
531 SAFE_SNPRINTF();
532 name = name->next;
533 }
534
535 return( (int) ( size - n ) );
536}
537
538/*
539 * Store the serial in printable form into buf; no more
540 * than size characters will be written
541 */
542int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
543{
544 int ret;
545 size_t i, n, nr;
546 char *p;
547
548 p = buf;
549 n = size;
550
551 nr = ( serial->len <= 32 )
552 ? serial->len : 28;
553
554 for( i = 0; i < nr; i++ )
555 {
556 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
557 continue;
558
559 ret = snprintf( p, n, "%02X%s",
560 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
561 SAFE_SNPRINTF();
562 }
563
564 if( nr != serial->len )
565 {
566 ret = snprintf( p, n, "...." );
567 SAFE_SNPRINTF();
568 }
569
570 return( (int) ( size - n ) );
571}
572
573/*
574 * Helper for writing "RSA key size", "EC key size", etc
575 */
576int x509_key_size_helper( char *buf, size_t size, const char *name )
577{
578 char *p = buf;
579 size_t n = size;
580 int ret;
581
582 if( strlen( name ) + sizeof( " key size" ) > size )
583 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
584
585 ret = snprintf( p, n, "%s key size", name );
586 SAFE_SNPRINTF();
587
588 return( 0 );
589}
590
591/*
592 * Return an informational string describing the given OID
593 */
594const char *x509_oid_get_description( x509_buf *oid )
595{
596 const char *desc = NULL;
597 int ret;
598
599 ret = oid_get_extended_key_usage( oid, &desc );
600
601 if( ret != 0 )
602 return( NULL );
603
604 return( desc );
605}
606
607/* Return the x.y.z.... style numeric string for the given OID */
608int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
609{
610 return oid_get_numeric_string( buf, size, oid );
611}
612
613/*
614 * Return 0 if the x509_time is still valid, or 1 otherwise.
615 */
616#if defined(POLARSSL_HAVE_TIME)
617int x509parse_time_expired( const x509_time *to )
618{
619 int year, mon, day;
620 int hour, min, sec;
621
622#if defined(_WIN32)
623 SYSTEMTIME st;
624
625 GetLocalTime(&st);
626
627 year = st.wYear;
628 mon = st.wMonth;
629 day = st.wDay;
630 hour = st.wHour;
631 min = st.wMinute;
632 sec = st.wSecond;
633#else
634 struct tm *lt;
635 time_t tt;
636
637 tt = time( NULL );
638 lt = localtime( &tt );
639
640 year = lt->tm_year + 1900;
641 mon = lt->tm_mon + 1;
642 day = lt->tm_mday;
643 hour = lt->tm_hour;
644 min = lt->tm_min;
645 sec = lt->tm_sec;
646#endif
647
648 if( year > to->year )
649 return( 1 );
650
651 if( year == to->year &&
652 mon > to->mon )
653 return( 1 );
654
655 if( year == to->year &&
656 mon == to->mon &&
657 day > to->day )
658 return( 1 );
659
660 if( year == to->year &&
661 mon == to->mon &&
662 day == to->day &&
663 hour > to->hour )
664 return( 1 );
665
666 if( year == to->year &&
667 mon == to->mon &&
668 day == to->day &&
669 hour == to->hour &&
670 min > to->min )
671 return( 1 );
672
673 if( year == to->year &&
674 mon == to->mon &&
675 day == to->day &&
676 hour == to->hour &&
677 min == to->min &&
678 sec > to->sec )
679 return( 1 );
680
681 return( 0 );
682}
683#else /* POLARSSL_HAVE_TIME */
684int x509parse_time_expired( const x509_time *to )
685{
686 ((void) to);
687 return( 0 );
688}
689#endif /* POLARSSL_HAVE_TIME */
690
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200691#if defined(POLARSSL_SELF_TEST)
692
693#include "polarssl/x509_crt.h"
694#include "polarssl/certs.h"
695
696/*
697 * Checkup routine
698 */
699int x509_self_test( int verbose )
700{
701#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
702 int ret;
703 int flags;
704 x509_cert cacert;
705 x509_cert clicert;
706
707 if( verbose != 0 )
708 printf( " X.509 certificate load: " );
709
710 memset( &clicert, 0, sizeof( x509_cert ) );
711
712 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
713 strlen( test_cli_crt ) );
714 if( ret != 0 )
715 {
716 if( verbose != 0 )
717 printf( "failed\n" );
718
719 return( ret );
720 }
721
722 memset( &cacert, 0, sizeof( x509_cert ) );
723
724 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
725 strlen( test_ca_crt ) );
726 if( ret != 0 )
727 {
728 if( verbose != 0 )
729 printf( "failed\n" );
730
731 return( ret );
732 }
733
734 if( verbose != 0 )
735 printf( "passed\n X.509 signature verify: ");
736
737 ret = x509parse_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
738 if( ret != 0 )
739 {
740 if( verbose != 0 )
741 printf( "failed\n" );
742
743 printf("ret = %d, &flags = %04x\n", ret, flags);
744
745 return( ret );
746 }
747
748 if( verbose != 0 )
749 printf( "passed\n\n");
750
751 x509_crt_free( &cacert );
752 x509_crt_free( &clicert );
753
754 return( 0 );
755#else
756 ((void) verbose);
757 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
758#endif
759}
760
761#endif
762
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763#endif /* POLARSSL_X509_USE_C */