blob: 25df0f7af1749aa5a8996f06bed63c77de741b1f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerefc30292011-11-10 14:43:23 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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 certificat format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc2459.txt
29 * http://www.ietf.org/rfc/rfc3279.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
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000043#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/des.h"
45#include "polarssl/md2.h"
46#include "polarssl/md4.h"
47#include "polarssl/md5.h"
48#include "polarssl/sha1.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000049#include "polarssl/sha2.h"
50#include "polarssl/sha4.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000051#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53#include <string.h>
54#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000055#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000056#include <windows.h>
57#else
Paul Bakker5121ce52009-01-03 21:22:43 +000058#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakker335db3f2011-04-25 15:28:35 +000061#if defined(POLARSSL_FS_IO)
62#include <stdio.h>
63#endif
64
Paul Bakker5121ce52009-01-03 21:22:43 +000065/*
Paul Bakker5121ce52009-01-03 21:22:43 +000066 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
67 */
68static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000069 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000070 int *ver )
71{
Paul Bakker23986e52011-04-24 08:57:21 +000072 int ret;
73 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75 if( ( ret = asn1_get_tag( p, end, &len,
76 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
77 {
Paul Bakker40e46942009-01-03 21:51:57 +000078 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000079 {
80 *ver = 0;
81 return( 0 );
82 }
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84 return( ret );
85 }
86
87 end = *p + len;
88
89 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000090 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +000093 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +000094 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( 0 );
97}
98
99/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000100 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000101 */
102static int x509_crl_get_version( unsigned char **p,
103 const unsigned char *end,
104 int *ver )
105{
106 int ret;
107
108 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
109 {
110 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000111 {
112 *ver = 0;
113 return( 0 );
114 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000115
116 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
117 }
118
119 return( 0 );
120}
121
122/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 * CertificateSerialNumber ::= INTEGER
124 */
125static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000126 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 x509_buf *serial )
128{
129 int ret;
130
131 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000132 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000133 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
136 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000137 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000138 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 serial->tag = *(*p)++;
141
142 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000143 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145 serial->p = *p;
146 *p += serial->len;
147
148 return( 0 );
149}
150
151/*
152 * AlgorithmIdentifier ::= SEQUENCE {
153 * algorithm OBJECT IDENTIFIER,
154 * parameters ANY DEFINED BY algorithm OPTIONAL }
155 */
156static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000157 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 x509_buf *alg )
159{
Paul Bakker23986e52011-04-24 08:57:21 +0000160 int ret;
161 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 if( ( ret = asn1_get_tag( p, end, &len,
164 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000165 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167 end = *p + len;
168 alg->tag = **p;
169
170 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000171 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 alg->p = *p;
174 *p += alg->len;
175
176 if( *p == end )
177 return( 0 );
178
179 /*
180 * assume the algorithm parameters must be NULL
181 */
182 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000183 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000186 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000187 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189 return( 0 );
190}
191
192/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 * AttributeTypeAndValue ::= SEQUENCE {
194 * type AttributeType,
195 * value AttributeValue }
196 *
197 * AttributeType ::= OBJECT IDENTIFIER
198 *
199 * AttributeValue ::= ANY DEFINED BY AttributeType
200 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000201static int x509_get_attr_type_value( unsigned char **p,
202 const unsigned char *end,
203 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204{
Paul Bakker23986e52011-04-24 08:57:21 +0000205 int ret;
206 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 x509_buf *oid;
208 x509_buf *val;
209
210 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000212 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 oid = &cur->oid;
215 oid->tag = **p;
216
217 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000218 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220 oid->p = *p;
221 *p += oid->len;
222
223 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000225 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
228 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
229 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000230 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000231 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 val = &cur->val;
234 val->tag = *(*p)++;
235
236 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000237 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 val->p = *p;
240 *p += val->len;
241
242 cur->next = NULL;
243
Paul Bakker400ff6f2011-02-20 10:40:16 +0000244 return( 0 );
245}
246
247/*
248 * RelativeDistinguishedName ::=
249 * SET OF AttributeTypeAndValue
250 *
251 * AttributeTypeAndValue ::= SEQUENCE {
252 * type AttributeType,
253 * value AttributeValue }
254 *
255 * AttributeType ::= OBJECT IDENTIFIER
256 *
257 * AttributeValue ::= ANY DEFINED BY AttributeType
258 */
259static int x509_get_name( unsigned char **p,
260 const unsigned char *end,
261 x509_name *cur )
262{
Paul Bakker23986e52011-04-24 08:57:21 +0000263 int ret;
264 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000265 const unsigned char *end2;
266 x509_name *use;
267
268 if( ( ret = asn1_get_tag( p, end, &len,
269 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000270 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000271
272 end2 = end;
273 end = *p + len;
274 use = cur;
275
276 do
277 {
278 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
279 return( ret );
280
281 if( *p != end )
282 {
283 use->next = (x509_name *) malloc(
284 sizeof( x509_name ) );
285
286 if( use->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +0000287 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000288
289 memset( use->next, 0, sizeof( x509_name ) );
290
291 use = use->next;
292 }
293 }
294 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296 /*
297 * recurse until end of SEQUENCE is reached
298 */
299 if( *p == end2 )
300 return( 0 );
301
302 cur->next = (x509_name *) malloc(
303 sizeof( x509_name ) );
304
305 if( cur->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +0000306 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
Paul Bakker07156682012-05-30 07:33:30 +0000308 memset( cur->next, 0, sizeof( x509_name ) );
309
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 return( x509_get_name( p, end2, cur->next ) );
311}
312
313/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 * Time ::= CHOICE {
315 * utcTime UTCTime,
316 * generalTime GeneralizedTime }
317 */
Paul Bakker91200182010-02-18 21:26:15 +0000318static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000319 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000320 x509_time *time )
321{
Paul Bakker23986e52011-04-24 08:57:21 +0000322 int ret;
323 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000324 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000325 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000326
Paul Bakker91200182010-02-18 21:26:15 +0000327 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000328 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
329 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330
Paul Bakker91200182010-02-18 21:26:15 +0000331 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332
Paul Bakker91200182010-02-18 21:26:15 +0000333 if ( tag == ASN1_UTC_TIME )
334 {
335 (*p)++;
336 ret = asn1_get_len( p, end, &len );
337
338 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000339 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000340
Paul Bakker91200182010-02-18 21:26:15 +0000341 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000342 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
343 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344
Paul Bakker91200182010-02-18 21:26:15 +0000345 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
346 &time->year, &time->mon, &time->day,
347 &time->hour, &time->min, &time->sec ) < 5 )
348 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000349
Paul Bakker400ff6f2011-02-20 10:40:16 +0000350 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000351 time->year += 1900;
352
353 *p += len;
354
355 return( 0 );
356 }
357 else if ( tag == ASN1_GENERALIZED_TIME )
358 {
359 (*p)++;
360 ret = asn1_get_len( p, end, &len );
361
362 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000363 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000364
365 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000366 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
367 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000368
369 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
370 &time->year, &time->mon, &time->day,
371 &time->hour, &time->min, &time->sec ) < 5 )
372 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
373
374 *p += len;
375
376 return( 0 );
377 }
378 else
Paul Bakker9d781402011-05-09 16:17:09 +0000379 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000380}
381
382
383/*
384 * Validity ::= SEQUENCE {
385 * notBefore Time,
386 * notAfter Time }
387 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000388static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000389 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 x509_time *from,
391 x509_time *to )
392{
Paul Bakker23986e52011-04-24 08:57:21 +0000393 int ret;
394 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396 if( ( ret = asn1_get_tag( p, end, &len,
397 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000398 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400 end = *p + len;
401
Paul Bakker91200182010-02-18 21:26:15 +0000402 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000403 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
Paul Bakker91200182010-02-18 21:26:15 +0000405 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000406 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
408 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000409 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000410 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 return( 0 );
413}
414
415/*
416 * SubjectPublicKeyInfo ::= SEQUENCE {
417 * algorithm AlgorithmIdentifier,
418 * subjectPublicKey BIT STRING }
419 */
420static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000421 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 x509_buf *pk_alg_oid,
423 mpi *N, mpi *E )
424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 int ret, can_handle;
426 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 unsigned char *end2;
428
429 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
430 return( ret );
431
432 /*
433 * only RSA public keys handled at this time
434 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000435 can_handle = 0;
436
437 if( pk_alg_oid->len == 9 &&
438 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
439 can_handle = 1;
440
441 if( pk_alg_oid->len == 9 &&
442 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
443 {
444 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
445 can_handle = 1;
446
447 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
448 can_handle = 1;
449 }
450
451 if( pk_alg_oid->len == 5 &&
452 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
453 can_handle = 1;
454
455 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000456 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000459 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000462 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000463 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465 end2 = *p + len;
466
467 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000468 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 /*
471 * RSAPublicKey ::= SEQUENCE {
472 * modulus INTEGER, -- n
473 * publicExponent INTEGER -- e
474 * }
475 */
476 if( ( ret = asn1_get_tag( p, end2, &len,
477 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000481 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000482 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
485 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000486 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000490 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 return( 0 );
493}
494
495static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000496 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 x509_buf *sig )
498{
Paul Bakker23986e52011-04-24 08:57:21 +0000499 int ret;
500 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 sig->tag = **p;
503
504 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000505 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Paul Bakker74111d32011-01-15 16:57:55 +0000507
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000509 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 sig->len = len;
512 sig->p = *p;
513
514 *p += len;
515
516 return( 0 );
517}
518
519/*
520 * X.509 v2/v3 unique identifier (not parsed)
521 */
522static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000523 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 x509_buf *uid, int n )
525{
526 int ret;
527
528 if( *p == end )
529 return( 0 );
530
531 uid->tag = **p;
532
533 if( ( ret = asn1_get_tag( p, end, &uid->len,
534 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
535 {
Paul Bakker40e46942009-01-03 21:51:57 +0000536 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 return( 0 );
538
539 return( ret );
540 }
541
542 uid->p = *p;
543 *p += uid->len;
544
545 return( 0 );
546}
547
548/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000549 * X.509 Extensions (No parsing of extensions, pointer should
550 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 */
552static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000553 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000554 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000555{
Paul Bakker23986e52011-04-24 08:57:21 +0000556 int ret;
557 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 if( *p == end )
560 return( 0 );
561
562 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000563
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000565 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568 ext->p = *p;
569 end = *p + ext->len;
570
571 /*
572 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
573 *
574 * Extension ::= SEQUENCE {
575 * extnID OBJECT IDENTIFIER,
576 * critical BOOLEAN DEFAULT FALSE,
577 * extnValue OCTET STRING }
578 */
579 if( ( ret = asn1_get_tag( p, end, &len,
580 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000581 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000584 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000585 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Paul Bakkerd98030e2009-05-02 15:13:40 +0000587 return( 0 );
588}
589
590/*
591 * X.509 CRL v2 extensions (no extensions parsed yet.)
592 */
593static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000594 const unsigned char *end,
595 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000596{
Paul Bakker23986e52011-04-24 08:57:21 +0000597 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000598 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000599
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000600 /* Get explicit tag */
601 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000602 {
603 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
604 return( 0 );
605
606 return( ret );
607 }
608
609 while( *p < end )
610 {
611 if( ( ret = asn1_get_tag( p, end, &len,
612 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000613 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614
615 *p += len;
616 }
617
618 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000619 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000620 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
621
622 return( 0 );
623}
624
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000625/*
626 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
627 */
628static int x509_get_crl_entry_ext( unsigned char **p,
629 const unsigned char *end,
630 x509_buf *ext )
631{
632 int ret;
633 size_t len = 0;
634
635 /* OPTIONAL */
636 if (end <= *p)
637 return( 0 );
638
639 ext->tag = **p;
640 ext->p = *p;
641
642 /*
643 * Get CRL-entry extension sequence header
644 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
645 */
646 if( ( ret = asn1_get_tag( p, end, &ext->len,
647 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
648 {
649 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
650 {
651 ext->p = NULL;
652 return( 0 );
653 }
654 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
655 }
656
657 end = *p + ext->len;
658
659 if( end != *p + ext->len )
660 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
661 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
662
663 while( *p < end )
664 {
665 if( ( ret = asn1_get_tag( p, end, &len,
666 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
667 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
668
669 *p += len;
670 }
671
672 if( *p != end )
673 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
674 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
675
676 return( 0 );
677}
678
Paul Bakker74111d32011-01-15 16:57:55 +0000679static int x509_get_basic_constraints( unsigned char **p,
680 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000681 int *ca_istrue,
682 int *max_pathlen )
683{
Paul Bakker23986e52011-04-24 08:57:21 +0000684 int ret;
685 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000686
687 /*
688 * BasicConstraints ::= SEQUENCE {
689 * cA BOOLEAN DEFAULT FALSE,
690 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
691 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000692 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000693 *max_pathlen = 0; /* endless */
694
695 if( ( ret = asn1_get_tag( p, end, &len,
696 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000697 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000698
699 if( *p == end )
700 return 0;
701
Paul Bakker3cccddb2011-01-16 21:46:31 +0000702 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000703 {
704 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000705 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000706
707 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000708 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000709
Paul Bakker3cccddb2011-01-16 21:46:31 +0000710 if( *ca_istrue != 0 )
711 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000712 }
713
714 if( *p == end )
715 return 0;
716
717 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000718 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000719
720 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000721 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000722 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
723
724 (*max_pathlen)++;
725
Paul Bakker74111d32011-01-15 16:57:55 +0000726 return 0;
727}
728
729static int x509_get_ns_cert_type( unsigned char **p,
730 const unsigned char *end,
731 unsigned char *ns_cert_type)
732{
733 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000734 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000735
736 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000737 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000738
739 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000740 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000741 POLARSSL_ERR_ASN1_INVALID_LENGTH );
742
743 /* Get actual bitstring */
744 *ns_cert_type = *bs.p;
745 return 0;
746}
747
748static int x509_get_key_usage( unsigned char **p,
749 const unsigned char *end,
750 unsigned char *key_usage)
751{
752 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000753 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000754
755 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000756 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000757
Paul Bakkercebdf172011-11-11 15:01:31 +0000758 if( bs.len > 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000759 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000760 POLARSSL_ERR_ASN1_INVALID_LENGTH );
761
762 /* Get actual bitstring */
763 *key_usage = *bs.p;
764 return 0;
765}
766
Paul Bakkerd98030e2009-05-02 15:13:40 +0000767/*
Paul Bakker74111d32011-01-15 16:57:55 +0000768 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
769 *
770 * KeyPurposeId ::= OBJECT IDENTIFIER
771 */
772static int x509_get_ext_key_usage( unsigned char **p,
773 const unsigned char *end,
774 x509_sequence *ext_key_usage)
775{
776 int ret;
777
778 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000779 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000780
781 /* Sequence length must be >= 1 */
782 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000783 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000784 POLARSSL_ERR_ASN1_INVALID_LENGTH );
785
786 return 0;
787}
788
789/*
790 * X.509 v3 extensions
791 *
792 * TODO: Perform all of the basic constraints tests required by the RFC
793 * TODO: Set values for undetected extensions to a sane default?
794 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000795 */
796static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000797 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000798 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000799{
Paul Bakker23986e52011-04-24 08:57:21 +0000800 int ret;
801 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000802 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000803
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000804 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000805 {
806 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
807 return( 0 );
808
809 return( ret );
810 }
811
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 while( *p < end )
813 {
Paul Bakker74111d32011-01-15 16:57:55 +0000814 /*
815 * Extension ::= SEQUENCE {
816 * extnID OBJECT IDENTIFIER,
817 * critical BOOLEAN DEFAULT FALSE,
818 * extnValue OCTET STRING }
819 */
820 x509_buf extn_oid = {0, 0, NULL};
821 int is_critical = 0; /* DEFAULT FALSE */
822
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 if( ( ret = asn1_get_tag( p, end, &len,
824 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000825 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000826
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000827 end_ext_data = *p + len;
828
Paul Bakker74111d32011-01-15 16:57:55 +0000829 /* Get extension ID */
830 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Paul Bakker74111d32011-01-15 16:57:55 +0000832 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Paul Bakker74111d32011-01-15 16:57:55 +0000835 extn_oid.p = *p;
836 *p += extn_oid.len;
837
838 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000839 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000840 POLARSSL_ERR_ASN1_OUT_OF_DATA );
841
842 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000843 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000844 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Paul Bakker74111d32011-01-15 16:57:55 +0000847 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000848 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000850 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000852 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000853
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000854 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000855 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000856 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000857
Paul Bakker74111d32011-01-15 16:57:55 +0000858 /*
859 * Detect supported extensions
860 */
861 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
862 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 {
Paul Bakker74111d32011-01-15 16:57:55 +0000864 /* Parse basic constraints */
865 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000866 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000867 return ( ret );
868 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 }
Paul Bakker74111d32011-01-15 16:57:55 +0000870 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
871 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
872 {
873 /* Parse netscape certificate type */
874 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
875 &crt->ns_cert_type ) ) != 0 )
876 return ( ret );
877 crt->ext_types |= EXT_NS_CERT_TYPE;
878 }
879 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
880 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
881 {
882 /* Parse key usage */
883 if( ( ret = x509_get_key_usage( p, end_ext_octet,
884 &crt->key_usage ) ) != 0 )
885 return ( ret );
886 crt->ext_types |= EXT_KEY_USAGE;
887 }
888 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
889 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
890 {
891 /* Parse extended key usage */
892 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
893 &crt->ext_key_usage ) ) != 0 )
894 return ( ret );
895 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
896 }
897 else
898 {
899 /* No parser found, skip extension */
900 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000901
Paul Bakker5c721f92011-07-27 16:51:09 +0000902#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000903 if( is_critical )
904 {
905 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000906 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000907 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
908 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000909#endif
Paul Bakker74111d32011-01-15 16:57:55 +0000910 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 }
912
913 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000914 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000915 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 return( 0 );
918}
919
920/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000921 * X.509 CRL Entries
922 */
923static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000924 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000925 x509_crl_entry *entry )
926{
Paul Bakker23986e52011-04-24 08:57:21 +0000927 int ret;
928 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000929 x509_crl_entry *cur_entry = entry;
930
931 if( *p == end )
932 return( 0 );
933
Paul Bakker9be19372009-07-27 20:21:53 +0000934 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000935 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
936 {
937 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
938 return( 0 );
939
940 return( ret );
941 }
942
Paul Bakker9be19372009-07-27 20:21:53 +0000943 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000944
945 while( *p < end )
946 {
Paul Bakker23986e52011-04-24 08:57:21 +0000947 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000948 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000949
950 if( ( ret = asn1_get_tag( p, end, &len2,
951 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
952 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000953 return( ret );
954 }
955
Paul Bakker9be19372009-07-27 20:21:53 +0000956 cur_entry->raw.tag = **p;
957 cur_entry->raw.p = *p;
958 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000959 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +0000960
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000961 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000962 return( ret );
963
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000964 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000965 return( ret );
966
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000967 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000968 return( ret );
969
Paul Bakker74111d32011-01-15 16:57:55 +0000970 if ( *p < end )
971 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000972 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkere2e36d32012-01-23 09:56:51 +0000973
974 if( cur_entry->next == NULL )
975 return( POLARSSL_ERR_X509_MALLOC_FAILED );
976
Paul Bakkerd98030e2009-05-02 15:13:40 +0000977 cur_entry = cur_entry->next;
978 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
979 }
980 }
981
982 return( 0 );
983}
984
Paul Bakker27d66162010-03-17 06:56:01 +0000985static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
986{
987 if( sig_oid->len == 9 &&
988 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
989 {
990 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
991 {
992 *sig_alg = sig_oid->p[8];
993 return( 0 );
994 }
995
996 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
997 {
998 *sig_alg = sig_oid->p[8];
999 return( 0 );
1000 }
1001
1002 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1003 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001004 if( sig_oid->len == 5 &&
1005 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1006 {
1007 *sig_alg = SIG_RSA_SHA1;
1008 return( 0 );
1009 }
Paul Bakker27d66162010-03-17 06:56:01 +00001010
1011 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1012}
1013
Paul Bakkerd98030e2009-05-02 15:13:40 +00001014/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001015 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001017int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018{
Paul Bakker23986e52011-04-24 08:57:21 +00001019 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001020 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Paul Bakker320a4b52009-03-28 18:52:39 +00001023 /*
1024 * Check for valid input
1025 */
1026 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001027 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001028
Paul Bakker96743fc2011-02-12 14:30:57 +00001029 p = (unsigned char *) malloc( len = buflen );
1030
1031 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001032 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001033
1034 memcpy( p, buf, buflen );
1035
1036 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
1038 crt->raw.p = p;
1039 crt->raw.len = len;
1040 end = p + len;
1041
1042 /*
1043 * Certificate ::= SEQUENCE {
1044 * tbsCertificate TBSCertificate,
1045 * signatureAlgorithm AlgorithmIdentifier,
1046 * signatureValue BIT STRING }
1047 */
1048 if( ( ret = asn1_get_tag( &p, end, &len,
1049 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1050 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001051 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001052 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 }
1054
Paul Bakker23986e52011-04-24 08:57:21 +00001055 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001057 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001058 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001059 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 }
1061
1062 /*
1063 * TBSCertificate ::= SEQUENCE {
1064 */
1065 crt->tbs.p = p;
1066
1067 if( ( ret = asn1_get_tag( &p, end, &len,
1068 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1069 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001070 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001071 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 }
1073
1074 end = p + len;
1075 crt->tbs.len = end - crt->tbs.p;
1076
1077 /*
1078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1079 *
1080 * CertificateSerialNumber ::= INTEGER
1081 *
1082 * signature AlgorithmIdentifier
1083 */
1084 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1085 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1086 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1087 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001088 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 return( ret );
1090 }
1091
1092 crt->version++;
1093
1094 if( crt->version > 3 )
1095 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001096 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001097 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 }
1099
Paul Bakker27d66162010-03-17 06:56:01 +00001100 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001102 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001103 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 }
1105
1106 /*
1107 * issuer Name
1108 */
1109 crt->issuer_raw.p = p;
1110
1111 if( ( ret = asn1_get_tag( &p, end, &len,
1112 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1113 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001114 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001115 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 }
1117
1118 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1119 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001120 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 return( ret );
1122 }
1123
1124 crt->issuer_raw.len = p - crt->issuer_raw.p;
1125
1126 /*
1127 * Validity ::= SEQUENCE {
1128 * notBefore Time,
1129 * notAfter Time }
1130 *
1131 */
1132 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1133 &crt->valid_to ) ) != 0 )
1134 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001135 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 return( ret );
1137 }
1138
1139 /*
1140 * subject Name
1141 */
1142 crt->subject_raw.p = p;
1143
1144 if( ( ret = asn1_get_tag( &p, end, &len,
1145 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1146 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001147 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001148 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 }
1150
1151 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1152 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001153 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 return( ret );
1155 }
1156
1157 crt->subject_raw.len = p - crt->subject_raw.p;
1158
1159 /*
1160 * SubjectPublicKeyInfo ::= SEQUENCE
1161 * algorithm AlgorithmIdentifier,
1162 * subjectPublicKey BIT STRING }
1163 */
1164 if( ( ret = asn1_get_tag( &p, end, &len,
1165 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1166 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001167 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001168 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 }
1170
1171 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1172 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1173 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001174 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 return( ret );
1176 }
1177
1178 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1179 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001180 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 return( ret );
1182 }
1183
1184 crt->rsa.len = mpi_size( &crt->rsa.N );
1185
1186 /*
1187 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1188 * -- If present, version shall be v2 or v3
1189 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1190 * -- If present, version shall be v2 or v3
1191 * extensions [3] EXPLICIT Extensions OPTIONAL
1192 * -- If present, version shall be v3
1193 */
1194 if( crt->version == 2 || crt->version == 3 )
1195 {
1196 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1197 if( ret != 0 )
1198 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001199 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 return( ret );
1201 }
1202 }
1203
1204 if( crt->version == 2 || crt->version == 3 )
1205 {
1206 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1207 if( ret != 0 )
1208 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001209 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 return( ret );
1211 }
1212 }
1213
1214 if( crt->version == 3 )
1215 {
Paul Bakker74111d32011-01-15 16:57:55 +00001216 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 if( ret != 0 )
1218 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001219 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 return( ret );
1221 }
1222 }
1223
1224 if( p != end )
1225 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001226 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001227 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001228 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 }
1230
1231 end = crt->raw.p + crt->raw.len;
1232
1233 /*
1234 * signatureAlgorithm AlgorithmIdentifier,
1235 * signatureValue BIT STRING
1236 */
1237 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1238 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001239 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 return( ret );
1241 }
1242
Paul Bakker320a4b52009-03-28 18:52:39 +00001243 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001245 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001246 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 }
1248
1249 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1250 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001251 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 return( ret );
1253 }
1254
1255 if( p != end )
1256 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001257 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001258 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001259 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 }
1261
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001262 return( 0 );
1263}
1264
1265/*
1266 * Parse one or more PEM certificates from a buffer and add them to the chained list
1267 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001268int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001269{
Paul Bakker732e1a82011-12-11 16:35:09 +00001270 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001271 x509_cert *crt, *prev = NULL;
1272 int buf_format = X509_FORMAT_DER;
1273
1274 crt = chain;
1275
1276 /*
1277 * Check for valid input
1278 */
1279 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001280 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001281
1282 while( crt->version != 0 && crt->next != NULL )
1283 {
1284 prev = crt;
1285 crt = crt->next;
1286 }
1287
1288 /*
1289 * Add new certificate on the end of the chain if needed.
1290 */
1291 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001292 {
1293 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1294
Paul Bakker7d06ad22009-05-02 15:53:56 +00001295 if( crt->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001296 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001297
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001298 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001299 crt = crt->next;
1300 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001302
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001303 /*
1304 * Determine buffer content. Buffer contains either one DER certificate or
1305 * one or more PEM certificates.
1306 */
1307#if defined(POLARSSL_PEM_C)
1308 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1309 buf_format = X509_FORMAT_PEM;
1310#endif
1311
1312 if( buf_format == X509_FORMAT_DER )
1313 return x509parse_crt_der( crt, buf, buflen );
1314
1315#if defined(POLARSSL_PEM_C)
1316 if( buf_format == X509_FORMAT_PEM )
1317 {
1318 pem_context pem;
1319
1320 while( buflen > 0 )
1321 {
1322 size_t use_len;
1323 pem_init( &pem );
1324
1325 ret = pem_read_buffer( &pem,
1326 "-----BEGIN CERTIFICATE-----",
1327 "-----END CERTIFICATE-----",
1328 buf, NULL, 0, &use_len );
1329
1330 if( ret == 0 )
1331 {
1332 /*
1333 * Was PEM encoded
1334 */
1335 buflen -= use_len;
1336 buf += use_len;
1337 }
1338 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1339 {
1340 pem_free( &pem );
1341
1342 if( first_error == 0 )
1343 first_error = ret;
1344
1345 continue;
1346 }
1347 else
1348 break;
1349
1350 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1351
1352 pem_free( &pem );
1353
1354 if( ret != 0 )
1355 {
1356 /*
Paul Bakker732e1a82011-12-11 16:35:09 +00001357 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001358 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001359 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001360 {
1361 if( prev )
1362 prev->next = NULL;
1363
1364 if( crt != chain )
1365 free( crt );
1366
1367 return( ret );
1368 }
1369
1370 if( first_error == 0 )
1371 first_error = ret;
Paul Bakker732e1a82011-12-11 16:35:09 +00001372
1373 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001374
1375 memset( crt, 0, sizeof( x509_cert ) );
1376 continue;
1377 }
1378
1379 success = 1;
1380
1381 /*
1382 * Add new certificate to the list
1383 */
1384 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1385
1386 if( crt->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001387 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001388
1389 prev = crt;
1390 crt = crt->next;
1391 memset( crt, 0, sizeof( x509_cert ) );
1392 }
1393 }
1394#endif
1395
1396 if( crt->version == 0 )
1397 {
1398 if( prev )
1399 prev->next = NULL;
1400
1401 if( crt != chain )
1402 free( crt );
1403 }
1404
1405 if( success )
Paul Bakker732e1a82011-12-11 16:35:09 +00001406 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001407 else if( first_error )
1408 return( first_error );
1409 else
1410 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001411}
1412
1413/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001414 * Parse one or more CRLs and add them to the chained list
1415 */
Paul Bakker23986e52011-04-24 08:57:21 +00001416int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001417{
Paul Bakker23986e52011-04-24 08:57:21 +00001418 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001419 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001420 unsigned char *p, *end;
1421 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001422#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001423 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001424 pem_context pem;
1425#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001426
1427 crl = chain;
1428
1429 /*
1430 * Check for valid input
1431 */
1432 if( crl == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001433 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001434
1435 while( crl->version != 0 && crl->next != NULL )
1436 crl = crl->next;
1437
1438 /*
1439 * Add new CRL on the end of the chain if needed.
1440 */
1441 if ( crl->version != 0 && crl->next == NULL)
1442 {
1443 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1444
Paul Bakker7d06ad22009-05-02 15:53:56 +00001445 if( crl->next == NULL )
1446 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001447 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001448 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001449 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001450
Paul Bakker7d06ad22009-05-02 15:53:56 +00001451 crl = crl->next;
1452 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001453 }
1454
Paul Bakker96743fc2011-02-12 14:30:57 +00001455#if defined(POLARSSL_PEM_C)
1456 pem_init( &pem );
1457 ret = pem_read_buffer( &pem,
1458 "-----BEGIN X509 CRL-----",
1459 "-----END X509 CRL-----",
1460 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001461
Paul Bakker96743fc2011-02-12 14:30:57 +00001462 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001463 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001464 /*
1465 * Was PEM encoded
1466 */
1467 buflen -= use_len;
1468 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001469
1470 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001471 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001472 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001473 p = pem.buf;
1474 pem.buf = NULL;
1475 len = pem.buflen;
1476 pem_free( &pem );
1477 }
1478 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1479 {
1480 pem_free( &pem );
1481 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001482 }
1483 else
1484 {
1485 /*
1486 * nope, copy the raw DER data
1487 */
1488 p = (unsigned char *) malloc( len = buflen );
1489
1490 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001491 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001492
1493 memcpy( p, buf, buflen );
1494
1495 buflen = 0;
1496 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001497#else
1498 p = (unsigned char *) malloc( len = buflen );
1499
1500 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001501 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001502
1503 memcpy( p, buf, buflen );
1504
1505 buflen = 0;
1506#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001507
1508 crl->raw.p = p;
1509 crl->raw.len = len;
1510 end = p + len;
1511
1512 /*
1513 * CertificateList ::= SEQUENCE {
1514 * tbsCertList TBSCertList,
1515 * signatureAlgorithm AlgorithmIdentifier,
1516 * signatureValue BIT STRING }
1517 */
1518 if( ( ret = asn1_get_tag( &p, end, &len,
1519 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1520 {
1521 x509_crl_free( crl );
1522 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1523 }
1524
Paul Bakker23986e52011-04-24 08:57:21 +00001525 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001526 {
1527 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001528 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001529 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1530 }
1531
1532 /*
1533 * TBSCertList ::= SEQUENCE {
1534 */
1535 crl->tbs.p = p;
1536
1537 if( ( ret = asn1_get_tag( &p, end, &len,
1538 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1539 {
1540 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001541 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001542 }
1543
1544 end = p + len;
1545 crl->tbs.len = end - crl->tbs.p;
1546
1547 /*
1548 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1549 * -- if present, MUST be v2
1550 *
1551 * signature AlgorithmIdentifier
1552 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001553 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001554 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1555 {
1556 x509_crl_free( crl );
1557 return( ret );
1558 }
1559
1560 crl->version++;
1561
1562 if( crl->version > 2 )
1563 {
1564 x509_crl_free( crl );
1565 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1566 }
1567
Paul Bakker27d66162010-03-17 06:56:01 +00001568 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001569 {
1570 x509_crl_free( crl );
1571 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1572 }
1573
1574 /*
1575 * issuer Name
1576 */
1577 crl->issuer_raw.p = p;
1578
1579 if( ( ret = asn1_get_tag( &p, end, &len,
1580 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1581 {
1582 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001583 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001584 }
1585
1586 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1587 {
1588 x509_crl_free( crl );
1589 return( ret );
1590 }
1591
1592 crl->issuer_raw.len = p - crl->issuer_raw.p;
1593
1594 /*
1595 * thisUpdate Time
1596 * nextUpdate Time OPTIONAL
1597 */
Paul Bakker91200182010-02-18 21:26:15 +00001598 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001599 {
1600 x509_crl_free( crl );
1601 return( ret );
1602 }
1603
Paul Bakker91200182010-02-18 21:26:15 +00001604 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001605 {
Paul Bakker9d781402011-05-09 16:17:09 +00001606 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001607 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001608 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001609 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001610 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001611 x509_crl_free( crl );
1612 return( ret );
1613 }
1614 }
1615
1616 /*
1617 * revokedCertificates SEQUENCE OF SEQUENCE {
1618 * userCertificate CertificateSerialNumber,
1619 * revocationDate Time,
1620 * crlEntryExtensions Extensions OPTIONAL
1621 * -- if present, MUST be v2
1622 * } OPTIONAL
1623 */
1624 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1625 {
1626 x509_crl_free( crl );
1627 return( ret );
1628 }
1629
1630 /*
1631 * crlExtensions EXPLICIT Extensions OPTIONAL
1632 * -- if present, MUST be v2
1633 */
1634 if( crl->version == 2 )
1635 {
1636 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1637
1638 if( ret != 0 )
1639 {
1640 x509_crl_free( crl );
1641 return( ret );
1642 }
1643 }
1644
1645 if( p != end )
1646 {
1647 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001648 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001649 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1650 }
1651
1652 end = crl->raw.p + crl->raw.len;
1653
1654 /*
1655 * signatureAlgorithm AlgorithmIdentifier,
1656 * signatureValue BIT STRING
1657 */
1658 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1659 {
1660 x509_crl_free( crl );
1661 return( ret );
1662 }
1663
1664 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1665 {
1666 x509_crl_free( crl );
1667 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1668 }
1669
1670 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1671 {
1672 x509_crl_free( crl );
1673 return( ret );
1674 }
1675
1676 if( p != end )
1677 {
1678 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001679 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001680 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1681 }
1682
1683 if( buflen > 0 )
1684 {
1685 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1686
Paul Bakker7d06ad22009-05-02 15:53:56 +00001687 if( crl->next == NULL )
1688 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001689 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001690 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001691 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001692
Paul Bakker7d06ad22009-05-02 15:53:56 +00001693 crl = crl->next;
1694 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001695
1696 return( x509parse_crl( crl, buf, buflen ) );
1697 }
1698
1699 return( 0 );
1700}
1701
Paul Bakker335db3f2011-04-25 15:28:35 +00001702#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001703/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001704 * Load all data from a file into a given buffer.
1705 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001706int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001707{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001708 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001709
Paul Bakkerd98030e2009-05-02 15:13:40 +00001710 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001711 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001712
Paul Bakkerd98030e2009-05-02 15:13:40 +00001713 fseek( f, 0, SEEK_END );
1714 *n = (size_t) ftell( f );
1715 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001716
Paul Bakkerd98030e2009-05-02 15:13:40 +00001717 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001718 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001719
Paul Bakkerd98030e2009-05-02 15:13:40 +00001720 if( fread( *buf, 1, *n, f ) != *n )
1721 {
1722 fclose( f );
1723 free( *buf );
Paul Bakker732e1a82011-12-11 16:35:09 +00001724 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001726
Paul Bakkerd98030e2009-05-02 15:13:40 +00001727 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001728
Paul Bakkerd98030e2009-05-02 15:13:40 +00001729 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001730
Paul Bakkerd98030e2009-05-02 15:13:40 +00001731 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001732}
1733
1734/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001735 * Load one or more certificates and add them to the chained list
1736 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001737int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001738{
1739 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001740 size_t n;
1741 unsigned char *buf;
1742
Paul Bakker732e1a82011-12-11 16:35:09 +00001743 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1744 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
Paul Bakker732e1a82011-12-11 16:35:09 +00001746 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
1748 memset( buf, 0, n + 1 );
1749 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
1751 return( ret );
1752}
1753
Paul Bakkerd98030e2009-05-02 15:13:40 +00001754/*
1755 * Load one or more CRLs and add them to the chained list
1756 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001757int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001758{
1759 int ret;
1760 size_t n;
1761 unsigned char *buf;
1762
Paul Bakker732e1a82011-12-11 16:35:09 +00001763 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1764 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001765
Paul Bakker27fdf462011-06-09 13:55:13 +00001766 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001767
1768 memset( buf, 0, n + 1 );
1769 free( buf );
1770
1771 return( ret );
1772}
1773
Paul Bakker5121ce52009-01-03 21:22:43 +00001774/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001775 * Load and parse a private RSA key
1776 */
1777int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1778{
1779 int ret;
1780 size_t n;
1781 unsigned char *buf;
1782
Paul Bakker732e1a82011-12-11 16:35:09 +00001783 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1784 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001785
1786 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001787 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001788 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001789 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001790 (unsigned char *) pwd, strlen( pwd ) );
1791
1792 memset( buf, 0, n + 1 );
1793 free( buf );
1794
1795 return( ret );
1796}
1797
1798/*
1799 * Load and parse a public RSA key
1800 */
1801int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1802{
1803 int ret;
1804 size_t n;
1805 unsigned char *buf;
1806
Paul Bakker732e1a82011-12-11 16:35:09 +00001807 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1808 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001809
Paul Bakker27fdf462011-06-09 13:55:13 +00001810 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001811
1812 memset( buf, 0, n + 1 );
1813 free( buf );
1814
1815 return( ret );
1816}
1817#endif /* POLARSSL_FS_IO */
1818
1819/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001820 * Parse a private RSA key
1821 */
Paul Bakker23986e52011-04-24 08:57:21 +00001822int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1823 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001824{
Paul Bakker23986e52011-04-24 08:57:21 +00001825 int ret;
1826 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001827 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001828 unsigned char *p_alt;
1829 x509_buf pk_alg_oid;
1830
Paul Bakker96743fc2011-02-12 14:30:57 +00001831#if defined(POLARSSL_PEM_C)
1832 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Paul Bakker96743fc2011-02-12 14:30:57 +00001834 pem_init( &pem );
1835 ret = pem_read_buffer( &pem,
1836 "-----BEGIN RSA PRIVATE KEY-----",
1837 "-----END RSA PRIVATE KEY-----",
1838 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Paul Bakkered56b222011-07-13 11:26:43 +00001840 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1841 {
1842 ret = pem_read_buffer( &pem,
1843 "-----BEGIN PRIVATE KEY-----",
1844 "-----END PRIVATE KEY-----",
1845 key, pwd, pwdlen, &len );
1846 }
1847
Paul Bakker96743fc2011-02-12 14:30:57 +00001848 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001850 /*
1851 * Was PEM encoded
1852 */
1853 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001855 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001856 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001857 pem_free( &pem );
1858 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Paul Bakker96743fc2011-02-12 14:30:57 +00001861 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1862#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001863 ((void) pwd);
1864 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001865 p = (unsigned char *) key;
1866#endif
1867 end = p + keylen;
1868
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001870 * Note: Depending on the type of private key file one can expect either a
1871 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1872 *
1873 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001874 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001875 * algorithm AlgorithmIdentifier,
1876 * PrivateKey BIT STRING
1877 * }
1878 *
1879 * AlgorithmIdentifier ::= SEQUENCE {
1880 * algorithm OBJECT IDENTIFIER,
1881 * parameters ANY DEFINED BY algorithm OPTIONAL
1882 * }
1883 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 * RSAPrivateKey ::= SEQUENCE {
1885 * version Version,
1886 * modulus INTEGER, -- n
1887 * publicExponent INTEGER, -- e
1888 * privateExponent INTEGER, -- d
1889 * prime1 INTEGER, -- p
1890 * prime2 INTEGER, -- q
1891 * exponent1 INTEGER, -- d mod (p-1)
1892 * exponent2 INTEGER, -- d mod (q-1)
1893 * coefficient INTEGER, -- (inverse of q) mod p
1894 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1895 * }
1896 */
1897 if( ( ret = asn1_get_tag( &p, end, &len,
1898 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1899 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001900#if defined(POLARSSL_PEM_C)
1901 pem_free( &pem );
1902#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001904 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 }
1906
1907 end = p + len;
1908
1909 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1910 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001911#if defined(POLARSSL_PEM_C)
1912 pem_free( &pem );
1913#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001915 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 }
1917
1918 if( rsa->ver != 0 )
1919 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001920#if defined(POLARSSL_PEM_C)
1921 pem_free( &pem );
1922#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001924 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 }
1926
Paul Bakkered56b222011-07-13 11:26:43 +00001927 p_alt = p;
1928
1929 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1930 {
1931 // Assume that we have the PKCS#1 format if wrong
1932 // tag was encountered
1933 //
1934 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1935 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1936 {
1937#if defined(POLARSSL_PEM_C)
1938 pem_free( &pem );
1939#endif
1940 rsa_free( rsa );
1941 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1942 }
1943 }
1944 else
1945 {
1946 int can_handle;
1947
1948 /*
1949 * only RSA keys handled at this time
1950 */
1951 can_handle = 0;
1952
1953 if( pk_alg_oid.len == 9 &&
1954 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1955 can_handle = 1;
1956
1957 if( pk_alg_oid.len == 9 &&
1958 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1959 {
1960 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1961 can_handle = 1;
1962
1963 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1964 can_handle = 1;
1965 }
1966
1967 if( pk_alg_oid.len == 5 &&
1968 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
1969 can_handle = 1;
1970
1971 if( can_handle == 0 )
1972 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
1973
1974 /*
1975 * Parse the PKCS#8 format
1976 */
1977
1978 p = p_alt;
1979 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
1980 {
1981#if defined(POLARSSL_PEM_C)
1982 pem_free( &pem );
1983#endif
1984 rsa_free( rsa );
1985 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1986 }
1987
1988 if( ( end - p ) < 1 )
1989 {
1990#if defined(POLARSSL_PEM_C)
1991 pem_free( &pem );
1992#endif
1993 rsa_free( rsa );
1994 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
1995 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1996 }
1997
1998 end = p + len;
1999
2000 if( ( ret = asn1_get_tag( &p, end, &len,
2001 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2002 {
2003#if defined(POLARSSL_PEM_C)
2004 pem_free( &pem );
2005#endif
2006 rsa_free( rsa );
2007 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2008 }
2009
2010 end = p + len;
2011
2012 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2013 {
2014#if defined(POLARSSL_PEM_C)
2015 pem_free( &pem );
2016#endif
2017 rsa_free( rsa );
2018 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2019 }
2020
2021 if( rsa->ver != 0 )
2022 {
2023#if defined(POLARSSL_PEM_C)
2024 pem_free( &pem );
2025#endif
2026 rsa_free( rsa );
2027 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2028 }
2029 }
2030
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2032 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2033 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2034 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2035 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2036 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2037 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2038 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2039 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002040#if defined(POLARSSL_PEM_C)
2041 pem_free( &pem );
2042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002044 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 }
2046
2047 rsa->len = mpi_size( &rsa->N );
2048
2049 if( p != end )
2050 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002051#if defined(POLARSSL_PEM_C)
2052 pem_free( &pem );
2053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002055 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002056 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 }
2058
2059 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2060 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002061#if defined(POLARSSL_PEM_C)
2062 pem_free( &pem );
2063#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 rsa_free( rsa );
2065 return( ret );
2066 }
2067
Paul Bakker96743fc2011-02-12 14:30:57 +00002068#if defined(POLARSSL_PEM_C)
2069 pem_free( &pem );
2070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
2072 return( 0 );
2073}
2074
2075/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002076 * Parse a public RSA key
2077 */
Paul Bakker23986e52011-04-24 08:57:21 +00002078int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002079{
Paul Bakker23986e52011-04-24 08:57:21 +00002080 int ret;
2081 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002082 unsigned char *p, *end;
2083 x509_buf alg_oid;
2084#if defined(POLARSSL_PEM_C)
2085 pem_context pem;
2086
2087 pem_init( &pem );
2088 ret = pem_read_buffer( &pem,
2089 "-----BEGIN PUBLIC KEY-----",
2090 "-----END PUBLIC KEY-----",
2091 key, NULL, 0, &len );
2092
2093 if( ret == 0 )
2094 {
2095 /*
2096 * Was PEM encoded
2097 */
2098 keylen = pem.buflen;
2099 }
2100 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2101 {
2102 pem_free( &pem );
2103 return( ret );
2104 }
2105
2106 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2107#else
2108 p = (unsigned char *) key;
2109#endif
2110 end = p + keylen;
2111
2112 /*
2113 * PublicKeyInfo ::= SEQUENCE {
2114 * algorithm AlgorithmIdentifier,
2115 * PublicKey BIT STRING
2116 * }
2117 *
2118 * AlgorithmIdentifier ::= SEQUENCE {
2119 * algorithm OBJECT IDENTIFIER,
2120 * parameters ANY DEFINED BY algorithm OPTIONAL
2121 * }
2122 *
2123 * RSAPublicKey ::= SEQUENCE {
2124 * modulus INTEGER, -- n
2125 * publicExponent INTEGER -- e
2126 * }
2127 */
2128
2129 if( ( ret = asn1_get_tag( &p, end, &len,
2130 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2131 {
2132#if defined(POLARSSL_PEM_C)
2133 pem_free( &pem );
2134#endif
2135 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002136 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002137 }
2138
2139 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2140 {
2141#if defined(POLARSSL_PEM_C)
2142 pem_free( &pem );
2143#endif
2144 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002145 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002146 }
2147
2148 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2149 {
2150#if defined(POLARSSL_PEM_C)
2151 pem_free( &pem );
2152#endif
2153 rsa_free( rsa );
2154 return( ret );
2155 }
2156
2157 rsa->len = mpi_size( &rsa->N );
2158
2159#if defined(POLARSSL_PEM_C)
2160 pem_free( &pem );
2161#endif
2162
2163 return( 0 );
2164}
2165
Paul Bakkereaa89f82011-04-04 21:36:15 +00002166#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002167/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002168 * Parse DHM parameters
2169 */
Paul Bakker23986e52011-04-24 08:57:21 +00002170int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002171{
Paul Bakker23986e52011-04-24 08:57:21 +00002172 int ret;
2173 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002174 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002175#if defined(POLARSSL_PEM_C)
2176 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002177
Paul Bakker96743fc2011-02-12 14:30:57 +00002178 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002179
Paul Bakker96743fc2011-02-12 14:30:57 +00002180 ret = pem_read_buffer( &pem,
2181 "-----BEGIN DH PARAMETERS-----",
2182 "-----END DH PARAMETERS-----",
2183 dhmin, NULL, 0, &dhminlen );
2184
2185 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002186 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002187 /*
2188 * Was PEM encoded
2189 */
2190 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002191 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002192 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002193 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002194 pem_free( &pem );
2195 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002196 }
2197
Paul Bakker96743fc2011-02-12 14:30:57 +00002198 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2199#else
2200 p = (unsigned char *) dhmin;
2201#endif
2202 end = p + dhminlen;
2203
Paul Bakker1b57b062011-01-06 15:48:19 +00002204 memset( dhm, 0, sizeof( dhm_context ) );
2205
Paul Bakker1b57b062011-01-06 15:48:19 +00002206 /*
2207 * DHParams ::= SEQUENCE {
2208 * prime INTEGER, -- P
2209 * generator INTEGER, -- g
2210 * }
2211 */
2212 if( ( ret = asn1_get_tag( &p, end, &len,
2213 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2214 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002215#if defined(POLARSSL_PEM_C)
2216 pem_free( &pem );
2217#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002218 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002219 }
2220
2221 end = p + len;
2222
2223 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2224 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2225 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002226#if defined(POLARSSL_PEM_C)
2227 pem_free( &pem );
2228#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002229 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002230 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002231 }
2232
2233 if( p != end )
2234 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002235#if defined(POLARSSL_PEM_C)
2236 pem_free( &pem );
2237#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002238 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002239 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002240 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2241 }
2242
Paul Bakker96743fc2011-02-12 14:30:57 +00002243#if defined(POLARSSL_PEM_C)
2244 pem_free( &pem );
2245#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002246
2247 return( 0 );
2248}
2249
Paul Bakker335db3f2011-04-25 15:28:35 +00002250#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002251/*
2252 * Load and parse a private RSA key
2253 */
2254int x509parse_dhmfile( dhm_context *dhm, const char *path )
2255{
2256 int ret;
2257 size_t n;
2258 unsigned char *buf;
2259
Paul Bakker732e1a82011-12-11 16:35:09 +00002260 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2261 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002262
Paul Bakker27fdf462011-06-09 13:55:13 +00002263 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002264
2265 memset( buf, 0, n + 1 );
2266 free( buf );
2267
2268 return( ret );
2269}
Paul Bakker335db3f2011-04-25 15:28:35 +00002270#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002271#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002272
Paul Bakker5121ce52009-01-03 21:22:43 +00002273#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002274#include <stdarg.h>
2275
2276#if !defined vsnprintf
2277#define vsnprintf _vsnprintf
2278#endif // vsnprintf
2279
2280/*
2281 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2282 * Result value is not size of buffer needed, but -1 if no fit is possible.
2283 *
2284 * This fuction tries to 'fix' this by at least suggesting enlarging the
2285 * size by 20.
2286 */
2287int compat_snprintf(char *str, size_t size, const char *format, ...)
2288{
2289 va_list ap;
2290 int res = -1;
2291
2292 va_start( ap, format );
2293
2294 res = vsnprintf( str, size, format, ap );
2295
2296 va_end( ap );
2297
2298 // No quick fix possible
2299 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002300 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002301
2302 return res;
2303}
2304
2305#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002306#endif
2307
Paul Bakkerd98030e2009-05-02 15:13:40 +00002308#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2309
2310#define SAFE_SNPRINTF() \
2311{ \
2312 if( ret == -1 ) \
2313 return( -1 ); \
2314 \
Paul Bakker23986e52011-04-24 08:57:21 +00002315 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002316 p[n - 1] = '\0'; \
2317 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2318 } \
2319 \
Paul Bakker23986e52011-04-24 08:57:21 +00002320 n -= (unsigned int) ret; \
2321 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002322}
2323
Paul Bakker5121ce52009-01-03 21:22:43 +00002324/*
2325 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002326 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002328int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002329{
Paul Bakker23986e52011-04-24 08:57:21 +00002330 int ret;
2331 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002333 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 char s[128], *p;
2335
2336 memset( s, 0, sizeof( s ) );
2337
2338 name = dn;
2339 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002340 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002341
2342 while( name != NULL )
2343 {
Paul Bakker74111d32011-01-15 16:57:55 +00002344 if( name != dn )
2345 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002346 ret = snprintf( p, n, ", " );
2347 SAFE_SNPRINTF();
2348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
2350 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2351 {
2352 switch( name->oid.p[2] )
2353 {
2354 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002355 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
2357 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002358 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
2360 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002361 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002362
2363 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002364 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
2366 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002367 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
2369 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002370 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
2372 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002373 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 name->oid.p[2] );
2375 break;
2376 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002377 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 }
2379 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2380 {
2381 switch( name->oid.p[8] )
2382 {
2383 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002384 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
2386 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002387 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 name->oid.p[8] );
2389 break;
2390 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002391 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
2393 else
Paul Bakker74111d32011-01-15 16:57:55 +00002394 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002395 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002396 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
2399 for( i = 0; i < name->val.len; i++ )
2400 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002401 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 break;
2403
2404 c = name->val.p[i];
2405 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2406 s[i] = '?';
2407 else s[i] = c;
2408 }
2409 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002410 ret = snprintf( p, n, "%s", s );
2411 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 name = name->next;
2413 }
2414
Paul Bakker23986e52011-04-24 08:57:21 +00002415 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002416}
2417
2418/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002419 * Store the serial in printable form into buf; no more
2420 * than size characters will be written
2421 */
2422int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2423{
Paul Bakker23986e52011-04-24 08:57:21 +00002424 int ret;
2425 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002426 char *p;
2427
2428 p = buf;
2429 n = size;
2430
2431 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002432 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002433
2434 for( i = 0; i < nr; i++ )
2435 {
Paul Bakker93048802011-12-05 14:38:06 +00002436 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002437 continue;
2438
Paul Bakkerdd476992011-01-16 21:34:59 +00002439 ret = snprintf( p, n, "%02X%s",
2440 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2441 SAFE_SNPRINTF();
2442 }
2443
Paul Bakker03c7c252011-11-25 12:37:37 +00002444 if( nr != serial->len )
2445 {
2446 ret = snprintf( p, n, "...." );
2447 SAFE_SNPRINTF();
2448 }
2449
Paul Bakker23986e52011-04-24 08:57:21 +00002450 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002451}
2452
2453/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002454 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002456int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2457 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002458{
Paul Bakker23986e52011-04-24 08:57:21 +00002459 int ret;
2460 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002461 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
2463 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002464 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Paul Bakkerd98030e2009-05-02 15:13:40 +00002466 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002468 SAFE_SNPRINTF();
2469 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002471 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
Paul Bakkerdd476992011-01-16 21:34:59 +00002473 ret = x509parse_serial_gets( p, n, &crt->serial);
2474 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002475
Paul Bakkerd98030e2009-05-02 15:13:40 +00002476 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2477 SAFE_SNPRINTF();
2478 ret = x509parse_dn_gets( p, n, &crt->issuer );
2479 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Paul Bakkerd98030e2009-05-02 15:13:40 +00002481 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2482 SAFE_SNPRINTF();
2483 ret = x509parse_dn_gets( p, n, &crt->subject );
2484 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Paul Bakkerd98030e2009-05-02 15:13:40 +00002486 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2488 crt->valid_from.year, crt->valid_from.mon,
2489 crt->valid_from.day, crt->valid_from.hour,
2490 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002491 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Paul Bakkerd98030e2009-05-02 15:13:40 +00002493 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2495 crt->valid_to.year, crt->valid_to.mon,
2496 crt->valid_to.day, crt->valid_to.hour,
2497 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002498 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Paul Bakkerd98030e2009-05-02 15:13:40 +00002500 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2501 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Paul Bakker27d66162010-03-17 06:56:01 +00002503 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002505 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2506 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2507 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2508 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2509 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2510 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2511 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2512 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2513 default: ret = snprintf( p, n, "???" ); break;
2514 }
2515 SAFE_SNPRINTF();
2516
2517 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002518 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002519 SAFE_SNPRINTF();
2520
Paul Bakker23986e52011-04-24 08:57:21 +00002521 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002522}
2523
Paul Bakker74111d32011-01-15 16:57:55 +00002524/* Compare a given OID string with an OID x509_buf * */
2525#define OID_CMP(oid_str, oid_buf) \
2526 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2527 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2528
2529/*
2530 * Return an informational string describing the given OID
2531 */
2532const char *x509_oid_get_description( x509_buf *oid )
2533{
2534 if ( oid == NULL )
2535 return ( NULL );
2536
2537 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2538 return( STRING_SERVER_AUTH );
2539
2540 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2541 return( STRING_CLIENT_AUTH );
2542
2543 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2544 return( STRING_CODE_SIGNING );
2545
2546 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2547 return( STRING_EMAIL_PROTECTION );
2548
2549 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2550 return( STRING_TIME_STAMPING );
2551
2552 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2553 return( STRING_OCSP_SIGNING );
2554
2555 return( NULL );
2556}
2557
2558/* Return the x.y.z.... style numeric string for the given OID */
2559int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2560{
Paul Bakker23986e52011-04-24 08:57:21 +00002561 int ret;
2562 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002563 unsigned int value;
2564 char *p;
2565
2566 p = buf;
2567 n = size;
2568
2569 /* First byte contains first two dots */
2570 if( oid->len > 0 )
2571 {
2572 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2573 SAFE_SNPRINTF();
2574 }
2575
2576 /* TODO: value can overflow in value. */
2577 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002578 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002579 {
2580 value <<= 7;
2581 value += oid->p[i] & 0x7F;
2582
2583 if( !( oid->p[i] & 0x80 ) )
2584 {
2585 /* Last byte */
2586 ret = snprintf( p, n, ".%d", value );
2587 SAFE_SNPRINTF();
2588 value = 0;
2589 }
2590 }
2591
Paul Bakker23986e52011-04-24 08:57:21 +00002592 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002593}
2594
Paul Bakkerd98030e2009-05-02 15:13:40 +00002595/*
2596 * Return an informational string about the CRL.
2597 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002598int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2599 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002600{
Paul Bakker23986e52011-04-24 08:57:21 +00002601 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002602 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002603 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002604 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002605
2606 p = buf;
2607 n = size;
2608
2609 ret = snprintf( p, n, "%sCRL version : %d",
2610 prefix, crl->version );
2611 SAFE_SNPRINTF();
2612
2613 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2614 SAFE_SNPRINTF();
2615 ret = x509parse_dn_gets( p, n, &crl->issuer );
2616 SAFE_SNPRINTF();
2617
2618 ret = snprintf( p, n, "\n%sthis update : " \
2619 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2620 crl->this_update.year, crl->this_update.mon,
2621 crl->this_update.day, crl->this_update.hour,
2622 crl->this_update.min, crl->this_update.sec );
2623 SAFE_SNPRINTF();
2624
2625 ret = snprintf( p, n, "\n%snext update : " \
2626 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2627 crl->next_update.year, crl->next_update.mon,
2628 crl->next_update.day, crl->next_update.hour,
2629 crl->next_update.min, crl->next_update.sec );
2630 SAFE_SNPRINTF();
2631
2632 entry = &crl->entry;
2633
2634 ret = snprintf( p, n, "\n%sRevoked certificates:",
2635 prefix );
2636 SAFE_SNPRINTF();
2637
Paul Bakker9be19372009-07-27 20:21:53 +00002638 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002639 {
2640 ret = snprintf( p, n, "\n%sserial number: ",
2641 prefix );
2642 SAFE_SNPRINTF();
2643
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002644 ret = x509parse_serial_gets( p, n, &entry->serial);
2645 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002646
Paul Bakkerd98030e2009-05-02 15:13:40 +00002647 ret = snprintf( p, n, " revocation date: " \
2648 "%04d-%02d-%02d %02d:%02d:%02d",
2649 entry->revocation_date.year, entry->revocation_date.mon,
2650 entry->revocation_date.day, entry->revocation_date.hour,
2651 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002652 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002653
2654 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
Paul Bakkerd98030e2009-05-02 15:13:40 +00002657 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2658 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Paul Bakker27d66162010-03-17 06:56:01 +00002660 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002661 {
2662 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2663 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2664 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2665 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2666 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2667 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2668 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2669 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2670 default: ret = snprintf( p, n, "???" ); break;
2671 }
2672 SAFE_SNPRINTF();
2673
Paul Bakker1e27bb22009-07-19 20:25:25 +00002674 ret = snprintf( p, n, "\n" );
2675 SAFE_SNPRINTF();
2676
Paul Bakker23986e52011-04-24 08:57:21 +00002677 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678}
2679
2680/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002681 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002683int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002684{
Paul Bakkercce9d772011-11-18 14:26:47 +00002685 int year, mon, day;
2686 int hour, min, sec;
2687
2688#if defined(_WIN32)
2689 SYSTEMTIME st;
2690
2691 GetLocalTime(&st);
2692
2693 year = st.wYear;
2694 mon = st.wMonth;
2695 day = st.wDay;
2696 hour = st.wHour;
2697 min = st.wMinute;
2698 sec = st.wSecond;
2699#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 struct tm *lt;
2701 time_t tt;
2702
2703 tt = time( NULL );
2704 lt = localtime( &tt );
2705
Paul Bakkercce9d772011-11-18 14:26:47 +00002706 year = lt->tm_year + 1900;
2707 mon = lt->tm_mon + 1;
2708 day = lt->tm_mday;
2709 hour = lt->tm_hour;
2710 min = lt->tm_min;
2711 sec = lt->tm_sec;
2712#endif
2713
2714 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002715 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Paul Bakkercce9d772011-11-18 14:26:47 +00002717 if( year == to->year &&
2718 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002719 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Paul Bakkercce9d772011-11-18 14:26:47 +00002721 if( year == to->year &&
2722 mon == to->mon &&
2723 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002724 return( 1 );
2725
Paul Bakkercce9d772011-11-18 14:26:47 +00002726 if( year == to->year &&
2727 mon == to->mon &&
2728 day == to->day &&
2729 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002730 return( 1 );
2731
Paul Bakkercce9d772011-11-18 14:26:47 +00002732 if( year == to->year &&
2733 mon == to->mon &&
2734 day == to->day &&
2735 hour == to->hour &&
2736 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002737 return( 1 );
2738
Paul Bakkercce9d772011-11-18 14:26:47 +00002739 if( year == to->year &&
2740 mon == to->mon &&
2741 day == to->day &&
2742 hour == to->hour &&
2743 min == to->min &&
2744 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002745 return( 1 );
2746
Paul Bakker40ea7de2009-05-03 10:18:48 +00002747 return( 0 );
2748}
2749
2750/*
2751 * Return 1 if the certificate is revoked, or 0 otherwise.
2752 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002753int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002754{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002755 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002756
2757 while( cur != NULL && cur->serial.len != 0 )
2758 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002759 if( crt->serial.len == cur->serial.len &&
2760 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002761 {
2762 if( x509parse_time_expired( &cur->revocation_date ) )
2763 return( 1 );
2764 }
2765
2766 cur = cur->next;
2767 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002768
2769 return( 0 );
2770}
2771
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002772/*
2773 * Wrapper for x509 hashes.
2774 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002775 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002776 */
Paul Bakker23986e52011-04-24 08:57:21 +00002777static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 unsigned char *out )
2779{
2780 switch( alg )
2781 {
Paul Bakker40e46942009-01-03 21:51:57 +00002782#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002783 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002785#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002786 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002787#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002788#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002789 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002790#endif
2791#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002792 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002793#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002794#if defined(POLARSSL_SHA2_C)
2795 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2796 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2797#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002798#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002799 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2800 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2801#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002803 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002804 break;
2805 }
2806}
2807
2808/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002809 * Check that the given certificate is valid accoring to the CRL.
2810 */
2811static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2812 x509_crl *crl_list)
2813{
2814 int flags = 0;
2815 int hash_id;
2816 unsigned char hash[64];
2817
2818 /*
2819 * TODO: What happens if no CRL is present?
2820 * Suggestion: Revocation state should be unknown if no CRL is present.
2821 * For backwards compatibility this is not yet implemented.
2822 */
2823
2824 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2825 {
2826 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2827 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2828 crl_list->issuer_raw.len ) != 0 )
2829 {
2830 crl_list = crl_list->next;
2831 continue;
2832 }
2833
2834 /*
2835 * Check if CRL is correctly signed by the trusted CA
2836 */
2837 hash_id = crl_list->sig_alg;
2838
2839 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2840
2841 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2842 0, hash, crl_list->sig.p ) == 0 )
2843 {
2844 /*
2845 * CRL is not trusted
2846 */
2847 flags |= BADCRL_NOT_TRUSTED;
2848 break;
2849 }
2850
2851 /*
2852 * Check for validity of CRL (Do not drop out)
2853 */
2854 if( x509parse_time_expired( &crl_list->next_update ) )
2855 flags |= BADCRL_EXPIRED;
2856
2857 /*
2858 * Check if certificate is revoked
2859 */
2860 if( x509parse_revoked(crt, crl_list) )
2861 {
2862 flags |= BADCERT_REVOKED;
2863 break;
2864 }
2865
2866 crl_list = crl_list->next;
2867 }
2868 return flags;
2869}
2870
2871/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 * Verify the certificate validity
2873 */
2874int x509parse_verify( x509_cert *crt,
2875 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002876 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002877 const char *cn, int *flags,
2878 int (*f_vrfy)(void *, x509_cert *, int, int),
2879 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002880{
Paul Bakker23986e52011-04-24 08:57:21 +00002881 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 int hash_id;
2883 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002884 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002886 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Paul Bakker40ea7de2009-05-03 10:18:48 +00002888 *flags = 0;
2889
2890 if( x509parse_time_expired( &crt->valid_to ) )
2891 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002892
2893 if( cn != NULL )
2894 {
2895 name = &crt->subject;
2896 cn_len = strlen( cn );
2897
2898 while( name != NULL )
2899 {
2900 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2901 memcmp( name->val.p, cn, cn_len ) == 0 &&
2902 name->val.len == cn_len )
2903 break;
2904
2905 name = name->next;
2906 }
2907
2908 if( name == NULL )
2909 *flags |= BADCERT_CN_MISMATCH;
2910 }
2911
Paul Bakker5121ce52009-01-03 21:22:43 +00002912 /*
2913 * Iterate upwards in the given cert chain,
2914 * ignoring any upper cert with CA != TRUE.
2915 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002916 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
2918 pathlen = 1;
2919
Paul Bakker76fd75a2011-01-16 21:12:10 +00002920 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002921 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002922 if( parent->ca_istrue == 0 ||
2923 crt->issuer_raw.len != parent->subject_raw.len ||
2924 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 crt->issuer_raw.len ) != 0 )
2926 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002927 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 continue;
2929 }
2930
Paul Bakker27d66162010-03-17 06:56:01 +00002931 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
2933 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2934
Paul Bakker76fd75a2011-01-16 21:12:10 +00002935 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2936 crt->sig.p ) != 0 )
2937 *flags |= BADCERT_NOT_TRUSTED;
2938
2939 /* Check trusted CA's CRL for the given crt */
2940 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002941
2942 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002943 if( NULL != f_vrfy )
2944 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002945 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002946 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002947 else
2948 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002949 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002950 else if( *flags != 0 )
2951 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002952
2953 pathlen++;
2954
Paul Bakker76fd75a2011-01-16 21:12:10 +00002955 crt = parent;
2956 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 }
2958
2959 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002960 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002962 *flags |= BADCERT_NOT_TRUSTED;
2963
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002964 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002965 {
2966 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2967 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2968 crt->issuer_raw.len ) != 0 )
2969 {
2970 trust_ca = trust_ca->next;
2971 continue;
2972 }
2973
2974 if( trust_ca->max_pathlen > 0 &&
2975 trust_ca->max_pathlen < pathlen )
2976 break;
2977
Paul Bakker27d66162010-03-17 06:56:01 +00002978 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
2980 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2981
2982 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2983 0, hash, crt->sig.p ) == 0 )
2984 {
2985 /*
2986 * cert. is signed by a trusted CA
2987 */
2988 *flags &= ~BADCERT_NOT_TRUSTED;
2989 break;
2990 }
2991
2992 trust_ca = trust_ca->next;
2993 }
2994
Paul Bakker76fd75a2011-01-16 21:12:10 +00002995 /* Check trusted CA's CRL for the given crt */
2996 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002997
2998 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00002999 if( NULL != f_vrfy )
3000 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003001 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003002 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003003 else
3004 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003005 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003006 else if( *flags != 0 )
3007 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003008
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 return( 0 );
3010}
3011
3012/*
3013 * Unallocate all certificate data
3014 */
3015void x509_free( x509_cert *crt )
3016{
3017 x509_cert *cert_cur = crt;
3018 x509_cert *cert_prv;
3019 x509_name *name_cur;
3020 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003021 x509_sequence *seq_cur;
3022 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
3024 if( crt == NULL )
3025 return;
3026
3027 do
3028 {
3029 rsa_free( &cert_cur->rsa );
3030
3031 name_cur = cert_cur->issuer.next;
3032 while( name_cur != NULL )
3033 {
3034 name_prv = name_cur;
3035 name_cur = name_cur->next;
3036 memset( name_prv, 0, sizeof( x509_name ) );
3037 free( name_prv );
3038 }
3039
3040 name_cur = cert_cur->subject.next;
3041 while( name_cur != NULL )
3042 {
3043 name_prv = name_cur;
3044 name_cur = name_cur->next;
3045 memset( name_prv, 0, sizeof( x509_name ) );
3046 free( name_prv );
3047 }
3048
Paul Bakker74111d32011-01-15 16:57:55 +00003049 seq_cur = cert_cur->ext_key_usage.next;
3050 while( seq_cur != NULL )
3051 {
3052 seq_prv = seq_cur;
3053 seq_cur = seq_cur->next;
3054 memset( seq_prv, 0, sizeof( x509_sequence ) );
3055 free( seq_prv );
3056 }
3057
Paul Bakker5121ce52009-01-03 21:22:43 +00003058 if( cert_cur->raw.p != NULL )
3059 {
3060 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3061 free( cert_cur->raw.p );
3062 }
3063
3064 cert_cur = cert_cur->next;
3065 }
3066 while( cert_cur != NULL );
3067
3068 cert_cur = crt;
3069 do
3070 {
3071 cert_prv = cert_cur;
3072 cert_cur = cert_cur->next;
3073
3074 memset( cert_prv, 0, sizeof( x509_cert ) );
3075 if( cert_prv != crt )
3076 free( cert_prv );
3077 }
3078 while( cert_cur != NULL );
3079}
3080
Paul Bakkerd98030e2009-05-02 15:13:40 +00003081/*
3082 * Unallocate all CRL data
3083 */
3084void x509_crl_free( x509_crl *crl )
3085{
3086 x509_crl *crl_cur = crl;
3087 x509_crl *crl_prv;
3088 x509_name *name_cur;
3089 x509_name *name_prv;
3090 x509_crl_entry *entry_cur;
3091 x509_crl_entry *entry_prv;
3092
3093 if( crl == NULL )
3094 return;
3095
3096 do
3097 {
3098 name_cur = crl_cur->issuer.next;
3099 while( name_cur != NULL )
3100 {
3101 name_prv = name_cur;
3102 name_cur = name_cur->next;
3103 memset( name_prv, 0, sizeof( x509_name ) );
3104 free( name_prv );
3105 }
3106
3107 entry_cur = crl_cur->entry.next;
3108 while( entry_cur != NULL )
3109 {
3110 entry_prv = entry_cur;
3111 entry_cur = entry_cur->next;
3112 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3113 free( entry_prv );
3114 }
3115
3116 if( crl_cur->raw.p != NULL )
3117 {
3118 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3119 free( crl_cur->raw.p );
3120 }
3121
3122 crl_cur = crl_cur->next;
3123 }
3124 while( crl_cur != NULL );
3125
3126 crl_cur = crl;
3127 do
3128 {
3129 crl_prv = crl_cur;
3130 crl_cur = crl_cur->next;
3131
3132 memset( crl_prv, 0, sizeof( x509_crl ) );
3133 if( crl_prv != crl )
3134 free( crl_prv );
3135 }
3136 while( crl_cur != NULL );
3137}
3138
Paul Bakker40e46942009-01-03 21:51:57 +00003139#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Paul Bakker40e46942009-01-03 21:51:57 +00003141#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
3143/*
3144 * Checkup routine
3145 */
3146int x509_self_test( int verbose )
3147{
Paul Bakker5690efc2011-05-26 13:16:06 +00003148#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003149 int ret;
3150 int flags;
3151 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003152 x509_cert cacert;
3153 x509_cert clicert;
3154 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003155#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003156 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003157#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
3159 if( verbose != 0 )
3160 printf( " X.509 certificate load: " );
3161
3162 memset( &clicert, 0, sizeof( x509_cert ) );
3163
3164 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003165 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003166 if( ret != 0 )
3167 {
3168 if( verbose != 0 )
3169 printf( "failed\n" );
3170
3171 return( ret );
3172 }
3173
3174 memset( &cacert, 0, sizeof( x509_cert ) );
3175
3176 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003177 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 if( ret != 0 )
3179 {
3180 if( verbose != 0 )
3181 printf( "failed\n" );
3182
3183 return( ret );
3184 }
3185
3186 if( verbose != 0 )
3187 printf( "passed\n X.509 private key load: " );
3188
3189 i = strlen( test_ca_key );
3190 j = strlen( test_ca_pwd );
3191
Paul Bakker66b78b22011-03-25 14:22:50 +00003192 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3193
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 if( ( ret = x509parse_key( &rsa,
3195 (unsigned char *) test_ca_key, i,
3196 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3197 {
3198 if( verbose != 0 )
3199 printf( "failed\n" );
3200
3201 return( ret );
3202 }
3203
3204 if( verbose != 0 )
3205 printf( "passed\n X.509 signature verify: ");
3206
Paul Bakker23986e52011-04-24 08:57:21 +00003207 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 if( ret != 0 )
3209 {
Paul Bakker23986e52011-04-24 08:57:21 +00003210 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 if( verbose != 0 )
3212 printf( "failed\n" );
3213
3214 return( ret );
3215 }
3216
Paul Bakker5690efc2011-05-26 13:16:06 +00003217#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003219 printf( "passed\n X.509 DHM parameter load: " );
3220
3221 i = strlen( test_dhm_params );
3222 j = strlen( test_ca_pwd );
3223
3224 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3225 {
3226 if( verbose != 0 )
3227 printf( "failed\n" );
3228
3229 return( ret );
3230 }
3231
3232 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003234#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
3236 x509_free( &cacert );
3237 x509_free( &clicert );
3238 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003239#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003240 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003242
3243 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003244#else
3245 ((void) verbose);
3246 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3247#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003248}
3249
3250#endif
3251
3252#endif