blob: a43c6d4e3656ec7114b43322514e24ec8dde67a2 [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
Paul Bakkercefb3962012-06-27 11:51:09 +00001266 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 {
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 Bakkercefb3962012-06-27 11:51:09 +00002521 if( !name->oid.p )
2522 {
2523 name = name->next;
2524 continue;
2525 }
2526
Paul Bakker74111d32011-01-15 16:57:55 +00002527 if( name != dn )
2528 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002529 ret = snprintf( p, n, ", " );
2530 SAFE_SNPRINTF();
2531 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
2533 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2534 {
2535 switch( name->oid.p[2] )
2536 {
2537 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002538 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
2540 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002541 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
2543 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002544 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
2546 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002547 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
2549 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002550 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
2552 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002553 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
2555 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002556 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 name->oid.p[2] );
2558 break;
2559 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002560 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 }
2562 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2563 {
2564 switch( name->oid.p[8] )
2565 {
2566 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002567 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
2569 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002570 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 name->oid.p[8] );
2572 break;
2573 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002574 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
2576 else
Paul Bakker74111d32011-01-15 16:57:55 +00002577 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002578 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002579 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002580 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
2582 for( i = 0; i < name->val.len; i++ )
2583 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002584 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 break;
2586
2587 c = name->val.p[i];
2588 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2589 s[i] = '?';
2590 else s[i] = c;
2591 }
2592 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002593 ret = snprintf( p, n, "%s", s );
2594 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 name = name->next;
2596 }
2597
Paul Bakker23986e52011-04-24 08:57:21 +00002598 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599}
2600
2601/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002602 * Store the serial in printable form into buf; no more
2603 * than size characters will be written
2604 */
2605int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2606{
Paul Bakker23986e52011-04-24 08:57:21 +00002607 int ret;
2608 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002609 char *p;
2610
2611 p = buf;
2612 n = size;
2613
2614 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002615 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002616
2617 for( i = 0; i < nr; i++ )
2618 {
Paul Bakker93048802011-12-05 14:38:06 +00002619 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002620 continue;
2621
Paul Bakkerdd476992011-01-16 21:34:59 +00002622 ret = snprintf( p, n, "%02X%s",
2623 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2624 SAFE_SNPRINTF();
2625 }
2626
Paul Bakker03c7c252011-11-25 12:37:37 +00002627 if( nr != serial->len )
2628 {
2629 ret = snprintf( p, n, "...." );
2630 SAFE_SNPRINTF();
2631 }
2632
Paul Bakker23986e52011-04-24 08:57:21 +00002633 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002634}
2635
2636/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002637 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002639int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2640 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002641{
Paul Bakker23986e52011-04-24 08:57:21 +00002642 int ret;
2643 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002644 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
2646 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002647 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Paul Bakkerd98030e2009-05-02 15:13:40 +00002649 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002651 SAFE_SNPRINTF();
2652 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002654 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Paul Bakkerdd476992011-01-16 21:34:59 +00002656 ret = x509parse_serial_gets( p, n, &crt->serial);
2657 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Paul Bakkerd98030e2009-05-02 15:13:40 +00002659 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2660 SAFE_SNPRINTF();
2661 ret = x509parse_dn_gets( p, n, &crt->issuer );
2662 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Paul Bakkerd98030e2009-05-02 15:13:40 +00002664 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2665 SAFE_SNPRINTF();
2666 ret = x509parse_dn_gets( p, n, &crt->subject );
2667 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Paul Bakkerd98030e2009-05-02 15:13:40 +00002669 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2671 crt->valid_from.year, crt->valid_from.mon,
2672 crt->valid_from.day, crt->valid_from.hour,
2673 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002674 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Paul Bakkerd98030e2009-05-02 15:13:40 +00002676 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2678 crt->valid_to.year, crt->valid_to.mon,
2679 crt->valid_to.day, crt->valid_to.hour,
2680 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002681 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Paul Bakkerd98030e2009-05-02 15:13:40 +00002683 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2684 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Paul Bakker27d66162010-03-17 06:56:01 +00002686 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002688 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2689 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2690 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2691 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2692 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2693 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2694 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2695 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2696 default: ret = snprintf( p, n, "???" ); break;
2697 }
2698 SAFE_SNPRINTF();
2699
2700 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002701 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002702 SAFE_SNPRINTF();
2703
Paul Bakker23986e52011-04-24 08:57:21 +00002704 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002705}
2706
Paul Bakker74111d32011-01-15 16:57:55 +00002707/* Compare a given OID string with an OID x509_buf * */
2708#define OID_CMP(oid_str, oid_buf) \
2709 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2710 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2711
2712/*
2713 * Return an informational string describing the given OID
2714 */
2715const char *x509_oid_get_description( x509_buf *oid )
2716{
2717 if ( oid == NULL )
2718 return ( NULL );
2719
2720 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2721 return( STRING_SERVER_AUTH );
2722
2723 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2724 return( STRING_CLIENT_AUTH );
2725
2726 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2727 return( STRING_CODE_SIGNING );
2728
2729 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2730 return( STRING_EMAIL_PROTECTION );
2731
2732 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2733 return( STRING_TIME_STAMPING );
2734
2735 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2736 return( STRING_OCSP_SIGNING );
2737
2738 return( NULL );
2739}
2740
2741/* Return the x.y.z.... style numeric string for the given OID */
2742int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2743{
Paul Bakker23986e52011-04-24 08:57:21 +00002744 int ret;
2745 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002746 unsigned int value;
2747 char *p;
2748
2749 p = buf;
2750 n = size;
2751
2752 /* First byte contains first two dots */
2753 if( oid->len > 0 )
2754 {
2755 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2756 SAFE_SNPRINTF();
2757 }
2758
2759 /* TODO: value can overflow in value. */
2760 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002761 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002762 {
2763 value <<= 7;
2764 value += oid->p[i] & 0x7F;
2765
2766 if( !( oid->p[i] & 0x80 ) )
2767 {
2768 /* Last byte */
2769 ret = snprintf( p, n, ".%d", value );
2770 SAFE_SNPRINTF();
2771 value = 0;
2772 }
2773 }
2774
Paul Bakker23986e52011-04-24 08:57:21 +00002775 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002776}
2777
Paul Bakkerd98030e2009-05-02 15:13:40 +00002778/*
2779 * Return an informational string about the CRL.
2780 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002781int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2782 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002783{
Paul Bakker23986e52011-04-24 08:57:21 +00002784 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002785 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002786 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002787 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002788
2789 p = buf;
2790 n = size;
2791
2792 ret = snprintf( p, n, "%sCRL version : %d",
2793 prefix, crl->version );
2794 SAFE_SNPRINTF();
2795
2796 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2797 SAFE_SNPRINTF();
2798 ret = x509parse_dn_gets( p, n, &crl->issuer );
2799 SAFE_SNPRINTF();
2800
2801 ret = snprintf( p, n, "\n%sthis update : " \
2802 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2803 crl->this_update.year, crl->this_update.mon,
2804 crl->this_update.day, crl->this_update.hour,
2805 crl->this_update.min, crl->this_update.sec );
2806 SAFE_SNPRINTF();
2807
2808 ret = snprintf( p, n, "\n%snext update : " \
2809 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2810 crl->next_update.year, crl->next_update.mon,
2811 crl->next_update.day, crl->next_update.hour,
2812 crl->next_update.min, crl->next_update.sec );
2813 SAFE_SNPRINTF();
2814
2815 entry = &crl->entry;
2816
2817 ret = snprintf( p, n, "\n%sRevoked certificates:",
2818 prefix );
2819 SAFE_SNPRINTF();
2820
Paul Bakker9be19372009-07-27 20:21:53 +00002821 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002822 {
2823 ret = snprintf( p, n, "\n%sserial number: ",
2824 prefix );
2825 SAFE_SNPRINTF();
2826
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002827 ret = x509parse_serial_gets( p, n, &entry->serial);
2828 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002829
Paul Bakkerd98030e2009-05-02 15:13:40 +00002830 ret = snprintf( p, n, " revocation date: " \
2831 "%04d-%02d-%02d %02d:%02d:%02d",
2832 entry->revocation_date.year, entry->revocation_date.mon,
2833 entry->revocation_date.day, entry->revocation_date.hour,
2834 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002835 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002836
2837 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 }
2839
Paul Bakkerd98030e2009-05-02 15:13:40 +00002840 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2841 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Paul Bakker27d66162010-03-17 06:56:01 +00002843 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002844 {
2845 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2846 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2847 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2848 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2849 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2850 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2851 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2852 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2853 default: ret = snprintf( p, n, "???" ); break;
2854 }
2855 SAFE_SNPRINTF();
2856
Paul Bakker1e27bb22009-07-19 20:25:25 +00002857 ret = snprintf( p, n, "\n" );
2858 SAFE_SNPRINTF();
2859
Paul Bakker23986e52011-04-24 08:57:21 +00002860 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002861}
2862
2863/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002864 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002866int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002867{
Paul Bakkercce9d772011-11-18 14:26:47 +00002868 int year, mon, day;
2869 int hour, min, sec;
2870
2871#if defined(_WIN32)
2872 SYSTEMTIME st;
2873
2874 GetLocalTime(&st);
2875
2876 year = st.wYear;
2877 mon = st.wMonth;
2878 day = st.wDay;
2879 hour = st.wHour;
2880 min = st.wMinute;
2881 sec = st.wSecond;
2882#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 struct tm *lt;
2884 time_t tt;
2885
2886 tt = time( NULL );
2887 lt = localtime( &tt );
2888
Paul Bakkercce9d772011-11-18 14:26:47 +00002889 year = lt->tm_year + 1900;
2890 mon = lt->tm_mon + 1;
2891 day = lt->tm_mday;
2892 hour = lt->tm_hour;
2893 min = lt->tm_min;
2894 sec = lt->tm_sec;
2895#endif
2896
2897 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002898 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
Paul Bakkercce9d772011-11-18 14:26:47 +00002900 if( year == to->year &&
2901 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002902 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002903
Paul Bakkercce9d772011-11-18 14:26:47 +00002904 if( year == to->year &&
2905 mon == to->mon &&
2906 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +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 )
Paul Bakkerb6194992011-01-16 21:40:22 +00002913 return( 1 );
2914
Paul Bakkercce9d772011-11-18 14:26:47 +00002915 if( year == to->year &&
2916 mon == to->mon &&
2917 day == to->day &&
2918 hour == to->hour &&
2919 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002920 return( 1 );
2921
Paul Bakkercce9d772011-11-18 14:26:47 +00002922 if( year == to->year &&
2923 mon == to->mon &&
2924 day == to->day &&
2925 hour == to->hour &&
2926 min == to->min &&
2927 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002928 return( 1 );
2929
Paul Bakker40ea7de2009-05-03 10:18:48 +00002930 return( 0 );
2931}
2932
2933/*
2934 * Return 1 if the certificate is revoked, or 0 otherwise.
2935 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002936int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002937{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002938 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002939
2940 while( cur != NULL && cur->serial.len != 0 )
2941 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002942 if( crt->serial.len == cur->serial.len &&
2943 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002944 {
2945 if( x509parse_time_expired( &cur->revocation_date ) )
2946 return( 1 );
2947 }
2948
2949 cur = cur->next;
2950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
2952 return( 0 );
2953}
2954
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002955/*
2956 * Wrapper for x509 hashes.
2957 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002958 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002959 */
Paul Bakker23986e52011-04-24 08:57:21 +00002960static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 unsigned char *out )
2962{
2963 switch( alg )
2964 {
Paul Bakker40e46942009-01-03 21:51:57 +00002965#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002966 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002967#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002968#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002969 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002970#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002971#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002972 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002973#endif
2974#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002975 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002976#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002977#if defined(POLARSSL_SHA2_C)
2978 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2979 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2980#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002981#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002982 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2983 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2984#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002985 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002986 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 break;
2988 }
2989}
2990
2991/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002992 * Check that the given certificate is valid accoring to the CRL.
2993 */
2994static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2995 x509_crl *crl_list)
2996{
2997 int flags = 0;
2998 int hash_id;
2999 unsigned char hash[64];
3000
3001 /*
3002 * TODO: What happens if no CRL is present?
3003 * Suggestion: Revocation state should be unknown if no CRL is present.
3004 * For backwards compatibility this is not yet implemented.
3005 */
3006
3007 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
3008 {
3009 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
3010 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3011 crl_list->issuer_raw.len ) != 0 )
3012 {
3013 crl_list = crl_list->next;
3014 continue;
3015 }
3016
3017 /*
3018 * Check if CRL is correctly signed by the trusted CA
3019 */
3020 hash_id = crl_list->sig_alg;
3021
3022 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3023
3024 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
3025 0, hash, crl_list->sig.p ) == 0 )
3026 {
3027 /*
3028 * CRL is not trusted
3029 */
3030 flags |= BADCRL_NOT_TRUSTED;
3031 break;
3032 }
3033
3034 /*
3035 * Check for validity of CRL (Do not drop out)
3036 */
3037 if( x509parse_time_expired( &crl_list->next_update ) )
3038 flags |= BADCRL_EXPIRED;
3039
3040 /*
3041 * Check if certificate is revoked
3042 */
3043 if( x509parse_revoked(crt, crl_list) )
3044 {
3045 flags |= BADCERT_REVOKED;
3046 break;
3047 }
3048
3049 crl_list = crl_list->next;
3050 }
3051 return flags;
3052}
3053
Paul Bakker57b12982012-02-11 17:38:38 +00003054int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003055{
3056 size_t i;
3057 size_t cn_idx = 0;
3058
Paul Bakker57b12982012-02-11 17:38:38 +00003059 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003060 return( 0 );
3061
3062 for( i = 0; i < strlen( cn ); ++i )
3063 {
3064 if( cn[i] == '.' )
3065 {
3066 cn_idx = i;
3067 break;
3068 }
3069 }
3070
3071 if( cn_idx == 0 )
3072 return( 0 );
3073
Paul Bakker57b12982012-02-11 17:38:38 +00003074 if( memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 &&
3075 strlen( cn ) - cn_idx == name->len - 1 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003076 {
3077 return( 1 );
3078 }
3079
3080 return( 0 );
3081}
3082
Paul Bakker76fd75a2011-01-16 21:12:10 +00003083/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 * Verify the certificate validity
3085 */
3086int x509parse_verify( x509_cert *crt,
3087 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003088 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003089 const char *cn, int *flags,
3090 int (*f_vrfy)(void *, x509_cert *, int, int),
3091 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003092{
Paul Bakker23986e52011-04-24 08:57:21 +00003093 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 int hash_id;
3095 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003096 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00003098 unsigned char hash[64];
Paul Bakkera8cd2392012-02-11 16:09:32 +00003099 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Paul Bakker40ea7de2009-05-03 10:18:48 +00003101 *flags = 0;
3102
3103 if( x509parse_time_expired( &crt->valid_to ) )
3104 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
3106 if( cn != NULL )
3107 {
3108 name = &crt->subject;
3109 cn_len = strlen( cn );
3110
Paul Bakker4d2c1242012-05-10 14:12:46 +00003111 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003112 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003113 cur = &crt->subject_alt_names;
3114
3115 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003116 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003117 if( memcmp( cn, cur->buf.p, cn_len ) == 0 &&
3118 cur->buf.len == cn_len )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003119 break;
3120
Paul Bakker4d2c1242012-05-10 14:12:46 +00003121 if( memcmp( cur->buf.p, "*.", 2 ) == 0 &&
3122 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003123 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
Paul Bakker4d2c1242012-05-10 14:12:46 +00003125 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003126 }
3127
3128 if( cur == NULL )
3129 *flags |= BADCERT_CN_MISMATCH;
3130 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003131 else
3132 {
3133 while( name != NULL )
3134 {
3135 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 )
3136 {
3137 if( memcmp( name->val.p, cn, cn_len ) == 0 &&
3138 name->val.len == cn_len )
3139 break;
3140
3141 if( memcmp( name->val.p, "*.", 2 ) == 0 &&
3142 x509_wildcard_verify( cn, &name->val ) )
3143 break;
3144 }
3145
3146 name = name->next;
3147 }
3148
3149 if( name == NULL )
3150 *flags |= BADCERT_CN_MISMATCH;
3151 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003152 }
3153
Paul Bakker5121ce52009-01-03 21:22:43 +00003154 /*
3155 * Iterate upwards in the given cert chain,
3156 * ignoring any upper cert with CA != TRUE.
3157 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003158 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
3160 pathlen = 1;
3161
Paul Bakker76fd75a2011-01-16 21:12:10 +00003162 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003163 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003164 if( parent->ca_istrue == 0 ||
3165 crt->issuer_raw.len != parent->subject_raw.len ||
3166 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 crt->issuer_raw.len ) != 0 )
3168 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003169 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003170 continue;
3171 }
3172
Paul Bakker27d66162010-03-17 06:56:01 +00003173 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
3175 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3176
Paul Bakker76fd75a2011-01-16 21:12:10 +00003177 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3178 crt->sig.p ) != 0 )
3179 *flags |= BADCERT_NOT_TRUSTED;
3180
3181 /* Check trusted CA's CRL for the given crt */
3182 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003183
3184 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00003185 if( NULL != f_vrfy )
3186 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003187 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003188 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003189 else
3190 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003191 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003192 else if( *flags != 0 )
3193 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003194
3195 pathlen++;
3196
Paul Bakker76fd75a2011-01-16 21:12:10 +00003197 crt = parent;
3198 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 }
3200
3201 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003202 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003204 *flags |= BADCERT_NOT_TRUSTED;
3205
Paul Bakker7c6d4a42009-03-28 20:35:47 +00003206 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 {
3208 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
3209 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
3210 crt->issuer_raw.len ) != 0 )
3211 {
3212 trust_ca = trust_ca->next;
3213 continue;
3214 }
3215
3216 if( trust_ca->max_pathlen > 0 &&
3217 trust_ca->max_pathlen < pathlen )
3218 break;
3219
Paul Bakker27d66162010-03-17 06:56:01 +00003220 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
3222 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3223
3224 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3225 0, hash, crt->sig.p ) == 0 )
3226 {
3227 /*
3228 * cert. is signed by a trusted CA
3229 */
3230 *flags &= ~BADCERT_NOT_TRUSTED;
3231 break;
3232 }
3233
3234 trust_ca = trust_ca->next;
3235 }
3236
Paul Bakker76fd75a2011-01-16 21:12:10 +00003237 /* Check trusted CA's CRL for the given crt */
3238 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003239
3240 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003241 if( NULL != f_vrfy )
3242 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003243 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003244 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003245 else
3246 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003247 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003248 else if( *flags != 0 )
3249 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003250
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 return( 0 );
3252}
3253
3254/*
3255 * Unallocate all certificate data
3256 */
3257void x509_free( x509_cert *crt )
3258{
3259 x509_cert *cert_cur = crt;
3260 x509_cert *cert_prv;
3261 x509_name *name_cur;
3262 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003263 x509_sequence *seq_cur;
3264 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003265
3266 if( crt == NULL )
3267 return;
3268
3269 do
3270 {
3271 rsa_free( &cert_cur->rsa );
3272
3273 name_cur = cert_cur->issuer.next;
3274 while( name_cur != NULL )
3275 {
3276 name_prv = name_cur;
3277 name_cur = name_cur->next;
3278 memset( name_prv, 0, sizeof( x509_name ) );
3279 free( name_prv );
3280 }
3281
3282 name_cur = cert_cur->subject.next;
3283 while( name_cur != NULL )
3284 {
3285 name_prv = name_cur;
3286 name_cur = name_cur->next;
3287 memset( name_prv, 0, sizeof( x509_name ) );
3288 free( name_prv );
3289 }
3290
Paul Bakker74111d32011-01-15 16:57:55 +00003291 seq_cur = cert_cur->ext_key_usage.next;
3292 while( seq_cur != NULL )
3293 {
3294 seq_prv = seq_cur;
3295 seq_cur = seq_cur->next;
3296 memset( seq_prv, 0, sizeof( x509_sequence ) );
3297 free( seq_prv );
3298 }
3299
Paul Bakker8afa70d2012-02-11 18:42:45 +00003300 seq_cur = cert_cur->subject_alt_names.next;
3301 while( seq_cur != NULL )
3302 {
3303 seq_prv = seq_cur;
3304 seq_cur = seq_cur->next;
3305 memset( seq_prv, 0, sizeof( x509_sequence ) );
3306 free( seq_prv );
3307 }
3308
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 if( cert_cur->raw.p != NULL )
3310 {
3311 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3312 free( cert_cur->raw.p );
3313 }
3314
3315 cert_cur = cert_cur->next;
3316 }
3317 while( cert_cur != NULL );
3318
3319 cert_cur = crt;
3320 do
3321 {
3322 cert_prv = cert_cur;
3323 cert_cur = cert_cur->next;
3324
3325 memset( cert_prv, 0, sizeof( x509_cert ) );
3326 if( cert_prv != crt )
3327 free( cert_prv );
3328 }
3329 while( cert_cur != NULL );
3330}
3331
Paul Bakkerd98030e2009-05-02 15:13:40 +00003332/*
3333 * Unallocate all CRL data
3334 */
3335void x509_crl_free( x509_crl *crl )
3336{
3337 x509_crl *crl_cur = crl;
3338 x509_crl *crl_prv;
3339 x509_name *name_cur;
3340 x509_name *name_prv;
3341 x509_crl_entry *entry_cur;
3342 x509_crl_entry *entry_prv;
3343
3344 if( crl == NULL )
3345 return;
3346
3347 do
3348 {
3349 name_cur = crl_cur->issuer.next;
3350 while( name_cur != NULL )
3351 {
3352 name_prv = name_cur;
3353 name_cur = name_cur->next;
3354 memset( name_prv, 0, sizeof( x509_name ) );
3355 free( name_prv );
3356 }
3357
3358 entry_cur = crl_cur->entry.next;
3359 while( entry_cur != NULL )
3360 {
3361 entry_prv = entry_cur;
3362 entry_cur = entry_cur->next;
3363 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3364 free( entry_prv );
3365 }
3366
3367 if( crl_cur->raw.p != NULL )
3368 {
3369 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3370 free( crl_cur->raw.p );
3371 }
3372
3373 crl_cur = crl_cur->next;
3374 }
3375 while( crl_cur != NULL );
3376
3377 crl_cur = crl;
3378 do
3379 {
3380 crl_prv = crl_cur;
3381 crl_cur = crl_cur->next;
3382
3383 memset( crl_prv, 0, sizeof( x509_crl ) );
3384 if( crl_prv != crl )
3385 free( crl_prv );
3386 }
3387 while( crl_cur != NULL );
3388}
3389
Paul Bakker40e46942009-01-03 21:51:57 +00003390#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
Paul Bakker40e46942009-01-03 21:51:57 +00003392#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003393
3394/*
3395 * Checkup routine
3396 */
3397int x509_self_test( int verbose )
3398{
Paul Bakker5690efc2011-05-26 13:16:06 +00003399#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003400 int ret;
3401 int flags;
3402 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 x509_cert cacert;
3404 x509_cert clicert;
3405 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003406#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003407 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003408#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003409
3410 if( verbose != 0 )
3411 printf( " X.509 certificate load: " );
3412
3413 memset( &clicert, 0, sizeof( x509_cert ) );
3414
3415 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003416 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 if( ret != 0 )
3418 {
3419 if( verbose != 0 )
3420 printf( "failed\n" );
3421
3422 return( ret );
3423 }
3424
3425 memset( &cacert, 0, sizeof( x509_cert ) );
3426
3427 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003428 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 if( ret != 0 )
3430 {
3431 if( verbose != 0 )
3432 printf( "failed\n" );
3433
3434 return( ret );
3435 }
3436
3437 if( verbose != 0 )
3438 printf( "passed\n X.509 private key load: " );
3439
3440 i = strlen( test_ca_key );
3441 j = strlen( test_ca_pwd );
3442
Paul Bakker66b78b22011-03-25 14:22:50 +00003443 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3444
Paul Bakker5121ce52009-01-03 21:22:43 +00003445 if( ( ret = x509parse_key( &rsa,
3446 (unsigned char *) test_ca_key, i,
3447 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3448 {
3449 if( verbose != 0 )
3450 printf( "failed\n" );
3451
3452 return( ret );
3453 }
3454
3455 if( verbose != 0 )
3456 printf( "passed\n X.509 signature verify: ");
3457
Paul Bakker23986e52011-04-24 08:57:21 +00003458 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 if( ret != 0 )
3460 {
Paul Bakker23986e52011-04-24 08:57:21 +00003461 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 if( verbose != 0 )
3463 printf( "failed\n" );
3464
3465 return( ret );
3466 }
3467
Paul Bakker5690efc2011-05-26 13:16:06 +00003468#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003470 printf( "passed\n X.509 DHM parameter load: " );
3471
3472 i = strlen( test_dhm_params );
3473 j = strlen( test_ca_pwd );
3474
3475 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3476 {
3477 if( verbose != 0 )
3478 printf( "failed\n" );
3479
3480 return( ret );
3481 }
3482
3483 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003485#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003486
3487 x509_free( &cacert );
3488 x509_free( &clicert );
3489 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003490#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003491 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003492#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003493
3494 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003495#else
3496 ((void) verbose);
3497 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3498#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003499}
3500
3501#endif
3502
3503#endif