blob: 883ea251e2c6cd51f4d7094030082394706644fc [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/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
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>
Paul Bakkere4791f32012-06-04 21:29:15 +000063#if defined(_WIN32)
64#include <strsafe.h>
65#else
Paul Bakker8d914582012-06-04 12:46:42 +000066#include <sys/types.h>
67#include <dirent.h>
68#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000069#endif
70
Paul Bakker5121ce52009-01-03 21:22:43 +000071/*
Paul Bakker5121ce52009-01-03 21:22:43 +000072 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
73 */
74static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000075 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000076 int *ver )
77{
Paul Bakker23986e52011-04-24 08:57:21 +000078 int ret;
79 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 if( ( ret = asn1_get_tag( p, end, &len,
82 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
83 {
Paul Bakker40e46942009-01-03 21:51:57 +000084 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000085 {
86 *ver = 0;
87 return( 0 );
88 }
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90 return( ret );
91 }
92
93 end = *p + len;
94
95 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000096 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +000099 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000100 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102 return( 0 );
103}
104
105/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000106 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000107 */
108static int x509_crl_get_version( unsigned char **p,
109 const unsigned char *end,
110 int *ver )
111{
112 int ret;
113
114 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
115 {
116 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000117 {
118 *ver = 0;
119 return( 0 );
120 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000121
122 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
123 }
124
125 return( 0 );
126}
127
128/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 * CertificateSerialNumber ::= INTEGER
130 */
131static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000132 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 x509_buf *serial )
134{
135 int ret;
136
137 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000138 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000139 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
142 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000143 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000144 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 serial->tag = *(*p)++;
147
148 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 serial->p = *p;
152 *p += serial->len;
153
154 return( 0 );
155}
156
157/*
158 * AlgorithmIdentifier ::= SEQUENCE {
159 * algorithm OBJECT IDENTIFIER,
160 * parameters ANY DEFINED BY algorithm OPTIONAL }
161 */
162static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000163 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 x509_buf *alg )
165{
Paul Bakker23986e52011-04-24 08:57:21 +0000166 int ret;
167 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 if( ( ret = asn1_get_tag( p, end, &len,
170 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 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 end = *p + len;
174 alg->tag = **p;
175
176 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000177 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 alg->p = *p;
180 *p += alg->len;
181
182 if( *p == end )
183 return( 0 );
184
185 /*
186 * assume the algorithm parameters must be NULL
187 */
188 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000189 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000192 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000193 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 return( 0 );
196}
197
198/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * AttributeTypeAndValue ::= SEQUENCE {
200 * type AttributeType,
201 * value AttributeValue }
202 *
203 * AttributeType ::= OBJECT IDENTIFIER
204 *
205 * AttributeValue ::= ANY DEFINED BY AttributeType
206 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000207static int x509_get_attr_type_value( unsigned char **p,
208 const unsigned char *end,
209 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210{
Paul Bakker23986e52011-04-24 08:57:21 +0000211 int ret;
212 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 x509_buf *oid;
214 x509_buf *val;
215
216 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 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
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 oid = &cur->oid;
221 oid->tag = **p;
222
223 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
226 oid->p = *p;
227 *p += oid->len;
228
229 if( ( end - *p ) < 1 )
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_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
234 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
235 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000236 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000237 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 val = &cur->val;
240 val->tag = *(*p)++;
241
242 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000243 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245 val->p = *p;
246 *p += val->len;
247
248 cur->next = NULL;
249
Paul Bakker400ff6f2011-02-20 10:40:16 +0000250 return( 0 );
251}
252
253/*
254 * RelativeDistinguishedName ::=
255 * SET OF AttributeTypeAndValue
256 *
257 * AttributeTypeAndValue ::= SEQUENCE {
258 * type AttributeType,
259 * value AttributeValue }
260 *
261 * AttributeType ::= OBJECT IDENTIFIER
262 *
263 * AttributeValue ::= ANY DEFINED BY AttributeType
264 */
265static int x509_get_name( unsigned char **p,
266 const unsigned char *end,
267 x509_name *cur )
268{
Paul Bakker23986e52011-04-24 08:57:21 +0000269 int ret;
270 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000271 const unsigned char *end2;
272 x509_name *use;
273
274 if( ( ret = asn1_get_tag( p, end, &len,
275 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000276 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277
278 end2 = end;
279 end = *p + len;
280 use = cur;
281
282 do
283 {
284 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
285 return( ret );
286
287 if( *p != end )
288 {
289 use->next = (x509_name *) malloc(
290 sizeof( x509_name ) );
291
292 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000293 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000294
295 memset( use->next, 0, sizeof( x509_name ) );
296
297 use = use->next;
298 }
299 }
300 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302 /*
303 * recurse until end of SEQUENCE is reached
304 */
305 if( *p == end2 )
306 return( 0 );
307
308 cur->next = (x509_name *) malloc(
309 sizeof( x509_name ) );
310
311 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000312 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
Paul Bakker430ffbe2012-05-01 08:14:20 +0000314 memset( cur->next, 0, sizeof( x509_name ) );
315
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 return( x509_get_name( p, end2, cur->next ) );
317}
318
319/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 * Time ::= CHOICE {
321 * utcTime UTCTime,
322 * generalTime GeneralizedTime }
323 */
Paul Bakker91200182010-02-18 21:26:15 +0000324static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000325 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000326 x509_time *time )
327{
Paul Bakker23986e52011-04-24 08:57:21 +0000328 int ret;
329 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000331 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332
Paul Bakker91200182010-02-18 21:26:15 +0000333 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000334 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
335 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336
Paul Bakker91200182010-02-18 21:26:15 +0000337 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 if ( tag == ASN1_UTC_TIME )
340 {
341 (*p)++;
342 ret = asn1_get_len( p, end, &len );
343
344 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000345 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000346
Paul Bakker91200182010-02-18 21:26:15 +0000347 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000348 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
349 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000350
Paul Bakker91200182010-02-18 21:26:15 +0000351 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
352 &time->year, &time->mon, &time->day,
353 &time->hour, &time->min, &time->sec ) < 5 )
354 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000355
Paul Bakker400ff6f2011-02-20 10:40:16 +0000356 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000357 time->year += 1900;
358
359 *p += len;
360
361 return( 0 );
362 }
363 else if ( tag == ASN1_GENERALIZED_TIME )
364 {
365 (*p)++;
366 ret = asn1_get_len( p, end, &len );
367
368 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000369 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000370
371 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000372 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
373 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000374
375 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
376 &time->year, &time->mon, &time->day,
377 &time->hour, &time->min, &time->sec ) < 5 )
378 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
379
380 *p += len;
381
382 return( 0 );
383 }
384 else
Paul Bakker9d781402011-05-09 16:17:09 +0000385 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000386}
387
388
389/*
390 * Validity ::= SEQUENCE {
391 * notBefore Time,
392 * notAfter Time }
393 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000394static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000395 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 x509_time *from,
397 x509_time *to )
398{
Paul Bakker23986e52011-04-24 08:57:21 +0000399 int ret;
400 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402 if( ( ret = asn1_get_tag( p, end, &len,
403 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000404 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 end = *p + len;
407
Paul Bakker91200182010-02-18 21:26:15 +0000408 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000409 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
Paul Bakker91200182010-02-18 21:26:15 +0000411 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000415 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000416 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418 return( 0 );
419}
420
421/*
422 * SubjectPublicKeyInfo ::= SEQUENCE {
423 * algorithm AlgorithmIdentifier,
424 * subjectPublicKey BIT STRING }
425 */
426static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000427 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 x509_buf *pk_alg_oid,
429 mpi *N, mpi *E )
430{
Paul Bakker23986e52011-04-24 08:57:21 +0000431 int ret, can_handle;
432 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 unsigned char *end2;
434
435 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
436 return( ret );
437
438 /*
439 * only RSA public keys handled at this time
440 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000441 can_handle = 0;
442
443 if( pk_alg_oid->len == 9 &&
444 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
445 can_handle = 1;
446
447 if( pk_alg_oid->len == 9 &&
448 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
449 {
450 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
451 can_handle = 1;
452
453 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
454 can_handle = 1;
455 }
456
457 if( pk_alg_oid->len == 5 &&
458 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
459 can_handle = 1;
460
461 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000462 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000465 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000468 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000469 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 end2 = *p + len;
472
473 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000474 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 /*
477 * RSAPublicKey ::= SEQUENCE {
478 * modulus INTEGER, -- n
479 * publicExponent INTEGER -- e
480 * }
481 */
482 if( ( ret = asn1_get_tag( p, end2, &len,
483 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
491 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000492 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000495 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000496 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 return( 0 );
499}
500
501static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000502 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 x509_buf *sig )
504{
Paul Bakker23986e52011-04-24 08:57:21 +0000505 int ret;
506 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakker8afa70d2012-02-11 18:42:45 +0000508 if( ( end - *p ) < 1 )
509 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
510 POLARSSL_ERR_ASN1_OUT_OF_DATA );
511
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 sig->tag = **p;
513
514 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000515 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker74111d32011-01-15 16:57:55 +0000517
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000519 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521 sig->len = len;
522 sig->p = *p;
523
524 *p += len;
525
526 return( 0 );
527}
528
529/*
530 * X.509 v2/v3 unique identifier (not parsed)
531 */
532static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000533 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 x509_buf *uid, int n )
535{
536 int ret;
537
538 if( *p == end )
539 return( 0 );
540
541 uid->tag = **p;
542
543 if( ( ret = asn1_get_tag( p, end, &uid->len,
544 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
545 {
Paul Bakker40e46942009-01-03 21:51:57 +0000546 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 return( 0 );
548
549 return( ret );
550 }
551
552 uid->p = *p;
553 *p += uid->len;
554
555 return( 0 );
556}
557
558/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000559 * X.509 Extensions (No parsing of extensions, pointer should
560 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
562static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000563 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000564 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565{
Paul Bakker23986e52011-04-24 08:57:21 +0000566 int ret;
567 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 if( *p == end )
570 return( 0 );
571
572 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000573
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000575 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 ext->p = *p;
579 end = *p + ext->len;
580
581 /*
582 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
583 *
584 * Extension ::= SEQUENCE {
585 * extnID OBJECT IDENTIFIER,
586 * critical BOOLEAN DEFAULT FALSE,
587 * extnValue OCTET STRING }
588 */
589 if( ( ret = asn1_get_tag( p, end, &len,
590 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000591 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000594 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000595 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Paul Bakkerd98030e2009-05-02 15:13:40 +0000597 return( 0 );
598}
599
600/*
601 * X.509 CRL v2 extensions (no extensions parsed yet.)
602 */
603static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000604 const unsigned char *end,
605 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000606{
Paul Bakker23986e52011-04-24 08:57:21 +0000607 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000608 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000609
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000610 /* Get explicit tag */
611 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000612 {
613 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
614 return( 0 );
615
616 return( ret );
617 }
618
619 while( *p < end )
620 {
621 if( ( ret = asn1_get_tag( p, end, &len,
622 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000623 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000624
625 *p += len;
626 }
627
628 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000629 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000630 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
631
632 return( 0 );
633}
634
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000635/*
636 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
637 */
638static int x509_get_crl_entry_ext( unsigned char **p,
639 const unsigned char *end,
640 x509_buf *ext )
641{
642 int ret;
643 size_t len = 0;
644
645 /* OPTIONAL */
646 if (end <= *p)
647 return( 0 );
648
649 ext->tag = **p;
650 ext->p = *p;
651
652 /*
653 * Get CRL-entry extension sequence header
654 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
655 */
656 if( ( ret = asn1_get_tag( p, end, &ext->len,
657 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
658 {
659 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
660 {
661 ext->p = NULL;
662 return( 0 );
663 }
664 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
665 }
666
667 end = *p + ext->len;
668
669 if( end != *p + ext->len )
670 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
671 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
672
673 while( *p < end )
674 {
675 if( ( ret = asn1_get_tag( p, end, &len,
676 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
677 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
678
679 *p += len;
680 }
681
682 if( *p != end )
683 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
684 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
685
686 return( 0 );
687}
688
Paul Bakker74111d32011-01-15 16:57:55 +0000689static int x509_get_basic_constraints( unsigned char **p,
690 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000691 int *ca_istrue,
692 int *max_pathlen )
693{
Paul Bakker23986e52011-04-24 08:57:21 +0000694 int ret;
695 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000696
697 /*
698 * BasicConstraints ::= SEQUENCE {
699 * cA BOOLEAN DEFAULT FALSE,
700 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
701 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000702 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000703 *max_pathlen = 0; /* endless */
704
705 if( ( ret = asn1_get_tag( p, end, &len,
706 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000707 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000708
709 if( *p == end )
710 return 0;
711
Paul Bakker3cccddb2011-01-16 21:46:31 +0000712 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000713 {
714 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000715 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000716
717 if( ret != 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
Paul Bakker3cccddb2011-01-16 21:46:31 +0000720 if( *ca_istrue != 0 )
721 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000722 }
723
724 if( *p == end )
725 return 0;
726
727 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000728 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000729
730 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000731 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000732 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
733
734 (*max_pathlen)++;
735
Paul Bakker74111d32011-01-15 16:57:55 +0000736 return 0;
737}
738
739static int x509_get_ns_cert_type( unsigned char **p,
740 const unsigned char *end,
741 unsigned char *ns_cert_type)
742{
743 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000744 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000745
746 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000747 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000748
749 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000750 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000751 POLARSSL_ERR_ASN1_INVALID_LENGTH );
752
753 /* Get actual bitstring */
754 *ns_cert_type = *bs.p;
755 return 0;
756}
757
758static int x509_get_key_usage( unsigned char **p,
759 const unsigned char *end,
760 unsigned char *key_usage)
761{
762 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000763 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000764
765 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000766 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000767
Paul Bakkercebdf172011-11-11 15:01:31 +0000768 if( bs.len > 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000769 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000770 POLARSSL_ERR_ASN1_INVALID_LENGTH );
771
772 /* Get actual bitstring */
773 *key_usage = *bs.p;
774 return 0;
775}
776
Paul Bakkerd98030e2009-05-02 15:13:40 +0000777/*
Paul Bakker74111d32011-01-15 16:57:55 +0000778 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
779 *
780 * KeyPurposeId ::= OBJECT IDENTIFIER
781 */
782static int x509_get_ext_key_usage( unsigned char **p,
783 const unsigned char *end,
784 x509_sequence *ext_key_usage)
785{
786 int ret;
787
788 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000789 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000790
791 /* Sequence length must be >= 1 */
792 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000793 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000794 POLARSSL_ERR_ASN1_INVALID_LENGTH );
795
796 return 0;
797}
798
799/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000800 * SubjectAltName ::= GeneralNames
801 *
802 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
803 *
804 * GeneralName ::= CHOICE {
805 * otherName [0] OtherName,
806 * rfc822Name [1] IA5String,
807 * dNSName [2] IA5String,
808 * x400Address [3] ORAddress,
809 * directoryName [4] Name,
810 * ediPartyName [5] EDIPartyName,
811 * uniformResourceIdentifier [6] IA5String,
812 * iPAddress [7] OCTET STRING,
813 * registeredID [8] OBJECT IDENTIFIER }
814 *
815 * OtherName ::= SEQUENCE {
816 * type-id OBJECT IDENTIFIER,
817 * value [0] EXPLICIT ANY DEFINED BY type-id }
818 *
819 * EDIPartyName ::= SEQUENCE {
820 * nameAssigner [0] DirectoryString OPTIONAL,
821 * partyName [1] DirectoryString }
822 *
823 * NOTE: PolarSSL only parses and uses dNSName at this point.
824 */
825static int x509_get_subject_alt_name( unsigned char **p,
826 const unsigned char *end,
827 x509_sequence *subject_alt_name )
828{
829 int ret;
830 size_t len, tag_len;
831 asn1_buf *buf;
832 unsigned char tag;
833 asn1_sequence *cur = subject_alt_name;
834
835 /* Get main sequence tag */
836 if( ( ret = asn1_get_tag( p, end, &len,
837 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
838 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
839
840 if( *p + len != end )
841 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
842 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
843
844 while( *p < end )
845 {
846 if( ( end - *p ) < 1 )
847 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
848 POLARSSL_ERR_ASN1_OUT_OF_DATA );
849
850 tag = **p;
851 (*p)++;
852 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
853 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
854
855 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
856 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
857 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
858
859 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
860 {
861 *p += tag_len;
862 continue;
863 }
864
865 buf = &(cur->buf);
866 buf->tag = tag;
867 buf->p = *p;
868 buf->len = tag_len;
869 *p += buf->len;
870
871 /* Allocate and assign next pointer */
872 if (*p < end)
873 {
874 cur->next = (asn1_sequence *) malloc(
875 sizeof( asn1_sequence ) );
876
877 if( cur->next == NULL )
878 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
879 POLARSSL_ERR_ASN1_MALLOC_FAILED );
880
881 cur = cur->next;
882 }
883 }
884
885 /* Set final sequence entry's next pointer to NULL */
886 cur->next = NULL;
887
888 if( *p != end )
889 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
890 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
891
892 return( 0 );
893}
894
895/*
Paul Bakker74111d32011-01-15 16:57:55 +0000896 * X.509 v3 extensions
897 *
898 * TODO: Perform all of the basic constraints tests required by the RFC
899 * TODO: Set values for undetected extensions to a sane default?
900 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000901 */
902static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000903 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000904 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000905{
Paul Bakker23986e52011-04-24 08:57:21 +0000906 int ret;
907 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000908 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000909
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000910 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000911 {
912 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
913 return( 0 );
914
915 return( ret );
916 }
917
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 while( *p < end )
919 {
Paul Bakker74111d32011-01-15 16:57:55 +0000920 /*
921 * Extension ::= SEQUENCE {
922 * extnID OBJECT IDENTIFIER,
923 * critical BOOLEAN DEFAULT FALSE,
924 * extnValue OCTET STRING }
925 */
926 x509_buf extn_oid = {0, 0, NULL};
927 int is_critical = 0; /* DEFAULT FALSE */
928
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 if( ( ret = asn1_get_tag( p, end, &len,
930 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000931 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000933 end_ext_data = *p + len;
934
Paul Bakker74111d32011-01-15 16:57:55 +0000935 /* Get extension ID */
936 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Paul Bakker74111d32011-01-15 16:57:55 +0000938 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000939 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
Paul Bakker74111d32011-01-15 16:57:55 +0000941 extn_oid.p = *p;
942 *p += extn_oid.len;
943
944 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000945 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000946 POLARSSL_ERR_ASN1_OUT_OF_DATA );
947
948 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000949 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000950 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000951 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
Paul Bakker74111d32011-01-15 16:57:55 +0000953 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000954 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000956 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000958 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000959
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000960 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000961 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000962 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Paul Bakker74111d32011-01-15 16:57:55 +0000964 /*
965 * Detect supported extensions
966 */
967 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
968 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 {
Paul Bakker74111d32011-01-15 16:57:55 +0000970 /* Parse basic constraints */
971 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000972 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000973 return ( ret );
974 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 }
Paul Bakker74111d32011-01-15 16:57:55 +0000976 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
977 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
978 {
979 /* Parse netscape certificate type */
980 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
981 &crt->ns_cert_type ) ) != 0 )
982 return ( ret );
983 crt->ext_types |= EXT_NS_CERT_TYPE;
984 }
985 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
986 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
987 {
988 /* Parse key usage */
989 if( ( ret = x509_get_key_usage( p, end_ext_octet,
990 &crt->key_usage ) ) != 0 )
991 return ( ret );
992 crt->ext_types |= EXT_KEY_USAGE;
993 }
994 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
995 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
996 {
997 /* Parse extended key usage */
998 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
999 &crt->ext_key_usage ) ) != 0 )
1000 return ( ret );
1001 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1002 }
Paul Bakkera8cd2392012-02-11 16:09:32 +00001003 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == extn_oid.len ) &&
1004 memcmp( extn_oid.p, OID_SUBJECT_ALT_NAME, extn_oid.len ) == 0 )
1005 {
1006 /* Parse extended key usage */
1007 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1008 &crt->subject_alt_names ) ) != 0 )
1009 return ( ret );
1010 crt->ext_types |= EXT_SUBJECT_ALT_NAME;
1011 }
Paul Bakker74111d32011-01-15 16:57:55 +00001012 else
1013 {
1014 /* No parser found, skip extension */
1015 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Paul Bakker5c721f92011-07-27 16:51:09 +00001017#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001018 if( is_critical )
1019 {
1020 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001021 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001022 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1023 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001024#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001025 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 }
1027
1028 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001029 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001030 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 return( 0 );
1033}
1034
1035/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001036 * X.509 CRL Entries
1037 */
1038static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001039 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001040 x509_crl_entry *entry )
1041{
Paul Bakker23986e52011-04-24 08:57:21 +00001042 int ret;
1043 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001044 x509_crl_entry *cur_entry = entry;
1045
1046 if( *p == end )
1047 return( 0 );
1048
Paul Bakker9be19372009-07-27 20:21:53 +00001049 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001050 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1051 {
1052 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1053 return( 0 );
1054
1055 return( ret );
1056 }
1057
Paul Bakker9be19372009-07-27 20:21:53 +00001058 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001059
1060 while( *p < end )
1061 {
Paul Bakker23986e52011-04-24 08:57:21 +00001062 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001063 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001064
1065 if( ( ret = asn1_get_tag( p, end, &len2,
1066 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1067 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001068 return( ret );
1069 }
1070
Paul Bakker9be19372009-07-27 20:21:53 +00001071 cur_entry->raw.tag = **p;
1072 cur_entry->raw.p = *p;
1073 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001074 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001075
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001076 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001077 return( ret );
1078
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001079 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001080 return( ret );
1081
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001082 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001083 return( ret );
1084
Paul Bakker74111d32011-01-15 16:57:55 +00001085 if ( *p < end )
1086 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001087 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001088
1089 if( cur_entry->next == NULL )
1090 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1091
Paul Bakkerd98030e2009-05-02 15:13:40 +00001092 cur_entry = cur_entry->next;
1093 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1094 }
1095 }
1096
1097 return( 0 );
1098}
1099
Paul Bakker27d66162010-03-17 06:56:01 +00001100static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1101{
1102 if( sig_oid->len == 9 &&
1103 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1104 {
1105 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1106 {
1107 *sig_alg = sig_oid->p[8];
1108 return( 0 );
1109 }
1110
1111 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1112 {
1113 *sig_alg = sig_oid->p[8];
1114 return( 0 );
1115 }
1116
1117 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1118 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001119 if( sig_oid->len == 5 &&
1120 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1121 {
1122 *sig_alg = SIG_RSA_SHA1;
1123 return( 0 );
1124 }
Paul Bakker27d66162010-03-17 06:56:01 +00001125
1126 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1127}
1128
Paul Bakkerd98030e2009-05-02 15:13:40 +00001129/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001130 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001132int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001133{
Paul Bakker23986e52011-04-24 08:57:21 +00001134 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001135 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Paul Bakker320a4b52009-03-28 18:52:39 +00001138 /*
1139 * Check for valid input
1140 */
1141 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001142 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001143
Paul Bakker96743fc2011-02-12 14:30:57 +00001144 p = (unsigned char *) malloc( len = buflen );
1145
1146 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001147 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001148
1149 memcpy( p, buf, buflen );
1150
1151 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
1153 crt->raw.p = p;
1154 crt->raw.len = len;
1155 end = p + len;
1156
1157 /*
1158 * Certificate ::= SEQUENCE {
1159 * tbsCertificate TBSCertificate,
1160 * signatureAlgorithm AlgorithmIdentifier,
1161 * signatureValue BIT STRING }
1162 */
1163 if( ( ret = asn1_get_tag( &p, end, &len,
1164 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1165 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001166 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001167 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 }
1169
Paul Bakker23986e52011-04-24 08:57:21 +00001170 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001172 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001173 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001174 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 }
1176
1177 /*
1178 * TBSCertificate ::= SEQUENCE {
1179 */
1180 crt->tbs.p = p;
1181
1182 if( ( ret = asn1_get_tag( &p, end, &len,
1183 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1184 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001185 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001186 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 }
1188
1189 end = p + len;
1190 crt->tbs.len = end - crt->tbs.p;
1191
1192 /*
1193 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1194 *
1195 * CertificateSerialNumber ::= INTEGER
1196 *
1197 * signature AlgorithmIdentifier
1198 */
1199 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1200 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1201 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1202 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001203 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 return( ret );
1205 }
1206
1207 crt->version++;
1208
1209 if( crt->version > 3 )
1210 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001211 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001212 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 }
1214
Paul Bakker27d66162010-03-17 06:56:01 +00001215 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001217 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001218 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 }
1220
1221 /*
1222 * issuer Name
1223 */
1224 crt->issuer_raw.p = p;
1225
1226 if( ( ret = asn1_get_tag( &p, end, &len,
1227 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1228 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001229 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001230 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 }
1232
1233 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1234 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001235 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 return( ret );
1237 }
1238
1239 crt->issuer_raw.len = p - crt->issuer_raw.p;
1240
1241 /*
1242 * Validity ::= SEQUENCE {
1243 * notBefore Time,
1244 * notAfter Time }
1245 *
1246 */
1247 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1248 &crt->valid_to ) ) != 0 )
1249 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001250 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 return( ret );
1252 }
1253
1254 /*
1255 * subject Name
1256 */
1257 crt->subject_raw.p = p;
1258
1259 if( ( ret = asn1_get_tag( &p, end, &len,
1260 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1261 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001262 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001263 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 }
1265
1266 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1267 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001268 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 return( ret );
1270 }
1271
1272 crt->subject_raw.len = p - crt->subject_raw.p;
1273
1274 /*
1275 * SubjectPublicKeyInfo ::= SEQUENCE
1276 * algorithm AlgorithmIdentifier,
1277 * subjectPublicKey BIT STRING }
1278 */
1279 if( ( ret = asn1_get_tag( &p, end, &len,
1280 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1281 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001282 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001283 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 }
1285
1286 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1287 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1288 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001289 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 return( ret );
1291 }
1292
1293 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1294 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001295 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 return( ret );
1297 }
1298
1299 crt->rsa.len = mpi_size( &crt->rsa.N );
1300
1301 /*
1302 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1303 * -- If present, version shall be v2 or v3
1304 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1305 * -- If present, version shall be v2 or v3
1306 * extensions [3] EXPLICIT Extensions OPTIONAL
1307 * -- If present, version shall be v3
1308 */
1309 if( crt->version == 2 || crt->version == 3 )
1310 {
1311 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1312 if( ret != 0 )
1313 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001314 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 return( ret );
1316 }
1317 }
1318
1319 if( crt->version == 2 || crt->version == 3 )
1320 {
1321 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1322 if( ret != 0 )
1323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 return( ret );
1326 }
1327 }
1328
1329 if( crt->version == 3 )
1330 {
Paul Bakker74111d32011-01-15 16:57:55 +00001331 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 if( ret != 0 )
1333 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001334 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 return( ret );
1336 }
1337 }
1338
1339 if( p != end )
1340 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001341 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001342 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001343 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 }
1345
1346 end = crt->raw.p + crt->raw.len;
1347
1348 /*
1349 * signatureAlgorithm AlgorithmIdentifier,
1350 * signatureValue BIT STRING
1351 */
1352 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1353 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001354 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 return( ret );
1356 }
1357
Paul Bakker320a4b52009-03-28 18:52:39 +00001358 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001361 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 }
1363
1364 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1365 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001366 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 return( ret );
1368 }
1369
1370 if( p != end )
1371 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001372 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001373 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001374 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 }
1376
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001377 return( 0 );
1378}
1379
1380/*
1381 * Parse one or more PEM certificates from a buffer and add them to the chained list
1382 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001383int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001384{
Paul Bakker69e095c2011-12-10 21:55:01 +00001385 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001386 x509_cert *crt, *prev = NULL;
1387 int buf_format = X509_FORMAT_DER;
1388
1389 crt = chain;
1390
1391 /*
1392 * Check for valid input
1393 */
1394 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001395 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001396
1397 while( crt->version != 0 && crt->next != NULL )
1398 {
1399 prev = crt;
1400 crt = crt->next;
1401 }
1402
1403 /*
1404 * Add new certificate on the end of the chain if needed.
1405 */
1406 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001407 {
1408 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1409
Paul Bakker7d06ad22009-05-02 15:53:56 +00001410 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001411 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001412
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001413 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001414 crt = crt->next;
1415 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001418 /*
1419 * Determine buffer content. Buffer contains either one DER certificate or
1420 * one or more PEM certificates.
1421 */
1422#if defined(POLARSSL_PEM_C)
1423 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1424 buf_format = X509_FORMAT_PEM;
1425#endif
1426
1427 if( buf_format == X509_FORMAT_DER )
1428 return x509parse_crt_der( crt, buf, buflen );
1429
1430#if defined(POLARSSL_PEM_C)
1431 if( buf_format == X509_FORMAT_PEM )
1432 {
1433 pem_context pem;
1434
1435 while( buflen > 0 )
1436 {
1437 size_t use_len;
1438 pem_init( &pem );
1439
1440 ret = pem_read_buffer( &pem,
1441 "-----BEGIN CERTIFICATE-----",
1442 "-----END CERTIFICATE-----",
1443 buf, NULL, 0, &use_len );
1444
1445 if( ret == 0 )
1446 {
1447 /*
1448 * Was PEM encoded
1449 */
1450 buflen -= use_len;
1451 buf += use_len;
1452 }
1453 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1454 {
1455 pem_free( &pem );
1456
1457 if( first_error == 0 )
1458 first_error = ret;
1459
1460 continue;
1461 }
1462 else
1463 break;
1464
1465 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1466
1467 pem_free( &pem );
1468
1469 if( ret != 0 )
1470 {
1471 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001472 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001473 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001474 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001475 {
1476 if( prev )
1477 prev->next = NULL;
1478
1479 if( crt != chain )
1480 free( crt );
1481
1482 return( ret );
1483 }
1484
1485 if( first_error == 0 )
1486 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001487
1488 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001489
1490 memset( crt, 0, sizeof( x509_cert ) );
1491 continue;
1492 }
1493
1494 success = 1;
1495
1496 /*
1497 * Add new certificate to the list
1498 */
1499 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1500
1501 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001502 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001503
1504 prev = crt;
1505 crt = crt->next;
1506 memset( crt, 0, sizeof( x509_cert ) );
1507 }
1508 }
1509#endif
1510
1511 if( crt->version == 0 )
1512 {
1513 if( prev )
1514 prev->next = NULL;
1515
1516 if( crt != chain )
1517 free( crt );
1518 }
1519
1520 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001521 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001522 else if( first_error )
1523 return( first_error );
1524 else
1525 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001526}
1527
1528/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001529 * Parse one or more CRLs and add them to the chained list
1530 */
Paul Bakker23986e52011-04-24 08:57:21 +00001531int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001532{
Paul Bakker23986e52011-04-24 08:57:21 +00001533 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001534 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001535 unsigned char *p, *end;
1536 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001537#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001538 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001539 pem_context pem;
1540#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001541
1542 crl = chain;
1543
1544 /*
1545 * Check for valid input
1546 */
1547 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001548 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001549
1550 while( crl->version != 0 && crl->next != NULL )
1551 crl = crl->next;
1552
1553 /*
1554 * Add new CRL on the end of the chain if needed.
1555 */
1556 if ( crl->version != 0 && crl->next == NULL)
1557 {
1558 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1559
Paul Bakker7d06ad22009-05-02 15:53:56 +00001560 if( crl->next == NULL )
1561 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001562 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001563 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001564 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001565
Paul Bakker7d06ad22009-05-02 15:53:56 +00001566 crl = crl->next;
1567 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001568 }
1569
Paul Bakker96743fc2011-02-12 14:30:57 +00001570#if defined(POLARSSL_PEM_C)
1571 pem_init( &pem );
1572 ret = pem_read_buffer( &pem,
1573 "-----BEGIN X509 CRL-----",
1574 "-----END X509 CRL-----",
1575 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001576
Paul Bakker96743fc2011-02-12 14:30:57 +00001577 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001578 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001579 /*
1580 * Was PEM encoded
1581 */
1582 buflen -= use_len;
1583 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001584
1585 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001586 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001588 p = pem.buf;
1589 pem.buf = NULL;
1590 len = pem.buflen;
1591 pem_free( &pem );
1592 }
1593 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1594 {
1595 pem_free( &pem );
1596 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001597 }
1598 else
1599 {
1600 /*
1601 * nope, copy the raw DER data
1602 */
1603 p = (unsigned char *) malloc( len = buflen );
1604
1605 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001606 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001607
1608 memcpy( p, buf, buflen );
1609
1610 buflen = 0;
1611 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001612#else
1613 p = (unsigned char *) malloc( len = buflen );
1614
1615 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001616 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001617
1618 memcpy( p, buf, buflen );
1619
1620 buflen = 0;
1621#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001622
1623 crl->raw.p = p;
1624 crl->raw.len = len;
1625 end = p + len;
1626
1627 /*
1628 * CertificateList ::= SEQUENCE {
1629 * tbsCertList TBSCertList,
1630 * signatureAlgorithm AlgorithmIdentifier,
1631 * signatureValue BIT STRING }
1632 */
1633 if( ( ret = asn1_get_tag( &p, end, &len,
1634 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1635 {
1636 x509_crl_free( crl );
1637 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1638 }
1639
Paul Bakker23986e52011-04-24 08:57:21 +00001640 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001641 {
1642 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001643 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001644 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1645 }
1646
1647 /*
1648 * TBSCertList ::= SEQUENCE {
1649 */
1650 crl->tbs.p = p;
1651
1652 if( ( ret = asn1_get_tag( &p, end, &len,
1653 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1654 {
1655 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001656 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001657 }
1658
1659 end = p + len;
1660 crl->tbs.len = end - crl->tbs.p;
1661
1662 /*
1663 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1664 * -- if present, MUST be v2
1665 *
1666 * signature AlgorithmIdentifier
1667 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001668 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001669 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1670 {
1671 x509_crl_free( crl );
1672 return( ret );
1673 }
1674
1675 crl->version++;
1676
1677 if( crl->version > 2 )
1678 {
1679 x509_crl_free( crl );
1680 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1681 }
1682
Paul Bakker27d66162010-03-17 06:56:01 +00001683 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001684 {
1685 x509_crl_free( crl );
1686 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1687 }
1688
1689 /*
1690 * issuer Name
1691 */
1692 crl->issuer_raw.p = p;
1693
1694 if( ( ret = asn1_get_tag( &p, end, &len,
1695 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1696 {
1697 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001698 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001699 }
1700
1701 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1702 {
1703 x509_crl_free( crl );
1704 return( ret );
1705 }
1706
1707 crl->issuer_raw.len = p - crl->issuer_raw.p;
1708
1709 /*
1710 * thisUpdate Time
1711 * nextUpdate Time OPTIONAL
1712 */
Paul Bakker91200182010-02-18 21:26:15 +00001713 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001714 {
1715 x509_crl_free( crl );
1716 return( ret );
1717 }
1718
Paul Bakker91200182010-02-18 21:26:15 +00001719 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001720 {
Paul Bakker9d781402011-05-09 16:17:09 +00001721 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001722 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001723 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001724 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001725 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001726 x509_crl_free( crl );
1727 return( ret );
1728 }
1729 }
1730
1731 /*
1732 * revokedCertificates SEQUENCE OF SEQUENCE {
1733 * userCertificate CertificateSerialNumber,
1734 * revocationDate Time,
1735 * crlEntryExtensions Extensions OPTIONAL
1736 * -- if present, MUST be v2
1737 * } OPTIONAL
1738 */
1739 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1740 {
1741 x509_crl_free( crl );
1742 return( ret );
1743 }
1744
1745 /*
1746 * crlExtensions EXPLICIT Extensions OPTIONAL
1747 * -- if present, MUST be v2
1748 */
1749 if( crl->version == 2 )
1750 {
1751 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1752
1753 if( ret != 0 )
1754 {
1755 x509_crl_free( crl );
1756 return( ret );
1757 }
1758 }
1759
1760 if( p != end )
1761 {
1762 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001763 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001764 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1765 }
1766
1767 end = crl->raw.p + crl->raw.len;
1768
1769 /*
1770 * signatureAlgorithm AlgorithmIdentifier,
1771 * signatureValue BIT STRING
1772 */
1773 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1774 {
1775 x509_crl_free( crl );
1776 return( ret );
1777 }
1778
1779 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1780 {
1781 x509_crl_free( crl );
1782 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1783 }
1784
1785 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1786 {
1787 x509_crl_free( crl );
1788 return( ret );
1789 }
1790
1791 if( p != end )
1792 {
1793 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001794 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001795 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1796 }
1797
1798 if( buflen > 0 )
1799 {
1800 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1801
Paul Bakker7d06ad22009-05-02 15:53:56 +00001802 if( crl->next == NULL )
1803 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001804 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001805 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001806 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001807
Paul Bakker7d06ad22009-05-02 15:53:56 +00001808 crl = crl->next;
1809 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810
1811 return( x509parse_crl( crl, buf, buflen ) );
1812 }
1813
1814 return( 0 );
1815}
1816
Paul Bakker335db3f2011-04-25 15:28:35 +00001817#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001818/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001819 * Load all data from a file into a given buffer.
1820 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001821int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001822{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001823 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001824
Paul Bakkerd98030e2009-05-02 15:13:40 +00001825 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001826 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001827
Paul Bakkerd98030e2009-05-02 15:13:40 +00001828 fseek( f, 0, SEEK_END );
1829 *n = (size_t) ftell( f );
1830 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001831
Paul Bakkerd98030e2009-05-02 15:13:40 +00001832 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001833 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001834
Paul Bakkerd98030e2009-05-02 15:13:40 +00001835 if( fread( *buf, 1, *n, f ) != *n )
1836 {
1837 fclose( f );
1838 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001839 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001840 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001841
Paul Bakkerd98030e2009-05-02 15:13:40 +00001842 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001843
Paul Bakkerd98030e2009-05-02 15:13:40 +00001844 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001845
Paul Bakkerd98030e2009-05-02 15:13:40 +00001846 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001847}
1848
1849/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 * Load one or more certificates and add them to the chained list
1851 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001852int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001853{
1854 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001855 size_t n;
1856 unsigned char *buf;
1857
Paul Bakker69e095c2011-12-10 21:55:01 +00001858 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1859 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Paul Bakker69e095c2011-12-10 21:55:01 +00001861 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
1863 memset( buf, 0, n + 1 );
1864 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
1866 return( ret );
1867}
1868
Paul Bakker8d914582012-06-04 12:46:42 +00001869int x509parse_crtpath( x509_cert *chain, const char *path )
1870{
1871 int ret = 0;
1872#if defined(_WIN32)
1873 int t_ret;
1874 TCHAR szDir[MAX_PATH];
1875 WIN32_FIND_DATA file_data;
1876 HANDLE hFind;
1877 DWORD dwError = 0;
1878
1879 StringCchCopy(szDir, MAX_PATH, path);
1880 StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
1881
1882 hFind = FindFirstFile( szDir, &file_data );
1883 if (hFind == INVALID_HANDLE_VALUE)
1884 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1885
1886 do
1887 {
Paul Bakkere4791f32012-06-04 21:29:15 +00001888 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001889 continue;
1890
1891 t_ret = x509parse_crtfile( chain, entry_name );
1892 if( t_ret < 0 )
1893 return( t_ret );
1894
1895 ret += t_ret;
1896 }
1897 while( FindNextFile( hFind, &file_data ) != 0 );
1898
1899 dwError = GetLastError();
1900 if (dwError != ERROR_NO_MORE_FILES)
1901 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1902
1903 FindClose( hFind );
1904#else
1905 int t_ret;
1906 struct dirent *entry;
1907 char entry_name[255];
1908 DIR *dir = opendir( path );
1909
1910 if( dir == NULL)
1911 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1912
1913 while( ( entry = readdir( dir ) ) != NULL )
1914 {
1915 if( entry->d_type != DT_REG )
1916 continue;
1917
1918 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1919 t_ret = x509parse_crtfile( chain, entry_name );
1920 if( t_ret < 0 )
1921 return( t_ret );
1922
1923 ret += t_ret;
1924 }
1925 closedir( dir );
1926#endif
1927
1928 return( ret );
1929}
1930
Paul Bakkerd98030e2009-05-02 15:13:40 +00001931/*
1932 * Load one or more CRLs and add them to the chained list
1933 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001934int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001935{
1936 int ret;
1937 size_t n;
1938 unsigned char *buf;
1939
Paul Bakker69e095c2011-12-10 21:55:01 +00001940 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1941 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001942
Paul Bakker27fdf462011-06-09 13:55:13 +00001943 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001944
1945 memset( buf, 0, n + 1 );
1946 free( buf );
1947
1948 return( ret );
1949}
1950
Paul Bakker5121ce52009-01-03 21:22:43 +00001951/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001952 * Load and parse a private RSA key
1953 */
1954int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1955{
1956 int ret;
1957 size_t n;
1958 unsigned char *buf;
1959
Paul Bakker69e095c2011-12-10 21:55:01 +00001960 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1961 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001962
1963 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001964 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001965 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001966 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001967 (unsigned char *) pwd, strlen( pwd ) );
1968
1969 memset( buf, 0, n + 1 );
1970 free( buf );
1971
1972 return( ret );
1973}
1974
1975/*
1976 * Load and parse a public RSA key
1977 */
1978int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1979{
1980 int ret;
1981 size_t n;
1982 unsigned char *buf;
1983
Paul Bakker69e095c2011-12-10 21:55:01 +00001984 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1985 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001986
Paul Bakker27fdf462011-06-09 13:55:13 +00001987 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001988
1989 memset( buf, 0, n + 1 );
1990 free( buf );
1991
1992 return( ret );
1993}
1994#endif /* POLARSSL_FS_IO */
1995
1996/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 * Parse a private RSA key
1998 */
Paul Bakker23986e52011-04-24 08:57:21 +00001999int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2000 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002001{
Paul Bakker23986e52011-04-24 08:57:21 +00002002 int ret;
2003 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002005 unsigned char *p_alt;
2006 x509_buf pk_alg_oid;
2007
Paul Bakker96743fc2011-02-12 14:30:57 +00002008#if defined(POLARSSL_PEM_C)
2009 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
Paul Bakker96743fc2011-02-12 14:30:57 +00002011 pem_init( &pem );
2012 ret = pem_read_buffer( &pem,
2013 "-----BEGIN RSA PRIVATE KEY-----",
2014 "-----END RSA PRIVATE KEY-----",
2015 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
Paul Bakkered56b222011-07-13 11:26:43 +00002017 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2018 {
2019 ret = pem_read_buffer( &pem,
2020 "-----BEGIN PRIVATE KEY-----",
2021 "-----END PRIVATE KEY-----",
2022 key, pwd, pwdlen, &len );
2023 }
2024
Paul Bakker96743fc2011-02-12 14:30:57 +00002025 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002027 /*
2028 * Was PEM encoded
2029 */
2030 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002032 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002033 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002034 pem_free( &pem );
2035 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
Paul Bakker96743fc2011-02-12 14:30:57 +00002038 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2039#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002040 ((void) pwd);
2041 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002042 p = (unsigned char *) key;
2043#endif
2044 end = p + keylen;
2045
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002047 * Note: Depending on the type of private key file one can expect either a
2048 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2049 *
2050 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002051 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002052 * algorithm AlgorithmIdentifier,
2053 * PrivateKey BIT STRING
2054 * }
2055 *
2056 * AlgorithmIdentifier ::= SEQUENCE {
2057 * algorithm OBJECT IDENTIFIER,
2058 * parameters ANY DEFINED BY algorithm OPTIONAL
2059 * }
2060 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 * RSAPrivateKey ::= SEQUENCE {
2062 * version Version,
2063 * modulus INTEGER, -- n
2064 * publicExponent INTEGER, -- e
2065 * privateExponent INTEGER, -- d
2066 * prime1 INTEGER, -- p
2067 * prime2 INTEGER, -- q
2068 * exponent1 INTEGER, -- d mod (p-1)
2069 * exponent2 INTEGER, -- d mod (q-1)
2070 * coefficient INTEGER, -- (inverse of q) mod p
2071 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2072 * }
2073 */
2074 if( ( ret = asn1_get_tag( &p, end, &len,
2075 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2076 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002077#if defined(POLARSSL_PEM_C)
2078 pem_free( &pem );
2079#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002081 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 }
2083
2084 end = p + len;
2085
2086 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2087 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002088#if defined(POLARSSL_PEM_C)
2089 pem_free( &pem );
2090#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002092 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 }
2094
2095 if( rsa->ver != 0 )
2096 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002097#if defined(POLARSSL_PEM_C)
2098 pem_free( &pem );
2099#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002101 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 }
2103
Paul Bakkered56b222011-07-13 11:26:43 +00002104 p_alt = p;
2105
2106 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2107 {
2108 // Assume that we have the PKCS#1 format if wrong
2109 // tag was encountered
2110 //
2111 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2112 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2113 {
2114#if defined(POLARSSL_PEM_C)
2115 pem_free( &pem );
2116#endif
2117 rsa_free( rsa );
2118 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2119 }
2120 }
2121 else
2122 {
2123 int can_handle;
2124
2125 /*
2126 * only RSA keys handled at this time
2127 */
2128 can_handle = 0;
2129
2130 if( pk_alg_oid.len == 9 &&
2131 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2132 can_handle = 1;
2133
2134 if( pk_alg_oid.len == 9 &&
2135 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2136 {
2137 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2138 can_handle = 1;
2139
2140 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2141 can_handle = 1;
2142 }
2143
2144 if( pk_alg_oid.len == 5 &&
2145 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2146 can_handle = 1;
2147
2148 if( can_handle == 0 )
2149 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2150
2151 /*
2152 * Parse the PKCS#8 format
2153 */
2154
2155 p = p_alt;
2156 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2157 {
2158#if defined(POLARSSL_PEM_C)
2159 pem_free( &pem );
2160#endif
2161 rsa_free( rsa );
2162 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2163 }
2164
2165 if( ( end - p ) < 1 )
2166 {
2167#if defined(POLARSSL_PEM_C)
2168 pem_free( &pem );
2169#endif
2170 rsa_free( rsa );
2171 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2172 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2173 }
2174
2175 end = p + len;
2176
2177 if( ( ret = asn1_get_tag( &p, end, &len,
2178 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2179 {
2180#if defined(POLARSSL_PEM_C)
2181 pem_free( &pem );
2182#endif
2183 rsa_free( rsa );
2184 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2185 }
2186
2187 end = p + len;
2188
2189 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2190 {
2191#if defined(POLARSSL_PEM_C)
2192 pem_free( &pem );
2193#endif
2194 rsa_free( rsa );
2195 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2196 }
2197
2198 if( rsa->ver != 0 )
2199 {
2200#if defined(POLARSSL_PEM_C)
2201 pem_free( &pem );
2202#endif
2203 rsa_free( rsa );
2204 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2205 }
2206 }
2207
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2209 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2210 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2211 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2212 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2213 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2214 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2215 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2216 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002217#if defined(POLARSSL_PEM_C)
2218 pem_free( &pem );
2219#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002221 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 }
2223
2224 rsa->len = mpi_size( &rsa->N );
2225
2226 if( p != end )
2227 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002228#if defined(POLARSSL_PEM_C)
2229 pem_free( &pem );
2230#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002232 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002233 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 }
2235
2236 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2237 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002238#if defined(POLARSSL_PEM_C)
2239 pem_free( &pem );
2240#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 rsa_free( rsa );
2242 return( ret );
2243 }
2244
Paul Bakker96743fc2011-02-12 14:30:57 +00002245#if defined(POLARSSL_PEM_C)
2246 pem_free( &pem );
2247#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
2249 return( 0 );
2250}
2251
2252/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002253 * Parse a public RSA key
2254 */
Paul Bakker23986e52011-04-24 08:57:21 +00002255int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002256{
Paul Bakker23986e52011-04-24 08:57:21 +00002257 int ret;
2258 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002259 unsigned char *p, *end;
2260 x509_buf alg_oid;
2261#if defined(POLARSSL_PEM_C)
2262 pem_context pem;
2263
2264 pem_init( &pem );
2265 ret = pem_read_buffer( &pem,
2266 "-----BEGIN PUBLIC KEY-----",
2267 "-----END PUBLIC KEY-----",
2268 key, NULL, 0, &len );
2269
2270 if( ret == 0 )
2271 {
2272 /*
2273 * Was PEM encoded
2274 */
2275 keylen = pem.buflen;
2276 }
2277 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2278 {
2279 pem_free( &pem );
2280 return( ret );
2281 }
2282
2283 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2284#else
2285 p = (unsigned char *) key;
2286#endif
2287 end = p + keylen;
2288
2289 /*
2290 * PublicKeyInfo ::= SEQUENCE {
2291 * algorithm AlgorithmIdentifier,
2292 * PublicKey BIT STRING
2293 * }
2294 *
2295 * AlgorithmIdentifier ::= SEQUENCE {
2296 * algorithm OBJECT IDENTIFIER,
2297 * parameters ANY DEFINED BY algorithm OPTIONAL
2298 * }
2299 *
2300 * RSAPublicKey ::= SEQUENCE {
2301 * modulus INTEGER, -- n
2302 * publicExponent INTEGER -- e
2303 * }
2304 */
2305
2306 if( ( ret = asn1_get_tag( &p, end, &len,
2307 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2308 {
2309#if defined(POLARSSL_PEM_C)
2310 pem_free( &pem );
2311#endif
2312 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002313 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002314 }
2315
2316 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2317 {
2318#if defined(POLARSSL_PEM_C)
2319 pem_free( &pem );
2320#endif
2321 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002322 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002323 }
2324
2325 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2326 {
2327#if defined(POLARSSL_PEM_C)
2328 pem_free( &pem );
2329#endif
2330 rsa_free( rsa );
2331 return( ret );
2332 }
2333
2334 rsa->len = mpi_size( &rsa->N );
2335
2336#if defined(POLARSSL_PEM_C)
2337 pem_free( &pem );
2338#endif
2339
2340 return( 0 );
2341}
2342
Paul Bakkereaa89f82011-04-04 21:36:15 +00002343#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002344/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002345 * Parse DHM parameters
2346 */
Paul Bakker23986e52011-04-24 08:57:21 +00002347int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002348{
Paul Bakker23986e52011-04-24 08:57:21 +00002349 int ret;
2350 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002351 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002352#if defined(POLARSSL_PEM_C)
2353 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002354
Paul Bakker96743fc2011-02-12 14:30:57 +00002355 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002356
Paul Bakker96743fc2011-02-12 14:30:57 +00002357 ret = pem_read_buffer( &pem,
2358 "-----BEGIN DH PARAMETERS-----",
2359 "-----END DH PARAMETERS-----",
2360 dhmin, NULL, 0, &dhminlen );
2361
2362 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002363 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002364 /*
2365 * Was PEM encoded
2366 */
2367 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002368 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002369 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002370 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002371 pem_free( &pem );
2372 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002373 }
2374
Paul Bakker96743fc2011-02-12 14:30:57 +00002375 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2376#else
2377 p = (unsigned char *) dhmin;
2378#endif
2379 end = p + dhminlen;
2380
Paul Bakker1b57b062011-01-06 15:48:19 +00002381 memset( dhm, 0, sizeof( dhm_context ) );
2382
Paul Bakker1b57b062011-01-06 15:48:19 +00002383 /*
2384 * DHParams ::= SEQUENCE {
2385 * prime INTEGER, -- P
2386 * generator INTEGER, -- g
2387 * }
2388 */
2389 if( ( ret = asn1_get_tag( &p, end, &len,
2390 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2391 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002392#if defined(POLARSSL_PEM_C)
2393 pem_free( &pem );
2394#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002395 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002396 }
2397
2398 end = p + len;
2399
2400 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2401 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2402 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002403#if defined(POLARSSL_PEM_C)
2404 pem_free( &pem );
2405#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002406 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002407 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002408 }
2409
2410 if( p != end )
2411 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002412#if defined(POLARSSL_PEM_C)
2413 pem_free( &pem );
2414#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002415 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002416 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002417 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2418 }
2419
Paul Bakker96743fc2011-02-12 14:30:57 +00002420#if defined(POLARSSL_PEM_C)
2421 pem_free( &pem );
2422#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002423
2424 return( 0 );
2425}
2426
Paul Bakker335db3f2011-04-25 15:28:35 +00002427#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002428/*
2429 * Load and parse a private RSA key
2430 */
2431int x509parse_dhmfile( dhm_context *dhm, const char *path )
2432{
2433 int ret;
2434 size_t n;
2435 unsigned char *buf;
2436
Paul Bakker69e095c2011-12-10 21:55:01 +00002437 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2438 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002439
Paul Bakker27fdf462011-06-09 13:55:13 +00002440 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002441
2442 memset( buf, 0, n + 1 );
2443 free( buf );
2444
2445 return( ret );
2446}
Paul Bakker335db3f2011-04-25 15:28:35 +00002447#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002448#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002449
Paul Bakker5121ce52009-01-03 21:22:43 +00002450#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002451#include <stdarg.h>
2452
2453#if !defined vsnprintf
2454#define vsnprintf _vsnprintf
2455#endif // vsnprintf
2456
2457/*
2458 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2459 * Result value is not size of buffer needed, but -1 if no fit is possible.
2460 *
2461 * This fuction tries to 'fix' this by at least suggesting enlarging the
2462 * size by 20.
2463 */
2464int compat_snprintf(char *str, size_t size, const char *format, ...)
2465{
2466 va_list ap;
2467 int res = -1;
2468
2469 va_start( ap, format );
2470
2471 res = vsnprintf( str, size, format, ap );
2472
2473 va_end( ap );
2474
2475 // No quick fix possible
2476 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002477 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002478
2479 return res;
2480}
2481
2482#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002483#endif
2484
Paul Bakkerd98030e2009-05-02 15:13:40 +00002485#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2486
2487#define SAFE_SNPRINTF() \
2488{ \
2489 if( ret == -1 ) \
2490 return( -1 ); \
2491 \
Paul Bakker23986e52011-04-24 08:57:21 +00002492 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002493 p[n - 1] = '\0'; \
2494 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2495 } \
2496 \
Paul Bakker23986e52011-04-24 08:57:21 +00002497 n -= (unsigned int) ret; \
2498 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002499}
2500
Paul Bakker5121ce52009-01-03 21:22:43 +00002501/*
2502 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002503 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002505int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002506{
Paul Bakker23986e52011-04-24 08:57:21 +00002507 int ret;
2508 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002510 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 char s[128], *p;
2512
2513 memset( s, 0, sizeof( s ) );
2514
2515 name = dn;
2516 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002517 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
2519 while( name != NULL )
2520 {
Paul Bakker74111d32011-01-15 16:57:55 +00002521 if( name != dn )
2522 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002523 ret = snprintf( p, n, ", " );
2524 SAFE_SNPRINTF();
2525 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
2527 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2528 {
2529 switch( name->oid.p[2] )
2530 {
2531 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002532 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
2534 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002535 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
2537 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002538 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
2540 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002541 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
2543 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002544 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
2546 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002547 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
2549 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002550 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 name->oid.p[2] );
2552 break;
2553 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002554 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 }
2556 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2557 {
2558 switch( name->oid.p[8] )
2559 {
2560 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002561 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002564 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 name->oid.p[8] );
2566 break;
2567 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002568 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 }
2570 else
Paul Bakker74111d32011-01-15 16:57:55 +00002571 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002572 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002573 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
2576 for( i = 0; i < name->val.len; i++ )
2577 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002578 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 break;
2580
2581 c = name->val.p[i];
2582 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2583 s[i] = '?';
2584 else s[i] = c;
2585 }
2586 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002587 ret = snprintf( p, n, "%s", s );
2588 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 name = name->next;
2590 }
2591
Paul Bakker23986e52011-04-24 08:57:21 +00002592 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593}
2594
2595/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002596 * Store the serial in printable form into buf; no more
2597 * than size characters will be written
2598 */
2599int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2600{
Paul Bakker23986e52011-04-24 08:57:21 +00002601 int ret;
2602 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002603 char *p;
2604
2605 p = buf;
2606 n = size;
2607
2608 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002609 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002610
2611 for( i = 0; i < nr; i++ )
2612 {
Paul Bakker93048802011-12-05 14:38:06 +00002613 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002614 continue;
2615
Paul Bakkerdd476992011-01-16 21:34:59 +00002616 ret = snprintf( p, n, "%02X%s",
2617 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2618 SAFE_SNPRINTF();
2619 }
2620
Paul Bakker03c7c252011-11-25 12:37:37 +00002621 if( nr != serial->len )
2622 {
2623 ret = snprintf( p, n, "...." );
2624 SAFE_SNPRINTF();
2625 }
2626
Paul Bakker23986e52011-04-24 08:57:21 +00002627 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002628}
2629
2630/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002631 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002633int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2634 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002635{
Paul Bakker23986e52011-04-24 08:57:21 +00002636 int ret;
2637 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002638 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
2640 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002641 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
Paul Bakkerd98030e2009-05-02 15:13:40 +00002643 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002645 SAFE_SNPRINTF();
2646 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002648 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Paul Bakkerdd476992011-01-16 21:34:59 +00002650 ret = x509parse_serial_gets( p, n, &crt->serial);
2651 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Paul Bakkerd98030e2009-05-02 15:13:40 +00002653 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2654 SAFE_SNPRINTF();
2655 ret = x509parse_dn_gets( p, n, &crt->issuer );
2656 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Paul Bakkerd98030e2009-05-02 15:13:40 +00002658 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2659 SAFE_SNPRINTF();
2660 ret = x509parse_dn_gets( p, n, &crt->subject );
2661 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Paul Bakkerd98030e2009-05-02 15:13:40 +00002663 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2665 crt->valid_from.year, crt->valid_from.mon,
2666 crt->valid_from.day, crt->valid_from.hour,
2667 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002668 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Paul Bakkerd98030e2009-05-02 15:13:40 +00002670 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2672 crt->valid_to.year, crt->valid_to.mon,
2673 crt->valid_to.day, crt->valid_to.hour,
2674 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002675 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Paul Bakkerd98030e2009-05-02 15:13:40 +00002677 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2678 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Paul Bakker27d66162010-03-17 06:56:01 +00002680 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002682 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2683 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2684 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2685 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2686 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2687 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2688 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2689 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2690 default: ret = snprintf( p, n, "???" ); break;
2691 }
2692 SAFE_SNPRINTF();
2693
2694 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002695 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002696 SAFE_SNPRINTF();
2697
Paul Bakker23986e52011-04-24 08:57:21 +00002698 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002699}
2700
Paul Bakker74111d32011-01-15 16:57:55 +00002701/* Compare a given OID string with an OID x509_buf * */
2702#define OID_CMP(oid_str, oid_buf) \
2703 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2704 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2705
2706/*
2707 * Return an informational string describing the given OID
2708 */
2709const char *x509_oid_get_description( x509_buf *oid )
2710{
2711 if ( oid == NULL )
2712 return ( NULL );
2713
2714 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2715 return( STRING_SERVER_AUTH );
2716
2717 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2718 return( STRING_CLIENT_AUTH );
2719
2720 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2721 return( STRING_CODE_SIGNING );
2722
2723 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2724 return( STRING_EMAIL_PROTECTION );
2725
2726 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2727 return( STRING_TIME_STAMPING );
2728
2729 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2730 return( STRING_OCSP_SIGNING );
2731
2732 return( NULL );
2733}
2734
2735/* Return the x.y.z.... style numeric string for the given OID */
2736int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2737{
Paul Bakker23986e52011-04-24 08:57:21 +00002738 int ret;
2739 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002740 unsigned int value;
2741 char *p;
2742
2743 p = buf;
2744 n = size;
2745
2746 /* First byte contains first two dots */
2747 if( oid->len > 0 )
2748 {
2749 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2750 SAFE_SNPRINTF();
2751 }
2752
2753 /* TODO: value can overflow in value. */
2754 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002755 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002756 {
2757 value <<= 7;
2758 value += oid->p[i] & 0x7F;
2759
2760 if( !( oid->p[i] & 0x80 ) )
2761 {
2762 /* Last byte */
2763 ret = snprintf( p, n, ".%d", value );
2764 SAFE_SNPRINTF();
2765 value = 0;
2766 }
2767 }
2768
Paul Bakker23986e52011-04-24 08:57:21 +00002769 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002770}
2771
Paul Bakkerd98030e2009-05-02 15:13:40 +00002772/*
2773 * Return an informational string about the CRL.
2774 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002775int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2776 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002777{
Paul Bakker23986e52011-04-24 08:57:21 +00002778 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002779 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002780 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002781 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002782
2783 p = buf;
2784 n = size;
2785
2786 ret = snprintf( p, n, "%sCRL version : %d",
2787 prefix, crl->version );
2788 SAFE_SNPRINTF();
2789
2790 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2791 SAFE_SNPRINTF();
2792 ret = x509parse_dn_gets( p, n, &crl->issuer );
2793 SAFE_SNPRINTF();
2794
2795 ret = snprintf( p, n, "\n%sthis update : " \
2796 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2797 crl->this_update.year, crl->this_update.mon,
2798 crl->this_update.day, crl->this_update.hour,
2799 crl->this_update.min, crl->this_update.sec );
2800 SAFE_SNPRINTF();
2801
2802 ret = snprintf( p, n, "\n%snext update : " \
2803 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2804 crl->next_update.year, crl->next_update.mon,
2805 crl->next_update.day, crl->next_update.hour,
2806 crl->next_update.min, crl->next_update.sec );
2807 SAFE_SNPRINTF();
2808
2809 entry = &crl->entry;
2810
2811 ret = snprintf( p, n, "\n%sRevoked certificates:",
2812 prefix );
2813 SAFE_SNPRINTF();
2814
Paul Bakker9be19372009-07-27 20:21:53 +00002815 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002816 {
2817 ret = snprintf( p, n, "\n%sserial number: ",
2818 prefix );
2819 SAFE_SNPRINTF();
2820
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002821 ret = x509parse_serial_gets( p, n, &entry->serial);
2822 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002823
Paul Bakkerd98030e2009-05-02 15:13:40 +00002824 ret = snprintf( p, n, " revocation date: " \
2825 "%04d-%02d-%02d %02d:%02d:%02d",
2826 entry->revocation_date.year, entry->revocation_date.mon,
2827 entry->revocation_date.day, entry->revocation_date.hour,
2828 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002829 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002830
2831 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002832 }
2833
Paul Bakkerd98030e2009-05-02 15:13:40 +00002834 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2835 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Paul Bakker27d66162010-03-17 06:56:01 +00002837 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002838 {
2839 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2840 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2841 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2842 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2843 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2844 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2845 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2846 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2847 default: ret = snprintf( p, n, "???" ); break;
2848 }
2849 SAFE_SNPRINTF();
2850
Paul Bakker1e27bb22009-07-19 20:25:25 +00002851 ret = snprintf( p, n, "\n" );
2852 SAFE_SNPRINTF();
2853
Paul Bakker23986e52011-04-24 08:57:21 +00002854 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855}
2856
2857/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002858 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002859 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002860int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002861{
Paul Bakkercce9d772011-11-18 14:26:47 +00002862 int year, mon, day;
2863 int hour, min, sec;
2864
2865#if defined(_WIN32)
2866 SYSTEMTIME st;
2867
2868 GetLocalTime(&st);
2869
2870 year = st.wYear;
2871 mon = st.wMonth;
2872 day = st.wDay;
2873 hour = st.wHour;
2874 min = st.wMinute;
2875 sec = st.wSecond;
2876#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 struct tm *lt;
2878 time_t tt;
2879
2880 tt = time( NULL );
2881 lt = localtime( &tt );
2882
Paul Bakkercce9d772011-11-18 14:26:47 +00002883 year = lt->tm_year + 1900;
2884 mon = lt->tm_mon + 1;
2885 day = lt->tm_mday;
2886 hour = lt->tm_hour;
2887 min = lt->tm_min;
2888 sec = lt->tm_sec;
2889#endif
2890
2891 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002892 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Paul Bakkercce9d772011-11-18 14:26:47 +00002894 if( year == to->year &&
2895 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002896 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Paul Bakkercce9d772011-11-18 14:26:47 +00002898 if( year == to->year &&
2899 mon == to->mon &&
2900 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002901 return( 1 );
2902
Paul Bakkercce9d772011-11-18 14:26:47 +00002903 if( year == to->year &&
2904 mon == to->mon &&
2905 day == to->day &&
2906 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002907 return( 1 );
2908
Paul Bakkercce9d772011-11-18 14:26:47 +00002909 if( year == to->year &&
2910 mon == to->mon &&
2911 day == to->day &&
2912 hour == to->hour &&
2913 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002914 return( 1 );
2915
Paul Bakkercce9d772011-11-18 14:26:47 +00002916 if( year == to->year &&
2917 mon == to->mon &&
2918 day == to->day &&
2919 hour == to->hour &&
2920 min == to->min &&
2921 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002922 return( 1 );
2923
Paul Bakker40ea7de2009-05-03 10:18:48 +00002924 return( 0 );
2925}
2926
2927/*
2928 * Return 1 if the certificate is revoked, or 0 otherwise.
2929 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002930int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002931{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002932 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002933
2934 while( cur != NULL && cur->serial.len != 0 )
2935 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002936 if( crt->serial.len == cur->serial.len &&
2937 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002938 {
2939 if( x509parse_time_expired( &cur->revocation_date ) )
2940 return( 1 );
2941 }
2942
2943 cur = cur->next;
2944 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
2946 return( 0 );
2947}
2948
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002949/*
2950 * Wrapper for x509 hashes.
2951 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002952 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002953 */
Paul Bakker23986e52011-04-24 08:57:21 +00002954static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002955 unsigned char *out )
2956{
2957 switch( alg )
2958 {
Paul Bakker40e46942009-01-03 21:51:57 +00002959#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002960 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002961#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002962#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002963 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002964#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002965#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002966 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002967#endif
2968#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002969 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002970#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002971#if defined(POLARSSL_SHA2_C)
2972 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2973 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2974#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002975#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002976 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2977 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2978#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002980 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 break;
2982 }
2983}
2984
2985/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002986 * Check that the given certificate is valid accoring to the CRL.
2987 */
2988static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2989 x509_crl *crl_list)
2990{
2991 int flags = 0;
2992 int hash_id;
2993 unsigned char hash[64];
2994
2995 /*
2996 * TODO: What happens if no CRL is present?
2997 * Suggestion: Revocation state should be unknown if no CRL is present.
2998 * For backwards compatibility this is not yet implemented.
2999 */
3000
3001 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
3002 {
3003 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
3004 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3005 crl_list->issuer_raw.len ) != 0 )
3006 {
3007 crl_list = crl_list->next;
3008 continue;
3009 }
3010
3011 /*
3012 * Check if CRL is correctly signed by the trusted CA
3013 */
3014 hash_id = crl_list->sig_alg;
3015
3016 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3017
3018 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
3019 0, hash, crl_list->sig.p ) == 0 )
3020 {
3021 /*
3022 * CRL is not trusted
3023 */
3024 flags |= BADCRL_NOT_TRUSTED;
3025 break;
3026 }
3027
3028 /*
3029 * Check for validity of CRL (Do not drop out)
3030 */
3031 if( x509parse_time_expired( &crl_list->next_update ) )
3032 flags |= BADCRL_EXPIRED;
3033
3034 /*
3035 * Check if certificate is revoked
3036 */
3037 if( x509parse_revoked(crt, crl_list) )
3038 {
3039 flags |= BADCERT_REVOKED;
3040 break;
3041 }
3042
3043 crl_list = crl_list->next;
3044 }
3045 return flags;
3046}
3047
Paul Bakker57b12982012-02-11 17:38:38 +00003048int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003049{
3050 size_t i;
3051 size_t cn_idx = 0;
3052
Paul Bakker57b12982012-02-11 17:38:38 +00003053 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003054 return( 0 );
3055
3056 for( i = 0; i < strlen( cn ); ++i )
3057 {
3058 if( cn[i] == '.' )
3059 {
3060 cn_idx = i;
3061 break;
3062 }
3063 }
3064
3065 if( cn_idx == 0 )
3066 return( 0 );
3067
Paul Bakker57b12982012-02-11 17:38:38 +00003068 if( memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 &&
3069 strlen( cn ) - cn_idx == name->len - 1 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003070 {
3071 return( 1 );
3072 }
3073
3074 return( 0 );
3075}
3076
Paul Bakker76fd75a2011-01-16 21:12:10 +00003077/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 * Verify the certificate validity
3079 */
3080int x509parse_verify( x509_cert *crt,
3081 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003082 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003083 const char *cn, int *flags,
3084 int (*f_vrfy)(void *, x509_cert *, int, int),
3085 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003086{
Paul Bakker23986e52011-04-24 08:57:21 +00003087 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 int hash_id;
3089 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003090 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00003092 unsigned char hash[64];
Paul Bakkera8cd2392012-02-11 16:09:32 +00003093 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Paul Bakker40ea7de2009-05-03 10:18:48 +00003095 *flags = 0;
3096
3097 if( x509parse_time_expired( &crt->valid_to ) )
3098 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003099
3100 if( cn != NULL )
3101 {
3102 name = &crt->subject;
3103 cn_len = strlen( cn );
3104
Paul Bakker4d2c1242012-05-10 14:12:46 +00003105 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003106 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003107 cur = &crt->subject_alt_names;
3108
3109 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003110 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003111 if( memcmp( cn, cur->buf.p, cn_len ) == 0 &&
3112 cur->buf.len == cn_len )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003113 break;
3114
Paul Bakker4d2c1242012-05-10 14:12:46 +00003115 if( memcmp( cur->buf.p, "*.", 2 ) == 0 &&
3116 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003117 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003118
Paul Bakker4d2c1242012-05-10 14:12:46 +00003119 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003120 }
3121
3122 if( cur == NULL )
3123 *flags |= BADCERT_CN_MISMATCH;
3124 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003125 else
3126 {
3127 while( name != NULL )
3128 {
3129 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 )
3130 {
3131 if( memcmp( name->val.p, cn, cn_len ) == 0 &&
3132 name->val.len == cn_len )
3133 break;
3134
3135 if( memcmp( name->val.p, "*.", 2 ) == 0 &&
3136 x509_wildcard_verify( cn, &name->val ) )
3137 break;
3138 }
3139
3140 name = name->next;
3141 }
3142
3143 if( name == NULL )
3144 *flags |= BADCERT_CN_MISMATCH;
3145 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 }
3147
Paul Bakker5121ce52009-01-03 21:22:43 +00003148 /*
3149 * Iterate upwards in the given cert chain,
3150 * ignoring any upper cert with CA != TRUE.
3151 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003152 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003153
3154 pathlen = 1;
3155
Paul Bakker76fd75a2011-01-16 21:12:10 +00003156 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003157 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003158 if( parent->ca_istrue == 0 ||
3159 crt->issuer_raw.len != parent->subject_raw.len ||
3160 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 crt->issuer_raw.len ) != 0 )
3162 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003163 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 continue;
3165 }
3166
Paul Bakker27d66162010-03-17 06:56:01 +00003167 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
3169 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3170
Paul Bakker76fd75a2011-01-16 21:12:10 +00003171 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3172 crt->sig.p ) != 0 )
3173 *flags |= BADCERT_NOT_TRUSTED;
3174
3175 /* Check trusted CA's CRL for the given crt */
3176 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003177
3178 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00003179 if( NULL != f_vrfy )
3180 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003181 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003182 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003183 else
3184 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003185 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003186 else if( *flags != 0 )
3187 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003188
3189 pathlen++;
3190
Paul Bakker76fd75a2011-01-16 21:12:10 +00003191 crt = parent;
3192 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 }
3194
3195 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003196 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003198 *flags |= BADCERT_NOT_TRUSTED;
3199
Paul Bakker7c6d4a42009-03-28 20:35:47 +00003200 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 {
3202 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
3203 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
3204 crt->issuer_raw.len ) != 0 )
3205 {
3206 trust_ca = trust_ca->next;
3207 continue;
3208 }
3209
3210 if( trust_ca->max_pathlen > 0 &&
3211 trust_ca->max_pathlen < pathlen )
3212 break;
3213
Paul Bakker27d66162010-03-17 06:56:01 +00003214 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
3216 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3217
3218 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3219 0, hash, crt->sig.p ) == 0 )
3220 {
3221 /*
3222 * cert. is signed by a trusted CA
3223 */
3224 *flags &= ~BADCERT_NOT_TRUSTED;
3225 break;
3226 }
3227
3228 trust_ca = trust_ca->next;
3229 }
3230
Paul Bakker76fd75a2011-01-16 21:12:10 +00003231 /* Check trusted CA's CRL for the given crt */
3232 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003233
3234 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003235 if( NULL != f_vrfy )
3236 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003237 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003238 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003239 else
3240 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003241 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003242 else if( *flags != 0 )
3243 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003244
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 return( 0 );
3246}
3247
3248/*
3249 * Unallocate all certificate data
3250 */
3251void x509_free( x509_cert *crt )
3252{
3253 x509_cert *cert_cur = crt;
3254 x509_cert *cert_prv;
3255 x509_name *name_cur;
3256 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003257 x509_sequence *seq_cur;
3258 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
3260 if( crt == NULL )
3261 return;
3262
3263 do
3264 {
3265 rsa_free( &cert_cur->rsa );
3266
3267 name_cur = cert_cur->issuer.next;
3268 while( name_cur != NULL )
3269 {
3270 name_prv = name_cur;
3271 name_cur = name_cur->next;
3272 memset( name_prv, 0, sizeof( x509_name ) );
3273 free( name_prv );
3274 }
3275
3276 name_cur = cert_cur->subject.next;
3277 while( name_cur != NULL )
3278 {
3279 name_prv = name_cur;
3280 name_cur = name_cur->next;
3281 memset( name_prv, 0, sizeof( x509_name ) );
3282 free( name_prv );
3283 }
3284
Paul Bakker74111d32011-01-15 16:57:55 +00003285 seq_cur = cert_cur->ext_key_usage.next;
3286 while( seq_cur != NULL )
3287 {
3288 seq_prv = seq_cur;
3289 seq_cur = seq_cur->next;
3290 memset( seq_prv, 0, sizeof( x509_sequence ) );
3291 free( seq_prv );
3292 }
3293
Paul Bakker8afa70d2012-02-11 18:42:45 +00003294 seq_cur = cert_cur->subject_alt_names.next;
3295 while( seq_cur != NULL )
3296 {
3297 seq_prv = seq_cur;
3298 seq_cur = seq_cur->next;
3299 memset( seq_prv, 0, sizeof( x509_sequence ) );
3300 free( seq_prv );
3301 }
3302
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 if( cert_cur->raw.p != NULL )
3304 {
3305 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3306 free( cert_cur->raw.p );
3307 }
3308
3309 cert_cur = cert_cur->next;
3310 }
3311 while( cert_cur != NULL );
3312
3313 cert_cur = crt;
3314 do
3315 {
3316 cert_prv = cert_cur;
3317 cert_cur = cert_cur->next;
3318
3319 memset( cert_prv, 0, sizeof( x509_cert ) );
3320 if( cert_prv != crt )
3321 free( cert_prv );
3322 }
3323 while( cert_cur != NULL );
3324}
3325
Paul Bakkerd98030e2009-05-02 15:13:40 +00003326/*
3327 * Unallocate all CRL data
3328 */
3329void x509_crl_free( x509_crl *crl )
3330{
3331 x509_crl *crl_cur = crl;
3332 x509_crl *crl_prv;
3333 x509_name *name_cur;
3334 x509_name *name_prv;
3335 x509_crl_entry *entry_cur;
3336 x509_crl_entry *entry_prv;
3337
3338 if( crl == NULL )
3339 return;
3340
3341 do
3342 {
3343 name_cur = crl_cur->issuer.next;
3344 while( name_cur != NULL )
3345 {
3346 name_prv = name_cur;
3347 name_cur = name_cur->next;
3348 memset( name_prv, 0, sizeof( x509_name ) );
3349 free( name_prv );
3350 }
3351
3352 entry_cur = crl_cur->entry.next;
3353 while( entry_cur != NULL )
3354 {
3355 entry_prv = entry_cur;
3356 entry_cur = entry_cur->next;
3357 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3358 free( entry_prv );
3359 }
3360
3361 if( crl_cur->raw.p != NULL )
3362 {
3363 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3364 free( crl_cur->raw.p );
3365 }
3366
3367 crl_cur = crl_cur->next;
3368 }
3369 while( crl_cur != NULL );
3370
3371 crl_cur = crl;
3372 do
3373 {
3374 crl_prv = crl_cur;
3375 crl_cur = crl_cur->next;
3376
3377 memset( crl_prv, 0, sizeof( x509_crl ) );
3378 if( crl_prv != crl )
3379 free( crl_prv );
3380 }
3381 while( crl_cur != NULL );
3382}
3383
Paul Bakker40e46942009-01-03 21:51:57 +00003384#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003385
Paul Bakker40e46942009-01-03 21:51:57 +00003386#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003387
3388/*
3389 * Checkup routine
3390 */
3391int x509_self_test( int verbose )
3392{
Paul Bakker5690efc2011-05-26 13:16:06 +00003393#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003394 int ret;
3395 int flags;
3396 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003397 x509_cert cacert;
3398 x509_cert clicert;
3399 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003400#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003401 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003402#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003403
3404 if( verbose != 0 )
3405 printf( " X.509 certificate load: " );
3406
3407 memset( &clicert, 0, sizeof( x509_cert ) );
3408
3409 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003410 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 if( ret != 0 )
3412 {
3413 if( verbose != 0 )
3414 printf( "failed\n" );
3415
3416 return( ret );
3417 }
3418
3419 memset( &cacert, 0, sizeof( x509_cert ) );
3420
3421 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003422 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003423 if( ret != 0 )
3424 {
3425 if( verbose != 0 )
3426 printf( "failed\n" );
3427
3428 return( ret );
3429 }
3430
3431 if( verbose != 0 )
3432 printf( "passed\n X.509 private key load: " );
3433
3434 i = strlen( test_ca_key );
3435 j = strlen( test_ca_pwd );
3436
Paul Bakker66b78b22011-03-25 14:22:50 +00003437 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3438
Paul Bakker5121ce52009-01-03 21:22:43 +00003439 if( ( ret = x509parse_key( &rsa,
3440 (unsigned char *) test_ca_key, i,
3441 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3442 {
3443 if( verbose != 0 )
3444 printf( "failed\n" );
3445
3446 return( ret );
3447 }
3448
3449 if( verbose != 0 )
3450 printf( "passed\n X.509 signature verify: ");
3451
Paul Bakker23986e52011-04-24 08:57:21 +00003452 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 if( ret != 0 )
3454 {
Paul Bakker23986e52011-04-24 08:57:21 +00003455 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003456 if( verbose != 0 )
3457 printf( "failed\n" );
3458
3459 return( ret );
3460 }
3461
Paul Bakker5690efc2011-05-26 13:16:06 +00003462#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003464 printf( "passed\n X.509 DHM parameter load: " );
3465
3466 i = strlen( test_dhm_params );
3467 j = strlen( test_ca_pwd );
3468
3469 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3470 {
3471 if( verbose != 0 )
3472 printf( "failed\n" );
3473
3474 return( ret );
3475 }
3476
3477 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003479#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003480
3481 x509_free( &cacert );
3482 x509_free( &clicert );
3483 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003484#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003485 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003486#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003487
3488 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003489#else
3490 ((void) verbose);
3491 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3492#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003493}
3494
3495#endif
3496
3497#endif