blob: adbc75a9dd9e0edd1ce358b33912b05deda95df0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, 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 Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000045#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020046#if defined(POLARSSL_PKCS5_C)
47#include "polarssl/pkcs5.h"
48#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020049#if defined(POLARSSL_PKCS12_C)
50#include "polarssl/pkcs12.h"
51#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker6e339b52013-07-03 13:37:05 +020053#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <string.h>
61#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000062#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000063#include <windows.h>
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker335db3f2011-04-25 15:28:35 +000068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000070#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000071#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020072#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000073#include <dirent.h>
74#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000075#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
163/*
164 * AlgorithmIdentifier ::= SEQUENCE {
165 * algorithm OBJECT IDENTIFIER,
166 * parameters ANY DEFINED BY algorithm OPTIONAL }
167 */
168static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000169 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 x509_buf *alg )
171{
Paul Bakker23986e52011-04-24 08:57:21 +0000172 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200174 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000175 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 return( 0 );
178}
179
180/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 * AttributeTypeAndValue ::= SEQUENCE {
182 * type AttributeType,
183 * value AttributeValue }
184 *
185 * AttributeType ::= OBJECT IDENTIFIER
186 *
187 * AttributeValue ::= ANY DEFINED BY AttributeType
188 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000189static int x509_get_attr_type_value( unsigned char **p,
190 const unsigned char *end,
191 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000192{
Paul Bakker23986e52011-04-24 08:57:21 +0000193 int ret;
194 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 x509_buf *oid;
196 x509_buf *val;
197
198 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000200 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 oid = &cur->oid;
203 oid->tag = **p;
204
205 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000206 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 oid->p = *p;
209 *p += oid->len;
210
211 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000212 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000213 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
216 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
217 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000218 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000219 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 val = &cur->val;
222 val->tag = *(*p)++;
223
224 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000225 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 val->p = *p;
228 *p += val->len;
229
230 cur->next = NULL;
231
Paul Bakker400ff6f2011-02-20 10:40:16 +0000232 return( 0 );
233}
234
235/*
236 * RelativeDistinguishedName ::=
237 * SET OF AttributeTypeAndValue
238 *
239 * AttributeTypeAndValue ::= SEQUENCE {
240 * type AttributeType,
241 * value AttributeValue }
242 *
243 * AttributeType ::= OBJECT IDENTIFIER
244 *
245 * AttributeValue ::= ANY DEFINED BY AttributeType
246 */
247static int x509_get_name( unsigned char **p,
248 const unsigned char *end,
249 x509_name *cur )
250{
Paul Bakker23986e52011-04-24 08:57:21 +0000251 int ret;
252 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000253 const unsigned char *end2;
254 x509_name *use;
255
256 if( ( ret = asn1_get_tag( p, end, &len,
257 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000258 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000259
260 end2 = end;
261 end = *p + len;
262 use = cur;
263
264 do
265 {
266 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
267 return( ret );
268
269 if( *p != end )
270 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200271 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000272 sizeof( x509_name ) );
273
274 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000275 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000276
277 memset( use->next, 0, sizeof( x509_name ) );
278
279 use = use->next;
280 }
281 }
282 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284 /*
285 * recurse until end of SEQUENCE is reached
286 */
287 if( *p == end2 )
288 return( 0 );
289
Paul Bakker6e339b52013-07-03 13:37:05 +0200290 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 sizeof( x509_name ) );
292
293 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000294 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
Paul Bakker430ffbe2012-05-01 08:14:20 +0000296 memset( cur->next, 0, sizeof( x509_name ) );
297
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 return( x509_get_name( p, end2, cur->next ) );
299}
300
301/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 * Time ::= CHOICE {
303 * utcTime UTCTime,
304 * generalTime GeneralizedTime }
305 */
Paul Bakker91200182010-02-18 21:26:15 +0000306static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000307 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000308 x509_time *time )
309{
Paul Bakker23986e52011-04-24 08:57:21 +0000310 int ret;
311 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000312 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000313 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000314
Paul Bakker91200182010-02-18 21:26:15 +0000315 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000316 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
317 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000318
Paul Bakker91200182010-02-18 21:26:15 +0000319 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000320
Paul Bakker91200182010-02-18 21:26:15 +0000321 if ( tag == ASN1_UTC_TIME )
322 {
323 (*p)++;
324 ret = asn1_get_len( p, end, &len );
325
326 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000327 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000328
Paul Bakker91200182010-02-18 21:26:15 +0000329 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000330 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
331 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332
Paul Bakker91200182010-02-18 21:26:15 +0000333 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
334 &time->year, &time->mon, &time->day,
335 &time->hour, &time->min, &time->sec ) < 5 )
336 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000337
Paul Bakker400ff6f2011-02-20 10:40:16 +0000338 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000339 time->year += 1900;
340
341 *p += len;
342
343 return( 0 );
344 }
345 else if ( tag == ASN1_GENERALIZED_TIME )
346 {
347 (*p)++;
348 ret = asn1_get_len( p, end, &len );
349
350 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000351 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000352
353 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000354 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
355 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000356
357 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
358 &time->year, &time->mon, &time->day,
359 &time->hour, &time->min, &time->sec ) < 5 )
360 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
361
362 *p += len;
363
364 return( 0 );
365 }
366 else
Paul Bakker9d781402011-05-09 16:17:09 +0000367 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000368}
369
370
371/*
372 * Validity ::= SEQUENCE {
373 * notBefore Time,
374 * notAfter Time }
375 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000376static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000377 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 x509_time *from,
379 x509_time *to )
380{
Paul Bakker23986e52011-04-24 08:57:21 +0000381 int ret;
382 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
384 if( ( ret = asn1_get_tag( p, end, &len,
385 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000386 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388 end = *p + len;
389
Paul Bakker91200182010-02-18 21:26:15 +0000390 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000391 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
Paul Bakker91200182010-02-18 21:26:15 +0000393 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000394 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000397 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000398 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400 return( 0 );
401}
402
403/*
404 * SubjectPublicKeyInfo ::= SEQUENCE {
405 * algorithm AlgorithmIdentifier,
406 * subjectPublicKey BIT STRING }
407 */
408static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000409 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 x509_buf *pk_alg_oid,
411 mpi *N, mpi *E )
412{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200413 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000414 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 unsigned char *end2;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200416 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200418 if( ( ret = asn1_get_alg_null( p, end, pk_alg_oid ) ) != 0 )
419 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
421 /*
422 * only RSA public keys handled at this time
423 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200424 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000425 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000428 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
430 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000431 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000432 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434 end2 = *p + len;
435
436 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000437 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 /*
440 * RSAPublicKey ::= SEQUENCE {
441 * modulus INTEGER, -- n
442 * publicExponent INTEGER -- e
443 * }
444 */
445 if( ( ret = asn1_get_tag( p, end2, &len,
446 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000447 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000450 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000451 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
453 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
454 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000455 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000458 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000459 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 return( 0 );
462}
463
464static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000465 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 x509_buf *sig )
467{
Paul Bakker23986e52011-04-24 08:57:21 +0000468 int ret;
469 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Paul Bakker8afa70d2012-02-11 18:42:45 +0000471 if( ( end - *p ) < 1 )
472 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
473 POLARSSL_ERR_ASN1_OUT_OF_DATA );
474
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 sig->tag = **p;
476
477 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Paul Bakker74111d32011-01-15 16:57:55 +0000480
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000482 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484 sig->len = len;
485 sig->p = *p;
486
487 *p += len;
488
489 return( 0 );
490}
491
492/*
493 * X.509 v2/v3 unique identifier (not parsed)
494 */
495static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000496 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 x509_buf *uid, int n )
498{
499 int ret;
500
501 if( *p == end )
502 return( 0 );
503
504 uid->tag = **p;
505
506 if( ( ret = asn1_get_tag( p, end, &uid->len,
507 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
508 {
Paul Bakker40e46942009-01-03 21:51:57 +0000509 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 return( 0 );
511
512 return( ret );
513 }
514
515 uid->p = *p;
516 *p += uid->len;
517
518 return( 0 );
519}
520
521/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000522 * X.509 Extensions (No parsing of extensions, pointer should
523 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 */
525static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000526 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000527 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000528{
Paul Bakker23986e52011-04-24 08:57:21 +0000529 int ret;
530 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
532 if( *p == end )
533 return( 0 );
534
535 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000536
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000538 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
541 ext->p = *p;
542 end = *p + ext->len;
543
544 /*
545 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
546 *
547 * Extension ::= SEQUENCE {
548 * extnID OBJECT IDENTIFIER,
549 * critical BOOLEAN DEFAULT FALSE,
550 * extnValue OCTET STRING }
551 */
552 if( ( ret = asn1_get_tag( p, end, &len,
553 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000554 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000557 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000558 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Paul Bakkerd98030e2009-05-02 15:13:40 +0000560 return( 0 );
561}
562
563/*
564 * X.509 CRL v2 extensions (no extensions parsed yet.)
565 */
566static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000567 const unsigned char *end,
568 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000569{
Paul Bakker23986e52011-04-24 08:57:21 +0000570 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000571 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000572
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000573 /* Get explicit tag */
574 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000575 {
576 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
577 return( 0 );
578
579 return( ret );
580 }
581
582 while( *p < end )
583 {
584 if( ( ret = asn1_get_tag( p, end, &len,
585 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000586 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000587
588 *p += len;
589 }
590
591 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000592 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000593 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
594
595 return( 0 );
596}
597
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000598/*
599 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
600 */
601static int x509_get_crl_entry_ext( unsigned char **p,
602 const unsigned char *end,
603 x509_buf *ext )
604{
605 int ret;
606 size_t len = 0;
607
608 /* OPTIONAL */
609 if (end <= *p)
610 return( 0 );
611
612 ext->tag = **p;
613 ext->p = *p;
614
615 /*
616 * Get CRL-entry extension sequence header
617 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
618 */
619 if( ( ret = asn1_get_tag( p, end, &ext->len,
620 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
621 {
622 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
623 {
624 ext->p = NULL;
625 return( 0 );
626 }
627 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
628 }
629
630 end = *p + ext->len;
631
632 if( end != *p + ext->len )
633 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
634 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
635
636 while( *p < end )
637 {
638 if( ( ret = asn1_get_tag( p, end, &len,
639 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
640 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
641
642 *p += len;
643 }
644
645 if( *p != end )
646 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
647 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
648
649 return( 0 );
650}
651
Paul Bakker74111d32011-01-15 16:57:55 +0000652static int x509_get_basic_constraints( unsigned char **p,
653 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000654 int *ca_istrue,
655 int *max_pathlen )
656{
Paul Bakker23986e52011-04-24 08:57:21 +0000657 int ret;
658 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000659
660 /*
661 * BasicConstraints ::= SEQUENCE {
662 * cA BOOLEAN DEFAULT FALSE,
663 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
664 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000665 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000666 *max_pathlen = 0; /* endless */
667
668 if( ( ret = asn1_get_tag( p, end, &len,
669 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000670 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000671
672 if( *p == end )
673 return 0;
674
Paul Bakker3cccddb2011-01-16 21:46:31 +0000675 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000676 {
677 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000678 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000679
680 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000681 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000682
Paul Bakker3cccddb2011-01-16 21:46:31 +0000683 if( *ca_istrue != 0 )
684 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000685 }
686
687 if( *p == end )
688 return 0;
689
690 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000691 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000692
693 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000694 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000695 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
696
697 (*max_pathlen)++;
698
Paul Bakker74111d32011-01-15 16:57:55 +0000699 return 0;
700}
701
702static int x509_get_ns_cert_type( unsigned char **p,
703 const unsigned char *end,
704 unsigned char *ns_cert_type)
705{
706 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000707 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000708
709 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000710 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000711
712 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000713 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000714 POLARSSL_ERR_ASN1_INVALID_LENGTH );
715
716 /* Get actual bitstring */
717 *ns_cert_type = *bs.p;
718 return 0;
719}
720
721static int x509_get_key_usage( unsigned char **p,
722 const unsigned char *end,
723 unsigned char *key_usage)
724{
725 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000726 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000727
728 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000729 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000730
Paul Bakker94a67962012-08-23 13:03:52 +0000731 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000732 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000733 POLARSSL_ERR_ASN1_INVALID_LENGTH );
734
735 /* Get actual bitstring */
736 *key_usage = *bs.p;
737 return 0;
738}
739
Paul Bakkerd98030e2009-05-02 15:13:40 +0000740/*
Paul Bakker74111d32011-01-15 16:57:55 +0000741 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
742 *
743 * KeyPurposeId ::= OBJECT IDENTIFIER
744 */
745static int x509_get_ext_key_usage( unsigned char **p,
746 const unsigned char *end,
747 x509_sequence *ext_key_usage)
748{
749 int ret;
750
751 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000752 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000753
754 /* Sequence length must be >= 1 */
755 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000756 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000757 POLARSSL_ERR_ASN1_INVALID_LENGTH );
758
759 return 0;
760}
761
762/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000763 * SubjectAltName ::= GeneralNames
764 *
765 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
766 *
767 * GeneralName ::= CHOICE {
768 * otherName [0] OtherName,
769 * rfc822Name [1] IA5String,
770 * dNSName [2] IA5String,
771 * x400Address [3] ORAddress,
772 * directoryName [4] Name,
773 * ediPartyName [5] EDIPartyName,
774 * uniformResourceIdentifier [6] IA5String,
775 * iPAddress [7] OCTET STRING,
776 * registeredID [8] OBJECT IDENTIFIER }
777 *
778 * OtherName ::= SEQUENCE {
779 * type-id OBJECT IDENTIFIER,
780 * value [0] EXPLICIT ANY DEFINED BY type-id }
781 *
782 * EDIPartyName ::= SEQUENCE {
783 * nameAssigner [0] DirectoryString OPTIONAL,
784 * partyName [1] DirectoryString }
785 *
786 * NOTE: PolarSSL only parses and uses dNSName at this point.
787 */
788static int x509_get_subject_alt_name( unsigned char **p,
789 const unsigned char *end,
790 x509_sequence *subject_alt_name )
791{
792 int ret;
793 size_t len, tag_len;
794 asn1_buf *buf;
795 unsigned char tag;
796 asn1_sequence *cur = subject_alt_name;
797
798 /* Get main sequence tag */
799 if( ( ret = asn1_get_tag( p, end, &len,
800 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
801 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
802
803 if( *p + len != end )
804 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
805 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
806
807 while( *p < end )
808 {
809 if( ( end - *p ) < 1 )
810 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
811 POLARSSL_ERR_ASN1_OUT_OF_DATA );
812
813 tag = **p;
814 (*p)++;
815 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
816 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
817
818 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
819 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
820 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
821
822 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
823 {
824 *p += tag_len;
825 continue;
826 }
827
828 buf = &(cur->buf);
829 buf->tag = tag;
830 buf->p = *p;
831 buf->len = tag_len;
832 *p += buf->len;
833
834 /* Allocate and assign next pointer */
835 if (*p < end)
836 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200837 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000838 sizeof( asn1_sequence ) );
839
840 if( cur->next == NULL )
841 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
842 POLARSSL_ERR_ASN1_MALLOC_FAILED );
843
Paul Bakker535e97d2012-08-23 10:49:55 +0000844 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000845 cur = cur->next;
846 }
847 }
848
849 /* Set final sequence entry's next pointer to NULL */
850 cur->next = NULL;
851
852 if( *p != end )
853 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
854 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
855
856 return( 0 );
857}
858
859/*
Paul Bakker74111d32011-01-15 16:57:55 +0000860 * X.509 v3 extensions
861 *
862 * TODO: Perform all of the basic constraints tests required by the RFC
863 * TODO: Set values for undetected extensions to a sane default?
864 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000865 */
866static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000867 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000868 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000869{
Paul Bakker23986e52011-04-24 08:57:21 +0000870 int ret;
871 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000872 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000873
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000874 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000875 {
876 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
877 return( 0 );
878
879 return( ret );
880 }
881
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 while( *p < end )
883 {
Paul Bakker74111d32011-01-15 16:57:55 +0000884 /*
885 * Extension ::= SEQUENCE {
886 * extnID OBJECT IDENTIFIER,
887 * critical BOOLEAN DEFAULT FALSE,
888 * extnValue OCTET STRING }
889 */
890 x509_buf extn_oid = {0, 0, NULL};
891 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200892 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000893
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 if( ( ret = asn1_get_tag( p, end, &len,
895 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000896 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000897
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000898 end_ext_data = *p + len;
899
Paul Bakker74111d32011-01-15 16:57:55 +0000900 /* Get extension ID */
901 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Paul Bakker74111d32011-01-15 16:57:55 +0000903 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000904 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Paul Bakker74111d32011-01-15 16:57:55 +0000906 extn_oid.p = *p;
907 *p += extn_oid.len;
908
909 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000910 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000911 POLARSSL_ERR_ASN1_OUT_OF_DATA );
912
913 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000914 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000915 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000916 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
Paul Bakker74111d32011-01-15 16:57:55 +0000918 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000919 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000921 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000923 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000924
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000925 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000926 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000927 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Paul Bakker74111d32011-01-15 16:57:55 +0000929 /*
930 * Detect supported extensions
931 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200932 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
933
934 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000935 {
936 /* No parser found, skip extension */
937 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Paul Bakker5c721f92011-07-27 16:51:09 +0000939#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000940 if( is_critical )
941 {
942 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000943 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000944 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
945 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000946#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +0200947 continue;
948 }
949
950 crt->ext_types |= ext_type;
951
952 switch( ext_type )
953 {
954 case EXT_BASIC_CONSTRAINTS:
955 /* Parse basic constraints */
956 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
957 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
958 return ( ret );
959 break;
960
961 case EXT_KEY_USAGE:
962 /* Parse key usage */
963 if( ( ret = x509_get_key_usage( p, end_ext_octet,
964 &crt->key_usage ) ) != 0 )
965 return ( ret );
966 break;
967
968 case EXT_EXTENDED_KEY_USAGE:
969 /* Parse extended key usage */
970 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
971 &crt->ext_key_usage ) ) != 0 )
972 return ( ret );
973 break;
974
975 case EXT_SUBJECT_ALT_NAME:
976 /* Parse subject alt name */
977 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
978 &crt->subject_alt_names ) ) != 0 )
979 return ( ret );
980 break;
981
982 case EXT_NS_CERT_TYPE:
983 /* Parse netscape certificate type */
984 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
985 &crt->ns_cert_type ) ) != 0 )
986 return ( ret );
987 break;
988
989 default:
990 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +0000991 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 }
993
994 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000995 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000996 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 return( 0 );
999}
1000
1001/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001002 * X.509 CRL Entries
1003 */
1004static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001005 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001006 x509_crl_entry *entry )
1007{
Paul Bakker23986e52011-04-24 08:57:21 +00001008 int ret;
1009 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001010 x509_crl_entry *cur_entry = entry;
1011
1012 if( *p == end )
1013 return( 0 );
1014
Paul Bakker9be19372009-07-27 20:21:53 +00001015 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001016 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1017 {
1018 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1019 return( 0 );
1020
1021 return( ret );
1022 }
1023
Paul Bakker9be19372009-07-27 20:21:53 +00001024 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001025
1026 while( *p < end )
1027 {
Paul Bakker23986e52011-04-24 08:57:21 +00001028 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001029 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001030
1031 if( ( ret = asn1_get_tag( p, end, &len2,
1032 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1033 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001034 return( ret );
1035 }
1036
Paul Bakker9be19372009-07-27 20:21:53 +00001037 cur_entry->raw.tag = **p;
1038 cur_entry->raw.p = *p;
1039 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001040 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001041
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001042 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001043 return( ret );
1044
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001045 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001046 return( ret );
1047
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001048 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001049 return( ret );
1050
Paul Bakker74111d32011-01-15 16:57:55 +00001051 if ( *p < end )
1052 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001053 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001054
1055 if( cur_entry->next == NULL )
1056 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1057
Paul Bakkerd98030e2009-05-02 15:13:40 +00001058 cur_entry = cur_entry->next;
1059 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1060 }
1061 }
1062
1063 return( 0 );
1064}
1065
Paul Bakkerc70b9822013-04-07 22:00:46 +02001066static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1067 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001068{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001069 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001070
Paul Bakkerc70b9822013-04-07 22:00:46 +02001071 if( ret != 0 )
1072 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001073
Paul Bakkerc70b9822013-04-07 22:00:46 +02001074 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001075}
1076
Paul Bakkerd98030e2009-05-02 15:13:40 +00001077/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001078 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001080static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1081 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001082{
Paul Bakker23986e52011-04-24 08:57:21 +00001083 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001084 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001085 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
Paul Bakker320a4b52009-03-28 18:52:39 +00001087 /*
1088 * Check for valid input
1089 */
1090 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001091 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001092
Paul Bakker6e339b52013-07-03 13:37:05 +02001093 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001094
1095 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001096 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001097
1098 memcpy( p, buf, buflen );
1099
1100 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
1102 crt->raw.p = p;
1103 crt->raw.len = len;
1104 end = p + len;
1105
1106 /*
1107 * Certificate ::= SEQUENCE {
1108 * tbsCertificate TBSCertificate,
1109 * signatureAlgorithm AlgorithmIdentifier,
1110 * signatureValue BIT STRING }
1111 */
1112 if( ( ret = asn1_get_tag( &p, end, &len,
1113 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1114 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001115 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001116 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 }
1118
Paul Bakkerb00ca422012-09-25 12:10:00 +00001119 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001121 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001122 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001123 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001125 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001126
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 /*
1128 * TBSCertificate ::= SEQUENCE {
1129 */
1130 crt->tbs.p = p;
1131
1132 if( ( ret = asn1_get_tag( &p, end, &len,
1133 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1134 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001135 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001136 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 }
1138
1139 end = p + len;
1140 crt->tbs.len = end - crt->tbs.p;
1141
1142 /*
1143 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1144 *
1145 * CertificateSerialNumber ::= INTEGER
1146 *
1147 * signature AlgorithmIdentifier
1148 */
1149 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1150 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1151 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1152 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001153 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 return( ret );
1155 }
1156
1157 crt->version++;
1158
1159 if( crt->version > 3 )
1160 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001161 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001162 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 }
1164
Paul Bakkerc70b9822013-04-07 22:00:46 +02001165 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1166 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001168 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001169 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 }
1171
1172 /*
1173 * issuer Name
1174 */
1175 crt->issuer_raw.p = p;
1176
1177 if( ( ret = asn1_get_tag( &p, end, &len,
1178 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1179 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001180 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001181 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 }
1183
1184 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1185 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001186 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 return( ret );
1188 }
1189
1190 crt->issuer_raw.len = p - crt->issuer_raw.p;
1191
1192 /*
1193 * Validity ::= SEQUENCE {
1194 * notBefore Time,
1195 * notAfter Time }
1196 *
1197 */
1198 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1199 &crt->valid_to ) ) != 0 )
1200 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001201 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 return( ret );
1203 }
1204
1205 /*
1206 * subject Name
1207 */
1208 crt->subject_raw.p = p;
1209
1210 if( ( ret = asn1_get_tag( &p, end, &len,
1211 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1212 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001213 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001214 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 }
1216
Paul Bakkercefb3962012-06-27 11:51:09 +00001217 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001219 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 return( ret );
1221 }
1222
1223 crt->subject_raw.len = p - crt->subject_raw.p;
1224
1225 /*
1226 * SubjectPublicKeyInfo ::= SEQUENCE
1227 * algorithm AlgorithmIdentifier,
1228 * subjectPublicKey BIT STRING }
1229 */
1230 if( ( ret = asn1_get_tag( &p, end, &len,
1231 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1232 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001233 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001234 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 }
1236
1237 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1238 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1239 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001240 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 return( ret );
1242 }
1243
1244 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1245 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001246 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 return( ret );
1248 }
1249
1250 crt->rsa.len = mpi_size( &crt->rsa.N );
1251
1252 /*
1253 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1254 * -- If present, version shall be v2 or v3
1255 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1256 * -- If present, version shall be v2 or v3
1257 * extensions [3] EXPLICIT Extensions OPTIONAL
1258 * -- If present, version shall be v3
1259 */
1260 if( crt->version == 2 || crt->version == 3 )
1261 {
1262 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1263 if( ret != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 return( ret );
1267 }
1268 }
1269
1270 if( crt->version == 2 || crt->version == 3 )
1271 {
1272 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1273 if( ret != 0 )
1274 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001275 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 return( ret );
1277 }
1278 }
1279
1280 if( crt->version == 3 )
1281 {
Paul Bakker74111d32011-01-15 16:57:55 +00001282 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 if( ret != 0 )
1284 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001285 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 return( ret );
1287 }
1288 }
1289
1290 if( p != end )
1291 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001292 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001293 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001294 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 }
1296
Paul Bakkerb00ca422012-09-25 12:10:00 +00001297 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
1299 /*
1300 * signatureAlgorithm AlgorithmIdentifier,
1301 * signatureValue BIT STRING
1302 */
1303 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1304 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001305 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 return( ret );
1307 }
1308
Paul Bakker535e97d2012-08-23 10:49:55 +00001309 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1310 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001313 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 }
1315
1316 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1317 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001318 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 return( ret );
1320 }
1321
1322 if( p != end )
1323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001325 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001326 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 }
1328
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001329 return( 0 );
1330}
1331
1332/*
Paul Bakker42c65812013-06-24 19:21:59 +02001333 * Parse one X.509 certificate in DER format from a buffer and add them to a
1334 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001335 */
Paul Bakker42c65812013-06-24 19:21:59 +02001336int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001337{
Paul Bakker42c65812013-06-24 19:21:59 +02001338 int ret;
1339 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001340
1341 /*
1342 * Check for valid input
1343 */
1344 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001345 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001346
1347 while( crt->version != 0 && crt->next != NULL )
1348 {
1349 prev = crt;
1350 crt = crt->next;
1351 }
1352
1353 /*
1354 * Add new certificate on the end of the chain if needed.
1355 */
1356 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001357 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001358 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001359
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001361 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001362
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001363 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001364 crt = crt->next;
1365 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Paul Bakker42c65812013-06-24 19:21:59 +02001368 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1369 {
1370 if( prev )
1371 prev->next = NULL;
1372
1373 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001374 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001375
1376 return( ret );
1377 }
1378
1379 return( 0 );
1380}
1381
1382/*
1383 * Parse one or more PEM certificates from a buffer and add them to the chained list
1384 */
1385int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1386{
1387 int ret, success = 0, first_error = 0, total_failed = 0;
1388 int buf_format = X509_FORMAT_DER;
1389
1390 /*
1391 * Check for valid input
1392 */
1393 if( chain == NULL || buf == NULL )
1394 return( POLARSSL_ERR_X509_INVALID_INPUT );
1395
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001396 /*
1397 * Determine buffer content. Buffer contains either one DER certificate or
1398 * one or more PEM certificates.
1399 */
1400#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001401 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001402 buf_format = X509_FORMAT_PEM;
1403#endif
1404
1405 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001406 return x509parse_crt_der( chain, buf, buflen );
1407
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001408#if defined(POLARSSL_PEM_C)
1409 if( buf_format == X509_FORMAT_PEM )
1410 {
1411 pem_context pem;
1412
1413 while( buflen > 0 )
1414 {
1415 size_t use_len;
1416 pem_init( &pem );
1417
1418 ret = pem_read_buffer( &pem,
1419 "-----BEGIN CERTIFICATE-----",
1420 "-----END CERTIFICATE-----",
1421 buf, NULL, 0, &use_len );
1422
1423 if( ret == 0 )
1424 {
1425 /*
1426 * Was PEM encoded
1427 */
1428 buflen -= use_len;
1429 buf += use_len;
1430 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001431 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1432 {
1433 return( ret );
1434 }
Paul Bakker00b28602013-06-24 13:02:41 +02001435 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001436 {
1437 pem_free( &pem );
1438
Paul Bakker5ed3b342013-06-24 19:05:46 +02001439 /*
1440 * PEM header and footer were found
1441 */
1442 buflen -= use_len;
1443 buf += use_len;
1444
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001445 if( first_error == 0 )
1446 first_error = ret;
1447
1448 continue;
1449 }
1450 else
1451 break;
1452
Paul Bakker42c65812013-06-24 19:21:59 +02001453 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001454
1455 pem_free( &pem );
1456
1457 if( ret != 0 )
1458 {
1459 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001460 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001461 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001462 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001463 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001464
1465 if( first_error == 0 )
1466 first_error = ret;
1467
Paul Bakker42c65812013-06-24 19:21:59 +02001468 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001469 continue;
1470 }
1471
1472 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001473 }
1474 }
1475#endif
1476
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001477 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001478 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001479 else if( first_error )
1480 return( first_error );
1481 else
1482 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001483}
1484
1485/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001486 * Parse one or more CRLs and add them to the chained list
1487 */
Paul Bakker23986e52011-04-24 08:57:21 +00001488int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001489{
Paul Bakker23986e52011-04-24 08:57:21 +00001490 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001491 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001492 unsigned char *p, *end;
1493 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001494#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001495 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001496 pem_context pem;
1497#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001498
1499 crl = chain;
1500
1501 /*
1502 * Check for valid input
1503 */
1504 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001505 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001506
1507 while( crl->version != 0 && crl->next != NULL )
1508 crl = crl->next;
1509
1510 /*
1511 * Add new CRL on the end of the chain if needed.
1512 */
1513 if ( crl->version != 0 && crl->next == NULL)
1514 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001515 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001516
Paul Bakker7d06ad22009-05-02 15:53:56 +00001517 if( crl->next == NULL )
1518 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001519 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001520 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001521 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001522
Paul Bakker7d06ad22009-05-02 15:53:56 +00001523 crl = crl->next;
1524 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001525 }
1526
Paul Bakker96743fc2011-02-12 14:30:57 +00001527#if defined(POLARSSL_PEM_C)
1528 pem_init( &pem );
1529 ret = pem_read_buffer( &pem,
1530 "-----BEGIN X509 CRL-----",
1531 "-----END X509 CRL-----",
1532 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001533
Paul Bakker96743fc2011-02-12 14:30:57 +00001534 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001535 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001536 /*
1537 * Was PEM encoded
1538 */
1539 buflen -= use_len;
1540 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001541
1542 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001543 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001544 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001545 p = pem.buf;
1546 pem.buf = NULL;
1547 len = pem.buflen;
1548 pem_free( &pem );
1549 }
Paul Bakker00b28602013-06-24 13:02:41 +02001550 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001551 {
1552 pem_free( &pem );
1553 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001554 }
1555 else
1556 {
1557 /*
1558 * nope, copy the raw DER data
1559 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001560 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001561
1562 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001563 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001564
1565 memcpy( p, buf, buflen );
1566
1567 buflen = 0;
1568 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001569#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001570 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001571
1572 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001573 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001574
1575 memcpy( p, buf, buflen );
1576
1577 buflen = 0;
1578#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001579
1580 crl->raw.p = p;
1581 crl->raw.len = len;
1582 end = p + len;
1583
1584 /*
1585 * CertificateList ::= SEQUENCE {
1586 * tbsCertList TBSCertList,
1587 * signatureAlgorithm AlgorithmIdentifier,
1588 * signatureValue BIT STRING }
1589 */
1590 if( ( ret = asn1_get_tag( &p, end, &len,
1591 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1592 {
1593 x509_crl_free( crl );
1594 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1595 }
1596
Paul Bakker23986e52011-04-24 08:57:21 +00001597 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001598 {
1599 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001600 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001601 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1602 }
1603
1604 /*
1605 * TBSCertList ::= SEQUENCE {
1606 */
1607 crl->tbs.p = p;
1608
1609 if( ( ret = asn1_get_tag( &p, end, &len,
1610 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1611 {
1612 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001613 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001614 }
1615
1616 end = p + len;
1617 crl->tbs.len = end - crl->tbs.p;
1618
1619 /*
1620 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1621 * -- if present, MUST be v2
1622 *
1623 * signature AlgorithmIdentifier
1624 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001625 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001626 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1627 {
1628 x509_crl_free( crl );
1629 return( ret );
1630 }
1631
1632 crl->version++;
1633
1634 if( crl->version > 2 )
1635 {
1636 x509_crl_free( crl );
1637 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1638 }
1639
Paul Bakkerc70b9822013-04-07 22:00:46 +02001640 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1641 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001642 {
1643 x509_crl_free( crl );
1644 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1645 }
1646
1647 /*
1648 * issuer Name
1649 */
1650 crl->issuer_raw.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 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1660 {
1661 x509_crl_free( crl );
1662 return( ret );
1663 }
1664
1665 crl->issuer_raw.len = p - crl->issuer_raw.p;
1666
1667 /*
1668 * thisUpdate Time
1669 * nextUpdate Time OPTIONAL
1670 */
Paul Bakker91200182010-02-18 21:26:15 +00001671 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 {
1673 x509_crl_free( crl );
1674 return( ret );
1675 }
1676
Paul Bakker91200182010-02-18 21:26:15 +00001677 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678 {
Paul Bakker9d781402011-05-09 16:17:09 +00001679 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001680 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001681 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001682 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001683 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001684 x509_crl_free( crl );
1685 return( ret );
1686 }
1687 }
1688
1689 /*
1690 * revokedCertificates SEQUENCE OF SEQUENCE {
1691 * userCertificate CertificateSerialNumber,
1692 * revocationDate Time,
1693 * crlEntryExtensions Extensions OPTIONAL
1694 * -- if present, MUST be v2
1695 * } OPTIONAL
1696 */
1697 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1698 {
1699 x509_crl_free( crl );
1700 return( ret );
1701 }
1702
1703 /*
1704 * crlExtensions EXPLICIT Extensions OPTIONAL
1705 * -- if present, MUST be v2
1706 */
1707 if( crl->version == 2 )
1708 {
1709 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1710
1711 if( ret != 0 )
1712 {
1713 x509_crl_free( crl );
1714 return( ret );
1715 }
1716 }
1717
1718 if( p != end )
1719 {
1720 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001721 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001722 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1723 }
1724
1725 end = crl->raw.p + crl->raw.len;
1726
1727 /*
1728 * signatureAlgorithm AlgorithmIdentifier,
1729 * signatureValue BIT STRING
1730 */
1731 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1732 {
1733 x509_crl_free( crl );
1734 return( ret );
1735 }
1736
Paul Bakker535e97d2012-08-23 10:49:55 +00001737 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1738 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001739 {
1740 x509_crl_free( crl );
1741 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1742 }
1743
1744 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1745 {
1746 x509_crl_free( crl );
1747 return( ret );
1748 }
1749
1750 if( p != end )
1751 {
1752 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001753 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001754 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1755 }
1756
1757 if( buflen > 0 )
1758 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001759 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001760
Paul Bakker7d06ad22009-05-02 15:53:56 +00001761 if( crl->next == NULL )
1762 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001763 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001764 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001765 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001766
Paul Bakker7d06ad22009-05-02 15:53:56 +00001767 crl = crl->next;
1768 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001769
1770 return( x509parse_crl( crl, buf, buflen ) );
1771 }
1772
1773 return( 0 );
1774}
1775
Paul Bakker335db3f2011-04-25 15:28:35 +00001776#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001777/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001778 * Load all data from a file into a given buffer.
1779 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001780static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001781{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001782 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001783
Paul Bakkerd98030e2009-05-02 15:13:40 +00001784 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001785 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001786
Paul Bakkerd98030e2009-05-02 15:13:40 +00001787 fseek( f, 0, SEEK_END );
1788 *n = (size_t) ftell( f );
1789 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001790
Paul Bakker6e339b52013-07-03 13:37:05 +02001791 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001792 {
1793 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001794 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001795 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001796
Paul Bakkerd98030e2009-05-02 15:13:40 +00001797 if( fread( *buf, 1, *n, f ) != *n )
1798 {
1799 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001800 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001801 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001802 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001803
Paul Bakkerd98030e2009-05-02 15:13:40 +00001804 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001805
Paul Bakkerd98030e2009-05-02 15:13:40 +00001806 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001807
Paul Bakkerd98030e2009-05-02 15:13:40 +00001808 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001809}
1810
1811/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001812 * Load one or more certificates and add them to the chained list
1813 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001814int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001815{
1816 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001817 size_t n;
1818 unsigned char *buf;
1819
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001820 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001821 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Paul Bakker69e095c2011-12-10 21:55:01 +00001823 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
1825 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001826 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001827
1828 return( ret );
1829}
1830
Paul Bakker8d914582012-06-04 12:46:42 +00001831int x509parse_crtpath( x509_cert *chain, const char *path )
1832{
1833 int ret = 0;
1834#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001835 int w_ret;
1836 WCHAR szDir[MAX_PATH];
1837 char filename[MAX_PATH];
1838 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001839 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001840
Paul Bakker97872ac2012-11-02 12:53:26 +00001841 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001842 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001843
1844 if( len > MAX_PATH - 3 )
1845 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001846
Paul Bakker3338b792012-10-01 21:13:10 +00001847 memset( szDir, 0, sizeof(szDir) );
1848 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001849 memcpy( filename, path, len );
1850 filename[len++] = '\\';
1851 p = filename + len;
1852 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001853
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001854 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001855
Paul Bakker97872ac2012-11-02 12:53:26 +00001856 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001857 if (hFind == INVALID_HANDLE_VALUE)
1858 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1859
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001860 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001861 do
1862 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001863 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001864
Paul Bakkere4791f32012-06-04 21:29:15 +00001865 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001866 continue;
1867
Paul Bakker3338b792012-10-01 21:13:10 +00001868 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1869 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001870 p, len - 1,
1871 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001872
Paul Bakker3338b792012-10-01 21:13:10 +00001873 w_ret = x509parse_crtfile( chain, filename );
1874 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001875 ret++;
1876 else
1877 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001878 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001879 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001880
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001881 if (GetLastError() != ERROR_NO_MORE_FILES)
1882 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001883
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001884cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001885 FindClose( hFind );
1886#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001887 int t_ret, i;
1888 struct stat sb;
1889 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001890 char entry_name[255];
1891 DIR *dir = opendir( path );
1892
1893 if( dir == NULL)
1894 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1895
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001896 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001897 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001898 if( result == NULL )
1899 break;
1900
1901 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1902
1903 i = stat( entry_name, &sb );
1904
1905 if( i == -1 )
1906 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1907
1908 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00001909 continue;
1910
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001911 // Ignore parse errors
1912 //
Paul Bakker8d914582012-06-04 12:46:42 +00001913 t_ret = x509parse_crtfile( chain, entry_name );
1914 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001915 ret++;
1916 else
1917 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001918 }
1919 closedir( dir );
1920#endif
1921
1922 return( ret );
1923}
1924
Paul Bakkerd98030e2009-05-02 15:13:40 +00001925/*
1926 * Load one or more CRLs and add them to the chained list
1927 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001928int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001929{
1930 int ret;
1931 size_t n;
1932 unsigned char *buf;
1933
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001934 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001935 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001936
Paul Bakker27fdf462011-06-09 13:55:13 +00001937 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001938
1939 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001940 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001941
1942 return( ret );
1943}
1944
Paul Bakker5121ce52009-01-03 21:22:43 +00001945/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001946 * Load and parse a private RSA key
1947 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001948int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00001949{
1950 int ret;
1951 size_t n;
1952 unsigned char *buf;
1953
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001954 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001955 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001956
1957 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001958 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001959 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001960 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001961 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00001962
1963 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001964 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00001965
1966 return( ret );
1967}
1968
1969/*
1970 * Load and parse a public RSA key
1971 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001972int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00001973{
1974 int ret;
1975 size_t n;
1976 unsigned char *buf;
1977
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001978 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001979 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001980
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001981 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001982
1983 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001984 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00001985
1986 return( ret );
1987}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02001988
1989#if defined(POLARSSL_ECP_C)
1990/*
1991 * Load and parse a private EC key
1992 */
1993int x509parse_keyfile_ec( ecp_keypair *eckey,
1994 const char *path, const char *pwd )
1995{
1996 int ret;
1997 size_t n;
1998 unsigned char *buf;
1999
2000 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2001 return( ret );
2002
2003 if( pwd == NULL )
2004 ret = x509parse_key_ec( eckey, buf, n, NULL, 0 );
2005 else
2006 ret = x509parse_key_ec( eckey, buf, n,
2007 (const unsigned char *) pwd, strlen( pwd ) );
2008
2009 memset( buf, 0, n + 1 );
2010 free( buf );
2011
2012 return( ret );
2013}
2014
2015/*
2016 * Load and parse a public EC key
2017 */
2018int x509parse_public_keyfile_ec( ecp_keypair *eckey, const char *path )
2019{
2020 int ret;
2021 size_t n;
2022 unsigned char *buf;
2023
2024 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2025 return( ret );
2026
2027 ret = x509parse_public_key_ec( eckey, buf, n );
2028
2029 memset( buf, 0, n + 1 );
2030 free( buf );
2031
2032 return( ret );
2033}
2034#endif /* defined(POLARSSL_ECP_C) */
Paul Bakker335db3f2011-04-25 15:28:35 +00002035#endif /* POLARSSL_FS_IO */
2036
2037/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002038 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002040static int x509parse_key_pkcs1_der( rsa_context *rsa,
2041 const unsigned char *key,
2042 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002043{
Paul Bakker23986e52011-04-24 08:57:21 +00002044 int ret;
2045 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002047
Paul Bakker96743fc2011-02-12 14:30:57 +00002048 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002049 end = p + keylen;
2050
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002052 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002053 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 * RSAPrivateKey ::= SEQUENCE {
2055 * version Version,
2056 * modulus INTEGER, -- n
2057 * publicExponent INTEGER, -- e
2058 * privateExponent INTEGER, -- d
2059 * prime1 INTEGER, -- p
2060 * prime2 INTEGER, -- q
2061 * exponent1 INTEGER, -- d mod (p-1)
2062 * exponent2 INTEGER, -- d mod (q-1)
2063 * coefficient INTEGER, -- (inverse of q) mod p
2064 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2065 * }
2066 */
2067 if( ( ret = asn1_get_tag( &p, end, &len,
2068 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2069 {
Paul Bakker9d781402011-05-09 16:17:09 +00002070 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 }
2072
2073 end = p + len;
2074
2075 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2076 {
Paul Bakker9d781402011-05-09 16:17:09 +00002077 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 }
2079
2080 if( rsa->ver != 0 )
2081 {
Paul Bakker9d781402011-05-09 16:17:09 +00002082 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 }
2084
2085 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2086 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2087 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2088 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2089 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2090 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2091 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2092 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2093 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002095 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 }
2097
2098 rsa->len = mpi_size( &rsa->N );
2099
2100 if( p != end )
2101 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002103 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002104 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
2106
2107 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2108 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 rsa_free( rsa );
2110 return( ret );
2111 }
2112
Paul Bakkere2f50402013-06-24 19:00:59 +02002113 return( 0 );
2114}
2115
2116/*
2117 * Parse an unencrypted PKCS#8 encoded private RSA key
2118 */
2119static int x509parse_key_pkcs8_unencrypted_der(
2120 rsa_context *rsa,
2121 const unsigned char *key,
2122 size_t keylen )
2123{
2124 int ret;
2125 size_t len;
2126 unsigned char *p, *end;
2127 x509_buf pk_alg_oid;
2128 pk_type_t pk_alg = POLARSSL_PK_NONE;
2129
2130 p = (unsigned char *) key;
2131 end = p + keylen;
2132
2133 /*
2134 * This function parses the PrivatKeyInfo object (PKCS#8)
2135 *
2136 * PrivateKeyInfo ::= SEQUENCE {
2137 * version Version,
2138 * algorithm AlgorithmIdentifier,
2139 * PrivateKey BIT STRING
2140 * }
2141 *
2142 * AlgorithmIdentifier ::= SEQUENCE {
2143 * algorithm OBJECT IDENTIFIER,
2144 * parameters ANY DEFINED BY algorithm OPTIONAL
2145 * }
2146 *
2147 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2148 */
2149 if( ( ret = asn1_get_tag( &p, end, &len,
2150 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2151 {
2152 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2153 }
2154
2155 end = p + len;
2156
2157 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2158 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2159
2160 if( rsa->ver != 0 )
2161 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2162
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002163 if( ( ret = asn1_get_alg_null( &p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002164 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2165
2166 /*
2167 * only RSA keys handled at this time
2168 */
2169 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2170 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2171
2172 /*
2173 * Get the OCTET STRING and parse the PKCS#1 format inside
2174 */
2175 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2176 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2177
2178 if( ( end - p ) < 1 )
2179 {
2180 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2181 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2182 }
2183
2184 end = p + len;
2185
2186 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2187 return( ret );
2188
2189 return( 0 );
2190}
2191
2192/*
Paul Bakkerbda7cb72013-06-24 19:34:25 +02002193 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002194 */
2195static int x509parse_key_pkcs8_encrypted_der(
2196 rsa_context *rsa,
2197 const unsigned char *key,
2198 size_t keylen,
2199 const unsigned char *pwd,
2200 size_t pwdlen )
2201{
2202 int ret;
2203 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002204 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002205 x509_buf pbe_alg_oid, pbe_params;
2206 unsigned char buf[2048];
Paul Bakker7749a222013-06-28 17:28:20 +02002207#if defined(POLARSSL_PKCS12_C)
2208 cipher_type_t cipher_alg;
2209 md_type_t md_alg;
2210#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002211
2212 memset(buf, 0, 2048);
2213
2214 p = (unsigned char *) key;
2215 end = p + keylen;
2216
Paul Bakker28144de2013-06-24 19:28:55 +02002217 if( pwdlen == 0 )
2218 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2219
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002220 /*
2221 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2222 *
2223 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2224 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2225 * encryptedData EncryptedData
2226 * }
2227 *
2228 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2229 *
2230 * EncryptedData ::= OCTET STRING
2231 *
2232 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2233 */
2234 if( ( ret = asn1_get_tag( &p, end, &len,
2235 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2236 {
2237 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2238 }
2239
2240 end = p + len;
2241
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002242 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002243 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002244
2245 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2246 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2247
2248 // buf has been sized to 2048 bytes
2249 if( len > 2048 )
2250 return( POLARSSL_ERR_X509_INVALID_INPUT );
2251
2252 /*
2253 * Decrypt EncryptedData with appropriate PDE
2254 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002255#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002256 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002257 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002258 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002259 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002260 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002261 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002262 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2263 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2264
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002265 return( ret );
2266 }
2267 }
2268 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2269 {
2270 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2271 PKCS12_PBE_DECRYPT,
2272 pwd, pwdlen,
2273 p, len, buf ) ) != 0 )
2274 {
2275 return( ret );
2276 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002277
2278 // Best guess for password mismatch when using RC4. If first tag is
2279 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2280 //
2281 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2282 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002283 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002284 else
2285#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002286#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002287 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002288 {
2289 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2290 p, len, buf ) ) != 0 )
2291 {
2292 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2293 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2294
2295 return( ret );
2296 }
2297 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002298 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002299#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002300 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2301
2302 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2303}
2304
2305/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002306 * Parse a private RSA key
2307 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002308int x509parse_key_rsa( rsa_context *rsa,
2309 const unsigned char *key, size_t keylen,
2310 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002311{
2312 int ret;
2313
Paul Bakker96743fc2011-02-12 14:30:57 +00002314#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002315 size_t len;
2316 pem_context pem;
2317
2318 pem_init( &pem );
2319 ret = pem_read_buffer( &pem,
2320 "-----BEGIN RSA PRIVATE KEY-----",
2321 "-----END RSA PRIVATE KEY-----",
2322 key, pwd, pwdlen, &len );
2323 if( ret == 0 )
2324 {
2325 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2326 {
2327 rsa_free( rsa );
2328 }
2329
2330 pem_free( &pem );
2331 return( ret );
2332 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002333 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2334 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2335 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2336 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002337 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002338 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002339
2340 ret = pem_read_buffer( &pem,
2341 "-----BEGIN PRIVATE KEY-----",
2342 "-----END PRIVATE KEY-----",
2343 key, NULL, 0, &len );
2344 if( ret == 0 )
2345 {
2346 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2347 pem.buf, pem.buflen ) ) != 0 )
2348 {
2349 rsa_free( rsa );
2350 }
2351
2352 pem_free( &pem );
2353 return( ret );
2354 }
2355 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002356 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002357
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002358 ret = pem_read_buffer( &pem,
2359 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2360 "-----END ENCRYPTED PRIVATE KEY-----",
2361 key, NULL, 0, &len );
2362 if( ret == 0 )
2363 {
2364 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2365 pem.buf, pem.buflen,
2366 pwd, pwdlen ) ) != 0 )
2367 {
2368 rsa_free( rsa );
2369 }
2370
2371 pem_free( &pem );
2372 return( ret );
2373 }
2374 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002375 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002376#else
2377 ((void) pwd);
2378 ((void) pwdlen);
2379#endif /* POLARSSL_PEM_C */
2380
2381 // At this point we only know it's not a PEM formatted key. Could be any
2382 // of the known DER encoded private key formats
2383 //
2384 // We try the different DER format parsers to see if one passes without
2385 // error
2386 //
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002387 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2388 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002389 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002390 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002393 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002394
2395 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2396 {
2397 return( ret );
2398 }
2399
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002400 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2401 return( 0 );
2402
2403 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002404
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002405 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2406 return( 0 );
2407
2408 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002409
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002410 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411}
2412
2413/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002414 * Parse a public RSA key
2415 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002416int x509parse_public_key_rsa( rsa_context *rsa,
2417 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002418{
Paul Bakker23986e52011-04-24 08:57:21 +00002419 int ret;
2420 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002421 unsigned char *p, *end;
2422 x509_buf alg_oid;
2423#if defined(POLARSSL_PEM_C)
2424 pem_context pem;
2425
2426 pem_init( &pem );
2427 ret = pem_read_buffer( &pem,
2428 "-----BEGIN PUBLIC KEY-----",
2429 "-----END PUBLIC KEY-----",
2430 key, NULL, 0, &len );
2431
2432 if( ret == 0 )
2433 {
2434 /*
2435 * Was PEM encoded
2436 */
2437 keylen = pem.buflen;
2438 }
Paul Bakker00b28602013-06-24 13:02:41 +02002439 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002440 {
2441 pem_free( &pem );
2442 return( ret );
2443 }
2444
2445 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2446#else
2447 p = (unsigned char *) key;
2448#endif
2449 end = p + keylen;
2450
2451 /*
2452 * PublicKeyInfo ::= SEQUENCE {
2453 * algorithm AlgorithmIdentifier,
2454 * PublicKey BIT STRING
2455 * }
2456 *
2457 * AlgorithmIdentifier ::= SEQUENCE {
2458 * algorithm OBJECT IDENTIFIER,
2459 * parameters ANY DEFINED BY algorithm OPTIONAL
2460 * }
2461 *
2462 * RSAPublicKey ::= SEQUENCE {
2463 * modulus INTEGER, -- n
2464 * publicExponent INTEGER -- e
2465 * }
2466 */
2467
2468 if( ( ret = asn1_get_tag( &p, end, &len,
2469 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2470 {
2471#if defined(POLARSSL_PEM_C)
2472 pem_free( &pem );
2473#endif
2474 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002475 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002476 }
2477
2478 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2479 {
2480#if defined(POLARSSL_PEM_C)
2481 pem_free( &pem );
2482#endif
2483 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002484 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002485 }
2486
2487 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2488 {
2489#if defined(POLARSSL_PEM_C)
2490 pem_free( &pem );
2491#endif
2492 rsa_free( rsa );
2493 return( ret );
2494 }
2495
2496 rsa->len = mpi_size( &rsa->N );
2497
2498#if defined(POLARSSL_PEM_C)
2499 pem_free( &pem );
2500#endif
2501
2502 return( 0 );
2503}
2504
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002505#if defined(POLARSSL_ECP_C)
2506/*
2507 * Parse a private EC key
2508 */
2509int x509parse_key_ec( ecp_keypair *eckey,
2510 const unsigned char *key, size_t keylen,
2511 const unsigned char *pwd, size_t pwdlen )
2512{
2513 int ret;
2514
2515#if defined(POLARSSL_PEM_C)
2516 size_t len;
2517 pem_context pem;
2518
2519 pem_init( &pem );
2520 /* TODO: get list of correct PEM headers */
2521 ret = pem_read_buffer( &pem,
2522 "-----BEGIN EC PRIVATE KEY-----",
2523 "-----END EC PRIVATE KEY-----",
2524 key, pwd, pwdlen, &len );
2525 if( ret == 0 )
2526 {
2527 /* TODO: write der decoding function
2528 if( ( ret = x509parse_key_pkcs8_encrypted_der( eckey,
2529 pem.buf, pem.buflen,
2530 pwd, pwdlen ) ) != 0 )
2531 {
2532 ecp_keypair_free( eckey );
2533 }
2534 */
2535
2536 pem_free( &pem );
2537 return( ret );
2538 }
2539 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2540 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2541 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2542 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2543 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2544 return( ret );
2545
2546 /* TODO: now repeat with other valid PEM headers */
2547
2548#else
2549 ((void) pwd);
2550 ((void) pwdlen);
2551#endif /* POLARSSL_PEM_C */
2552
2553 ((void) keylen);
2554 /* TODO: write der decoding functions (encrypted, unencnrypted)
2555 if( ( ret = x509parse_key_pkcs8_encrypted_der( eckey, key, keylen,
2556 pwd, pwdlen ) ) == 0 )
2557 {
2558 return( 0 );
2559 }
2560
2561 ecp_keypair_free( eckey );
2562
2563 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2564 {
2565 return( ret );
2566 }
2567
2568 if( ( ret = x509parse_key_pkcs8_unencrypted_der( eckey, key, keylen ) )
2569 == 0 )
2570 {
2571 return( 0 );
2572 }
2573 */
2574
2575 ecp_keypair_free( eckey );
2576
2577 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2578}
2579
2580/*
2581 * Parse a public EC key
2582 */
2583int x509parse_public_key_ec( ecp_keypair *eckey,
2584 const unsigned char *key, size_t keylen )
2585{
2586 int ret;
2587 size_t len;
2588 unsigned char *p, *end;
2589 x509_buf alg_oid;
2590#if defined(POLARSSL_PEM_C)
2591 pem_context pem;
2592
2593 pem_init( &pem );
2594 ret = pem_read_buffer( &pem, /* TODO: check header */
2595 "-----BEGIN PUBLIC KEY-----",
2596 "-----END PUBLIC KEY-----",
2597 key, NULL, 0, &len );
2598
2599 if( ret == 0 )
2600 {
2601 /*
2602 * Was PEM encoded
2603 */
2604 keylen = pem.buflen;
2605 }
2606 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2607 {
2608 pem_free( &pem );
2609 return( ret );
2610 }
2611
2612 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2613#else
2614 p = (unsigned char *) key;
2615#endif
2616 end = p + keylen;
2617
2618 /* TODO: parse key */
2619 (void) alg_oid;
2620 (void) end;
2621 (void) eckey;
2622
2623 if( ( ret = ecp_check_pubkey( &eckey->grp, &eckey->Q ) ) != 0 )
2624 {
2625#if defined(POLARSSL_PEM_C)
2626 pem_free( &pem );
2627#endif
2628 ecp_keypair_free( eckey );
2629 return( ret );
2630 }
2631
2632#if defined(POLARSSL_PEM_C)
2633 pem_free( &pem );
2634#endif
2635
2636 return( 0 );
2637}
2638#endif /* defined(POLARSSL_ECP_C) */
2639
Paul Bakkereaa89f82011-04-04 21:36:15 +00002640#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002641/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002642 * Parse DHM parameters
2643 */
Paul Bakker23986e52011-04-24 08:57:21 +00002644int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002645{
Paul Bakker23986e52011-04-24 08:57:21 +00002646 int ret;
2647 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002648 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002649#if defined(POLARSSL_PEM_C)
2650 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002651
Paul Bakker96743fc2011-02-12 14:30:57 +00002652 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002653
Paul Bakker96743fc2011-02-12 14:30:57 +00002654 ret = pem_read_buffer( &pem,
2655 "-----BEGIN DH PARAMETERS-----",
2656 "-----END DH PARAMETERS-----",
2657 dhmin, NULL, 0, &dhminlen );
2658
2659 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002660 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002661 /*
2662 * Was PEM encoded
2663 */
2664 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002665 }
Paul Bakker00b28602013-06-24 13:02:41 +02002666 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002667 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002668 pem_free( &pem );
2669 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002670 }
2671
Paul Bakker96743fc2011-02-12 14:30:57 +00002672 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2673#else
2674 p = (unsigned char *) dhmin;
2675#endif
2676 end = p + dhminlen;
2677
Paul Bakker1b57b062011-01-06 15:48:19 +00002678 memset( dhm, 0, sizeof( dhm_context ) );
2679
Paul Bakker1b57b062011-01-06 15:48:19 +00002680 /*
2681 * DHParams ::= SEQUENCE {
2682 * prime INTEGER, -- P
2683 * generator INTEGER, -- g
2684 * }
2685 */
2686 if( ( ret = asn1_get_tag( &p, end, &len,
2687 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2688 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002689#if defined(POLARSSL_PEM_C)
2690 pem_free( &pem );
2691#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002692 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002693 }
2694
2695 end = p + len;
2696
2697 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2698 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2699 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002700#if defined(POLARSSL_PEM_C)
2701 pem_free( &pem );
2702#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002703 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002704 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002705 }
2706
2707 if( p != end )
2708 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002709#if defined(POLARSSL_PEM_C)
2710 pem_free( &pem );
2711#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002712 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002713 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002714 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2715 }
2716
Paul Bakker96743fc2011-02-12 14:30:57 +00002717#if defined(POLARSSL_PEM_C)
2718 pem_free( &pem );
2719#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002720
2721 return( 0 );
2722}
2723
Paul Bakker335db3f2011-04-25 15:28:35 +00002724#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002725/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002726 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002727 */
2728int x509parse_dhmfile( dhm_context *dhm, const char *path )
2729{
2730 int ret;
2731 size_t n;
2732 unsigned char *buf;
2733
Paul Bakker69e095c2011-12-10 21:55:01 +00002734 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2735 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002736
Paul Bakker27fdf462011-06-09 13:55:13 +00002737 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002738
2739 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002740 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002741
2742 return( ret );
2743}
Paul Bakker335db3f2011-04-25 15:28:35 +00002744#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002745#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002746
Paul Bakker5121ce52009-01-03 21:22:43 +00002747#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002748#include <stdarg.h>
2749
2750#if !defined vsnprintf
2751#define vsnprintf _vsnprintf
2752#endif // vsnprintf
2753
2754/*
2755 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2756 * Result value is not size of buffer needed, but -1 if no fit is possible.
2757 *
2758 * This fuction tries to 'fix' this by at least suggesting enlarging the
2759 * size by 20.
2760 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002761static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002762{
2763 va_list ap;
2764 int res = -1;
2765
2766 va_start( ap, format );
2767
2768 res = vsnprintf( str, size, format, ap );
2769
2770 va_end( ap );
2771
2772 // No quick fix possible
2773 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002774 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002775
2776 return res;
2777}
2778
2779#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002780#endif
2781
Paul Bakkerd98030e2009-05-02 15:13:40 +00002782#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2783
2784#define SAFE_SNPRINTF() \
2785{ \
2786 if( ret == -1 ) \
2787 return( -1 ); \
2788 \
Paul Bakker23986e52011-04-24 08:57:21 +00002789 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002790 p[n - 1] = '\0'; \
2791 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2792 } \
2793 \
Paul Bakker23986e52011-04-24 08:57:21 +00002794 n -= (unsigned int) ret; \
2795 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002796}
2797
Paul Bakker5121ce52009-01-03 21:22:43 +00002798/*
2799 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002800 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002802int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002803{
Paul Bakker23986e52011-04-24 08:57:21 +00002804 int ret;
2805 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002807 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002808 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 char s[128], *p;
2810
2811 memset( s, 0, sizeof( s ) );
2812
2813 name = dn;
2814 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002815 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
2817 while( name != NULL )
2818 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002819 if( !name->oid.p )
2820 {
2821 name = name->next;
2822 continue;
2823 }
2824
Paul Bakker74111d32011-01-15 16:57:55 +00002825 if( name != dn )
2826 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002827 ret = snprintf( p, n, ", " );
2828 SAFE_SNPRINTF();
2829 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Paul Bakkerc70b9822013-04-07 22:00:46 +02002831 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Paul Bakkerc70b9822013-04-07 22:00:46 +02002833 if( ret == 0 )
2834 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002836 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002837 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
2839 for( i = 0; i < name->val.len; i++ )
2840 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002841 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 break;
2843
2844 c = name->val.p[i];
2845 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2846 s[i] = '?';
2847 else s[i] = c;
2848 }
2849 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002850 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002851 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 name = name->next;
2853 }
2854
Paul Bakker23986e52011-04-24 08:57:21 +00002855 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856}
2857
2858/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002859 * Store the serial in printable form into buf; no more
2860 * than size characters will be written
2861 */
2862int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2863{
Paul Bakker23986e52011-04-24 08:57:21 +00002864 int ret;
2865 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002866 char *p;
2867
2868 p = buf;
2869 n = size;
2870
2871 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002872 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002873
2874 for( i = 0; i < nr; i++ )
2875 {
Paul Bakker93048802011-12-05 14:38:06 +00002876 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002877 continue;
2878
Paul Bakkerdd476992011-01-16 21:34:59 +00002879 ret = snprintf( p, n, "%02X%s",
2880 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2881 SAFE_SNPRINTF();
2882 }
2883
Paul Bakker03c7c252011-11-25 12:37:37 +00002884 if( nr != serial->len )
2885 {
2886 ret = snprintf( p, n, "...." );
2887 SAFE_SNPRINTF();
2888 }
2889
Paul Bakker23986e52011-04-24 08:57:21 +00002890 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002891}
2892
2893/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002894 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002896int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2897 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002898{
Paul Bakker23986e52011-04-24 08:57:21 +00002899 int ret;
2900 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002901 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002902 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002903
2904 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002905 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Paul Bakkerd98030e2009-05-02 15:13:40 +00002907 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002909 SAFE_SNPRINTF();
2910 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002912 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
Paul Bakkerdd476992011-01-16 21:34:59 +00002914 ret = x509parse_serial_gets( p, n, &crt->serial);
2915 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakkerd98030e2009-05-02 15:13:40 +00002917 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2918 SAFE_SNPRINTF();
2919 ret = x509parse_dn_gets( p, n, &crt->issuer );
2920 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Paul Bakkerd98030e2009-05-02 15:13:40 +00002922 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2923 SAFE_SNPRINTF();
2924 ret = x509parse_dn_gets( p, n, &crt->subject );
2925 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Paul Bakkerd98030e2009-05-02 15:13:40 +00002927 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2929 crt->valid_from.year, crt->valid_from.mon,
2930 crt->valid_from.day, crt->valid_from.hour,
2931 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002932 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Paul Bakkerd98030e2009-05-02 15:13:40 +00002934 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2936 crt->valid_to.year, crt->valid_to.mon,
2937 crt->valid_to.day, crt->valid_to.hour,
2938 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002939 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Paul Bakkerc70b9822013-04-07 22:00:46 +02002941 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002942 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Paul Bakkerc70b9822013-04-07 22:00:46 +02002944 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2945 if( ret != 0 )
2946 ret = snprintf( p, n, "???" );
2947 else
2948 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002949 SAFE_SNPRINTF();
2950
2951 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002952 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002953 SAFE_SNPRINTF();
2954
Paul Bakker23986e52011-04-24 08:57:21 +00002955 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002956}
2957
Paul Bakker74111d32011-01-15 16:57:55 +00002958/*
2959 * Return an informational string describing the given OID
2960 */
2961const char *x509_oid_get_description( x509_buf *oid )
2962{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002963 const char *desc = NULL;
2964 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002965
Paul Bakkerc70b9822013-04-07 22:00:46 +02002966 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002967
Paul Bakkerc70b9822013-04-07 22:00:46 +02002968 if( ret != 0 )
2969 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002970
Paul Bakkerc70b9822013-04-07 22:00:46 +02002971 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002972}
2973
2974/* Return the x.y.z.... style numeric string for the given OID */
2975int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2976{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002977 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002978}
2979
Paul Bakkerd98030e2009-05-02 15:13:40 +00002980/*
2981 * Return an informational string about the CRL.
2982 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002983int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2984 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002985{
Paul Bakker23986e52011-04-24 08:57:21 +00002986 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002987 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002988 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002989 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002990 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002991
2992 p = buf;
2993 n = size;
2994
2995 ret = snprintf( p, n, "%sCRL version : %d",
2996 prefix, crl->version );
2997 SAFE_SNPRINTF();
2998
2999 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3000 SAFE_SNPRINTF();
3001 ret = x509parse_dn_gets( p, n, &crl->issuer );
3002 SAFE_SNPRINTF();
3003
3004 ret = snprintf( p, n, "\n%sthis update : " \
3005 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3006 crl->this_update.year, crl->this_update.mon,
3007 crl->this_update.day, crl->this_update.hour,
3008 crl->this_update.min, crl->this_update.sec );
3009 SAFE_SNPRINTF();
3010
3011 ret = snprintf( p, n, "\n%snext update : " \
3012 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3013 crl->next_update.year, crl->next_update.mon,
3014 crl->next_update.day, crl->next_update.hour,
3015 crl->next_update.min, crl->next_update.sec );
3016 SAFE_SNPRINTF();
3017
3018 entry = &crl->entry;
3019
3020 ret = snprintf( p, n, "\n%sRevoked certificates:",
3021 prefix );
3022 SAFE_SNPRINTF();
3023
Paul Bakker9be19372009-07-27 20:21:53 +00003024 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003025 {
3026 ret = snprintf( p, n, "\n%sserial number: ",
3027 prefix );
3028 SAFE_SNPRINTF();
3029
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003030 ret = x509parse_serial_gets( p, n, &entry->serial);
3031 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003032
Paul Bakkerd98030e2009-05-02 15:13:40 +00003033 ret = snprintf( p, n, " revocation date: " \
3034 "%04d-%02d-%02d %02d:%02d:%02d",
3035 entry->revocation_date.year, entry->revocation_date.mon,
3036 entry->revocation_date.day, entry->revocation_date.hour,
3037 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003038 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003039
3040 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 }
3042
Paul Bakkerc70b9822013-04-07 22:00:46 +02003043 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003044 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Paul Bakkerc70b9822013-04-07 22:00:46 +02003046 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3047 if( ret != 0 )
3048 ret = snprintf( p, n, "???" );
3049 else
3050 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003051 SAFE_SNPRINTF();
3052
Paul Bakker1e27bb22009-07-19 20:25:25 +00003053 ret = snprintf( p, n, "\n" );
3054 SAFE_SNPRINTF();
3055
Paul Bakker23986e52011-04-24 08:57:21 +00003056 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003057}
3058
3059/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003060 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003061 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003062#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003063int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003064{
Paul Bakkercce9d772011-11-18 14:26:47 +00003065 int year, mon, day;
3066 int hour, min, sec;
3067
3068#if defined(_WIN32)
3069 SYSTEMTIME st;
3070
3071 GetLocalTime(&st);
3072
3073 year = st.wYear;
3074 mon = st.wMonth;
3075 day = st.wDay;
3076 hour = st.wHour;
3077 min = st.wMinute;
3078 sec = st.wSecond;
3079#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 struct tm *lt;
3081 time_t tt;
3082
3083 tt = time( NULL );
3084 lt = localtime( &tt );
3085
Paul Bakkercce9d772011-11-18 14:26:47 +00003086 year = lt->tm_year + 1900;
3087 mon = lt->tm_mon + 1;
3088 day = lt->tm_mday;
3089 hour = lt->tm_hour;
3090 min = lt->tm_min;
3091 sec = lt->tm_sec;
3092#endif
3093
3094 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003095 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Paul Bakkercce9d772011-11-18 14:26:47 +00003097 if( year == to->year &&
3098 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003099 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Paul Bakkercce9d772011-11-18 14:26:47 +00003101 if( year == to->year &&
3102 mon == to->mon &&
3103 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003104 return( 1 );
3105
Paul Bakkercce9d772011-11-18 14:26:47 +00003106 if( year == to->year &&
3107 mon == to->mon &&
3108 day == to->day &&
3109 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003110 return( 1 );
3111
Paul Bakkercce9d772011-11-18 14:26:47 +00003112 if( year == to->year &&
3113 mon == to->mon &&
3114 day == to->day &&
3115 hour == to->hour &&
3116 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003117 return( 1 );
3118
Paul Bakkercce9d772011-11-18 14:26:47 +00003119 if( year == to->year &&
3120 mon == to->mon &&
3121 day == to->day &&
3122 hour == to->hour &&
3123 min == to->min &&
3124 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003125 return( 1 );
3126
Paul Bakker40ea7de2009-05-03 10:18:48 +00003127 return( 0 );
3128}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003129#else /* POLARSSL_HAVE_TIME */
3130int x509parse_time_expired( const x509_time *to )
3131{
3132 ((void) to);
3133 return( 0 );
3134}
3135#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003136
3137/*
3138 * Return 1 if the certificate is revoked, or 0 otherwise.
3139 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003140int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003141{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003142 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003143
3144 while( cur != NULL && cur->serial.len != 0 )
3145 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003146 if( crt->serial.len == cur->serial.len &&
3147 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003148 {
3149 if( x509parse_time_expired( &cur->revocation_date ) )
3150 return( 1 );
3151 }
3152
3153 cur = cur->next;
3154 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
3156 return( 0 );
3157}
3158
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003159/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003160 * Check that the given certificate is valid accoring to the CRL.
3161 */
3162static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3163 x509_crl *crl_list)
3164{
3165 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003166 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3167 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003168
Paul Bakker915275b2012-09-28 07:10:55 +00003169 if( ca == NULL )
3170 return( flags );
3171
Paul Bakker76fd75a2011-01-16 21:12:10 +00003172 /*
3173 * TODO: What happens if no CRL is present?
3174 * Suggestion: Revocation state should be unknown if no CRL is present.
3175 * For backwards compatibility this is not yet implemented.
3176 */
3177
Paul Bakker915275b2012-09-28 07:10:55 +00003178 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003179 {
Paul Bakker915275b2012-09-28 07:10:55 +00003180 if( crl_list->version == 0 ||
3181 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003182 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3183 crl_list->issuer_raw.len ) != 0 )
3184 {
3185 crl_list = crl_list->next;
3186 continue;
3187 }
3188
3189 /*
3190 * Check if CRL is correctly signed by the trusted CA
3191 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003192 md_info = md_info_from_type( crl_list->sig_md );
3193 if( md_info == NULL )
3194 {
3195 /*
3196 * Cannot check 'unknown' hash
3197 */
3198 flags |= BADCRL_NOT_TRUSTED;
3199 break;
3200 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003201
Paul Bakkerc70b9822013-04-07 22:00:46 +02003202 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003203
Paul Bakkerc70b9822013-04-07 22:00:46 +02003204 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003205 0, hash, crl_list->sig.p ) == 0 )
3206 {
3207 /*
3208 * CRL is not trusted
3209 */
3210 flags |= BADCRL_NOT_TRUSTED;
3211 break;
3212 }
3213
3214 /*
3215 * Check for validity of CRL (Do not drop out)
3216 */
3217 if( x509parse_time_expired( &crl_list->next_update ) )
3218 flags |= BADCRL_EXPIRED;
3219
3220 /*
3221 * Check if certificate is revoked
3222 */
3223 if( x509parse_revoked(crt, crl_list) )
3224 {
3225 flags |= BADCERT_REVOKED;
3226 break;
3227 }
3228
3229 crl_list = crl_list->next;
3230 }
3231 return flags;
3232}
3233
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003234static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003235{
3236 size_t i;
3237 size_t cn_idx = 0;
3238
Paul Bakker57b12982012-02-11 17:38:38 +00003239 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003240 return( 0 );
3241
3242 for( i = 0; i < strlen( cn ); ++i )
3243 {
3244 if( cn[i] == '.' )
3245 {
3246 cn_idx = i;
3247 break;
3248 }
3249 }
3250
3251 if( cn_idx == 0 )
3252 return( 0 );
3253
Paul Bakker535e97d2012-08-23 10:49:55 +00003254 if( strlen( cn ) - cn_idx == name->len - 1 &&
3255 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003256 {
3257 return( 1 );
3258 }
3259
3260 return( 0 );
3261}
3262
Paul Bakker915275b2012-09-28 07:10:55 +00003263static int x509parse_verify_top(
3264 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003265 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003266 int (*f_vrfy)(void *, x509_cert *, int, int *),
3267 void *p_vrfy )
3268{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003269 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003270 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003271 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3272 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003273
3274 if( x509parse_time_expired( &child->valid_to ) )
3275 *flags |= BADCERT_EXPIRED;
3276
3277 /*
3278 * Child is the top of the chain. Check against the trust_ca list.
3279 */
3280 *flags |= BADCERT_NOT_TRUSTED;
3281
3282 while( trust_ca != NULL )
3283 {
3284 if( trust_ca->version == 0 ||
3285 child->issuer_raw.len != trust_ca->subject_raw.len ||
3286 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3287 child->issuer_raw.len ) != 0 )
3288 {
3289 trust_ca = trust_ca->next;
3290 continue;
3291 }
3292
Paul Bakker9a736322012-11-14 12:39:52 +00003293 /*
3294 * Reduce path_len to check against if top of the chain is
3295 * the same as the trusted CA
3296 */
3297 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3298 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3299 child->issuer_raw.len ) == 0 )
3300 {
3301 check_path_cnt--;
3302 }
3303
Paul Bakker915275b2012-09-28 07:10:55 +00003304 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003305 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003306 {
3307 trust_ca = trust_ca->next;
3308 continue;
3309 }
3310
Paul Bakkerc70b9822013-04-07 22:00:46 +02003311 md_info = md_info_from_type( child->sig_md );
3312 if( md_info == NULL )
3313 {
3314 /*
3315 * Cannot check 'unknown' hash
3316 */
3317 continue;
3318 }
Paul Bakker915275b2012-09-28 07:10:55 +00003319
Paul Bakkerc70b9822013-04-07 22:00:46 +02003320 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003321
Paul Bakkerc70b9822013-04-07 22:00:46 +02003322 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003323 0, hash, child->sig.p ) != 0 )
3324 {
3325 trust_ca = trust_ca->next;
3326 continue;
3327 }
3328
3329 /*
3330 * Top of chain is signed by a trusted CA
3331 */
3332 *flags &= ~BADCERT_NOT_TRUSTED;
3333 break;
3334 }
3335
Paul Bakker9a736322012-11-14 12:39:52 +00003336 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003337 * If top of chain is not the same as the trusted CA send a verify request
3338 * to the callback for any issues with validity and CRL presence for the
3339 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003340 */
3341 if( trust_ca != NULL &&
3342 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3343 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3344 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003345 {
3346 /* Check trusted CA's CRL for then chain's top crt */
3347 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3348
3349 if( x509parse_time_expired( &trust_ca->valid_to ) )
3350 ca_flags |= BADCERT_EXPIRED;
3351
Paul Bakker915275b2012-09-28 07:10:55 +00003352 if( NULL != f_vrfy )
3353 {
Paul Bakker9a736322012-11-14 12:39:52 +00003354 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003355 return( ret );
3356 }
3357 }
3358
3359 /* Call callback on top cert */
3360 if( NULL != f_vrfy )
3361 {
Paul Bakker9a736322012-11-14 12:39:52 +00003362 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003363 return( ret );
3364 }
3365
Paul Bakker915275b2012-09-28 07:10:55 +00003366 *flags |= ca_flags;
3367
3368 return( 0 );
3369}
3370
3371static int x509parse_verify_child(
3372 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003373 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003374 int (*f_vrfy)(void *, x509_cert *, int, int *),
3375 void *p_vrfy )
3376{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003377 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003378 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003379 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003380 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003381 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003382
3383 if( x509parse_time_expired( &child->valid_to ) )
3384 *flags |= BADCERT_EXPIRED;
3385
Paul Bakkerc70b9822013-04-07 22:00:46 +02003386 md_info = md_info_from_type( child->sig_md );
3387 if( md_info == NULL )
3388 {
3389 /*
3390 * Cannot check 'unknown' hash
3391 */
Paul Bakker915275b2012-09-28 07:10:55 +00003392 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003393 }
3394 else
3395 {
3396 md( md_info, child->tbs.p, child->tbs.len, hash );
3397
3398 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3399 child->sig.p ) != 0 )
3400 *flags |= BADCERT_NOT_TRUSTED;
3401 }
3402
Paul Bakker915275b2012-09-28 07:10:55 +00003403 /* Check trusted CA's CRL for the given crt */
3404 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3405
3406 grandparent = parent->next;
3407
3408 while( grandparent != NULL )
3409 {
3410 if( grandparent->version == 0 ||
3411 grandparent->ca_istrue == 0 ||
3412 parent->issuer_raw.len != grandparent->subject_raw.len ||
3413 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3414 parent->issuer_raw.len ) != 0 )
3415 {
3416 grandparent = grandparent->next;
3417 continue;
3418 }
3419 break;
3420 }
3421
Paul Bakker915275b2012-09-28 07:10:55 +00003422 if( grandparent != NULL )
3423 {
3424 /*
3425 * Part of the chain
3426 */
Paul Bakker9a736322012-11-14 12:39:52 +00003427 ret = x509parse_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003428 if( ret != 0 )
3429 return( ret );
3430 }
3431 else
3432 {
Paul Bakker9a736322012-11-14 12:39:52 +00003433 ret = x509parse_verify_top( parent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003434 if( ret != 0 )
3435 return( ret );
3436 }
3437
3438 /* child is verified to be a child of the parent, call verify callback */
3439 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003440 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003441 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003442
3443 *flags |= parent_flags;
3444
3445 return( 0 );
3446}
3447
Paul Bakker76fd75a2011-01-16 21:12:10 +00003448/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 * Verify the certificate validity
3450 */
3451int x509parse_verify( x509_cert *crt,
3452 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003453 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003454 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003455 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003456 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003457{
Paul Bakker23986e52011-04-24 08:57:21 +00003458 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003459 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003460 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003461 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003463 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003464
Paul Bakker40ea7de2009-05-03 10:18:48 +00003465 *flags = 0;
3466
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 if( cn != NULL )
3468 {
3469 name = &crt->subject;
3470 cn_len = strlen( cn );
3471
Paul Bakker4d2c1242012-05-10 14:12:46 +00003472 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003474 cur = &crt->subject_alt_names;
3475
3476 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003477 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003478 if( cur->buf.len == cn_len &&
3479 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003480 break;
3481
Paul Bakker535e97d2012-08-23 10:49:55 +00003482 if( cur->buf.len > 2 &&
3483 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003484 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003485 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003486
Paul Bakker4d2c1242012-05-10 14:12:46 +00003487 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003488 }
3489
3490 if( cur == NULL )
3491 *flags |= BADCERT_CN_MISMATCH;
3492 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003493 else
3494 {
3495 while( name != NULL )
3496 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003497 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003498 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003499 if( name->val.len == cn_len &&
3500 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003501 break;
3502
Paul Bakker535e97d2012-08-23 10:49:55 +00003503 if( name->val.len > 2 &&
3504 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003505 x509_wildcard_verify( cn, &name->val ) )
3506 break;
3507 }
3508
3509 name = name->next;
3510 }
3511
3512 if( name == NULL )
3513 *flags |= BADCERT_CN_MISMATCH;
3514 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 }
3516
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003518 * Iterate upwards in the given cert chain, to find our crt parent.
3519 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003521 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003522
Paul Bakker76fd75a2011-01-16 21:12:10 +00003523 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003524 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003525 if( parent->ca_istrue == 0 ||
3526 crt->issuer_raw.len != parent->subject_raw.len ||
3527 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003528 crt->issuer_raw.len ) != 0 )
3529 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003530 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003531 continue;
3532 }
Paul Bakker915275b2012-09-28 07:10:55 +00003533 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003534 }
3535
Paul Bakker915275b2012-09-28 07:10:55 +00003536 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003537 {
Paul Bakker915275b2012-09-28 07:10:55 +00003538 /*
3539 * Part of the chain
3540 */
Paul Bakker9a736322012-11-14 12:39:52 +00003541 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003542 if( ret != 0 )
3543 return( ret );
3544 }
3545 else
Paul Bakker74111d32011-01-15 16:57:55 +00003546 {
Paul Bakker9a736322012-11-14 12:39:52 +00003547 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003548 if( ret != 0 )
3549 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003550 }
Paul Bakker915275b2012-09-28 07:10:55 +00003551
3552 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003553 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003554
Paul Bakker5121ce52009-01-03 21:22:43 +00003555 return( 0 );
3556}
3557
3558/*
3559 * Unallocate all certificate data
3560 */
3561void x509_free( x509_cert *crt )
3562{
3563 x509_cert *cert_cur = crt;
3564 x509_cert *cert_prv;
3565 x509_name *name_cur;
3566 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003567 x509_sequence *seq_cur;
3568 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003569
3570 if( crt == NULL )
3571 return;
3572
3573 do
3574 {
3575 rsa_free( &cert_cur->rsa );
3576
3577 name_cur = cert_cur->issuer.next;
3578 while( name_cur != NULL )
3579 {
3580 name_prv = name_cur;
3581 name_cur = name_cur->next;
3582 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003583 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003584 }
3585
3586 name_cur = cert_cur->subject.next;
3587 while( name_cur != NULL )
3588 {
3589 name_prv = name_cur;
3590 name_cur = name_cur->next;
3591 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003592 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 }
3594
Paul Bakker74111d32011-01-15 16:57:55 +00003595 seq_cur = cert_cur->ext_key_usage.next;
3596 while( seq_cur != NULL )
3597 {
3598 seq_prv = seq_cur;
3599 seq_cur = seq_cur->next;
3600 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003601 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003602 }
3603
Paul Bakker8afa70d2012-02-11 18:42:45 +00003604 seq_cur = cert_cur->subject_alt_names.next;
3605 while( seq_cur != NULL )
3606 {
3607 seq_prv = seq_cur;
3608 seq_cur = seq_cur->next;
3609 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003610 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003611 }
3612
Paul Bakker5121ce52009-01-03 21:22:43 +00003613 if( cert_cur->raw.p != NULL )
3614 {
3615 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003616 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 }
3618
3619 cert_cur = cert_cur->next;
3620 }
3621 while( cert_cur != NULL );
3622
3623 cert_cur = crt;
3624 do
3625 {
3626 cert_prv = cert_cur;
3627 cert_cur = cert_cur->next;
3628
3629 memset( cert_prv, 0, sizeof( x509_cert ) );
3630 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003631 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003632 }
3633 while( cert_cur != NULL );
3634}
3635
Paul Bakkerd98030e2009-05-02 15:13:40 +00003636/*
3637 * Unallocate all CRL data
3638 */
3639void x509_crl_free( x509_crl *crl )
3640{
3641 x509_crl *crl_cur = crl;
3642 x509_crl *crl_prv;
3643 x509_name *name_cur;
3644 x509_name *name_prv;
3645 x509_crl_entry *entry_cur;
3646 x509_crl_entry *entry_prv;
3647
3648 if( crl == NULL )
3649 return;
3650
3651 do
3652 {
3653 name_cur = crl_cur->issuer.next;
3654 while( name_cur != NULL )
3655 {
3656 name_prv = name_cur;
3657 name_cur = name_cur->next;
3658 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003659 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003660 }
3661
3662 entry_cur = crl_cur->entry.next;
3663 while( entry_cur != NULL )
3664 {
3665 entry_prv = entry_cur;
3666 entry_cur = entry_cur->next;
3667 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003668 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003669 }
3670
3671 if( crl_cur->raw.p != NULL )
3672 {
3673 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003674 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003675 }
3676
3677 crl_cur = crl_cur->next;
3678 }
3679 while( crl_cur != NULL );
3680
3681 crl_cur = crl;
3682 do
3683 {
3684 crl_prv = crl_cur;
3685 crl_cur = crl_cur->next;
3686
3687 memset( crl_prv, 0, sizeof( x509_crl ) );
3688 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003689 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003690 }
3691 while( crl_cur != NULL );
3692}
3693
Paul Bakker40e46942009-01-03 21:51:57 +00003694#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003695
Paul Bakker40e46942009-01-03 21:51:57 +00003696#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003697
3698/*
3699 * Checkup routine
3700 */
3701int x509_self_test( int verbose )
3702{
Paul Bakker5690efc2011-05-26 13:16:06 +00003703#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003704 int ret;
3705 int flags;
3706 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 x509_cert cacert;
3708 x509_cert clicert;
3709 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003710#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003711 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003712#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
3714 if( verbose != 0 )
3715 printf( " X.509 certificate load: " );
3716
3717 memset( &clicert, 0, sizeof( x509_cert ) );
3718
Paul Bakker3c2122f2013-06-24 19:03:14 +02003719 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003720 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003721 if( ret != 0 )
3722 {
3723 if( verbose != 0 )
3724 printf( "failed\n" );
3725
3726 return( ret );
3727 }
3728
3729 memset( &cacert, 0, sizeof( x509_cert ) );
3730
Paul Bakker3c2122f2013-06-24 19:03:14 +02003731 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003732 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 if( ret != 0 )
3734 {
3735 if( verbose != 0 )
3736 printf( "failed\n" );
3737
3738 return( ret );
3739 }
3740
3741 if( verbose != 0 )
3742 printf( "passed\n X.509 private key load: " );
3743
3744 i = strlen( test_ca_key );
3745 j = strlen( test_ca_pwd );
3746
Paul Bakker66b78b22011-03-25 14:22:50 +00003747 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3748
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003749 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003750 (const unsigned char *) test_ca_key, i,
3751 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 {
3753 if( verbose != 0 )
3754 printf( "failed\n" );
3755
3756 return( ret );
3757 }
3758
3759 if( verbose != 0 )
3760 printf( "passed\n X.509 signature verify: ");
3761
Paul Bakker23986e52011-04-24 08:57:21 +00003762 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003763 if( ret != 0 )
3764 {
Paul Bakker23986e52011-04-24 08:57:21 +00003765 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 if( verbose != 0 )
3767 printf( "failed\n" );
3768
3769 return( ret );
3770 }
3771
Paul Bakker5690efc2011-05-26 13:16:06 +00003772#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003774 printf( "passed\n X.509 DHM parameter load: " );
3775
3776 i = strlen( test_dhm_params );
3777 j = strlen( test_ca_pwd );
3778
Paul Bakker3c2122f2013-06-24 19:03:14 +02003779 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003780 {
3781 if( verbose != 0 )
3782 printf( "failed\n" );
3783
3784 return( ret );
3785 }
3786
3787 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003789#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003790
3791 x509_free( &cacert );
3792 x509_free( &clicert );
3793 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003794#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003795 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003796#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003797
3798 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003799#else
3800 ((void) verbose);
3801 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3802#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003803}
3804
3805#endif
3806
3807#endif