blob: 8abbd0ec88c437e23d5fa4b6b27db9ec3e04625c [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
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200163/* Get a PK algorithm identifier
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200164 *
165 * AlgorithmIdentifier ::= SEQUENCE {
166 * algorithm OBJECT IDENTIFIER,
167 * parameters ANY DEFINED BY algorithm OPTIONAL }
168 */
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200169static int x509_get_pk_alg( unsigned char **p,
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200170 const unsigned char *end,
171 pk_type_t *pk_alg, x509_buf *params )
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200172{
173 int ret;
174 x509_buf alg_oid;
175
176 memset( params, 0, sizeof(asn1_buf) );
177
178 if( ( ret = asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
179 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
180
181 if( oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
182 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
183
184 /*
185 * No parameters with RSA (only for EC)
186 */
187 if( *pk_alg == POLARSSL_PK_RSA &&
188 ( ( params->tag != ASN1_NULL && params->tag != 0 ) ||
189 params->len != 0 ) )
190 {
191 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
192 }
193
194 return( 0 );
195}
196
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200197/* Get an algorithm identifier without parameters (eg for signatures)
198 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * AlgorithmIdentifier ::= SEQUENCE {
200 * algorithm OBJECT IDENTIFIER,
201 * parameters ANY DEFINED BY algorithm OPTIONAL }
202 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200203static int x509_get_alg_null( unsigned char **p, const unsigned char *end,
204 x509_buf *alg )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Paul Bakker23986e52011-04-24 08:57:21 +0000206 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200208 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000209 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 return( 0 );
212}
213
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200214/* Get an EC group id from an ECParameters buffer
215 *
216 * ECParameters ::= CHOICE {
217 * namedCurve OBJECT IDENTIFIER
218 * -- implicitCurve NULL
219 * -- specifiedCurve SpecifiedECDomain
220 * }
221 */
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200222static int x509_ecparams_get_grp_id( const x509_buf *params,
223 ecp_group_id *grp_id )
224{
225 if( oid_get_ec_grp( params, grp_id ) != 0 )
226 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
227
228 return( 0 );
229}
230
231/* Get an EC group id from an ECParameters buffer
232 *
233 * ECParameters ::= CHOICE {
234 * namedCurve OBJECT IDENTIFIER
235 * -- implicitCurve NULL
236 * -- specifiedCurve SpecifiedECDomain
237 * }
238 */
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200239static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
240 ecp_group_id *grp_id )
241{
242 int ret;
243 x509_buf curve;
244
245 curve.tag = **p;
246
247 if( ( ret = asn1_get_tag( p, end, &curve.len, ASN1_OID ) ) != 0 )
248 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
249
250 curve.p = *p;
251 *p += curve.len;
252
253 if( *p != end )
254 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
255 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
256
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200257 return( x509_ecparams_get_grp_id( &curve, grp_id ) );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200258}
259
Paul Bakker5121ce52009-01-03 21:22:43 +0000260/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200261 * subjectPublicKey BIT STRING
262 * -- which, in our case, contains
263 * ECPoint ::= octet string (not ASN.1)
264 */
265static int x509_get_subpubkey_ec( unsigned char **p, const unsigned char *end,
266 const ecp_group *grp, ecp_point *pt )
267{
268 int ret;
269 size_t len;
270
271 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
272 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
273
274 if( *p + len != end )
275 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
276 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
277
278 /*
279 * First byte in the content of BIT STRING is the nummber of padding bit.
280 * Here it is always 0 since ECPoint is an octet string, so skip it.
281 */
282 ++*p;
283 --len;
284
285 if( ( ret = ecp_point_read_binary( grp, pt,
286 (const unsigned char *) *p, len ) ) != 0 )
287 {
288 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
289 }
290
291 return( 0 );
292}
293
294/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 * AttributeTypeAndValue ::= SEQUENCE {
296 * type AttributeType,
297 * value AttributeValue }
298 *
299 * AttributeType ::= OBJECT IDENTIFIER
300 *
301 * AttributeValue ::= ANY DEFINED BY AttributeType
302 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000303static int x509_get_attr_type_value( unsigned char **p,
304 const unsigned char *end,
305 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000306{
Paul Bakker23986e52011-04-24 08:57:21 +0000307 int ret;
308 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 x509_buf *oid;
310 x509_buf *val;
311
312 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000314 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 oid = &cur->oid;
317 oid->tag = **p;
318
319 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000320 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322 oid->p = *p;
323 *p += oid->len;
324
325 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000326 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000327 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
330 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
331 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000332 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000333 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
335 val = &cur->val;
336 val->tag = *(*p)++;
337
338 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000339 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
341 val->p = *p;
342 *p += val->len;
343
344 cur->next = NULL;
345
Paul Bakker400ff6f2011-02-20 10:40:16 +0000346 return( 0 );
347}
348
349/*
350 * RelativeDistinguishedName ::=
351 * SET OF AttributeTypeAndValue
352 *
353 * AttributeTypeAndValue ::= SEQUENCE {
354 * type AttributeType,
355 * value AttributeValue }
356 *
357 * AttributeType ::= OBJECT IDENTIFIER
358 *
359 * AttributeValue ::= ANY DEFINED BY AttributeType
360 */
361static int x509_get_name( unsigned char **p,
362 const unsigned char *end,
363 x509_name *cur )
364{
Paul Bakker23986e52011-04-24 08:57:21 +0000365 int ret;
366 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000367 const unsigned char *end2;
368 x509_name *use;
369
370 if( ( ret = asn1_get_tag( p, end, &len,
371 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000372 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000373
374 end2 = end;
375 end = *p + len;
376 use = cur;
377
378 do
379 {
380 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
381 return( ret );
382
383 if( *p != end )
384 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200385 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000386 sizeof( x509_name ) );
387
388 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000389 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000390
391 memset( use->next, 0, sizeof( x509_name ) );
392
393 use = use->next;
394 }
395 }
396 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
398 /*
399 * recurse until end of SEQUENCE is reached
400 */
401 if( *p == end2 )
402 return( 0 );
403
Paul Bakker6e339b52013-07-03 13:37:05 +0200404 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 sizeof( x509_name ) );
406
407 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000408 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
Paul Bakker430ffbe2012-05-01 08:14:20 +0000410 memset( cur->next, 0, sizeof( x509_name ) );
411
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 return( x509_get_name( p, end2, cur->next ) );
413}
414
415/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 * Time ::= CHOICE {
417 * utcTime UTCTime,
418 * generalTime GeneralizedTime }
419 */
Paul Bakker91200182010-02-18 21:26:15 +0000420static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000421 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000422 x509_time *time )
423{
Paul Bakker23986e52011-04-24 08:57:21 +0000424 int ret;
425 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000426 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000427 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000428
Paul Bakker91200182010-02-18 21:26:15 +0000429 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000430 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
431 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000432
Paul Bakker91200182010-02-18 21:26:15 +0000433 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000434
Paul Bakker91200182010-02-18 21:26:15 +0000435 if ( tag == ASN1_UTC_TIME )
436 {
437 (*p)++;
438 ret = asn1_get_len( p, end, &len );
439
440 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000441 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000442
Paul Bakker91200182010-02-18 21:26:15 +0000443 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000444 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
445 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000446
Paul Bakker91200182010-02-18 21:26:15 +0000447 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
448 &time->year, &time->mon, &time->day,
449 &time->hour, &time->min, &time->sec ) < 5 )
450 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000451
Paul Bakker400ff6f2011-02-20 10:40:16 +0000452 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000453 time->year += 1900;
454
455 *p += len;
456
457 return( 0 );
458 }
459 else if ( tag == ASN1_GENERALIZED_TIME )
460 {
461 (*p)++;
462 ret = asn1_get_len( p, end, &len );
463
464 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000465 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000466
467 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000468 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
469 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000470
471 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
472 &time->year, &time->mon, &time->day,
473 &time->hour, &time->min, &time->sec ) < 5 )
474 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
475
476 *p += len;
477
478 return( 0 );
479 }
480 else
Paul Bakker9d781402011-05-09 16:17:09 +0000481 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000482}
483
484
485/*
486 * Validity ::= SEQUENCE {
487 * notBefore Time,
488 * notAfter Time }
489 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000490static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000491 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 x509_time *from,
493 x509_time *to )
494{
Paul Bakker23986e52011-04-24 08:57:21 +0000495 int ret;
496 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 if( ( ret = asn1_get_tag( p, end, &len,
499 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000500 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 end = *p + len;
503
Paul Bakker91200182010-02-18 21:26:15 +0000504 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000505 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Paul Bakker91200182010-02-18 21:26:15 +0000507 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000508 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000511 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000512 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514 return( 0 );
515}
516
517/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200518 * RSAPublicKey ::= SEQUENCE {
519 * modulus INTEGER, -- n
520 * publicExponent INTEGER -- e
521 * }
522 */
523static int x509_get_rsapubkey( unsigned char **p,
524 const unsigned char *end,
525 rsa_context *rsa )
526{
527 int ret;
528 size_t len;
529
530 if( ( ret = asn1_get_tag( p, end, &len,
531 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
532 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
533
534 if( *p + len != end )
535 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
536 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
537
538 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
539 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
540 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
541
542 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
543 return( ret );
544
545 rsa->len = mpi_size( &rsa->N );
546
547 return( 0 );
548}
549
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200550static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
551 x509_buf *alg_params, ecp_keypair *key )
552{
553 int ret;
554 ecp_group_id grp_id;
555
556 if( ( ret = x509_ecparams_get_grp_id( alg_params, &grp_id ) ) != 0 )
557 return( ret );
558
559 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
560 return( ret );
561
562 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
563 (const unsigned char *) *p, end - *p ) ) != 0 )
564 {
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200565 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200566 }
567
568 return( 0 );
569}
570
571/*
572 * SubjectPublicKeyInfo ::= SEQUENCE {
573 * algorithm AlgorithmIdentifier,
574 * subjectPublicKey BIT STRING }
575 */
576static int x509_get_pubkey( unsigned char **p,
577 const unsigned char *end,
578 pk_context *pk )
579{
580 int ret;
581 size_t len;
582 x509_buf alg_params;
583 pk_type_t pk_alg = POLARSSL_PK_NONE;
584
585 if( ( ret = asn1_get_tag( p, end, &len,
586 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
587 {
588 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
589 }
590
591 end = *p + len;
592
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200593 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200594 return( ret );
595
596 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
597 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
598
599 if( ( end - *p ) < 1 )
600 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
601 POLARSSL_ERR_ASN1_OUT_OF_DATA );
602
603 if( *p + len != end )
604 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
605 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
606
607 if( *(*p)++ != 0 )
608 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
609
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200610 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
611 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200612
613 switch( pk_alg )
614 {
615 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200616 case POLARSSL_PK_ECDSA:
617 /* Should never happen */
618 ret = POLARSSL_ERR_X509_CERT_INVALID_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200619 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200620
621 case POLARSSL_PK_RSA:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200622 ret = x509_get_rsapubkey( p, end, pk->data );
623 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200624
625 case POLARSSL_PK_ECKEY_DH:
626 ((ecp_keypair *) pk->data)->alg = POLARSSL_ECP_KEY_ALG_ECDH;
627 /* FALLTHROUGH */
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200628
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200629 case POLARSSL_PK_ECKEY:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200630 ret = x509_get_ecpubkey( p, end, &alg_params, pk->data );
631 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200632 }
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200633
634 if( ret != 0 )
635 pk_free( pk );
636
637 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200638}
639
Paul Bakker5121ce52009-01-03 21:22:43 +0000640static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000641 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 x509_buf *sig )
643{
Paul Bakker23986e52011-04-24 08:57:21 +0000644 int ret;
645 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Paul Bakker8afa70d2012-02-11 18:42:45 +0000647 if( ( end - *p ) < 1 )
648 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
649 POLARSSL_ERR_ASN1_OUT_OF_DATA );
650
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 sig->tag = **p;
652
653 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000654 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakker74111d32011-01-15 16:57:55 +0000656
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000658 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000659
660 sig->len = len;
661 sig->p = *p;
662
663 *p += len;
664
665 return( 0 );
666}
667
668/*
669 * X.509 v2/v3 unique identifier (not parsed)
670 */
671static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000672 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 x509_buf *uid, int n )
674{
675 int ret;
676
677 if( *p == end )
678 return( 0 );
679
680 uid->tag = **p;
681
682 if( ( ret = asn1_get_tag( p, end, &uid->len,
683 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
684 {
Paul Bakker40e46942009-01-03 21:51:57 +0000685 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 return( 0 );
687
688 return( ret );
689 }
690
691 uid->p = *p;
692 *p += uid->len;
693
694 return( 0 );
695}
696
697/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000698 * X.509 Extensions (No parsing of extensions, pointer should
699 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 */
701static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000702 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000703 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000704{
Paul Bakker23986e52011-04-24 08:57:21 +0000705 int ret;
706 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
708 if( *p == end )
709 return( 0 );
710
711 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000712
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000714 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
717 ext->p = *p;
718 end = *p + ext->len;
719
720 /*
721 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
722 *
723 * Extension ::= SEQUENCE {
724 * extnID OBJECT IDENTIFIER,
725 * critical BOOLEAN DEFAULT FALSE,
726 * extnValue OCTET STRING }
727 */
728 if( ( ret = asn1_get_tag( p, end, &len,
729 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000730 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
732 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000733 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000734 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
Paul Bakkerd98030e2009-05-02 15:13:40 +0000736 return( 0 );
737}
738
739/*
740 * X.509 CRL v2 extensions (no extensions parsed yet.)
741 */
742static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000743 const unsigned char *end,
744 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000745{
Paul Bakker23986e52011-04-24 08:57:21 +0000746 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000747 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000748
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000749 /* Get explicit tag */
750 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000751 {
752 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
753 return( 0 );
754
755 return( ret );
756 }
757
758 while( *p < end )
759 {
760 if( ( ret = asn1_get_tag( p, end, &len,
761 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000762 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000763
764 *p += len;
765 }
766
767 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000768 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000769 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
770
771 return( 0 );
772}
773
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000774/*
775 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
776 */
777static int x509_get_crl_entry_ext( unsigned char **p,
778 const unsigned char *end,
779 x509_buf *ext )
780{
781 int ret;
782 size_t len = 0;
783
784 /* OPTIONAL */
785 if (end <= *p)
786 return( 0 );
787
788 ext->tag = **p;
789 ext->p = *p;
790
791 /*
792 * Get CRL-entry extension sequence header
793 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
794 */
795 if( ( ret = asn1_get_tag( p, end, &ext->len,
796 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
797 {
798 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
799 {
800 ext->p = NULL;
801 return( 0 );
802 }
803 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
804 }
805
806 end = *p + ext->len;
807
808 if( end != *p + ext->len )
809 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
810 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
811
812 while( *p < end )
813 {
814 if( ( ret = asn1_get_tag( p, end, &len,
815 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
816 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
817
818 *p += len;
819 }
820
821 if( *p != end )
822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
823 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
824
825 return( 0 );
826}
827
Paul Bakker74111d32011-01-15 16:57:55 +0000828static int x509_get_basic_constraints( unsigned char **p,
829 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000830 int *ca_istrue,
831 int *max_pathlen )
832{
Paul Bakker23986e52011-04-24 08:57:21 +0000833 int ret;
834 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000835
836 /*
837 * BasicConstraints ::= SEQUENCE {
838 * cA BOOLEAN DEFAULT FALSE,
839 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
840 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000841 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000842 *max_pathlen = 0; /* endless */
843
844 if( ( ret = asn1_get_tag( p, end, &len,
845 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000847
848 if( *p == end )
849 return 0;
850
Paul Bakker3cccddb2011-01-16 21:46:31 +0000851 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000852 {
853 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000854 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000855
856 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000857 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000858
Paul Bakker3cccddb2011-01-16 21:46:31 +0000859 if( *ca_istrue != 0 )
860 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000861 }
862
863 if( *p == end )
864 return 0;
865
866 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000867 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000868
869 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000870 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000871 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
872
873 (*max_pathlen)++;
874
Paul Bakker74111d32011-01-15 16:57:55 +0000875 return 0;
876}
877
878static int x509_get_ns_cert_type( unsigned char **p,
879 const unsigned char *end,
880 unsigned char *ns_cert_type)
881{
882 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000883 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000884
885 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000886 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000887
888 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000889 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000890 POLARSSL_ERR_ASN1_INVALID_LENGTH );
891
892 /* Get actual bitstring */
893 *ns_cert_type = *bs.p;
894 return 0;
895}
896
897static int x509_get_key_usage( unsigned char **p,
898 const unsigned char *end,
899 unsigned char *key_usage)
900{
901 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000902 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000903
904 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000905 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000906
Paul Bakker94a67962012-08-23 13:03:52 +0000907 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000908 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000909 POLARSSL_ERR_ASN1_INVALID_LENGTH );
910
911 /* Get actual bitstring */
912 *key_usage = *bs.p;
913 return 0;
914}
915
Paul Bakkerd98030e2009-05-02 15:13:40 +0000916/*
Paul Bakker74111d32011-01-15 16:57:55 +0000917 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
918 *
919 * KeyPurposeId ::= OBJECT IDENTIFIER
920 */
921static int x509_get_ext_key_usage( unsigned char **p,
922 const unsigned char *end,
923 x509_sequence *ext_key_usage)
924{
925 int ret;
926
927 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000928 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000929
930 /* Sequence length must be >= 1 */
931 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000932 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000933 POLARSSL_ERR_ASN1_INVALID_LENGTH );
934
935 return 0;
936}
937
938/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000939 * SubjectAltName ::= GeneralNames
940 *
941 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
942 *
943 * GeneralName ::= CHOICE {
944 * otherName [0] OtherName,
945 * rfc822Name [1] IA5String,
946 * dNSName [2] IA5String,
947 * x400Address [3] ORAddress,
948 * directoryName [4] Name,
949 * ediPartyName [5] EDIPartyName,
950 * uniformResourceIdentifier [6] IA5String,
951 * iPAddress [7] OCTET STRING,
952 * registeredID [8] OBJECT IDENTIFIER }
953 *
954 * OtherName ::= SEQUENCE {
955 * type-id OBJECT IDENTIFIER,
956 * value [0] EXPLICIT ANY DEFINED BY type-id }
957 *
958 * EDIPartyName ::= SEQUENCE {
959 * nameAssigner [0] DirectoryString OPTIONAL,
960 * partyName [1] DirectoryString }
961 *
962 * NOTE: PolarSSL only parses and uses dNSName at this point.
963 */
964static int x509_get_subject_alt_name( unsigned char **p,
965 const unsigned char *end,
966 x509_sequence *subject_alt_name )
967{
968 int ret;
969 size_t len, tag_len;
970 asn1_buf *buf;
971 unsigned char tag;
972 asn1_sequence *cur = subject_alt_name;
973
974 /* Get main sequence tag */
975 if( ( ret = asn1_get_tag( p, end, &len,
976 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
977 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
978
979 if( *p + len != end )
980 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
981 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
982
983 while( *p < end )
984 {
985 if( ( end - *p ) < 1 )
986 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
987 POLARSSL_ERR_ASN1_OUT_OF_DATA );
988
989 tag = **p;
990 (*p)++;
991 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
992 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
993
994 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
995 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
996 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
997
998 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
999 {
1000 *p += tag_len;
1001 continue;
1002 }
1003
1004 buf = &(cur->buf);
1005 buf->tag = tag;
1006 buf->p = *p;
1007 buf->len = tag_len;
1008 *p += buf->len;
1009
1010 /* Allocate and assign next pointer */
1011 if (*p < end)
1012 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001013 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +00001014 sizeof( asn1_sequence ) );
1015
1016 if( cur->next == NULL )
1017 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1018 POLARSSL_ERR_ASN1_MALLOC_FAILED );
1019
Paul Bakker535e97d2012-08-23 10:49:55 +00001020 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +00001021 cur = cur->next;
1022 }
1023 }
1024
1025 /* Set final sequence entry's next pointer to NULL */
1026 cur->next = NULL;
1027
1028 if( *p != end )
1029 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1030 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1031
1032 return( 0 );
1033}
1034
1035/*
Paul Bakker74111d32011-01-15 16:57:55 +00001036 * X.509 v3 extensions
1037 *
1038 * TODO: Perform all of the basic constraints tests required by the RFC
1039 * TODO: Set values for undetected extensions to a sane default?
1040 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001041 */
1042static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001043 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001044 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001045{
Paul Bakker23986e52011-04-24 08:57:21 +00001046 int ret;
1047 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001048 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001049
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001050 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001051 {
1052 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1053 return( 0 );
1054
1055 return( ret );
1056 }
1057
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 while( *p < end )
1059 {
Paul Bakker74111d32011-01-15 16:57:55 +00001060 /*
1061 * Extension ::= SEQUENCE {
1062 * extnID OBJECT IDENTIFIER,
1063 * critical BOOLEAN DEFAULT FALSE,
1064 * extnValue OCTET STRING }
1065 */
1066 x509_buf extn_oid = {0, 0, NULL};
1067 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001068 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001069
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 if( ( ret = asn1_get_tag( p, end, &len,
1071 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001072 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001074 end_ext_data = *p + len;
1075
Paul Bakker74111d32011-01-15 16:57:55 +00001076 /* Get extension ID */
1077 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Paul Bakker74111d32011-01-15 16:57:55 +00001079 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001080 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Paul Bakker74111d32011-01-15 16:57:55 +00001082 extn_oid.p = *p;
1083 *p += extn_oid.len;
1084
1085 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001086 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001087 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1088
1089 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001090 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001091 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001092 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
Paul Bakker74111d32011-01-15 16:57:55 +00001094 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001095 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001097 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001099 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001100
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001101 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001102 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001103 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
Paul Bakker74111d32011-01-15 16:57:55 +00001105 /*
1106 * Detect supported extensions
1107 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001108 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1109
1110 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001111 {
1112 /* No parser found, skip extension */
1113 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Paul Bakker5c721f92011-07-27 16:51:09 +00001115#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001116 if( is_critical )
1117 {
1118 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001119 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001120 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1121 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001122#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001123 continue;
1124 }
1125
1126 crt->ext_types |= ext_type;
1127
1128 switch( ext_type )
1129 {
1130 case EXT_BASIC_CONSTRAINTS:
1131 /* Parse basic constraints */
1132 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1133 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1134 return ( ret );
1135 break;
1136
1137 case EXT_KEY_USAGE:
1138 /* Parse key usage */
1139 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1140 &crt->key_usage ) ) != 0 )
1141 return ( ret );
1142 break;
1143
1144 case EXT_EXTENDED_KEY_USAGE:
1145 /* Parse extended key usage */
1146 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1147 &crt->ext_key_usage ) ) != 0 )
1148 return ( ret );
1149 break;
1150
1151 case EXT_SUBJECT_ALT_NAME:
1152 /* Parse subject alt name */
1153 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1154 &crt->subject_alt_names ) ) != 0 )
1155 return ( ret );
1156 break;
1157
1158 case EXT_NS_CERT_TYPE:
1159 /* Parse netscape certificate type */
1160 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1161 &crt->ns_cert_type ) ) != 0 )
1162 return ( ret );
1163 break;
1164
1165 default:
1166 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 }
1169
1170 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001171 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001172 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 return( 0 );
1175}
1176
1177/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001178 * X.509 CRL Entries
1179 */
1180static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001181 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001182 x509_crl_entry *entry )
1183{
Paul Bakker23986e52011-04-24 08:57:21 +00001184 int ret;
1185 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001186 x509_crl_entry *cur_entry = entry;
1187
1188 if( *p == end )
1189 return( 0 );
1190
Paul Bakker9be19372009-07-27 20:21:53 +00001191 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001192 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1193 {
1194 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1195 return( 0 );
1196
1197 return( ret );
1198 }
1199
Paul Bakker9be19372009-07-27 20:21:53 +00001200 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001201
1202 while( *p < end )
1203 {
Paul Bakker23986e52011-04-24 08:57:21 +00001204 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001205 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001206
1207 if( ( ret = asn1_get_tag( p, end, &len2,
1208 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1209 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001210 return( ret );
1211 }
1212
Paul Bakker9be19372009-07-27 20:21:53 +00001213 cur_entry->raw.tag = **p;
1214 cur_entry->raw.p = *p;
1215 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001216 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001217
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001218 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001219 return( ret );
1220
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001221 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001222 return( ret );
1223
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001224 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001225 return( ret );
1226
Paul Bakker74111d32011-01-15 16:57:55 +00001227 if ( *p < end )
1228 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001229 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001230
1231 if( cur_entry->next == NULL )
1232 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1233
Paul Bakkerd98030e2009-05-02 15:13:40 +00001234 cur_entry = cur_entry->next;
1235 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1236 }
1237 }
1238
1239 return( 0 );
1240}
1241
Paul Bakkerc70b9822013-04-07 22:00:46 +02001242static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1243 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001244{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001245 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001246
Paul Bakkerc70b9822013-04-07 22:00:46 +02001247 if( ret != 0 )
1248 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001249
Paul Bakkerc70b9822013-04-07 22:00:46 +02001250 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001251}
1252
Paul Bakkerd98030e2009-05-02 15:13:40 +00001253/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001254 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001256static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1257 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258{
Paul Bakker23986e52011-04-24 08:57:21 +00001259 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001260 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001261 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Paul Bakker320a4b52009-03-28 18:52:39 +00001263 /*
1264 * Check for valid input
1265 */
1266 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001267 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001268
Paul Bakker6e339b52013-07-03 13:37:05 +02001269 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001270
1271 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001272 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001273
1274 memcpy( p, buf, buflen );
1275
1276 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
1278 crt->raw.p = p;
1279 crt->raw.len = len;
1280 end = p + len;
1281
1282 /*
1283 * Certificate ::= SEQUENCE {
1284 * tbsCertificate TBSCertificate,
1285 * signatureAlgorithm AlgorithmIdentifier,
1286 * signatureValue BIT STRING }
1287 */
1288 if( ( ret = asn1_get_tag( &p, end, &len,
1289 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001292 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
1294
Paul Bakkerb00ca422012-09-25 12:10:00 +00001295 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001298 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001299 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001301 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001302
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 /*
1304 * TBSCertificate ::= SEQUENCE {
1305 */
1306 crt->tbs.p = p;
1307
1308 if( ( ret = asn1_get_tag( &p, end, &len,
1309 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1310 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001311 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001312 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 }
1314
1315 end = p + len;
1316 crt->tbs.len = end - crt->tbs.p;
1317
1318 /*
1319 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1320 *
1321 * CertificateSerialNumber ::= INTEGER
1322 *
1323 * signature AlgorithmIdentifier
1324 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001325 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1326 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1327 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001329 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 return( ret );
1331 }
1332
1333 crt->version++;
1334
1335 if( crt->version > 3 )
1336 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001337 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001338 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
1340
Paul Bakkerc70b9822013-04-07 22:00:46 +02001341 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1342 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001344 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001345 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
1348 /*
1349 * issuer Name
1350 */
1351 crt->issuer_raw.p = p;
1352
1353 if( ( ret = asn1_get_tag( &p, end, &len,
1354 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1355 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001356 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001357 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 }
1359
1360 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1361 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001362 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 return( ret );
1364 }
1365
1366 crt->issuer_raw.len = p - crt->issuer_raw.p;
1367
1368 /*
1369 * Validity ::= SEQUENCE {
1370 * notBefore Time,
1371 * notAfter Time }
1372 *
1373 */
1374 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1375 &crt->valid_to ) ) != 0 )
1376 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001377 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 return( ret );
1379 }
1380
1381 /*
1382 * subject Name
1383 */
1384 crt->subject_raw.p = p;
1385
1386 if( ( ret = asn1_get_tag( &p, end, &len,
1387 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1388 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001389 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001390 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 }
1392
Paul Bakkercefb3962012-06-27 11:51:09 +00001393 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001395 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 return( ret );
1397 }
1398
1399 crt->subject_raw.len = p - crt->subject_raw.p;
1400
1401 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001402 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001404 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001406 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 return( ret );
1408 }
1409
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 /*
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001411 * Temporary hack for compatibility while transitioning to PK abstraction
1412 * (Cannot use rsa_wrap above since it would force RSA key type.)
1413 */
1414 if( crt->pk.type == POLARSSL_PK_RSA ) {
1415 memcpy( &crt->rsa, pk_rsa( crt->pk ), sizeof( rsa_context ) );
1416 free( crt->pk.data );
1417 crt->pk.data = &crt->rsa;
1418 crt->pk.dont_free = 1;
1419 }
1420
1421 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1423 * -- If present, version shall be v2 or v3
1424 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1425 * -- If present, version shall be v2 or v3
1426 * extensions [3] EXPLICIT Extensions OPTIONAL
1427 * -- If present, version shall be v3
1428 */
1429 if( crt->version == 2 || crt->version == 3 )
1430 {
1431 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1432 if( ret != 0 )
1433 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001434 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 return( ret );
1436 }
1437 }
1438
1439 if( crt->version == 2 || crt->version == 3 )
1440 {
1441 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1442 if( ret != 0 )
1443 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001444 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 return( ret );
1446 }
1447 }
1448
1449 if( crt->version == 3 )
1450 {
Paul Bakker74111d32011-01-15 16:57:55 +00001451 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 if( ret != 0 )
1453 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001454 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 return( ret );
1456 }
1457 }
1458
1459 if( p != end )
1460 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001461 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001462 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001463 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 }
1465
Paul Bakkerb00ca422012-09-25 12:10:00 +00001466 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
1468 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001469 * }
1470 * -- end of TBSCertificate
1471 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 * signatureAlgorithm AlgorithmIdentifier,
1473 * signatureValue BIT STRING
1474 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001475 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001477 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 return( ret );
1479 }
1480
Paul Bakker535e97d2012-08-23 10:49:55 +00001481 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1482 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001484 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001485 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 }
1487
1488 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1489 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001490 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 return( ret );
1492 }
1493
1494 if( p != end )
1495 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001496 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001497 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001498 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 }
1500
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001501 return( 0 );
1502}
1503
1504/*
Paul Bakker42c65812013-06-24 19:21:59 +02001505 * Parse one X.509 certificate in DER format from a buffer and add them to a
1506 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001507 */
Paul Bakker42c65812013-06-24 19:21:59 +02001508int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001509{
Paul Bakker42c65812013-06-24 19:21:59 +02001510 int ret;
1511 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001512
1513 /*
1514 * Check for valid input
1515 */
1516 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001517 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001518
1519 while( crt->version != 0 && crt->next != NULL )
1520 {
1521 prev = crt;
1522 crt = crt->next;
1523 }
1524
1525 /*
1526 * Add new certificate on the end of the chain if needed.
1527 */
1528 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001529 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001530 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001531
Paul Bakker7d06ad22009-05-02 15:53:56 +00001532 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001533 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001534
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001535 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001536 crt = crt->next;
1537 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
Paul Bakker42c65812013-06-24 19:21:59 +02001540 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1541 {
1542 if( prev )
1543 prev->next = NULL;
1544
1545 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001546 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001547
1548 return( ret );
1549 }
1550
1551 return( 0 );
1552}
1553
1554/*
1555 * Parse one or more PEM certificates from a buffer and add them to the chained list
1556 */
1557int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1558{
1559 int ret, success = 0, first_error = 0, total_failed = 0;
1560 int buf_format = X509_FORMAT_DER;
1561
1562 /*
1563 * Check for valid input
1564 */
1565 if( chain == NULL || buf == NULL )
1566 return( POLARSSL_ERR_X509_INVALID_INPUT );
1567
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001568 /*
1569 * Determine buffer content. Buffer contains either one DER certificate or
1570 * one or more PEM certificates.
1571 */
1572#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001573 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001574 buf_format = X509_FORMAT_PEM;
1575#endif
1576
1577 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001578 return x509parse_crt_der( chain, buf, buflen );
1579
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001580#if defined(POLARSSL_PEM_C)
1581 if( buf_format == X509_FORMAT_PEM )
1582 {
1583 pem_context pem;
1584
1585 while( buflen > 0 )
1586 {
1587 size_t use_len;
1588 pem_init( &pem );
1589
1590 ret = pem_read_buffer( &pem,
1591 "-----BEGIN CERTIFICATE-----",
1592 "-----END CERTIFICATE-----",
1593 buf, NULL, 0, &use_len );
1594
1595 if( ret == 0 )
1596 {
1597 /*
1598 * Was PEM encoded
1599 */
1600 buflen -= use_len;
1601 buf += use_len;
1602 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001603 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1604 {
1605 return( ret );
1606 }
Paul Bakker00b28602013-06-24 13:02:41 +02001607 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001608 {
1609 pem_free( &pem );
1610
Paul Bakker5ed3b342013-06-24 19:05:46 +02001611 /*
1612 * PEM header and footer were found
1613 */
1614 buflen -= use_len;
1615 buf += use_len;
1616
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001617 if( first_error == 0 )
1618 first_error = ret;
1619
1620 continue;
1621 }
1622 else
1623 break;
1624
Paul Bakker42c65812013-06-24 19:21:59 +02001625 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001626
1627 pem_free( &pem );
1628
1629 if( ret != 0 )
1630 {
1631 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001632 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001633 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001634 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001635 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001636
1637 if( first_error == 0 )
1638 first_error = ret;
1639
Paul Bakker42c65812013-06-24 19:21:59 +02001640 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001641 continue;
1642 }
1643
1644 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001645 }
1646 }
1647#endif
1648
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001649 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001650 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001651 else if( first_error )
1652 return( first_error );
1653 else
1654 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001655}
1656
1657/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001658 * Parse one or more CRLs and add them to the chained list
1659 */
Paul Bakker23986e52011-04-24 08:57:21 +00001660int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001661{
Paul Bakker23986e52011-04-24 08:57:21 +00001662 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001663 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001664 unsigned char *p, *end;
1665 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001666#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001667 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001668 pem_context pem;
1669#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001670
1671 crl = chain;
1672
1673 /*
1674 * Check for valid input
1675 */
1676 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001677 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678
1679 while( crl->version != 0 && crl->next != NULL )
1680 crl = crl->next;
1681
1682 /*
1683 * Add new CRL on the end of the chain if needed.
1684 */
1685 if ( crl->version != 0 && crl->next == NULL)
1686 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001687 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001688
Paul Bakker7d06ad22009-05-02 15:53:56 +00001689 if( crl->next == NULL )
1690 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001692 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001693 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001694
Paul Bakker7d06ad22009-05-02 15:53:56 +00001695 crl = crl->next;
1696 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001697 }
1698
Paul Bakker96743fc2011-02-12 14:30:57 +00001699#if defined(POLARSSL_PEM_C)
1700 pem_init( &pem );
1701 ret = pem_read_buffer( &pem,
1702 "-----BEGIN X509 CRL-----",
1703 "-----END X509 CRL-----",
1704 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001705
Paul Bakker96743fc2011-02-12 14:30:57 +00001706 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001707 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001708 /*
1709 * Was PEM encoded
1710 */
1711 buflen -= use_len;
1712 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001713
1714 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001715 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001716 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001717 p = pem.buf;
1718 pem.buf = NULL;
1719 len = pem.buflen;
1720 pem_free( &pem );
1721 }
Paul Bakker00b28602013-06-24 13:02:41 +02001722 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001723 {
1724 pem_free( &pem );
1725 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001726 }
1727 else
1728 {
1729 /*
1730 * nope, copy the raw DER data
1731 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001732 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001733
1734 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001735 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001736
1737 memcpy( p, buf, buflen );
1738
1739 buflen = 0;
1740 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001741#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001742 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001743
1744 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001745 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001746
1747 memcpy( p, buf, buflen );
1748
1749 buflen = 0;
1750#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001751
1752 crl->raw.p = p;
1753 crl->raw.len = len;
1754 end = p + len;
1755
1756 /*
1757 * CertificateList ::= SEQUENCE {
1758 * tbsCertList TBSCertList,
1759 * signatureAlgorithm AlgorithmIdentifier,
1760 * signatureValue BIT STRING }
1761 */
1762 if( ( ret = asn1_get_tag( &p, end, &len,
1763 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1764 {
1765 x509_crl_free( crl );
1766 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1767 }
1768
Paul Bakker23986e52011-04-24 08:57:21 +00001769 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001770 {
1771 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001772 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001773 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1774 }
1775
1776 /*
1777 * TBSCertList ::= SEQUENCE {
1778 */
1779 crl->tbs.p = p;
1780
1781 if( ( ret = asn1_get_tag( &p, end, &len,
1782 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1783 {
1784 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001785 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001786 }
1787
1788 end = p + len;
1789 crl->tbs.len = end - crl->tbs.p;
1790
1791 /*
1792 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1793 * -- if present, MUST be v2
1794 *
1795 * signature AlgorithmIdentifier
1796 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001797 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001798 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799 {
1800 x509_crl_free( crl );
1801 return( ret );
1802 }
1803
1804 crl->version++;
1805
1806 if( crl->version > 2 )
1807 {
1808 x509_crl_free( crl );
1809 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1810 }
1811
Paul Bakkerc70b9822013-04-07 22:00:46 +02001812 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1813 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814 {
1815 x509_crl_free( crl );
1816 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1817 }
1818
1819 /*
1820 * issuer Name
1821 */
1822 crl->issuer_raw.p = p;
1823
1824 if( ( ret = asn1_get_tag( &p, end, &len,
1825 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1826 {
1827 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001828 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001829 }
1830
1831 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1832 {
1833 x509_crl_free( crl );
1834 return( ret );
1835 }
1836
1837 crl->issuer_raw.len = p - crl->issuer_raw.p;
1838
1839 /*
1840 * thisUpdate Time
1841 * nextUpdate Time OPTIONAL
1842 */
Paul Bakker91200182010-02-18 21:26:15 +00001843 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001844 {
1845 x509_crl_free( crl );
1846 return( ret );
1847 }
1848
Paul Bakker91200182010-02-18 21:26:15 +00001849 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850 {
Paul Bakker9d781402011-05-09 16:17:09 +00001851 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001852 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001853 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001854 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001855 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001856 x509_crl_free( crl );
1857 return( ret );
1858 }
1859 }
1860
1861 /*
1862 * revokedCertificates SEQUENCE OF SEQUENCE {
1863 * userCertificate CertificateSerialNumber,
1864 * revocationDate Time,
1865 * crlEntryExtensions Extensions OPTIONAL
1866 * -- if present, MUST be v2
1867 * } OPTIONAL
1868 */
1869 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1870 {
1871 x509_crl_free( crl );
1872 return( ret );
1873 }
1874
1875 /*
1876 * crlExtensions EXPLICIT Extensions OPTIONAL
1877 * -- if present, MUST be v2
1878 */
1879 if( crl->version == 2 )
1880 {
1881 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1882
1883 if( ret != 0 )
1884 {
1885 x509_crl_free( crl );
1886 return( ret );
1887 }
1888 }
1889
1890 if( p != end )
1891 {
1892 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001893 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001894 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1895 }
1896
1897 end = crl->raw.p + crl->raw.len;
1898
1899 /*
1900 * signatureAlgorithm AlgorithmIdentifier,
1901 * signatureValue BIT STRING
1902 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001903 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001904 {
1905 x509_crl_free( crl );
1906 return( ret );
1907 }
1908
Paul Bakker535e97d2012-08-23 10:49:55 +00001909 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1910 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001911 {
1912 x509_crl_free( crl );
1913 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1914 }
1915
1916 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1917 {
1918 x509_crl_free( crl );
1919 return( ret );
1920 }
1921
1922 if( p != end )
1923 {
1924 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001925 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001926 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1927 }
1928
1929 if( buflen > 0 )
1930 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001931 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001932
Paul Bakker7d06ad22009-05-02 15:53:56 +00001933 if( crl->next == NULL )
1934 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001935 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001936 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001937 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001938
Paul Bakker7d06ad22009-05-02 15:53:56 +00001939 crl = crl->next;
1940 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001941
1942 return( x509parse_crl( crl, buf, buflen ) );
1943 }
1944
1945 return( 0 );
1946}
1947
Paul Bakker335db3f2011-04-25 15:28:35 +00001948#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001949/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001950 * Load all data from a file into a given buffer.
1951 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001952static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001953{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001954 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001955
Paul Bakkerd98030e2009-05-02 15:13:40 +00001956 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001957 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001958
Paul Bakkerd98030e2009-05-02 15:13:40 +00001959 fseek( f, 0, SEEK_END );
1960 *n = (size_t) ftell( f );
1961 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001962
Paul Bakker6e339b52013-07-03 13:37:05 +02001963 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001964 {
1965 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001966 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001967 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001968
Paul Bakkerd98030e2009-05-02 15:13:40 +00001969 if( fread( *buf, 1, *n, f ) != *n )
1970 {
1971 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001972 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001973 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001974 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001975
Paul Bakkerd98030e2009-05-02 15:13:40 +00001976 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001977
Paul Bakkerd98030e2009-05-02 15:13:40 +00001978 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001979
Paul Bakkerd98030e2009-05-02 15:13:40 +00001980 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001981}
1982
1983/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 * Load one or more certificates and add them to the chained list
1985 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001986int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001987{
1988 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 size_t n;
1990 unsigned char *buf;
1991
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001992 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001993 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001994
Paul Bakker69e095c2011-12-10 21:55:01 +00001995 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
1997 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001998 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999
2000 return( ret );
2001}
2002
Paul Bakker8d914582012-06-04 12:46:42 +00002003int x509parse_crtpath( x509_cert *chain, const char *path )
2004{
2005 int ret = 0;
2006#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00002007 int w_ret;
2008 WCHAR szDir[MAX_PATH];
2009 char filename[MAX_PATH];
2010 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002011 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00002012
Paul Bakker97872ac2012-11-02 12:53:26 +00002013 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00002014 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002015
2016 if( len > MAX_PATH - 3 )
2017 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00002018
Paul Bakker3338b792012-10-01 21:13:10 +00002019 memset( szDir, 0, sizeof(szDir) );
2020 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002021 memcpy( filename, path, len );
2022 filename[len++] = '\\';
2023 p = filename + len;
2024 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002025
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002026 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002027
Paul Bakker97872ac2012-11-02 12:53:26 +00002028 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002029 if (hFind == INVALID_HANDLE_VALUE)
2030 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2031
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002032 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002033 do
2034 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002035 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002036
Paul Bakkere4791f32012-06-04 21:29:15 +00002037 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002038 continue;
2039
Paul Bakker3338b792012-10-01 21:13:10 +00002040 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2041 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002042 p, len - 1,
2043 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002044
Paul Bakker3338b792012-10-01 21:13:10 +00002045 w_ret = x509parse_crtfile( chain, filename );
2046 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002047 ret++;
2048 else
2049 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002050 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002051 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002052
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002053 if (GetLastError() != ERROR_NO_MORE_FILES)
2054 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002055
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002056cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002057 FindClose( hFind );
2058#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002059 int t_ret, i;
2060 struct stat sb;
2061 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002062 char entry_name[255];
2063 DIR *dir = opendir( path );
2064
2065 if( dir == NULL)
2066 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2067
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002068 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002069 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002070 if( result == NULL )
2071 break;
2072
2073 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2074
2075 i = stat( entry_name, &sb );
2076
2077 if( i == -1 )
2078 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2079
2080 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002081 continue;
2082
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002083 // Ignore parse errors
2084 //
Paul Bakker8d914582012-06-04 12:46:42 +00002085 t_ret = x509parse_crtfile( chain, entry_name );
2086 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002087 ret++;
2088 else
2089 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002090 }
2091 closedir( dir );
2092#endif
2093
2094 return( ret );
2095}
2096
Paul Bakkerd98030e2009-05-02 15:13:40 +00002097/*
2098 * Load one or more CRLs and add them to the chained list
2099 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002100int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002101{
2102 int ret;
2103 size_t n;
2104 unsigned char *buf;
2105
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002106 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002107 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002108
Paul Bakker27fdf462011-06-09 13:55:13 +00002109 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002110
2111 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002112 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002113
2114 return( ret );
2115}
2116
Paul Bakker5121ce52009-01-03 21:22:43 +00002117/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002118 * Load and parse a private RSA key
2119 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002120int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002121{
2122 int ret;
2123 size_t n;
2124 unsigned char *buf;
2125
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002126 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002127 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002128
2129 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002130 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002131 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002132 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002133 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002134
2135 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002136 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002137
2138 return( ret );
2139}
2140
2141/*
2142 * Load and parse a public RSA key
2143 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002144int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002145{
2146 int ret;
2147 size_t n;
2148 unsigned char *buf;
2149
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002150 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002151 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002152
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002153 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002154
2155 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002156 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002157
2158 return( ret );
2159}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002160
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002161/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002162 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002163 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002164int x509parse_keyfile( pk_context *ctx,
2165 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002166{
2167 int ret;
2168 size_t n;
2169 unsigned char *buf;
2170
2171 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2172 return( ret );
2173
2174 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002175 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002176 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002177 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002178 (const unsigned char *) pwd, strlen( pwd ) );
2179
2180 memset( buf, 0, n + 1 );
2181 free( buf );
2182
2183 return( ret );
2184}
2185
2186/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002187 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002188 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002189int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002190{
2191 int ret;
2192 size_t n;
2193 unsigned char *buf;
2194
2195 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2196 return( ret );
2197
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002198 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002199
2200 memset( buf, 0, n + 1 );
2201 free( buf );
2202
2203 return( ret );
2204}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002205
Paul Bakker335db3f2011-04-25 15:28:35 +00002206#endif /* POLARSSL_FS_IO */
2207
2208/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002209 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002211static int x509parse_key_pkcs1_der( rsa_context *rsa,
2212 const unsigned char *key,
2213 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002214{
Paul Bakker23986e52011-04-24 08:57:21 +00002215 int ret;
2216 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002218
Paul Bakker96743fc2011-02-12 14:30:57 +00002219 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002220 end = p + keylen;
2221
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002223 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002224 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 * RSAPrivateKey ::= SEQUENCE {
2226 * version Version,
2227 * modulus INTEGER, -- n
2228 * publicExponent INTEGER, -- e
2229 * privateExponent INTEGER, -- d
2230 * prime1 INTEGER, -- p
2231 * prime2 INTEGER, -- q
2232 * exponent1 INTEGER, -- d mod (p-1)
2233 * exponent2 INTEGER, -- d mod (q-1)
2234 * coefficient INTEGER, -- (inverse of q) mod p
2235 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2236 * }
2237 */
2238 if( ( ret = asn1_get_tag( &p, end, &len,
2239 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2240 {
Paul Bakker9d781402011-05-09 16:17:09 +00002241 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
2244 end = p + len;
2245
2246 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2247 {
Paul Bakker9d781402011-05-09 16:17:09 +00002248 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 }
2250
2251 if( rsa->ver != 0 )
2252 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002253 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 }
2255
2256 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2257 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2258 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2259 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2260 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2261 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2262 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2263 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2264 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002266 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 }
2268
2269 rsa->len = mpi_size( &rsa->N );
2270
2271 if( p != end )
2272 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002274 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002275 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 }
2277
2278 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2279 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 rsa_free( rsa );
2281 return( ret );
2282 }
2283
Paul Bakkere2f50402013-06-24 19:00:59 +02002284 return( 0 );
2285}
2286
2287/*
2288 * Parse an unencrypted PKCS#8 encoded private RSA key
2289 */
2290static int x509parse_key_pkcs8_unencrypted_der(
2291 rsa_context *rsa,
2292 const unsigned char *key,
2293 size_t keylen )
2294{
2295 int ret;
2296 size_t len;
2297 unsigned char *p, *end;
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002298 x509_buf alg_params;
Paul Bakkere2f50402013-06-24 19:00:59 +02002299 pk_type_t pk_alg = POLARSSL_PK_NONE;
2300
2301 p = (unsigned char *) key;
2302 end = p + keylen;
2303
2304 /*
2305 * This function parses the PrivatKeyInfo object (PKCS#8)
2306 *
2307 * PrivateKeyInfo ::= SEQUENCE {
2308 * version Version,
2309 * algorithm AlgorithmIdentifier,
2310 * PrivateKey BIT STRING
2311 * }
2312 *
2313 * AlgorithmIdentifier ::= SEQUENCE {
2314 * algorithm OBJECT IDENTIFIER,
2315 * parameters ANY DEFINED BY algorithm OPTIONAL
2316 * }
2317 *
2318 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2319 */
2320 if( ( ret = asn1_get_tag( &p, end, &len,
2321 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2322 {
2323 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2324 }
2325
2326 end = p + len;
2327
2328 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2329 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2330
2331 if( rsa->ver != 0 )
2332 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2333
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002334 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002335 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2336
2337 /*
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002338 * We explicitly want RSA keys only
Paul Bakkere2f50402013-06-24 19:00:59 +02002339 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002340 if (pk_alg != POLARSSL_PK_RSA )
2341 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
2342
Paul Bakkere2f50402013-06-24 19:00:59 +02002343 /*
2344 * Get the OCTET STRING and parse the PKCS#1 format inside
2345 */
2346 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2347 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2348
2349 if( ( end - p ) < 1 )
2350 {
2351 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2352 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2353 }
2354
2355 end = p + len;
2356
2357 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2358 return( ret );
2359
2360 return( 0 );
2361}
2362
2363/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002364 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002365 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002366static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2367 size_t *used_len,
2368 const unsigned char *key, size_t keylen,
2369 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002370{
2371 int ret;
2372 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002373 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002374 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002375#if defined(POLARSSL_PKCS12_C)
2376 cipher_type_t cipher_alg;
2377 md_type_t md_alg;
2378#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002379
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002380 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002381
2382 p = (unsigned char *) key;
2383 end = p + keylen;
2384
Paul Bakker28144de2013-06-24 19:28:55 +02002385 if( pwdlen == 0 )
2386 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2387
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002388 /*
2389 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2390 *
2391 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2392 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2393 * encryptedData EncryptedData
2394 * }
2395 *
2396 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2397 *
2398 * EncryptedData ::= OCTET STRING
2399 *
2400 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2401 */
2402 if( ( ret = asn1_get_tag( &p, end, &len,
2403 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2404 {
2405 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2406 }
2407
2408 end = p + len;
2409
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002410 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002411 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002412
2413 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2414 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2415
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002416 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002417 return( POLARSSL_ERR_X509_INVALID_INPUT );
2418
2419 /*
2420 * Decrypt EncryptedData with appropriate PDE
2421 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002422#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002423 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002424 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002425 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002426 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002427 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002428 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002429 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2430 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2431
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002432 return( ret );
2433 }
2434 }
2435 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2436 {
2437 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2438 PKCS12_PBE_DECRYPT,
2439 pwd, pwdlen,
2440 p, len, buf ) ) != 0 )
2441 {
2442 return( ret );
2443 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002444
2445 // Best guess for password mismatch when using RC4. If first tag is
2446 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2447 //
2448 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2449 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002450 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002451 else
2452#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002453#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002454 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002455 {
2456 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2457 p, len, buf ) ) != 0 )
2458 {
2459 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2460 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2461
2462 return( ret );
2463 }
2464 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002465 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002466#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002467 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2468
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002469 *used_len = len;
2470 return( 0 );
2471}
2472
2473/*
2474 * Parse an encrypted PKCS#8 encoded private RSA key
2475 */
2476static int x509parse_key_pkcs8_encrypted_der(
2477 rsa_context *rsa,
2478 const unsigned char *key, size_t keylen,
2479 const unsigned char *pwd, size_t pwdlen )
2480{
2481 int ret;
2482 unsigned char buf[2048];
2483 size_t len = 0;
2484
2485 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2486 key, keylen, pwd, pwdlen ) ) != 0 )
2487 {
2488 return( ret );
2489 }
2490
2491 return( x509parse_key_pkcs8_unencrypted_der( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002492}
2493
2494/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002495 * Parse a private RSA key
2496 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002497int x509parse_key_rsa( rsa_context *rsa,
2498 const unsigned char *key, size_t keylen,
2499 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002500{
2501 int ret;
2502
Paul Bakker96743fc2011-02-12 14:30:57 +00002503#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002504 size_t len;
2505 pem_context pem;
2506
2507 pem_init( &pem );
2508 ret = pem_read_buffer( &pem,
2509 "-----BEGIN RSA PRIVATE KEY-----",
2510 "-----END RSA PRIVATE KEY-----",
2511 key, pwd, pwdlen, &len );
2512 if( ret == 0 )
2513 {
2514 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2515 {
2516 rsa_free( rsa );
2517 }
2518
2519 pem_free( &pem );
2520 return( ret );
2521 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002522 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2523 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2524 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2525 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002526 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002527 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002528
2529 ret = pem_read_buffer( &pem,
2530 "-----BEGIN PRIVATE KEY-----",
2531 "-----END PRIVATE KEY-----",
2532 key, NULL, 0, &len );
2533 if( ret == 0 )
2534 {
2535 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2536 pem.buf, pem.buflen ) ) != 0 )
2537 {
2538 rsa_free( rsa );
2539 }
2540
2541 pem_free( &pem );
2542 return( ret );
2543 }
2544 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002545 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002546
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002547 ret = pem_read_buffer( &pem,
2548 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2549 "-----END ENCRYPTED PRIVATE KEY-----",
2550 key, NULL, 0, &len );
2551 if( ret == 0 )
2552 {
2553 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2554 pem.buf, pem.buflen,
2555 pwd, pwdlen ) ) != 0 )
2556 {
2557 rsa_free( rsa );
2558 }
2559
2560 pem_free( &pem );
2561 return( ret );
2562 }
2563 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002564 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002565#else
2566 ((void) pwd);
2567 ((void) pwdlen);
2568#endif /* POLARSSL_PEM_C */
2569
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002570 /*
2571 * At this point we only know it's not a PEM formatted key. Could be any
2572 * of the known DER encoded private key formats
2573 *
2574 * We try the different DER format parsers to see if one passes without
2575 * error
2576 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002577 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2578 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002579 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002580 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002583 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002584
2585 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2586 {
2587 return( ret );
2588 }
2589
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002590 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2591 return( 0 );
2592
2593 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002594
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002595 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2596 return( 0 );
2597
2598 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002599
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002600 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601}
2602
2603/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002604 * Parse a public RSA key
2605 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002606int x509parse_public_key_rsa( rsa_context *rsa,
2607 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002608{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002609 pk_context pk_ctx;
Paul Bakker53019ae2011-03-25 13:58:48 +00002610
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002611 pk_init( &pk_ctx );
2612 pk_wrap_rsa( &pk_ctx, rsa );
Paul Bakker53019ae2011-03-25 13:58:48 +00002613
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002614 return( x509parse_public_key( &pk_ctx, key, keylen ) );
Paul Bakker53019ae2011-03-25 13:58:48 +00002615}
2616
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002617#if defined(POLARSSL_ECP_C)
2618/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002619 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002620 */
2621static int x509parse_key_sec1_der( ecp_keypair *eck,
2622 const unsigned char *key,
2623 size_t keylen )
2624{
2625 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002626 int version;
2627 size_t len;
2628 ecp_group_id grp_id;
2629 unsigned char *p = (unsigned char *) key;
2630 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002631
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002632 /*
2633 * RFC 5915, orf SEC1 Appendix C.4
2634 *
2635 * ECPrivateKey ::= SEQUENCE {
2636 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2637 * privateKey OCTET STRING,
2638 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2639 * publicKey [1] BIT STRING OPTIONAL
2640 * }
2641 */
2642 if( ( ret = asn1_get_tag( &p, end, &len,
2643 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2644 {
2645 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2646 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002647
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002648 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002649
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002650 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2651 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002652
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002653 if( version != 1 )
2654 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2655
2656 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2657 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2658
2659 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2660 {
2661 ecp_keypair_free( eck );
2662 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2663 }
2664
2665 p += len;
2666
2667 /*
2668 * Is 'parameters' present?
2669 */
2670 if( ( ret = asn1_get_tag( &p, end, &len,
2671 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2672 {
2673 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2674 return( ret );
2675
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002676 /*
2677 * If we're wrapped in a bigger structure (eg PKCS#8), grp may have been
2678 * defined externally. In this case, make sure both definitions match.
2679 */
2680 if( eck->grp.id != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002681 {
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002682 if( eck->grp.id != grp_id )
2683 {
2684 ecp_keypair_free( eck );
2685 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2686 }
2687 }
2688 else
2689 {
2690 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2691 {
2692 ecp_keypair_free( eck );
2693 return( ret );
2694 }
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002695 }
2696 }
2697 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2698 {
2699 ecp_keypair_free( eck );
2700 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2701 }
2702
2703 /*
2704 * Is 'publickey' present?
2705 */
2706 if( ( ret = asn1_get_tag( &p, end, &len,
2707 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2708 {
2709 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2710 != 0 )
2711 {
2712 ecp_keypair_free( eck );
2713 return( ret );
2714 }
2715
2716 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2717 {
2718 ecp_keypair_free( eck );
2719 return( ret );
2720 }
2721 }
2722 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2723 {
2724 ecp_keypair_free( eck );
2725 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2726 }
2727
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002728 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002729 {
2730 ecp_keypair_free( eck );
2731 return( ret );
2732 }
2733
2734 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002735}
2736
2737/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002738 * Parse an unencrypted PKCS#8 encoded private EC key
2739 */
2740static int x509parse_key_pkcs8_unencrypted_der_ec(
2741 ecp_keypair *eck,
2742 const unsigned char* key,
2743 size_t keylen )
2744{
2745 int ret, version;
2746 size_t len;
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002747 x509_buf alg_params;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002748 ecp_group_id grp_id;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002749 unsigned char *p = (unsigned char *) key;
2750 unsigned char *end = p + keylen;
2751 pk_type_t pk_alg = POLARSSL_PK_NONE;
2752
2753 /*
2754 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2755 *
2756 * PrivateKeyInfo ::= SEQUENCE {
2757 * version Version,
2758 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2759 * privateKey PrivateKey,
2760 * attributes [0] IMPLICIT Attributes OPTIONAL }
2761 *
2762 * Version ::= INTEGER
2763 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2764 * PrivateKey ::= OCTET STRING
2765 *
2766 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2767 */
2768
2769 if( ( ret = asn1_get_tag( &p, end, &len,
2770 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2771 {
2772 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2773 }
2774
2775 end = p + len;
2776
2777 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2778 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2779
2780 if( version != 0 )
2781 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2782
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +02002783 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002784 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2785
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002786 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002787 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002788
2789 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2790 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2791
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002792 if( ( ret = x509_ecparams_get_grp_id( &alg_params, &grp_id ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002793 {
2794 ecp_keypair_free( eck );
2795 return( ret );
2796 }
2797
2798 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2799 {
2800 ecp_keypair_free( eck );
2801 return( ret );
2802 }
2803
2804 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2805 {
2806 ecp_keypair_free( eck );
2807 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2808 }
2809
2810 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2811 {
2812 ecp_keypair_free( eck );
2813 return( ret );
2814 }
2815
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002816 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002817 {
2818 ecp_keypair_free( eck );
2819 return( ret );
2820 }
2821
2822 return 0;
2823}
2824
2825/*
2826 * Parse an encrypted PKCS#8 encoded private EC key
2827 */
2828static int x509parse_key_pkcs8_encrypted_der_ec(
2829 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002830 const unsigned char *key, size_t keylen,
2831 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002832{
2833 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002834 unsigned char buf[2048];
2835 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002836
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002837 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2838 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002839 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002840 return( ret );
2841 }
2842
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002843 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002844}
2845
2846/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002847 * Parse a private EC key
2848 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002849static int x509parse_key_ec( ecp_keypair *eck,
2850 const unsigned char *key, size_t keylen,
2851 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002852{
2853 int ret;
2854
2855#if defined(POLARSSL_PEM_C)
2856 size_t len;
2857 pem_context pem;
2858
2859 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002860 ret = pem_read_buffer( &pem,
2861 "-----BEGIN EC PRIVATE KEY-----",
2862 "-----END EC PRIVATE KEY-----",
2863 key, pwd, pwdlen, &len );
2864 if( ret == 0 )
2865 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002866 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002867 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002868 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002869 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002870
2871 pem_free( &pem );
2872 return( ret );
2873 }
2874 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2875 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2876 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2877 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2878 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2879 return( ret );
2880
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002881 ret = pem_read_buffer( &pem,
2882 "-----BEGIN PRIVATE KEY-----",
2883 "-----END PRIVATE KEY-----",
2884 key, NULL, 0, &len );
2885 if( ret == 0 )
2886 {
2887 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2888 pem.buf, pem.buflen ) ) != 0 )
2889 {
2890 ecp_keypair_free( eck );
2891 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002892
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002893 pem_free( &pem );
2894 return( ret );
2895 }
2896 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2897 return( ret );
2898
2899 ret = pem_read_buffer( &pem,
2900 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2901 "-----END ENCRYPTED PRIVATE KEY-----",
2902 key, NULL, 0, &len );
2903 if( ret == 0 )
2904 {
2905 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2906 pem.buf, pem.buflen,
2907 pwd, pwdlen ) ) != 0 )
2908 {
2909 ecp_keypair_free( eck );
2910 }
2911
2912 pem_free( &pem );
2913 return( ret );
2914 }
2915 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2916 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002917#else
2918 ((void) pwd);
2919 ((void) pwdlen);
2920#endif /* POLARSSL_PEM_C */
2921
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002922 /*
2923 * At this point we only know it's not a PEM formatted key. Could be any
2924 * of the known DER encoded private key formats
2925 *
2926 * We try the different DER format parsers to see if one passes without
2927 * error
2928 */
2929 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2930 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002931 {
2932 return( 0 );
2933 }
2934
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002935 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002936
2937 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2938 {
2939 return( ret );
2940 }
2941
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002942 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2943 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002944 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002945
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002946 ecp_keypair_free( eck );
2947
2948 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2949 return( 0 );
2950
2951 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002952
2953 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2954}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002955#endif /* defined(POLARSSL_ECP_C) */
2956
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002957/*
2958 * Parse a private key
2959 */
2960int x509parse_key( pk_context *ctx,
2961 const unsigned char *key, size_t keylen,
2962 const unsigned char *pwd, size_t pwdlen )
2963{
2964 int ret;
2965
2966 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
2967 return( ret );
2968
2969 if( ( ret = x509parse_key_rsa( ctx->data, key, keylen, pwd, pwdlen ) )
2970 == 0 )
2971 {
2972 return( 0 );
2973 }
2974
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002975 pk_free( ctx );
2976
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002977 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
2978 return( ret );
2979
2980 if( ( ret = x509parse_key_ec( ctx->data, key, keylen, pwd, pwdlen ) ) == 0 )
2981 {
2982 return( 0 );
2983 }
2984
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002985 pk_free( ctx );
2986
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002987 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
2988}
2989
2990/*
2991 * Parse a public key
2992 */
2993int x509parse_public_key( pk_context *ctx,
2994 const unsigned char *key, size_t keylen )
2995{
2996 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002997 unsigned char *p;
2998#if defined(POLARSSL_PEM_C)
2999 size_t len;
3000 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003001
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003002 pem_init( &pem );
3003 ret = pem_read_buffer( &pem,
3004 "-----BEGIN PUBLIC KEY-----",
3005 "-----END PUBLIC KEY-----",
3006 key, NULL, 0, &len );
3007
3008 if( ret == 0 )
3009 {
3010 /*
3011 * Was PEM encoded
3012 */
3013 key = pem.buf;
3014 keylen = pem.buflen;
3015 }
3016 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3017 {
3018 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003019 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003020 }
3021#endif
3022 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003023
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003024 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003025
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003026#if defined(POLARSSL_PEM_C)
3027 pem_free( &pem );
3028#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003029
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003030 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003031}
3032
Paul Bakkereaa89f82011-04-04 21:36:15 +00003033#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003034/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003035 * Parse DHM parameters
3036 */
Paul Bakker23986e52011-04-24 08:57:21 +00003037int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003038{
Paul Bakker23986e52011-04-24 08:57:21 +00003039 int ret;
3040 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003041 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003042#if defined(POLARSSL_PEM_C)
3043 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003044
Paul Bakker96743fc2011-02-12 14:30:57 +00003045 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003046
Paul Bakker96743fc2011-02-12 14:30:57 +00003047 ret = pem_read_buffer( &pem,
3048 "-----BEGIN DH PARAMETERS-----",
3049 "-----END DH PARAMETERS-----",
3050 dhmin, NULL, 0, &dhminlen );
3051
3052 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003053 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003054 /*
3055 * Was PEM encoded
3056 */
3057 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003058 }
Paul Bakker00b28602013-06-24 13:02:41 +02003059 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003060 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003061 pem_free( &pem );
3062 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003063 }
3064
Paul Bakker96743fc2011-02-12 14:30:57 +00003065 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3066#else
3067 p = (unsigned char *) dhmin;
3068#endif
3069 end = p + dhminlen;
3070
Paul Bakker1b57b062011-01-06 15:48:19 +00003071 memset( dhm, 0, sizeof( dhm_context ) );
3072
Paul Bakker1b57b062011-01-06 15:48:19 +00003073 /*
3074 * DHParams ::= SEQUENCE {
3075 * prime INTEGER, -- P
3076 * generator INTEGER, -- g
3077 * }
3078 */
3079 if( ( ret = asn1_get_tag( &p, end, &len,
3080 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3081 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003082#if defined(POLARSSL_PEM_C)
3083 pem_free( &pem );
3084#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003085 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003086 }
3087
3088 end = p + len;
3089
3090 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3091 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3092 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003093#if defined(POLARSSL_PEM_C)
3094 pem_free( &pem );
3095#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003096 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003097 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003098 }
3099
3100 if( p != end )
3101 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003102#if defined(POLARSSL_PEM_C)
3103 pem_free( &pem );
3104#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003105 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003106 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003107 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3108 }
3109
Paul Bakker96743fc2011-02-12 14:30:57 +00003110#if defined(POLARSSL_PEM_C)
3111 pem_free( &pem );
3112#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003113
3114 return( 0 );
3115}
3116
Paul Bakker335db3f2011-04-25 15:28:35 +00003117#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003118/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003119 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003120 */
3121int x509parse_dhmfile( dhm_context *dhm, const char *path )
3122{
3123 int ret;
3124 size_t n;
3125 unsigned char *buf;
3126
Paul Bakker69e095c2011-12-10 21:55:01 +00003127 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3128 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003129
Paul Bakker27fdf462011-06-09 13:55:13 +00003130 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003131
3132 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003133 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003134
3135 return( ret );
3136}
Paul Bakker335db3f2011-04-25 15:28:35 +00003137#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003138#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003139
Paul Bakker5121ce52009-01-03 21:22:43 +00003140#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003141#include <stdarg.h>
3142
3143#if !defined vsnprintf
3144#define vsnprintf _vsnprintf
3145#endif // vsnprintf
3146
3147/*
3148 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3149 * Result value is not size of buffer needed, but -1 if no fit is possible.
3150 *
3151 * This fuction tries to 'fix' this by at least suggesting enlarging the
3152 * size by 20.
3153 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003154static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003155{
3156 va_list ap;
3157 int res = -1;
3158
3159 va_start( ap, format );
3160
3161 res = vsnprintf( str, size, format, ap );
3162
3163 va_end( ap );
3164
3165 // No quick fix possible
3166 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003167 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003168
3169 return res;
3170}
3171
3172#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003173#endif
3174
Paul Bakkerd98030e2009-05-02 15:13:40 +00003175#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3176
3177#define SAFE_SNPRINTF() \
3178{ \
3179 if( ret == -1 ) \
3180 return( -1 ); \
3181 \
Paul Bakker23986e52011-04-24 08:57:21 +00003182 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003183 p[n - 1] = '\0'; \
3184 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3185 } \
3186 \
Paul Bakker23986e52011-04-24 08:57:21 +00003187 n -= (unsigned int) ret; \
3188 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003189}
3190
Paul Bakker5121ce52009-01-03 21:22:43 +00003191/*
3192 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003193 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003195int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003196{
Paul Bakker23986e52011-04-24 08:57:21 +00003197 int ret;
3198 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003200 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003201 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 char s[128], *p;
3203
3204 memset( s, 0, sizeof( s ) );
3205
3206 name = dn;
3207 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003208 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
3210 while( name != NULL )
3211 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003212 if( !name->oid.p )
3213 {
3214 name = name->next;
3215 continue;
3216 }
3217
Paul Bakker74111d32011-01-15 16:57:55 +00003218 if( name != dn )
3219 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003220 ret = snprintf( p, n, ", " );
3221 SAFE_SNPRINTF();
3222 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
Paul Bakkerc70b9822013-04-07 22:00:46 +02003224 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Paul Bakkerc70b9822013-04-07 22:00:46 +02003226 if( ret == 0 )
3227 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003229 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003230 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003231
3232 for( i = 0; i < name->val.len; i++ )
3233 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003234 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 break;
3236
3237 c = name->val.p[i];
3238 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3239 s[i] = '?';
3240 else s[i] = c;
3241 }
3242 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003243 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003244 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 name = name->next;
3246 }
3247
Paul Bakker23986e52011-04-24 08:57:21 +00003248 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249}
3250
3251/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003252 * Store the serial in printable form into buf; no more
3253 * than size characters will be written
3254 */
3255int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3256{
Paul Bakker23986e52011-04-24 08:57:21 +00003257 int ret;
3258 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003259 char *p;
3260
3261 p = buf;
3262 n = size;
3263
3264 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003265 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003266
3267 for( i = 0; i < nr; i++ )
3268 {
Paul Bakker93048802011-12-05 14:38:06 +00003269 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003270 continue;
3271
Paul Bakkerdd476992011-01-16 21:34:59 +00003272 ret = snprintf( p, n, "%02X%s",
3273 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3274 SAFE_SNPRINTF();
3275 }
3276
Paul Bakker03c7c252011-11-25 12:37:37 +00003277 if( nr != serial->len )
3278 {
3279 ret = snprintf( p, n, "...." );
3280 SAFE_SNPRINTF();
3281 }
3282
Paul Bakker23986e52011-04-24 08:57:21 +00003283 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003284}
3285
3286/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003287 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003289int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3290 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003291{
Paul Bakker23986e52011-04-24 08:57:21 +00003292 int ret;
3293 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003294 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003295 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
3297 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003298 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003299
Paul Bakkerd98030e2009-05-02 15:13:40 +00003300 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003302 SAFE_SNPRINTF();
3303 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003305 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Paul Bakkerdd476992011-01-16 21:34:59 +00003307 ret = x509parse_serial_gets( p, n, &crt->serial);
3308 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003309
Paul Bakkerd98030e2009-05-02 15:13:40 +00003310 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3311 SAFE_SNPRINTF();
3312 ret = x509parse_dn_gets( p, n, &crt->issuer );
3313 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Paul Bakkerd98030e2009-05-02 15:13:40 +00003315 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3316 SAFE_SNPRINTF();
3317 ret = x509parse_dn_gets( p, n, &crt->subject );
3318 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003319
Paul Bakkerd98030e2009-05-02 15:13:40 +00003320 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3322 crt->valid_from.year, crt->valid_from.mon,
3323 crt->valid_from.day, crt->valid_from.hour,
3324 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003325 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003326
Paul Bakkerd98030e2009-05-02 15:13:40 +00003327 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3329 crt->valid_to.year, crt->valid_to.mon,
3330 crt->valid_to.day, crt->valid_to.hour,
3331 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003332 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003333
Paul Bakkerc70b9822013-04-07 22:00:46 +02003334 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003335 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003336
Paul Bakkerc70b9822013-04-07 22:00:46 +02003337 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3338 if( ret != 0 )
3339 ret = snprintf( p, n, "???" );
3340 else
3341 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003342 SAFE_SNPRINTF();
3343
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +02003344 switch( crt->pk.type )
3345 {
3346 case POLARSSL_PK_NONE:
3347 case POLARSSL_PK_ECDSA:
3348 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
3349 break;
3350
3351 case POLARSSL_PK_RSA:
3352 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3353 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3354 break;
3355
3356 case POLARSSL_PK_ECKEY:
3357 case POLARSSL_PK_ECKEY_DH:
3358 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3359 (int) pk_ec( crt->pk )->grp.pbits );
3360 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00003361 SAFE_SNPRINTF();
3362
Paul Bakker23986e52011-04-24 08:57:21 +00003363 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003364}
3365
Paul Bakker74111d32011-01-15 16:57:55 +00003366/*
3367 * Return an informational string describing the given OID
3368 */
3369const char *x509_oid_get_description( x509_buf *oid )
3370{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003371 const char *desc = NULL;
3372 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003373
Paul Bakkerc70b9822013-04-07 22:00:46 +02003374 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003375
Paul Bakkerc70b9822013-04-07 22:00:46 +02003376 if( ret != 0 )
3377 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003378
Paul Bakkerc70b9822013-04-07 22:00:46 +02003379 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003380}
3381
3382/* Return the x.y.z.... style numeric string for the given OID */
3383int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3384{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003385 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003386}
3387
Paul Bakkerd98030e2009-05-02 15:13:40 +00003388/*
3389 * Return an informational string about the CRL.
3390 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003391int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3392 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003393{
Paul Bakker23986e52011-04-24 08:57:21 +00003394 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003395 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003396 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003397 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003398 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003399
3400 p = buf;
3401 n = size;
3402
3403 ret = snprintf( p, n, "%sCRL version : %d",
3404 prefix, crl->version );
3405 SAFE_SNPRINTF();
3406
3407 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3408 SAFE_SNPRINTF();
3409 ret = x509parse_dn_gets( p, n, &crl->issuer );
3410 SAFE_SNPRINTF();
3411
3412 ret = snprintf( p, n, "\n%sthis update : " \
3413 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3414 crl->this_update.year, crl->this_update.mon,
3415 crl->this_update.day, crl->this_update.hour,
3416 crl->this_update.min, crl->this_update.sec );
3417 SAFE_SNPRINTF();
3418
3419 ret = snprintf( p, n, "\n%snext update : " \
3420 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3421 crl->next_update.year, crl->next_update.mon,
3422 crl->next_update.day, crl->next_update.hour,
3423 crl->next_update.min, crl->next_update.sec );
3424 SAFE_SNPRINTF();
3425
3426 entry = &crl->entry;
3427
3428 ret = snprintf( p, n, "\n%sRevoked certificates:",
3429 prefix );
3430 SAFE_SNPRINTF();
3431
Paul Bakker9be19372009-07-27 20:21:53 +00003432 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003433 {
3434 ret = snprintf( p, n, "\n%sserial number: ",
3435 prefix );
3436 SAFE_SNPRINTF();
3437
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003438 ret = x509parse_serial_gets( p, n, &entry->serial);
3439 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003440
Paul Bakkerd98030e2009-05-02 15:13:40 +00003441 ret = snprintf( p, n, " revocation date: " \
3442 "%04d-%02d-%02d %02d:%02d:%02d",
3443 entry->revocation_date.year, entry->revocation_date.mon,
3444 entry->revocation_date.day, entry->revocation_date.hour,
3445 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003446 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003447
3448 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 }
3450
Paul Bakkerc70b9822013-04-07 22:00:46 +02003451 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003452 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003453
Paul Bakkerc70b9822013-04-07 22:00:46 +02003454 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3455 if( ret != 0 )
3456 ret = snprintf( p, n, "???" );
3457 else
3458 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003459 SAFE_SNPRINTF();
3460
Paul Bakker1e27bb22009-07-19 20:25:25 +00003461 ret = snprintf( p, n, "\n" );
3462 SAFE_SNPRINTF();
3463
Paul Bakker23986e52011-04-24 08:57:21 +00003464 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465}
3466
3467/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003468 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003470#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003471int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003472{
Paul Bakkercce9d772011-11-18 14:26:47 +00003473 int year, mon, day;
3474 int hour, min, sec;
3475
3476#if defined(_WIN32)
3477 SYSTEMTIME st;
3478
3479 GetLocalTime(&st);
3480
3481 year = st.wYear;
3482 mon = st.wMonth;
3483 day = st.wDay;
3484 hour = st.wHour;
3485 min = st.wMinute;
3486 sec = st.wSecond;
3487#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 struct tm *lt;
3489 time_t tt;
3490
3491 tt = time( NULL );
3492 lt = localtime( &tt );
3493
Paul Bakkercce9d772011-11-18 14:26:47 +00003494 year = lt->tm_year + 1900;
3495 mon = lt->tm_mon + 1;
3496 day = lt->tm_mday;
3497 hour = lt->tm_hour;
3498 min = lt->tm_min;
3499 sec = lt->tm_sec;
3500#endif
3501
3502 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003503 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003504
Paul Bakkercce9d772011-11-18 14:26:47 +00003505 if( year == to->year &&
3506 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003507 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
Paul Bakkercce9d772011-11-18 14:26:47 +00003509 if( year == to->year &&
3510 mon == to->mon &&
3511 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003512 return( 1 );
3513
Paul Bakkercce9d772011-11-18 14:26:47 +00003514 if( year == to->year &&
3515 mon == to->mon &&
3516 day == to->day &&
3517 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003518 return( 1 );
3519
Paul Bakkercce9d772011-11-18 14:26:47 +00003520 if( year == to->year &&
3521 mon == to->mon &&
3522 day == to->day &&
3523 hour == to->hour &&
3524 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003525 return( 1 );
3526
Paul Bakkercce9d772011-11-18 14:26:47 +00003527 if( year == to->year &&
3528 mon == to->mon &&
3529 day == to->day &&
3530 hour == to->hour &&
3531 min == to->min &&
3532 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003533 return( 1 );
3534
Paul Bakker40ea7de2009-05-03 10:18:48 +00003535 return( 0 );
3536}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003537#else /* POLARSSL_HAVE_TIME */
3538int x509parse_time_expired( const x509_time *to )
3539{
3540 ((void) to);
3541 return( 0 );
3542}
3543#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003544
3545/*
3546 * Return 1 if the certificate is revoked, or 0 otherwise.
3547 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003548int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003549{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003550 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003551
3552 while( cur != NULL && cur->serial.len != 0 )
3553 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003554 if( crt->serial.len == cur->serial.len &&
3555 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003556 {
3557 if( x509parse_time_expired( &cur->revocation_date ) )
3558 return( 1 );
3559 }
3560
3561 cur = cur->next;
3562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003563
3564 return( 0 );
3565}
3566
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003567/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003568 * Check that the given certificate is valid accoring to the CRL.
3569 */
3570static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3571 x509_crl *crl_list)
3572{
3573 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003574 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3575 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003576
Paul Bakker915275b2012-09-28 07:10:55 +00003577 if( ca == NULL )
3578 return( flags );
3579
Paul Bakker76fd75a2011-01-16 21:12:10 +00003580 /*
3581 * TODO: What happens if no CRL is present?
3582 * Suggestion: Revocation state should be unknown if no CRL is present.
3583 * For backwards compatibility this is not yet implemented.
3584 */
3585
Paul Bakker915275b2012-09-28 07:10:55 +00003586 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003587 {
Paul Bakker915275b2012-09-28 07:10:55 +00003588 if( crl_list->version == 0 ||
3589 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003590 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3591 crl_list->issuer_raw.len ) != 0 )
3592 {
3593 crl_list = crl_list->next;
3594 continue;
3595 }
3596
3597 /*
3598 * Check if CRL is correctly signed by the trusted CA
3599 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003600 md_info = md_info_from_type( crl_list->sig_md );
3601 if( md_info == NULL )
3602 {
3603 /*
3604 * Cannot check 'unknown' hash
3605 */
3606 flags |= BADCRL_NOT_TRUSTED;
3607 break;
3608 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003609
Paul Bakkerc70b9822013-04-07 22:00:46 +02003610 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003611
Paul Bakkerc70b9822013-04-07 22:00:46 +02003612 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003613 0, hash, crl_list->sig.p ) == 0 )
3614 {
3615 /*
3616 * CRL is not trusted
3617 */
3618 flags |= BADCRL_NOT_TRUSTED;
3619 break;
3620 }
3621
3622 /*
3623 * Check for validity of CRL (Do not drop out)
3624 */
3625 if( x509parse_time_expired( &crl_list->next_update ) )
3626 flags |= BADCRL_EXPIRED;
3627
3628 /*
3629 * Check if certificate is revoked
3630 */
3631 if( x509parse_revoked(crt, crl_list) )
3632 {
3633 flags |= BADCERT_REVOKED;
3634 break;
3635 }
3636
3637 crl_list = crl_list->next;
3638 }
3639 return flags;
3640}
3641
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003642static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003643{
3644 size_t i;
3645 size_t cn_idx = 0;
3646
Paul Bakker57b12982012-02-11 17:38:38 +00003647 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003648 return( 0 );
3649
3650 for( i = 0; i < strlen( cn ); ++i )
3651 {
3652 if( cn[i] == '.' )
3653 {
3654 cn_idx = i;
3655 break;
3656 }
3657 }
3658
3659 if( cn_idx == 0 )
3660 return( 0 );
3661
Paul Bakker535e97d2012-08-23 10:49:55 +00003662 if( strlen( cn ) - cn_idx == name->len - 1 &&
3663 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003664 {
3665 return( 1 );
3666 }
3667
3668 return( 0 );
3669}
3670
Paul Bakker915275b2012-09-28 07:10:55 +00003671static int x509parse_verify_top(
3672 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003673 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003674 int (*f_vrfy)(void *, x509_cert *, int, int *),
3675 void *p_vrfy )
3676{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003677 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003678 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003679 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3680 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003681
3682 if( x509parse_time_expired( &child->valid_to ) )
3683 *flags |= BADCERT_EXPIRED;
3684
3685 /*
3686 * Child is the top of the chain. Check against the trust_ca list.
3687 */
3688 *flags |= BADCERT_NOT_TRUSTED;
3689
3690 while( trust_ca != NULL )
3691 {
3692 if( trust_ca->version == 0 ||
3693 child->issuer_raw.len != trust_ca->subject_raw.len ||
3694 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3695 child->issuer_raw.len ) != 0 )
3696 {
3697 trust_ca = trust_ca->next;
3698 continue;
3699 }
3700
Paul Bakker9a736322012-11-14 12:39:52 +00003701 /*
3702 * Reduce path_len to check against if top of the chain is
3703 * the same as the trusted CA
3704 */
3705 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3706 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3707 child->issuer_raw.len ) == 0 )
3708 {
3709 check_path_cnt--;
3710 }
3711
Paul Bakker915275b2012-09-28 07:10:55 +00003712 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003713 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003714 {
3715 trust_ca = trust_ca->next;
3716 continue;
3717 }
3718
Paul Bakkerc70b9822013-04-07 22:00:46 +02003719 md_info = md_info_from_type( child->sig_md );
3720 if( md_info == NULL )
3721 {
3722 /*
3723 * Cannot check 'unknown' hash
3724 */
3725 continue;
3726 }
Paul Bakker915275b2012-09-28 07:10:55 +00003727
Paul Bakkerc70b9822013-04-07 22:00:46 +02003728 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003729
Paul Bakkerc70b9822013-04-07 22:00:46 +02003730 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003731 0, hash, child->sig.p ) != 0 )
3732 {
3733 trust_ca = trust_ca->next;
3734 continue;
3735 }
3736
3737 /*
3738 * Top of chain is signed by a trusted CA
3739 */
3740 *flags &= ~BADCERT_NOT_TRUSTED;
3741 break;
3742 }
3743
Paul Bakker9a736322012-11-14 12:39:52 +00003744 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003745 * If top of chain is not the same as the trusted CA send a verify request
3746 * to the callback for any issues with validity and CRL presence for the
3747 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003748 */
3749 if( trust_ca != NULL &&
3750 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3751 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3752 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003753 {
3754 /* Check trusted CA's CRL for then chain's top crt */
3755 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3756
3757 if( x509parse_time_expired( &trust_ca->valid_to ) )
3758 ca_flags |= BADCERT_EXPIRED;
3759
Paul Bakker915275b2012-09-28 07:10:55 +00003760 if( NULL != f_vrfy )
3761 {
Paul Bakker9a736322012-11-14 12:39:52 +00003762 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003763 return( ret );
3764 }
3765 }
3766
3767 /* Call callback on top cert */
3768 if( NULL != f_vrfy )
3769 {
Paul Bakker9a736322012-11-14 12:39:52 +00003770 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003771 return( ret );
3772 }
3773
Paul Bakker915275b2012-09-28 07:10:55 +00003774 *flags |= ca_flags;
3775
3776 return( 0 );
3777}
3778
3779static int x509parse_verify_child(
3780 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003781 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003782 int (*f_vrfy)(void *, x509_cert *, int, int *),
3783 void *p_vrfy )
3784{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003785 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003786 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003787 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003788 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003789 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003790
3791 if( x509parse_time_expired( &child->valid_to ) )
3792 *flags |= BADCERT_EXPIRED;
3793
Paul Bakkerc70b9822013-04-07 22:00:46 +02003794 md_info = md_info_from_type( child->sig_md );
3795 if( md_info == NULL )
3796 {
3797 /*
3798 * Cannot check 'unknown' hash
3799 */
Paul Bakker915275b2012-09-28 07:10:55 +00003800 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003801 }
3802 else
3803 {
3804 md( md_info, child->tbs.p, child->tbs.len, hash );
3805
3806 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3807 child->sig.p ) != 0 )
3808 *flags |= BADCERT_NOT_TRUSTED;
3809 }
3810
Paul Bakker915275b2012-09-28 07:10:55 +00003811 /* Check trusted CA's CRL for the given crt */
3812 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3813
3814 grandparent = parent->next;
3815
3816 while( grandparent != NULL )
3817 {
3818 if( grandparent->version == 0 ||
3819 grandparent->ca_istrue == 0 ||
3820 parent->issuer_raw.len != grandparent->subject_raw.len ||
3821 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3822 parent->issuer_raw.len ) != 0 )
3823 {
3824 grandparent = grandparent->next;
3825 continue;
3826 }
3827 break;
3828 }
3829
Paul Bakker915275b2012-09-28 07:10:55 +00003830 if( grandparent != NULL )
3831 {
3832 /*
3833 * Part of the chain
3834 */
Paul Bakker9a736322012-11-14 12:39:52 +00003835 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 +00003836 if( ret != 0 )
3837 return( ret );
3838 }
3839 else
3840 {
Paul Bakker9a736322012-11-14 12:39:52 +00003841 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 +00003842 if( ret != 0 )
3843 return( ret );
3844 }
3845
3846 /* child is verified to be a child of the parent, call verify callback */
3847 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003848 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003849 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003850
3851 *flags |= parent_flags;
3852
3853 return( 0 );
3854}
3855
Paul Bakker76fd75a2011-01-16 21:12:10 +00003856/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 * Verify the certificate validity
3858 */
3859int x509parse_verify( x509_cert *crt,
3860 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003861 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003862 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003863 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003864 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003865{
Paul Bakker23986e52011-04-24 08:57:21 +00003866 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003867 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003868 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003869 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003871 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003872
Paul Bakker40ea7de2009-05-03 10:18:48 +00003873 *flags = 0;
3874
Paul Bakker5121ce52009-01-03 21:22:43 +00003875 if( cn != NULL )
3876 {
3877 name = &crt->subject;
3878 cn_len = strlen( cn );
3879
Paul Bakker4d2c1242012-05-10 14:12:46 +00003880 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003881 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003882 cur = &crt->subject_alt_names;
3883
3884 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003885 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003886 if( cur->buf.len == cn_len &&
3887 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003888 break;
3889
Paul Bakker535e97d2012-08-23 10:49:55 +00003890 if( cur->buf.len > 2 &&
3891 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003892 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003893 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003894
Paul Bakker4d2c1242012-05-10 14:12:46 +00003895 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003896 }
3897
3898 if( cur == NULL )
3899 *flags |= BADCERT_CN_MISMATCH;
3900 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003901 else
3902 {
3903 while( name != NULL )
3904 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003905 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003906 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003907 if( name->val.len == cn_len &&
3908 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003909 break;
3910
Paul Bakker535e97d2012-08-23 10:49:55 +00003911 if( name->val.len > 2 &&
3912 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003913 x509_wildcard_verify( cn, &name->val ) )
3914 break;
3915 }
3916
3917 name = name->next;
3918 }
3919
3920 if( name == NULL )
3921 *flags |= BADCERT_CN_MISMATCH;
3922 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003923 }
3924
Paul Bakker5121ce52009-01-03 21:22:43 +00003925 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003926 * Iterate upwards in the given cert chain, to find our crt parent.
3927 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003928 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003929 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003930
Paul Bakker76fd75a2011-01-16 21:12:10 +00003931 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003932 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003933 if( parent->ca_istrue == 0 ||
3934 crt->issuer_raw.len != parent->subject_raw.len ||
3935 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003936 crt->issuer_raw.len ) != 0 )
3937 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003938 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003939 continue;
3940 }
Paul Bakker915275b2012-09-28 07:10:55 +00003941 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 }
3943
Paul Bakker915275b2012-09-28 07:10:55 +00003944 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003945 {
Paul Bakker915275b2012-09-28 07:10:55 +00003946 /*
3947 * Part of the chain
3948 */
Paul Bakker9a736322012-11-14 12:39:52 +00003949 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003950 if( ret != 0 )
3951 return( ret );
3952 }
3953 else
Paul Bakker74111d32011-01-15 16:57:55 +00003954 {
Paul Bakker9a736322012-11-14 12:39:52 +00003955 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003956 if( ret != 0 )
3957 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003958 }
Paul Bakker915275b2012-09-28 07:10:55 +00003959
3960 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003961 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003962
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 return( 0 );
3964}
3965
3966/*
3967 * Unallocate all certificate data
3968 */
3969void x509_free( x509_cert *crt )
3970{
3971 x509_cert *cert_cur = crt;
3972 x509_cert *cert_prv;
3973 x509_name *name_cur;
3974 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003975 x509_sequence *seq_cur;
3976 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003977
3978 if( crt == NULL )
3979 return;
3980
3981 do
3982 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003983 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003984 rsa_free( &cert_cur->rsa );
3985
3986 name_cur = cert_cur->issuer.next;
3987 while( name_cur != NULL )
3988 {
3989 name_prv = name_cur;
3990 name_cur = name_cur->next;
3991 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003992 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003993 }
3994
3995 name_cur = cert_cur->subject.next;
3996 while( name_cur != NULL )
3997 {
3998 name_prv = name_cur;
3999 name_cur = name_cur->next;
4000 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004001 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004002 }
4003
Paul Bakker74111d32011-01-15 16:57:55 +00004004 seq_cur = cert_cur->ext_key_usage.next;
4005 while( seq_cur != NULL )
4006 {
4007 seq_prv = seq_cur;
4008 seq_cur = seq_cur->next;
4009 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004010 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004011 }
4012
Paul Bakker8afa70d2012-02-11 18:42:45 +00004013 seq_cur = cert_cur->subject_alt_names.next;
4014 while( seq_cur != NULL )
4015 {
4016 seq_prv = seq_cur;
4017 seq_cur = seq_cur->next;
4018 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004019 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004020 }
4021
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 if( cert_cur->raw.p != NULL )
4023 {
4024 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004025 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004026 }
4027
4028 cert_cur = cert_cur->next;
4029 }
4030 while( cert_cur != NULL );
4031
4032 cert_cur = crt;
4033 do
4034 {
4035 cert_prv = cert_cur;
4036 cert_cur = cert_cur->next;
4037
4038 memset( cert_prv, 0, sizeof( x509_cert ) );
4039 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004040 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004041 }
4042 while( cert_cur != NULL );
4043}
4044
Paul Bakkerd98030e2009-05-02 15:13:40 +00004045/*
4046 * Unallocate all CRL data
4047 */
4048void x509_crl_free( x509_crl *crl )
4049{
4050 x509_crl *crl_cur = crl;
4051 x509_crl *crl_prv;
4052 x509_name *name_cur;
4053 x509_name *name_prv;
4054 x509_crl_entry *entry_cur;
4055 x509_crl_entry *entry_prv;
4056
4057 if( crl == NULL )
4058 return;
4059
4060 do
4061 {
4062 name_cur = crl_cur->issuer.next;
4063 while( name_cur != NULL )
4064 {
4065 name_prv = name_cur;
4066 name_cur = name_cur->next;
4067 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004068 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004069 }
4070
4071 entry_cur = crl_cur->entry.next;
4072 while( entry_cur != NULL )
4073 {
4074 entry_prv = entry_cur;
4075 entry_cur = entry_cur->next;
4076 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004077 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004078 }
4079
4080 if( crl_cur->raw.p != NULL )
4081 {
4082 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004083 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004084 }
4085
4086 crl_cur = crl_cur->next;
4087 }
4088 while( crl_cur != NULL );
4089
4090 crl_cur = crl;
4091 do
4092 {
4093 crl_prv = crl_cur;
4094 crl_cur = crl_cur->next;
4095
4096 memset( crl_prv, 0, sizeof( x509_crl ) );
4097 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004098 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004099 }
4100 while( crl_cur != NULL );
4101}
4102
Paul Bakker40e46942009-01-03 21:51:57 +00004103#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004104
Paul Bakker40e46942009-01-03 21:51:57 +00004105#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004106
4107/*
4108 * Checkup routine
4109 */
4110int x509_self_test( int verbose )
4111{
Paul Bakker5690efc2011-05-26 13:16:06 +00004112#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004113 int ret;
4114 int flags;
4115 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004116 x509_cert cacert;
4117 x509_cert clicert;
4118 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004119#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004120 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004122
4123 if( verbose != 0 )
4124 printf( " X.509 certificate load: " );
4125
4126 memset( &clicert, 0, sizeof( x509_cert ) );
4127
Paul Bakker3c2122f2013-06-24 19:03:14 +02004128 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004129 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004130 if( ret != 0 )
4131 {
4132 if( verbose != 0 )
4133 printf( "failed\n" );
4134
4135 return( ret );
4136 }
4137
4138 memset( &cacert, 0, sizeof( x509_cert ) );
4139
Paul Bakker3c2122f2013-06-24 19:03:14 +02004140 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004141 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004142 if( ret != 0 )
4143 {
4144 if( verbose != 0 )
4145 printf( "failed\n" );
4146
4147 return( ret );
4148 }
4149
4150 if( verbose != 0 )
4151 printf( "passed\n X.509 private key load: " );
4152
4153 i = strlen( test_ca_key );
4154 j = strlen( test_ca_pwd );
4155
Paul Bakker66b78b22011-03-25 14:22:50 +00004156 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4157
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004158 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004159 (const unsigned char *) test_ca_key, i,
4160 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004161 {
4162 if( verbose != 0 )
4163 printf( "failed\n" );
4164
4165 return( ret );
4166 }
4167
4168 if( verbose != 0 )
4169 printf( "passed\n X.509 signature verify: ");
4170
Paul Bakker23986e52011-04-24 08:57:21 +00004171 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004172 if( ret != 0 )
4173 {
Paul Bakker23986e52011-04-24 08:57:21 +00004174 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004175 if( verbose != 0 )
4176 printf( "failed\n" );
4177
4178 return( ret );
4179 }
4180
Paul Bakker5690efc2011-05-26 13:16:06 +00004181#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004182 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004183 printf( "passed\n X.509 DHM parameter load: " );
4184
4185 i = strlen( test_dhm_params );
4186 j = strlen( test_ca_pwd );
4187
Paul Bakker3c2122f2013-06-24 19:03:14 +02004188 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004189 {
4190 if( verbose != 0 )
4191 printf( "failed\n" );
4192
4193 return( ret );
4194 }
4195
4196 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004197 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004198#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004199
4200 x509_free( &cacert );
4201 x509_free( &clicert );
4202 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004203#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004204 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004205#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004206
4207 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004208#else
4209 ((void) verbose);
4210 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004212}
4213
4214#endif
4215
4216#endif