blob: a2a866047c4617fc33b5ba03a9b5292e1a0b73a2 [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
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200640/*
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200641 * Get an RSA public key (compatibility wrapper)
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200643static int x509_get_pubkey_rsa( unsigned char **p,
644 const unsigned char *end,
645 rsa_context *rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000646{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200647 pk_context pk_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200649 pk_init( &pk_ctx );
650 pk_wrap_rsa( &pk_ctx, rsa );
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +0200651
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200652 return( x509_get_pubkey( p, end, &pk_ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000653}
654
655static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000656 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 x509_buf *sig )
658{
Paul Bakker23986e52011-04-24 08:57:21 +0000659 int ret;
660 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
Paul Bakker8afa70d2012-02-11 18:42:45 +0000662 if( ( end - *p ) < 1 )
663 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
664 POLARSSL_ERR_ASN1_OUT_OF_DATA );
665
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 sig->tag = **p;
667
668 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000669 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
Paul Bakker74111d32011-01-15 16:57:55 +0000671
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000673 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 sig->len = len;
676 sig->p = *p;
677
678 *p += len;
679
680 return( 0 );
681}
682
683/*
684 * X.509 v2/v3 unique identifier (not parsed)
685 */
686static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000687 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 x509_buf *uid, int n )
689{
690 int ret;
691
692 if( *p == end )
693 return( 0 );
694
695 uid->tag = **p;
696
697 if( ( ret = asn1_get_tag( p, end, &uid->len,
698 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
699 {
Paul Bakker40e46942009-01-03 21:51:57 +0000700 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 return( 0 );
702
703 return( ret );
704 }
705
706 uid->p = *p;
707 *p += uid->len;
708
709 return( 0 );
710}
711
712/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000713 * X.509 Extensions (No parsing of extensions, pointer should
714 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 */
716static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000717 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000718 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000719{
Paul Bakker23986e52011-04-24 08:57:21 +0000720 int ret;
721 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
723 if( *p == end )
724 return( 0 );
725
726 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000727
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000729 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
732 ext->p = *p;
733 end = *p + ext->len;
734
735 /*
736 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
737 *
738 * Extension ::= SEQUENCE {
739 * extnID OBJECT IDENTIFIER,
740 * critical BOOLEAN DEFAULT FALSE,
741 * extnValue OCTET STRING }
742 */
743 if( ( ret = asn1_get_tag( p, end, &len,
744 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000745 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
747 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000748 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000749 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Paul Bakkerd98030e2009-05-02 15:13:40 +0000751 return( 0 );
752}
753
754/*
755 * X.509 CRL v2 extensions (no extensions parsed yet.)
756 */
757static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000758 const unsigned char *end,
759 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000760{
Paul Bakker23986e52011-04-24 08:57:21 +0000761 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000762 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000763
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000764 /* Get explicit tag */
765 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000766 {
767 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
768 return( 0 );
769
770 return( ret );
771 }
772
773 while( *p < end )
774 {
775 if( ( ret = asn1_get_tag( p, end, &len,
776 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000777 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000778
779 *p += len;
780 }
781
782 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000783 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000784 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
785
786 return( 0 );
787}
788
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000789/*
790 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
791 */
792static int x509_get_crl_entry_ext( unsigned char **p,
793 const unsigned char *end,
794 x509_buf *ext )
795{
796 int ret;
797 size_t len = 0;
798
799 /* OPTIONAL */
800 if (end <= *p)
801 return( 0 );
802
803 ext->tag = **p;
804 ext->p = *p;
805
806 /*
807 * Get CRL-entry extension sequence header
808 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
809 */
810 if( ( ret = asn1_get_tag( p, end, &ext->len,
811 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
812 {
813 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
814 {
815 ext->p = NULL;
816 return( 0 );
817 }
818 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
819 }
820
821 end = *p + ext->len;
822
823 if( end != *p + ext->len )
824 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
825 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
826
827 while( *p < end )
828 {
829 if( ( ret = asn1_get_tag( p, end, &len,
830 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
831 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
832
833 *p += len;
834 }
835
836 if( *p != end )
837 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
838 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
839
840 return( 0 );
841}
842
Paul Bakker74111d32011-01-15 16:57:55 +0000843static int x509_get_basic_constraints( unsigned char **p,
844 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000845 int *ca_istrue,
846 int *max_pathlen )
847{
Paul Bakker23986e52011-04-24 08:57:21 +0000848 int ret;
849 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000850
851 /*
852 * BasicConstraints ::= SEQUENCE {
853 * cA BOOLEAN DEFAULT FALSE,
854 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
855 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000856 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000857 *max_pathlen = 0; /* endless */
858
859 if( ( ret = asn1_get_tag( p, end, &len,
860 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000861 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000862
863 if( *p == end )
864 return 0;
865
Paul Bakker3cccddb2011-01-16 21:46:31 +0000866 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000867 {
868 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000869 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000870
871 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000872 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000873
Paul Bakker3cccddb2011-01-16 21:46:31 +0000874 if( *ca_istrue != 0 )
875 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000876 }
877
878 if( *p == end )
879 return 0;
880
881 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000882 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000883
884 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000885 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000886 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
887
888 (*max_pathlen)++;
889
Paul Bakker74111d32011-01-15 16:57:55 +0000890 return 0;
891}
892
893static int x509_get_ns_cert_type( unsigned char **p,
894 const unsigned char *end,
895 unsigned char *ns_cert_type)
896{
897 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000898 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000899
900 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000901 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000902
903 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000904 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000905 POLARSSL_ERR_ASN1_INVALID_LENGTH );
906
907 /* Get actual bitstring */
908 *ns_cert_type = *bs.p;
909 return 0;
910}
911
912static int x509_get_key_usage( unsigned char **p,
913 const unsigned char *end,
914 unsigned char *key_usage)
915{
916 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000917 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000918
919 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000920 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000921
Paul Bakker94a67962012-08-23 13:03:52 +0000922 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000923 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000924 POLARSSL_ERR_ASN1_INVALID_LENGTH );
925
926 /* Get actual bitstring */
927 *key_usage = *bs.p;
928 return 0;
929}
930
Paul Bakkerd98030e2009-05-02 15:13:40 +0000931/*
Paul Bakker74111d32011-01-15 16:57:55 +0000932 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
933 *
934 * KeyPurposeId ::= OBJECT IDENTIFIER
935 */
936static int x509_get_ext_key_usage( unsigned char **p,
937 const unsigned char *end,
938 x509_sequence *ext_key_usage)
939{
940 int ret;
941
942 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000943 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000944
945 /* Sequence length must be >= 1 */
946 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000947 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000948 POLARSSL_ERR_ASN1_INVALID_LENGTH );
949
950 return 0;
951}
952
953/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000954 * SubjectAltName ::= GeneralNames
955 *
956 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
957 *
958 * GeneralName ::= CHOICE {
959 * otherName [0] OtherName,
960 * rfc822Name [1] IA5String,
961 * dNSName [2] IA5String,
962 * x400Address [3] ORAddress,
963 * directoryName [4] Name,
964 * ediPartyName [5] EDIPartyName,
965 * uniformResourceIdentifier [6] IA5String,
966 * iPAddress [7] OCTET STRING,
967 * registeredID [8] OBJECT IDENTIFIER }
968 *
969 * OtherName ::= SEQUENCE {
970 * type-id OBJECT IDENTIFIER,
971 * value [0] EXPLICIT ANY DEFINED BY type-id }
972 *
973 * EDIPartyName ::= SEQUENCE {
974 * nameAssigner [0] DirectoryString OPTIONAL,
975 * partyName [1] DirectoryString }
976 *
977 * NOTE: PolarSSL only parses and uses dNSName at this point.
978 */
979static int x509_get_subject_alt_name( unsigned char **p,
980 const unsigned char *end,
981 x509_sequence *subject_alt_name )
982{
983 int ret;
984 size_t len, tag_len;
985 asn1_buf *buf;
986 unsigned char tag;
987 asn1_sequence *cur = subject_alt_name;
988
989 /* Get main sequence tag */
990 if( ( ret = asn1_get_tag( p, end, &len,
991 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
992 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
993
994 if( *p + len != end )
995 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
996 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
997
998 while( *p < end )
999 {
1000 if( ( end - *p ) < 1 )
1001 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1002 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1003
1004 tag = **p;
1005 (*p)++;
1006 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
1007 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
1008
1009 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
1010 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1011 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1012
1013 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
1014 {
1015 *p += tag_len;
1016 continue;
1017 }
1018
1019 buf = &(cur->buf);
1020 buf->tag = tag;
1021 buf->p = *p;
1022 buf->len = tag_len;
1023 *p += buf->len;
1024
1025 /* Allocate and assign next pointer */
1026 if (*p < end)
1027 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001028 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +00001029 sizeof( asn1_sequence ) );
1030
1031 if( cur->next == NULL )
1032 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1033 POLARSSL_ERR_ASN1_MALLOC_FAILED );
1034
Paul Bakker535e97d2012-08-23 10:49:55 +00001035 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +00001036 cur = cur->next;
1037 }
1038 }
1039
1040 /* Set final sequence entry's next pointer to NULL */
1041 cur->next = NULL;
1042
1043 if( *p != end )
1044 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1045 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1046
1047 return( 0 );
1048}
1049
1050/*
Paul Bakker74111d32011-01-15 16:57:55 +00001051 * X.509 v3 extensions
1052 *
1053 * TODO: Perform all of the basic constraints tests required by the RFC
1054 * TODO: Set values for undetected extensions to a sane default?
1055 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001056 */
1057static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001058 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001059 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001060{
Paul Bakker23986e52011-04-24 08:57:21 +00001061 int ret;
1062 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001063 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001064
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001065 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001066 {
1067 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1068 return( 0 );
1069
1070 return( ret );
1071 }
1072
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 while( *p < end )
1074 {
Paul Bakker74111d32011-01-15 16:57:55 +00001075 /*
1076 * Extension ::= SEQUENCE {
1077 * extnID OBJECT IDENTIFIER,
1078 * critical BOOLEAN DEFAULT FALSE,
1079 * extnValue OCTET STRING }
1080 */
1081 x509_buf extn_oid = {0, 0, NULL};
1082 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001083 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001084
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 if( ( ret = asn1_get_tag( p, end, &len,
1086 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001087 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001089 end_ext_data = *p + len;
1090
Paul Bakker74111d32011-01-15 16:57:55 +00001091 /* Get extension ID */
1092 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
Paul Bakker74111d32011-01-15 16:57:55 +00001094 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001095 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
Paul Bakker74111d32011-01-15 16:57:55 +00001097 extn_oid.p = *p;
1098 *p += extn_oid.len;
1099
1100 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001101 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001102 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1103
1104 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001105 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001106 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001107 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
Paul Bakker74111d32011-01-15 16:57:55 +00001109 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001110 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001112 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001114 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001115
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001116 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001117 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001118 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
Paul Bakker74111d32011-01-15 16:57:55 +00001120 /*
1121 * Detect supported extensions
1122 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001123 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1124
1125 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001126 {
1127 /* No parser found, skip extension */
1128 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001129
Paul Bakker5c721f92011-07-27 16:51:09 +00001130#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001131 if( is_critical )
1132 {
1133 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001134 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001135 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1136 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001137#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001138 continue;
1139 }
1140
1141 crt->ext_types |= ext_type;
1142
1143 switch( ext_type )
1144 {
1145 case EXT_BASIC_CONSTRAINTS:
1146 /* Parse basic constraints */
1147 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1148 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1149 return ( ret );
1150 break;
1151
1152 case EXT_KEY_USAGE:
1153 /* Parse key usage */
1154 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1155 &crt->key_usage ) ) != 0 )
1156 return ( ret );
1157 break;
1158
1159 case EXT_EXTENDED_KEY_USAGE:
1160 /* Parse extended key usage */
1161 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1162 &crt->ext_key_usage ) ) != 0 )
1163 return ( ret );
1164 break;
1165
1166 case EXT_SUBJECT_ALT_NAME:
1167 /* Parse subject alt name */
1168 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1169 &crt->subject_alt_names ) ) != 0 )
1170 return ( ret );
1171 break;
1172
1173 case EXT_NS_CERT_TYPE:
1174 /* Parse netscape certificate type */
1175 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1176 &crt->ns_cert_type ) ) != 0 )
1177 return ( ret );
1178 break;
1179
1180 default:
1181 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 }
1184
1185 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001186 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001187 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001188
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 return( 0 );
1190}
1191
1192/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001193 * X.509 CRL Entries
1194 */
1195static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001196 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001197 x509_crl_entry *entry )
1198{
Paul Bakker23986e52011-04-24 08:57:21 +00001199 int ret;
1200 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001201 x509_crl_entry *cur_entry = entry;
1202
1203 if( *p == end )
1204 return( 0 );
1205
Paul Bakker9be19372009-07-27 20:21:53 +00001206 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001207 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1208 {
1209 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1210 return( 0 );
1211
1212 return( ret );
1213 }
1214
Paul Bakker9be19372009-07-27 20:21:53 +00001215 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001216
1217 while( *p < end )
1218 {
Paul Bakker23986e52011-04-24 08:57:21 +00001219 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001220 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001221
1222 if( ( ret = asn1_get_tag( p, end, &len2,
1223 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1224 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001225 return( ret );
1226 }
1227
Paul Bakker9be19372009-07-27 20:21:53 +00001228 cur_entry->raw.tag = **p;
1229 cur_entry->raw.p = *p;
1230 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001231 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001232
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001233 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001234 return( ret );
1235
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001236 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001237 return( ret );
1238
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001239 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001240 return( ret );
1241
Paul Bakker74111d32011-01-15 16:57:55 +00001242 if ( *p < end )
1243 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001244 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001245
1246 if( cur_entry->next == NULL )
1247 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1248
Paul Bakkerd98030e2009-05-02 15:13:40 +00001249 cur_entry = cur_entry->next;
1250 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1251 }
1252 }
1253
1254 return( 0 );
1255}
1256
Paul Bakkerc70b9822013-04-07 22:00:46 +02001257static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1258 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001259{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001260 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001261
Paul Bakkerc70b9822013-04-07 22:00:46 +02001262 if( ret != 0 )
1263 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001264
Paul Bakkerc70b9822013-04-07 22:00:46 +02001265 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001266}
1267
Paul Bakkerd98030e2009-05-02 15:13:40 +00001268/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001269 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001271static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1272 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001273{
Paul Bakker23986e52011-04-24 08:57:21 +00001274 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001275 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001276 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Paul Bakker320a4b52009-03-28 18:52:39 +00001278 /*
1279 * Check for valid input
1280 */
1281 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001282 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001283
Paul Bakker6e339b52013-07-03 13:37:05 +02001284 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001285
1286 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001287 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001288
1289 memcpy( p, buf, buflen );
1290
1291 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
1293 crt->raw.p = p;
1294 crt->raw.len = len;
1295 end = p + len;
1296
1297 /*
1298 * Certificate ::= SEQUENCE {
1299 * tbsCertificate TBSCertificate,
1300 * signatureAlgorithm AlgorithmIdentifier,
1301 * signatureValue BIT STRING }
1302 */
1303 if( ( ret = asn1_get_tag( &p, end, &len,
1304 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1305 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001306 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001307 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 }
1309
Paul Bakkerb00ca422012-09-25 12:10:00 +00001310 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001313 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001314 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001316 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001317
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 /*
1319 * TBSCertificate ::= SEQUENCE {
1320 */
1321 crt->tbs.p = p;
1322
1323 if( ( ret = asn1_get_tag( &p, end, &len,
1324 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1325 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001326 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001327 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 }
1329
1330 end = p + len;
1331 crt->tbs.len = end - crt->tbs.p;
1332
1333 /*
1334 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1335 *
1336 * CertificateSerialNumber ::= INTEGER
1337 *
1338 * signature AlgorithmIdentifier
1339 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001340 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1341 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1342 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001344 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 return( ret );
1346 }
1347
1348 crt->version++;
1349
1350 if( crt->version > 3 )
1351 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001352 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001353 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
1355
Paul Bakkerc70b9822013-04-07 22:00:46 +02001356 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1357 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001359 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001360 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
1362
1363 /*
1364 * issuer Name
1365 */
1366 crt->issuer_raw.p = p;
1367
1368 if( ( ret = asn1_get_tag( &p, end, &len,
1369 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1370 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001371 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001372 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 }
1374
1375 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 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 crt->issuer_raw.len = p - crt->issuer_raw.p;
1382
1383 /*
1384 * Validity ::= SEQUENCE {
1385 * notBefore Time,
1386 * notAfter Time }
1387 *
1388 */
1389 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1390 &crt->valid_to ) ) != 0 )
1391 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001392 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 return( ret );
1394 }
1395
1396 /*
1397 * subject Name
1398 */
1399 crt->subject_raw.p = p;
1400
1401 if( ( ret = asn1_get_tag( &p, end, &len,
1402 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1403 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001404 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001405 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 }
1407
Paul Bakkercefb3962012-06-27 11:51:09 +00001408 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001410 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 return( ret );
1412 }
1413
1414 crt->subject_raw.len = p - crt->subject_raw.p;
1415
1416 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001417 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +02001419 if( ( ret = x509_get_pubkey_rsa( &p, end, &crt->rsa ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001421 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 return( ret );
1423 }
1424
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 /*
1426 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1427 * -- If present, version shall be v2 or v3
1428 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1429 * -- If present, version shall be v2 or v3
1430 * extensions [3] EXPLICIT Extensions OPTIONAL
1431 * -- If present, version shall be v3
1432 */
1433 if( crt->version == 2 || crt->version == 3 )
1434 {
1435 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1436 if( ret != 0 )
1437 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001438 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 return( ret );
1440 }
1441 }
1442
1443 if( crt->version == 2 || crt->version == 3 )
1444 {
1445 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1446 if( ret != 0 )
1447 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001448 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 return( ret );
1450 }
1451 }
1452
1453 if( crt->version == 3 )
1454 {
Paul Bakker74111d32011-01-15 16:57:55 +00001455 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 if( ret != 0 )
1457 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001458 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 return( ret );
1460 }
1461 }
1462
1463 if( p != end )
1464 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001465 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001466 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001467 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 }
1469
Paul Bakkerb00ca422012-09-25 12:10:00 +00001470 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001471
1472 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001473 * }
1474 * -- end of TBSCertificate
1475 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 * signatureAlgorithm AlgorithmIdentifier,
1477 * signatureValue BIT STRING
1478 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001479 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001481 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 return( ret );
1483 }
1484
Paul Bakker535e97d2012-08-23 10:49:55 +00001485 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1486 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001488 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001489 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 }
1491
1492 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1493 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001494 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 return( ret );
1496 }
1497
1498 if( p != end )
1499 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001500 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001501 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001502 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 }
1504
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001505 return( 0 );
1506}
1507
1508/*
Paul Bakker42c65812013-06-24 19:21:59 +02001509 * Parse one X.509 certificate in DER format from a buffer and add them to a
1510 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001511 */
Paul Bakker42c65812013-06-24 19:21:59 +02001512int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001513{
Paul Bakker42c65812013-06-24 19:21:59 +02001514 int ret;
1515 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001516
1517 /*
1518 * Check for valid input
1519 */
1520 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001521 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001522
1523 while( crt->version != 0 && crt->next != NULL )
1524 {
1525 prev = crt;
1526 crt = crt->next;
1527 }
1528
1529 /*
1530 * Add new certificate on the end of the chain if needed.
1531 */
1532 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001533 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001534 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001535
Paul Bakker7d06ad22009-05-02 15:53:56 +00001536 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001537 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001538
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001539 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001540 crt = crt->next;
1541 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
Paul Bakker42c65812013-06-24 19:21:59 +02001544 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1545 {
1546 if( prev )
1547 prev->next = NULL;
1548
1549 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001550 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001551
1552 return( ret );
1553 }
1554
1555 return( 0 );
1556}
1557
1558/*
1559 * Parse one or more PEM certificates from a buffer and add them to the chained list
1560 */
1561int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1562{
1563 int ret, success = 0, first_error = 0, total_failed = 0;
1564 int buf_format = X509_FORMAT_DER;
1565
1566 /*
1567 * Check for valid input
1568 */
1569 if( chain == NULL || buf == NULL )
1570 return( POLARSSL_ERR_X509_INVALID_INPUT );
1571
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001572 /*
1573 * Determine buffer content. Buffer contains either one DER certificate or
1574 * one or more PEM certificates.
1575 */
1576#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001577 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001578 buf_format = X509_FORMAT_PEM;
1579#endif
1580
1581 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001582 return x509parse_crt_der( chain, buf, buflen );
1583
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001584#if defined(POLARSSL_PEM_C)
1585 if( buf_format == X509_FORMAT_PEM )
1586 {
1587 pem_context pem;
1588
1589 while( buflen > 0 )
1590 {
1591 size_t use_len;
1592 pem_init( &pem );
1593
1594 ret = pem_read_buffer( &pem,
1595 "-----BEGIN CERTIFICATE-----",
1596 "-----END CERTIFICATE-----",
1597 buf, NULL, 0, &use_len );
1598
1599 if( ret == 0 )
1600 {
1601 /*
1602 * Was PEM encoded
1603 */
1604 buflen -= use_len;
1605 buf += use_len;
1606 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001607 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1608 {
1609 return( ret );
1610 }
Paul Bakker00b28602013-06-24 13:02:41 +02001611 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001612 {
1613 pem_free( &pem );
1614
Paul Bakker5ed3b342013-06-24 19:05:46 +02001615 /*
1616 * PEM header and footer were found
1617 */
1618 buflen -= use_len;
1619 buf += use_len;
1620
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001621 if( first_error == 0 )
1622 first_error = ret;
1623
1624 continue;
1625 }
1626 else
1627 break;
1628
Paul Bakker42c65812013-06-24 19:21:59 +02001629 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001630
1631 pem_free( &pem );
1632
1633 if( ret != 0 )
1634 {
1635 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001636 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001637 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001638 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001639 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001640
1641 if( first_error == 0 )
1642 first_error = ret;
1643
Paul Bakker42c65812013-06-24 19:21:59 +02001644 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001645 continue;
1646 }
1647
1648 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001649 }
1650 }
1651#endif
1652
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001653 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001654 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001655 else if( first_error )
1656 return( first_error );
1657 else
1658 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659}
1660
1661/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001662 * Parse one or more CRLs and add them to the chained list
1663 */
Paul Bakker23986e52011-04-24 08:57:21 +00001664int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001665{
Paul Bakker23986e52011-04-24 08:57:21 +00001666 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001667 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001668 unsigned char *p, *end;
1669 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001670#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001671 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001672 pem_context pem;
1673#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001674
1675 crl = chain;
1676
1677 /*
1678 * Check for valid input
1679 */
1680 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001681 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001682
1683 while( crl->version != 0 && crl->next != NULL )
1684 crl = crl->next;
1685
1686 /*
1687 * Add new CRL on the end of the chain if needed.
1688 */
1689 if ( crl->version != 0 && crl->next == NULL)
1690 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001691 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001692
Paul Bakker7d06ad22009-05-02 15:53:56 +00001693 if( crl->next == NULL )
1694 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001695 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001696 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001697 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001698
Paul Bakker7d06ad22009-05-02 15:53:56 +00001699 crl = crl->next;
1700 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001701 }
1702
Paul Bakker96743fc2011-02-12 14:30:57 +00001703#if defined(POLARSSL_PEM_C)
1704 pem_init( &pem );
1705 ret = pem_read_buffer( &pem,
1706 "-----BEGIN X509 CRL-----",
1707 "-----END X509 CRL-----",
1708 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001709
Paul Bakker96743fc2011-02-12 14:30:57 +00001710 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001711 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001712 /*
1713 * Was PEM encoded
1714 */
1715 buflen -= use_len;
1716 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001717
1718 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001719 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001720 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001721 p = pem.buf;
1722 pem.buf = NULL;
1723 len = pem.buflen;
1724 pem_free( &pem );
1725 }
Paul Bakker00b28602013-06-24 13:02:41 +02001726 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001727 {
1728 pem_free( &pem );
1729 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001730 }
1731 else
1732 {
1733 /*
1734 * nope, copy the raw DER data
1735 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001736 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001737
1738 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001739 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001740
1741 memcpy( p, buf, buflen );
1742
1743 buflen = 0;
1744 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001745#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001746 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001747
1748 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001749 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001750
1751 memcpy( p, buf, buflen );
1752
1753 buflen = 0;
1754#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755
1756 crl->raw.p = p;
1757 crl->raw.len = len;
1758 end = p + len;
1759
1760 /*
1761 * CertificateList ::= SEQUENCE {
1762 * tbsCertList TBSCertList,
1763 * signatureAlgorithm AlgorithmIdentifier,
1764 * signatureValue BIT STRING }
1765 */
1766 if( ( ret = asn1_get_tag( &p, end, &len,
1767 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1768 {
1769 x509_crl_free( crl );
1770 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1771 }
1772
Paul Bakker23986e52011-04-24 08:57:21 +00001773 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001774 {
1775 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001776 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001777 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1778 }
1779
1780 /*
1781 * TBSCertList ::= SEQUENCE {
1782 */
1783 crl->tbs.p = p;
1784
1785 if( ( ret = asn1_get_tag( &p, end, &len,
1786 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1787 {
1788 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001789 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790 }
1791
1792 end = p + len;
1793 crl->tbs.len = end - crl->tbs.p;
1794
1795 /*
1796 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1797 * -- if present, MUST be v2
1798 *
1799 * signature AlgorithmIdentifier
1800 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001801 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001802 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001803 {
1804 x509_crl_free( crl );
1805 return( ret );
1806 }
1807
1808 crl->version++;
1809
1810 if( crl->version > 2 )
1811 {
1812 x509_crl_free( crl );
1813 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1814 }
1815
Paul Bakkerc70b9822013-04-07 22:00:46 +02001816 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1817 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001818 {
1819 x509_crl_free( crl );
1820 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1821 }
1822
1823 /*
1824 * issuer Name
1825 */
1826 crl->issuer_raw.p = p;
1827
1828 if( ( ret = asn1_get_tag( &p, end, &len,
1829 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1830 {
1831 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001832 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001833 }
1834
1835 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1836 {
1837 x509_crl_free( crl );
1838 return( ret );
1839 }
1840
1841 crl->issuer_raw.len = p - crl->issuer_raw.p;
1842
1843 /*
1844 * thisUpdate Time
1845 * nextUpdate Time OPTIONAL
1846 */
Paul Bakker91200182010-02-18 21:26:15 +00001847 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001848 {
1849 x509_crl_free( crl );
1850 return( ret );
1851 }
1852
Paul Bakker91200182010-02-18 21:26:15 +00001853 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001854 {
Paul Bakker9d781402011-05-09 16:17:09 +00001855 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001856 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001857 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001858 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001859 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001860 x509_crl_free( crl );
1861 return( ret );
1862 }
1863 }
1864
1865 /*
1866 * revokedCertificates SEQUENCE OF SEQUENCE {
1867 * userCertificate CertificateSerialNumber,
1868 * revocationDate Time,
1869 * crlEntryExtensions Extensions OPTIONAL
1870 * -- if present, MUST be v2
1871 * } OPTIONAL
1872 */
1873 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1874 {
1875 x509_crl_free( crl );
1876 return( ret );
1877 }
1878
1879 /*
1880 * crlExtensions EXPLICIT Extensions OPTIONAL
1881 * -- if present, MUST be v2
1882 */
1883 if( crl->version == 2 )
1884 {
1885 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1886
1887 if( ret != 0 )
1888 {
1889 x509_crl_free( crl );
1890 return( ret );
1891 }
1892 }
1893
1894 if( p != end )
1895 {
1896 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001897 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001898 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1899 }
1900
1901 end = crl->raw.p + crl->raw.len;
1902
1903 /*
1904 * signatureAlgorithm AlgorithmIdentifier,
1905 * signatureValue BIT STRING
1906 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001907 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001908 {
1909 x509_crl_free( crl );
1910 return( ret );
1911 }
1912
Paul Bakker535e97d2012-08-23 10:49:55 +00001913 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1914 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001915 {
1916 x509_crl_free( crl );
1917 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1918 }
1919
1920 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1921 {
1922 x509_crl_free( crl );
1923 return( ret );
1924 }
1925
1926 if( p != end )
1927 {
1928 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001929 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001930 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1931 }
1932
1933 if( buflen > 0 )
1934 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001935 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001936
Paul Bakker7d06ad22009-05-02 15:53:56 +00001937 if( crl->next == NULL )
1938 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001939 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001940 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001941 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001942
Paul Bakker7d06ad22009-05-02 15:53:56 +00001943 crl = crl->next;
1944 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001945
1946 return( x509parse_crl( crl, buf, buflen ) );
1947 }
1948
1949 return( 0 );
1950}
1951
Paul Bakker335db3f2011-04-25 15:28:35 +00001952#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001953/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001954 * Load all data from a file into a given buffer.
1955 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001956static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001957{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001958 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001959
Paul Bakkerd98030e2009-05-02 15:13:40 +00001960 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001961 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001962
Paul Bakkerd98030e2009-05-02 15:13:40 +00001963 fseek( f, 0, SEEK_END );
1964 *n = (size_t) ftell( f );
1965 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001966
Paul Bakker6e339b52013-07-03 13:37:05 +02001967 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001968 {
1969 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001970 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001971 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001972
Paul Bakkerd98030e2009-05-02 15:13:40 +00001973 if( fread( *buf, 1, *n, f ) != *n )
1974 {
1975 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001976 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001977 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001978 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001979
Paul Bakkerd98030e2009-05-02 15:13:40 +00001980 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001981
Paul Bakkerd98030e2009-05-02 15:13:40 +00001982 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001983
Paul Bakkerd98030e2009-05-02 15:13:40 +00001984 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001985}
1986
1987/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 * Load one or more certificates and add them to the chained list
1989 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001990int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001991{
1992 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 size_t n;
1994 unsigned char *buf;
1995
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001996 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001997 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
Paul Bakker69e095c2011-12-10 21:55:01 +00001999 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
2001 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002002 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002003
2004 return( ret );
2005}
2006
Paul Bakker8d914582012-06-04 12:46:42 +00002007int x509parse_crtpath( x509_cert *chain, const char *path )
2008{
2009 int ret = 0;
2010#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00002011 int w_ret;
2012 WCHAR szDir[MAX_PATH];
2013 char filename[MAX_PATH];
2014 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002015 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00002016
Paul Bakker97872ac2012-11-02 12:53:26 +00002017 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00002018 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002019
2020 if( len > MAX_PATH - 3 )
2021 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00002022
Paul Bakker3338b792012-10-01 21:13:10 +00002023 memset( szDir, 0, sizeof(szDir) );
2024 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002025 memcpy( filename, path, len );
2026 filename[len++] = '\\';
2027 p = filename + len;
2028 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002029
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002030 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002031
Paul Bakker97872ac2012-11-02 12:53:26 +00002032 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002033 if (hFind == INVALID_HANDLE_VALUE)
2034 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2035
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002036 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002037 do
2038 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002039 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002040
Paul Bakkere4791f32012-06-04 21:29:15 +00002041 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002042 continue;
2043
Paul Bakker3338b792012-10-01 21:13:10 +00002044 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2045 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002046 p, len - 1,
2047 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002048
Paul Bakker3338b792012-10-01 21:13:10 +00002049 w_ret = x509parse_crtfile( chain, filename );
2050 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002051 ret++;
2052 else
2053 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002054 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002055 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002056
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002057 if (GetLastError() != ERROR_NO_MORE_FILES)
2058 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002059
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002060cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002061 FindClose( hFind );
2062#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002063 int t_ret, i;
2064 struct stat sb;
2065 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002066 char entry_name[255];
2067 DIR *dir = opendir( path );
2068
2069 if( dir == NULL)
2070 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2071
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002072 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002073 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002074 if( result == NULL )
2075 break;
2076
2077 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2078
2079 i = stat( entry_name, &sb );
2080
2081 if( i == -1 )
2082 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2083
2084 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002085 continue;
2086
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002087 // Ignore parse errors
2088 //
Paul Bakker8d914582012-06-04 12:46:42 +00002089 t_ret = x509parse_crtfile( chain, entry_name );
2090 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002091 ret++;
2092 else
2093 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002094 }
2095 closedir( dir );
2096#endif
2097
2098 return( ret );
2099}
2100
Paul Bakkerd98030e2009-05-02 15:13:40 +00002101/*
2102 * Load one or more CRLs and add them to the chained list
2103 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002104int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002105{
2106 int ret;
2107 size_t n;
2108 unsigned char *buf;
2109
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002110 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002111 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002112
Paul Bakker27fdf462011-06-09 13:55:13 +00002113 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002114
2115 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002116 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002117
2118 return( ret );
2119}
2120
Paul Bakker5121ce52009-01-03 21:22:43 +00002121/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002122 * Load and parse a private RSA key
2123 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002124int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002125{
2126 int ret;
2127 size_t n;
2128 unsigned char *buf;
2129
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002130 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002131 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002132
2133 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002134 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002135 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002136 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002137 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002138
2139 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002140 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002141
2142 return( ret );
2143}
2144
2145/*
2146 * Load and parse a public RSA key
2147 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002148int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002149{
2150 int ret;
2151 size_t n;
2152 unsigned char *buf;
2153
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002154 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002155 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002156
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002157 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002158
2159 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002160 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002161
2162 return( ret );
2163}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002164
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002165/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002166 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002167 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002168int x509parse_keyfile( pk_context *ctx,
2169 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002170{
2171 int ret;
2172 size_t n;
2173 unsigned char *buf;
2174
2175 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2176 return( ret );
2177
2178 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002179 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002180 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002181 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002182 (const unsigned char *) pwd, strlen( pwd ) );
2183
2184 memset( buf, 0, n + 1 );
2185 free( buf );
2186
2187 return( ret );
2188}
2189
2190/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002191 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002192 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002193int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002194{
2195 int ret;
2196 size_t n;
2197 unsigned char *buf;
2198
2199 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2200 return( ret );
2201
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002202 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002203
2204 memset( buf, 0, n + 1 );
2205 free( buf );
2206
2207 return( ret );
2208}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002209
Paul Bakker335db3f2011-04-25 15:28:35 +00002210#endif /* POLARSSL_FS_IO */
2211
2212/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002213 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002215static int x509parse_key_pkcs1_der( rsa_context *rsa,
2216 const unsigned char *key,
2217 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002218{
Paul Bakker23986e52011-04-24 08:57:21 +00002219 int ret;
2220 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002222
Paul Bakker96743fc2011-02-12 14:30:57 +00002223 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002224 end = p + keylen;
2225
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002227 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002228 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 * RSAPrivateKey ::= SEQUENCE {
2230 * version Version,
2231 * modulus INTEGER, -- n
2232 * publicExponent INTEGER, -- e
2233 * privateExponent INTEGER, -- d
2234 * prime1 INTEGER, -- p
2235 * prime2 INTEGER, -- q
2236 * exponent1 INTEGER, -- d mod (p-1)
2237 * exponent2 INTEGER, -- d mod (q-1)
2238 * coefficient INTEGER, -- (inverse of q) mod p
2239 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2240 * }
2241 */
2242 if( ( ret = asn1_get_tag( &p, end, &len,
2243 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2244 {
Paul Bakker9d781402011-05-09 16:17:09 +00002245 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 }
2247
2248 end = p + len;
2249
2250 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2251 {
Paul Bakker9d781402011-05-09 16:17:09 +00002252 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
2254
2255 if( rsa->ver != 0 )
2256 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002257 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 }
2259
2260 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2261 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2262 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2263 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2264 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2265 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2266 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2267 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2268 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002270 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 }
2272
2273 rsa->len = mpi_size( &rsa->N );
2274
2275 if( p != end )
2276 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002278 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002279 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 }
2281
2282 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2283 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 rsa_free( rsa );
2285 return( ret );
2286 }
2287
Paul Bakkere2f50402013-06-24 19:00:59 +02002288 return( 0 );
2289}
2290
2291/*
2292 * Parse an unencrypted PKCS#8 encoded private RSA key
2293 */
2294static int x509parse_key_pkcs8_unencrypted_der(
2295 rsa_context *rsa,
2296 const unsigned char *key,
2297 size_t keylen )
2298{
2299 int ret;
2300 size_t len;
2301 unsigned char *p, *end;
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002302 x509_buf alg_params;
Paul Bakkere2f50402013-06-24 19:00:59 +02002303 pk_type_t pk_alg = POLARSSL_PK_NONE;
2304
2305 p = (unsigned char *) key;
2306 end = p + keylen;
2307
2308 /*
2309 * This function parses the PrivatKeyInfo object (PKCS#8)
2310 *
2311 * PrivateKeyInfo ::= SEQUENCE {
2312 * version Version,
2313 * algorithm AlgorithmIdentifier,
2314 * PrivateKey BIT STRING
2315 * }
2316 *
2317 * AlgorithmIdentifier ::= SEQUENCE {
2318 * algorithm OBJECT IDENTIFIER,
2319 * parameters ANY DEFINED BY algorithm OPTIONAL
2320 * }
2321 *
2322 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2323 */
2324 if( ( ret = asn1_get_tag( &p, end, &len,
2325 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2326 {
2327 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2328 }
2329
2330 end = p + len;
2331
2332 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2333 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2334
2335 if( rsa->ver != 0 )
2336 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2337
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002338 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002339 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2340
2341 /*
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002342 * We explicitly want RSA keys only
Paul Bakkere2f50402013-06-24 19:00:59 +02002343 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002344 if (pk_alg != POLARSSL_PK_RSA )
2345 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
2346
Paul Bakkere2f50402013-06-24 19:00:59 +02002347 /*
2348 * Get the OCTET STRING and parse the PKCS#1 format inside
2349 */
2350 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2351 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2352
2353 if( ( end - p ) < 1 )
2354 {
2355 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2356 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2357 }
2358
2359 end = p + len;
2360
2361 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2362 return( ret );
2363
2364 return( 0 );
2365}
2366
2367/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002368 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002369 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002370static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2371 size_t *used_len,
2372 const unsigned char *key, size_t keylen,
2373 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002374{
2375 int ret;
2376 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002377 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002378 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002379#if defined(POLARSSL_PKCS12_C)
2380 cipher_type_t cipher_alg;
2381 md_type_t md_alg;
2382#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002383
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002384 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002385
2386 p = (unsigned char *) key;
2387 end = p + keylen;
2388
Paul Bakker28144de2013-06-24 19:28:55 +02002389 if( pwdlen == 0 )
2390 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2391
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002392 /*
2393 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2394 *
2395 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2396 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2397 * encryptedData EncryptedData
2398 * }
2399 *
2400 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2401 *
2402 * EncryptedData ::= OCTET STRING
2403 *
2404 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2405 */
2406 if( ( ret = asn1_get_tag( &p, end, &len,
2407 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2408 {
2409 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2410 }
2411
2412 end = p + len;
2413
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002414 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002415 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002416
2417 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2418 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2419
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002420 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002421 return( POLARSSL_ERR_X509_INVALID_INPUT );
2422
2423 /*
2424 * Decrypt EncryptedData with appropriate PDE
2425 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002426#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002427 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002428 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002429 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002430 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002431 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002432 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002433 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2434 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2435
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002436 return( ret );
2437 }
2438 }
2439 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2440 {
2441 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2442 PKCS12_PBE_DECRYPT,
2443 pwd, pwdlen,
2444 p, len, buf ) ) != 0 )
2445 {
2446 return( ret );
2447 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002448
2449 // Best guess for password mismatch when using RC4. If first tag is
2450 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2451 //
2452 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2453 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002454 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002455 else
2456#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002457#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002458 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002459 {
2460 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2461 p, len, buf ) ) != 0 )
2462 {
2463 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2464 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2465
2466 return( ret );
2467 }
2468 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002469 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002470#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002471 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2472
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002473 *used_len = len;
2474 return( 0 );
2475}
2476
2477/*
2478 * Parse an encrypted PKCS#8 encoded private RSA key
2479 */
2480static int x509parse_key_pkcs8_encrypted_der(
2481 rsa_context *rsa,
2482 const unsigned char *key, size_t keylen,
2483 const unsigned char *pwd, size_t pwdlen )
2484{
2485 int ret;
2486 unsigned char buf[2048];
2487 size_t len = 0;
2488
2489 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2490 key, keylen, pwd, pwdlen ) ) != 0 )
2491 {
2492 return( ret );
2493 }
2494
2495 return( x509parse_key_pkcs8_unencrypted_der( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002496}
2497
2498/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002499 * Parse a private RSA key
2500 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002501int x509parse_key_rsa( rsa_context *rsa,
2502 const unsigned char *key, size_t keylen,
2503 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002504{
2505 int ret;
2506
Paul Bakker96743fc2011-02-12 14:30:57 +00002507#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002508 size_t len;
2509 pem_context pem;
2510
2511 pem_init( &pem );
2512 ret = pem_read_buffer( &pem,
2513 "-----BEGIN RSA PRIVATE KEY-----",
2514 "-----END RSA PRIVATE KEY-----",
2515 key, pwd, pwdlen, &len );
2516 if( ret == 0 )
2517 {
2518 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2519 {
2520 rsa_free( rsa );
2521 }
2522
2523 pem_free( &pem );
2524 return( ret );
2525 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002526 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2527 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2528 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2529 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002530 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002531 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002532
2533 ret = pem_read_buffer( &pem,
2534 "-----BEGIN PRIVATE KEY-----",
2535 "-----END PRIVATE KEY-----",
2536 key, NULL, 0, &len );
2537 if( ret == 0 )
2538 {
2539 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2540 pem.buf, pem.buflen ) ) != 0 )
2541 {
2542 rsa_free( rsa );
2543 }
2544
2545 pem_free( &pem );
2546 return( ret );
2547 }
2548 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002549 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002550
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002551 ret = pem_read_buffer( &pem,
2552 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2553 "-----END ENCRYPTED PRIVATE KEY-----",
2554 key, NULL, 0, &len );
2555 if( ret == 0 )
2556 {
2557 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2558 pem.buf, pem.buflen,
2559 pwd, pwdlen ) ) != 0 )
2560 {
2561 rsa_free( rsa );
2562 }
2563
2564 pem_free( &pem );
2565 return( ret );
2566 }
2567 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002568 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002569#else
2570 ((void) pwd);
2571 ((void) pwdlen);
2572#endif /* POLARSSL_PEM_C */
2573
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002574 /*
2575 * At this point we only know it's not a PEM formatted key. Could be any
2576 * of the known DER encoded private key formats
2577 *
2578 * We try the different DER format parsers to see if one passes without
2579 * error
2580 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002581 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2582 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002583 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002584 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002587 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002588
2589 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2590 {
2591 return( ret );
2592 }
2593
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002594 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2595 return( 0 );
2596
2597 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002598
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002599 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2600 return( 0 );
2601
2602 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002603
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002604 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605}
2606
2607/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002608 * Parse a public RSA key
2609 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002610int x509parse_public_key_rsa( rsa_context *rsa,
2611 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002612{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002613 pk_context pk_ctx;
Paul Bakker53019ae2011-03-25 13:58:48 +00002614
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002615 pk_init( &pk_ctx );
2616 pk_wrap_rsa( &pk_ctx, rsa );
Paul Bakker53019ae2011-03-25 13:58:48 +00002617
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002618 return( x509parse_public_key( &pk_ctx, key, keylen ) );
Paul Bakker53019ae2011-03-25 13:58:48 +00002619}
2620
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002621#if defined(POLARSSL_ECP_C)
2622/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002623 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002624 */
2625static int x509parse_key_sec1_der( ecp_keypair *eck,
2626 const unsigned char *key,
2627 size_t keylen )
2628{
2629 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002630 int version;
2631 size_t len;
2632 ecp_group_id grp_id;
2633 unsigned char *p = (unsigned char *) key;
2634 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002635
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002636 /*
2637 * RFC 5915, orf SEC1 Appendix C.4
2638 *
2639 * ECPrivateKey ::= SEQUENCE {
2640 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2641 * privateKey OCTET STRING,
2642 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2643 * publicKey [1] BIT STRING OPTIONAL
2644 * }
2645 */
2646 if( ( ret = asn1_get_tag( &p, end, &len,
2647 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2648 {
2649 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2650 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002651
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002652 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002653
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002654 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2655 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002656
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002657 if( version != 1 )
2658 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2659
2660 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2661 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2662
2663 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2664 {
2665 ecp_keypair_free( eck );
2666 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2667 }
2668
2669 p += len;
2670
2671 /*
2672 * Is 'parameters' present?
2673 */
2674 if( ( ret = asn1_get_tag( &p, end, &len,
2675 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2676 {
2677 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2678 return( ret );
2679
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002680 /*
2681 * If we're wrapped in a bigger structure (eg PKCS#8), grp may have been
2682 * defined externally. In this case, make sure both definitions match.
2683 */
2684 if( eck->grp.id != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002685 {
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002686 if( eck->grp.id != grp_id )
2687 {
2688 ecp_keypair_free( eck );
2689 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2690 }
2691 }
2692 else
2693 {
2694 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2695 {
2696 ecp_keypair_free( eck );
2697 return( ret );
2698 }
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002699 }
2700 }
2701 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2702 {
2703 ecp_keypair_free( eck );
2704 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2705 }
2706
2707 /*
2708 * Is 'publickey' present?
2709 */
2710 if( ( ret = asn1_get_tag( &p, end, &len,
2711 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2712 {
2713 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2714 != 0 )
2715 {
2716 ecp_keypair_free( eck );
2717 return( ret );
2718 }
2719
2720 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2721 {
2722 ecp_keypair_free( eck );
2723 return( ret );
2724 }
2725 }
2726 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2727 {
2728 ecp_keypair_free( eck );
2729 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2730 }
2731
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002732 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002733 {
2734 ecp_keypair_free( eck );
2735 return( ret );
2736 }
2737
2738 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002739}
2740
2741/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002742 * Parse an unencrypted PKCS#8 encoded private EC key
2743 */
2744static int x509parse_key_pkcs8_unencrypted_der_ec(
2745 ecp_keypair *eck,
2746 const unsigned char* key,
2747 size_t keylen )
2748{
2749 int ret, version;
2750 size_t len;
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002751 x509_buf alg_params;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002752 ecp_group_id grp_id;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002753 unsigned char *p = (unsigned char *) key;
2754 unsigned char *end = p + keylen;
2755 pk_type_t pk_alg = POLARSSL_PK_NONE;
2756
2757 /*
2758 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2759 *
2760 * PrivateKeyInfo ::= SEQUENCE {
2761 * version Version,
2762 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2763 * privateKey PrivateKey,
2764 * attributes [0] IMPLICIT Attributes OPTIONAL }
2765 *
2766 * Version ::= INTEGER
2767 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2768 * PrivateKey ::= OCTET STRING
2769 *
2770 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2771 */
2772
2773 if( ( ret = asn1_get_tag( &p, end, &len,
2774 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2775 {
2776 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2777 }
2778
2779 end = p + len;
2780
2781 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2782 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2783
2784 if( version != 0 )
2785 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2786
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +02002787 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002788 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2789
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002790 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002791 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002792
2793 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2794 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2795
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002796 if( ( ret = x509_ecparams_get_grp_id( &alg_params, &grp_id ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002797 {
2798 ecp_keypair_free( eck );
2799 return( ret );
2800 }
2801
2802 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2803 {
2804 ecp_keypair_free( eck );
2805 return( ret );
2806 }
2807
2808 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2809 {
2810 ecp_keypair_free( eck );
2811 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2812 }
2813
2814 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2815 {
2816 ecp_keypair_free( eck );
2817 return( ret );
2818 }
2819
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002820 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002821 {
2822 ecp_keypair_free( eck );
2823 return( ret );
2824 }
2825
2826 return 0;
2827}
2828
2829/*
2830 * Parse an encrypted PKCS#8 encoded private EC key
2831 */
2832static int x509parse_key_pkcs8_encrypted_der_ec(
2833 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002834 const unsigned char *key, size_t keylen,
2835 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002836{
2837 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002838 unsigned char buf[2048];
2839 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002840
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002841 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2842 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002843 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002844 return( ret );
2845 }
2846
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002847 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002848}
2849
2850/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002851 * Parse a private EC key
2852 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002853static int x509parse_key_ec( ecp_keypair *eck,
2854 const unsigned char *key, size_t keylen,
2855 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002856{
2857 int ret;
2858
2859#if defined(POLARSSL_PEM_C)
2860 size_t len;
2861 pem_context pem;
2862
2863 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002864 ret = pem_read_buffer( &pem,
2865 "-----BEGIN EC PRIVATE KEY-----",
2866 "-----END EC PRIVATE KEY-----",
2867 key, pwd, pwdlen, &len );
2868 if( ret == 0 )
2869 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002870 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002871 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002872 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002873 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002874
2875 pem_free( &pem );
2876 return( ret );
2877 }
2878 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2879 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2880 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2881 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2882 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2883 return( ret );
2884
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002885 ret = pem_read_buffer( &pem,
2886 "-----BEGIN PRIVATE KEY-----",
2887 "-----END PRIVATE KEY-----",
2888 key, NULL, 0, &len );
2889 if( ret == 0 )
2890 {
2891 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2892 pem.buf, pem.buflen ) ) != 0 )
2893 {
2894 ecp_keypair_free( eck );
2895 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002896
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002897 pem_free( &pem );
2898 return( ret );
2899 }
2900 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2901 return( ret );
2902
2903 ret = pem_read_buffer( &pem,
2904 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2905 "-----END ENCRYPTED PRIVATE KEY-----",
2906 key, NULL, 0, &len );
2907 if( ret == 0 )
2908 {
2909 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2910 pem.buf, pem.buflen,
2911 pwd, pwdlen ) ) != 0 )
2912 {
2913 ecp_keypair_free( eck );
2914 }
2915
2916 pem_free( &pem );
2917 return( ret );
2918 }
2919 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2920 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002921#else
2922 ((void) pwd);
2923 ((void) pwdlen);
2924#endif /* POLARSSL_PEM_C */
2925
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002926 /*
2927 * At this point we only know it's not a PEM formatted key. Could be any
2928 * of the known DER encoded private key formats
2929 *
2930 * We try the different DER format parsers to see if one passes without
2931 * error
2932 */
2933 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2934 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002935 {
2936 return( 0 );
2937 }
2938
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002939 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002940
2941 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2942 {
2943 return( ret );
2944 }
2945
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002946 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2947 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002948 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002949
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002950 ecp_keypair_free( eck );
2951
2952 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2953 return( 0 );
2954
2955 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002956
2957 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2958}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002959#endif /* defined(POLARSSL_ECP_C) */
2960
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002961/*
2962 * Parse a private key
2963 */
2964int x509parse_key( pk_context *ctx,
2965 const unsigned char *key, size_t keylen,
2966 const unsigned char *pwd, size_t pwdlen )
2967{
2968 int ret;
2969
2970 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
2971 return( ret );
2972
2973 if( ( ret = x509parse_key_rsa( ctx->data, key, keylen, pwd, pwdlen ) )
2974 == 0 )
2975 {
2976 return( 0 );
2977 }
2978
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002979 pk_free( ctx );
2980
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002981 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
2982 return( ret );
2983
2984 if( ( ret = x509parse_key_ec( ctx->data, key, keylen, pwd, pwdlen ) ) == 0 )
2985 {
2986 return( 0 );
2987 }
2988
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002989 pk_free( ctx );
2990
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002991 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
2992}
2993
2994/*
2995 * Parse a public key
2996 */
2997int x509parse_public_key( pk_context *ctx,
2998 const unsigned char *key, size_t keylen )
2999{
3000 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003001 unsigned char *p;
3002#if defined(POLARSSL_PEM_C)
3003 size_t len;
3004 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003005
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003006 pem_init( &pem );
3007 ret = pem_read_buffer( &pem,
3008 "-----BEGIN PUBLIC KEY-----",
3009 "-----END PUBLIC KEY-----",
3010 key, NULL, 0, &len );
3011
3012 if( ret == 0 )
3013 {
3014 /*
3015 * Was PEM encoded
3016 */
3017 key = pem.buf;
3018 keylen = pem.buflen;
3019 }
3020 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3021 {
3022 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003023 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003024 }
3025#endif
3026 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003027
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003028 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003029
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003030#if defined(POLARSSL_PEM_C)
3031 pem_free( &pem );
3032#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003033
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003034 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003035}
3036
Paul Bakkereaa89f82011-04-04 21:36:15 +00003037#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003038/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003039 * Parse DHM parameters
3040 */
Paul Bakker23986e52011-04-24 08:57:21 +00003041int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003042{
Paul Bakker23986e52011-04-24 08:57:21 +00003043 int ret;
3044 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003045 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003046#if defined(POLARSSL_PEM_C)
3047 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003048
Paul Bakker96743fc2011-02-12 14:30:57 +00003049 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003050
Paul Bakker96743fc2011-02-12 14:30:57 +00003051 ret = pem_read_buffer( &pem,
3052 "-----BEGIN DH PARAMETERS-----",
3053 "-----END DH PARAMETERS-----",
3054 dhmin, NULL, 0, &dhminlen );
3055
3056 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003057 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003058 /*
3059 * Was PEM encoded
3060 */
3061 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003062 }
Paul Bakker00b28602013-06-24 13:02:41 +02003063 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003064 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003065 pem_free( &pem );
3066 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003067 }
3068
Paul Bakker96743fc2011-02-12 14:30:57 +00003069 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3070#else
3071 p = (unsigned char *) dhmin;
3072#endif
3073 end = p + dhminlen;
3074
Paul Bakker1b57b062011-01-06 15:48:19 +00003075 memset( dhm, 0, sizeof( dhm_context ) );
3076
Paul Bakker1b57b062011-01-06 15:48:19 +00003077 /*
3078 * DHParams ::= SEQUENCE {
3079 * prime INTEGER, -- P
3080 * generator INTEGER, -- g
3081 * }
3082 */
3083 if( ( ret = asn1_get_tag( &p, end, &len,
3084 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3085 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003086#if defined(POLARSSL_PEM_C)
3087 pem_free( &pem );
3088#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003089 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003090 }
3091
3092 end = p + len;
3093
3094 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3095 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3096 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003097#if defined(POLARSSL_PEM_C)
3098 pem_free( &pem );
3099#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003100 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003101 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003102 }
3103
3104 if( p != end )
3105 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003106#if defined(POLARSSL_PEM_C)
3107 pem_free( &pem );
3108#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003109 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003110 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003111 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3112 }
3113
Paul Bakker96743fc2011-02-12 14:30:57 +00003114#if defined(POLARSSL_PEM_C)
3115 pem_free( &pem );
3116#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003117
3118 return( 0 );
3119}
3120
Paul Bakker335db3f2011-04-25 15:28:35 +00003121#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003122/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003123 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003124 */
3125int x509parse_dhmfile( dhm_context *dhm, const char *path )
3126{
3127 int ret;
3128 size_t n;
3129 unsigned char *buf;
3130
Paul Bakker69e095c2011-12-10 21:55:01 +00003131 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3132 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003133
Paul Bakker27fdf462011-06-09 13:55:13 +00003134 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003135
3136 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003137 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003138
3139 return( ret );
3140}
Paul Bakker335db3f2011-04-25 15:28:35 +00003141#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003142#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003143
Paul Bakker5121ce52009-01-03 21:22:43 +00003144#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003145#include <stdarg.h>
3146
3147#if !defined vsnprintf
3148#define vsnprintf _vsnprintf
3149#endif // vsnprintf
3150
3151/*
3152 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3153 * Result value is not size of buffer needed, but -1 if no fit is possible.
3154 *
3155 * This fuction tries to 'fix' this by at least suggesting enlarging the
3156 * size by 20.
3157 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003158static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003159{
3160 va_list ap;
3161 int res = -1;
3162
3163 va_start( ap, format );
3164
3165 res = vsnprintf( str, size, format, ap );
3166
3167 va_end( ap );
3168
3169 // No quick fix possible
3170 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003171 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003172
3173 return res;
3174}
3175
3176#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003177#endif
3178
Paul Bakkerd98030e2009-05-02 15:13:40 +00003179#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3180
3181#define SAFE_SNPRINTF() \
3182{ \
3183 if( ret == -1 ) \
3184 return( -1 ); \
3185 \
Paul Bakker23986e52011-04-24 08:57:21 +00003186 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003187 p[n - 1] = '\0'; \
3188 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3189 } \
3190 \
Paul Bakker23986e52011-04-24 08:57:21 +00003191 n -= (unsigned int) ret; \
3192 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003193}
3194
Paul Bakker5121ce52009-01-03 21:22:43 +00003195/*
3196 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003197 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003199int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003200{
Paul Bakker23986e52011-04-24 08:57:21 +00003201 int ret;
3202 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003204 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003205 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 char s[128], *p;
3207
3208 memset( s, 0, sizeof( s ) );
3209
3210 name = dn;
3211 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003212 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
3214 while( name != NULL )
3215 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003216 if( !name->oid.p )
3217 {
3218 name = name->next;
3219 continue;
3220 }
3221
Paul Bakker74111d32011-01-15 16:57:55 +00003222 if( name != dn )
3223 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003224 ret = snprintf( p, n, ", " );
3225 SAFE_SNPRINTF();
3226 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
Paul Bakkerc70b9822013-04-07 22:00:46 +02003228 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003229
Paul Bakkerc70b9822013-04-07 22:00:46 +02003230 if( ret == 0 )
3231 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003233 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003234 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
3236 for( i = 0; i < name->val.len; i++ )
3237 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003238 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 break;
3240
3241 c = name->val.p[i];
3242 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3243 s[i] = '?';
3244 else s[i] = c;
3245 }
3246 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003247 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003248 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 name = name->next;
3250 }
3251
Paul Bakker23986e52011-04-24 08:57:21 +00003252 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253}
3254
3255/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003256 * Store the serial in printable form into buf; no more
3257 * than size characters will be written
3258 */
3259int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3260{
Paul Bakker23986e52011-04-24 08:57:21 +00003261 int ret;
3262 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003263 char *p;
3264
3265 p = buf;
3266 n = size;
3267
3268 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003269 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003270
3271 for( i = 0; i < nr; i++ )
3272 {
Paul Bakker93048802011-12-05 14:38:06 +00003273 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003274 continue;
3275
Paul Bakkerdd476992011-01-16 21:34:59 +00003276 ret = snprintf( p, n, "%02X%s",
3277 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3278 SAFE_SNPRINTF();
3279 }
3280
Paul Bakker03c7c252011-11-25 12:37:37 +00003281 if( nr != serial->len )
3282 {
3283 ret = snprintf( p, n, "...." );
3284 SAFE_SNPRINTF();
3285 }
3286
Paul Bakker23986e52011-04-24 08:57:21 +00003287 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003288}
3289
3290/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003291 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003293int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3294 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003295{
Paul Bakker23986e52011-04-24 08:57:21 +00003296 int ret;
3297 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003298 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003299 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
3301 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003302 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
Paul Bakkerd98030e2009-05-02 15:13:40 +00003304 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003306 SAFE_SNPRINTF();
3307 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003309 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003310
Paul Bakkerdd476992011-01-16 21:34:59 +00003311 ret = x509parse_serial_gets( p, n, &crt->serial);
3312 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003313
Paul Bakkerd98030e2009-05-02 15:13:40 +00003314 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3315 SAFE_SNPRINTF();
3316 ret = x509parse_dn_gets( p, n, &crt->issuer );
3317 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003318
Paul Bakkerd98030e2009-05-02 15:13:40 +00003319 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3320 SAFE_SNPRINTF();
3321 ret = x509parse_dn_gets( p, n, &crt->subject );
3322 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
Paul Bakkerd98030e2009-05-02 15:13:40 +00003324 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3326 crt->valid_from.year, crt->valid_from.mon,
3327 crt->valid_from.day, crt->valid_from.hour,
3328 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003329 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003330
Paul Bakkerd98030e2009-05-02 15:13:40 +00003331 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3333 crt->valid_to.year, crt->valid_to.mon,
3334 crt->valid_to.day, crt->valid_to.hour,
3335 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003336 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003337
Paul Bakkerc70b9822013-04-07 22:00:46 +02003338 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003339 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003340
Paul Bakkerc70b9822013-04-07 22:00:46 +02003341 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3342 if( ret != 0 )
3343 ret = snprintf( p, n, "???" );
3344 else
3345 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003346 SAFE_SNPRINTF();
3347
3348 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003349 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003350 SAFE_SNPRINTF();
3351
Paul Bakker23986e52011-04-24 08:57:21 +00003352 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003353}
3354
Paul Bakker74111d32011-01-15 16:57:55 +00003355/*
3356 * Return an informational string describing the given OID
3357 */
3358const char *x509_oid_get_description( x509_buf *oid )
3359{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003360 const char *desc = NULL;
3361 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003362
Paul Bakkerc70b9822013-04-07 22:00:46 +02003363 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003364
Paul Bakkerc70b9822013-04-07 22:00:46 +02003365 if( ret != 0 )
3366 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003367
Paul Bakkerc70b9822013-04-07 22:00:46 +02003368 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003369}
3370
3371/* Return the x.y.z.... style numeric string for the given OID */
3372int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3373{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003374 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003375}
3376
Paul Bakkerd98030e2009-05-02 15:13:40 +00003377/*
3378 * Return an informational string about the CRL.
3379 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003380int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3381 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003382{
Paul Bakker23986e52011-04-24 08:57:21 +00003383 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003384 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003385 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003386 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003387 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003388
3389 p = buf;
3390 n = size;
3391
3392 ret = snprintf( p, n, "%sCRL version : %d",
3393 prefix, crl->version );
3394 SAFE_SNPRINTF();
3395
3396 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3397 SAFE_SNPRINTF();
3398 ret = x509parse_dn_gets( p, n, &crl->issuer );
3399 SAFE_SNPRINTF();
3400
3401 ret = snprintf( p, n, "\n%sthis update : " \
3402 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3403 crl->this_update.year, crl->this_update.mon,
3404 crl->this_update.day, crl->this_update.hour,
3405 crl->this_update.min, crl->this_update.sec );
3406 SAFE_SNPRINTF();
3407
3408 ret = snprintf( p, n, "\n%snext update : " \
3409 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3410 crl->next_update.year, crl->next_update.mon,
3411 crl->next_update.day, crl->next_update.hour,
3412 crl->next_update.min, crl->next_update.sec );
3413 SAFE_SNPRINTF();
3414
3415 entry = &crl->entry;
3416
3417 ret = snprintf( p, n, "\n%sRevoked certificates:",
3418 prefix );
3419 SAFE_SNPRINTF();
3420
Paul Bakker9be19372009-07-27 20:21:53 +00003421 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003422 {
3423 ret = snprintf( p, n, "\n%sserial number: ",
3424 prefix );
3425 SAFE_SNPRINTF();
3426
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003427 ret = x509parse_serial_gets( p, n, &entry->serial);
3428 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003429
Paul Bakkerd98030e2009-05-02 15:13:40 +00003430 ret = snprintf( p, n, " revocation date: " \
3431 "%04d-%02d-%02d %02d:%02d:%02d",
3432 entry->revocation_date.year, entry->revocation_date.mon,
3433 entry->revocation_date.day, entry->revocation_date.hour,
3434 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003435 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003436
3437 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 }
3439
Paul Bakkerc70b9822013-04-07 22:00:46 +02003440 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003441 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003442
Paul Bakkerc70b9822013-04-07 22:00:46 +02003443 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3444 if( ret != 0 )
3445 ret = snprintf( p, n, "???" );
3446 else
3447 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003448 SAFE_SNPRINTF();
3449
Paul Bakker1e27bb22009-07-19 20:25:25 +00003450 ret = snprintf( p, n, "\n" );
3451 SAFE_SNPRINTF();
3452
Paul Bakker23986e52011-04-24 08:57:21 +00003453 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003454}
3455
3456/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003457 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003459#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003460int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003461{
Paul Bakkercce9d772011-11-18 14:26:47 +00003462 int year, mon, day;
3463 int hour, min, sec;
3464
3465#if defined(_WIN32)
3466 SYSTEMTIME st;
3467
3468 GetLocalTime(&st);
3469
3470 year = st.wYear;
3471 mon = st.wMonth;
3472 day = st.wDay;
3473 hour = st.wHour;
3474 min = st.wMinute;
3475 sec = st.wSecond;
3476#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 struct tm *lt;
3478 time_t tt;
3479
3480 tt = time( NULL );
3481 lt = localtime( &tt );
3482
Paul Bakkercce9d772011-11-18 14:26:47 +00003483 year = lt->tm_year + 1900;
3484 mon = lt->tm_mon + 1;
3485 day = lt->tm_mday;
3486 hour = lt->tm_hour;
3487 min = lt->tm_min;
3488 sec = lt->tm_sec;
3489#endif
3490
3491 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003492 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003493
Paul Bakkercce9d772011-11-18 14:26:47 +00003494 if( year == to->year &&
3495 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003496 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003497
Paul Bakkercce9d772011-11-18 14:26:47 +00003498 if( year == to->year &&
3499 mon == to->mon &&
3500 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003501 return( 1 );
3502
Paul Bakkercce9d772011-11-18 14:26:47 +00003503 if( year == to->year &&
3504 mon == to->mon &&
3505 day == to->day &&
3506 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003507 return( 1 );
3508
Paul Bakkercce9d772011-11-18 14:26:47 +00003509 if( year == to->year &&
3510 mon == to->mon &&
3511 day == to->day &&
3512 hour == to->hour &&
3513 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003514 return( 1 );
3515
Paul Bakkercce9d772011-11-18 14:26:47 +00003516 if( year == to->year &&
3517 mon == to->mon &&
3518 day == to->day &&
3519 hour == to->hour &&
3520 min == to->min &&
3521 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003522 return( 1 );
3523
Paul Bakker40ea7de2009-05-03 10:18:48 +00003524 return( 0 );
3525}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003526#else /* POLARSSL_HAVE_TIME */
3527int x509parse_time_expired( const x509_time *to )
3528{
3529 ((void) to);
3530 return( 0 );
3531}
3532#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003533
3534/*
3535 * Return 1 if the certificate is revoked, or 0 otherwise.
3536 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003537int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003538{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003539 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003540
3541 while( cur != NULL && cur->serial.len != 0 )
3542 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003543 if( crt->serial.len == cur->serial.len &&
3544 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003545 {
3546 if( x509parse_time_expired( &cur->revocation_date ) )
3547 return( 1 );
3548 }
3549
3550 cur = cur->next;
3551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
3553 return( 0 );
3554}
3555
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003556/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003557 * Check that the given certificate is valid accoring to the CRL.
3558 */
3559static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3560 x509_crl *crl_list)
3561{
3562 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003563 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3564 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003565
Paul Bakker915275b2012-09-28 07:10:55 +00003566 if( ca == NULL )
3567 return( flags );
3568
Paul Bakker76fd75a2011-01-16 21:12:10 +00003569 /*
3570 * TODO: What happens if no CRL is present?
3571 * Suggestion: Revocation state should be unknown if no CRL is present.
3572 * For backwards compatibility this is not yet implemented.
3573 */
3574
Paul Bakker915275b2012-09-28 07:10:55 +00003575 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003576 {
Paul Bakker915275b2012-09-28 07:10:55 +00003577 if( crl_list->version == 0 ||
3578 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003579 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3580 crl_list->issuer_raw.len ) != 0 )
3581 {
3582 crl_list = crl_list->next;
3583 continue;
3584 }
3585
3586 /*
3587 * Check if CRL is correctly signed by the trusted CA
3588 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003589 md_info = md_info_from_type( crl_list->sig_md );
3590 if( md_info == NULL )
3591 {
3592 /*
3593 * Cannot check 'unknown' hash
3594 */
3595 flags |= BADCRL_NOT_TRUSTED;
3596 break;
3597 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003598
Paul Bakkerc70b9822013-04-07 22:00:46 +02003599 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003600
Paul Bakkerc70b9822013-04-07 22:00:46 +02003601 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003602 0, hash, crl_list->sig.p ) == 0 )
3603 {
3604 /*
3605 * CRL is not trusted
3606 */
3607 flags |= BADCRL_NOT_TRUSTED;
3608 break;
3609 }
3610
3611 /*
3612 * Check for validity of CRL (Do not drop out)
3613 */
3614 if( x509parse_time_expired( &crl_list->next_update ) )
3615 flags |= BADCRL_EXPIRED;
3616
3617 /*
3618 * Check if certificate is revoked
3619 */
3620 if( x509parse_revoked(crt, crl_list) )
3621 {
3622 flags |= BADCERT_REVOKED;
3623 break;
3624 }
3625
3626 crl_list = crl_list->next;
3627 }
3628 return flags;
3629}
3630
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003631static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003632{
3633 size_t i;
3634 size_t cn_idx = 0;
3635
Paul Bakker57b12982012-02-11 17:38:38 +00003636 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003637 return( 0 );
3638
3639 for( i = 0; i < strlen( cn ); ++i )
3640 {
3641 if( cn[i] == '.' )
3642 {
3643 cn_idx = i;
3644 break;
3645 }
3646 }
3647
3648 if( cn_idx == 0 )
3649 return( 0 );
3650
Paul Bakker535e97d2012-08-23 10:49:55 +00003651 if( strlen( cn ) - cn_idx == name->len - 1 &&
3652 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003653 {
3654 return( 1 );
3655 }
3656
3657 return( 0 );
3658}
3659
Paul Bakker915275b2012-09-28 07:10:55 +00003660static int x509parse_verify_top(
3661 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003662 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003663 int (*f_vrfy)(void *, x509_cert *, int, int *),
3664 void *p_vrfy )
3665{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003666 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003667 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003668 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3669 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003670
3671 if( x509parse_time_expired( &child->valid_to ) )
3672 *flags |= BADCERT_EXPIRED;
3673
3674 /*
3675 * Child is the top of the chain. Check against the trust_ca list.
3676 */
3677 *flags |= BADCERT_NOT_TRUSTED;
3678
3679 while( trust_ca != NULL )
3680 {
3681 if( trust_ca->version == 0 ||
3682 child->issuer_raw.len != trust_ca->subject_raw.len ||
3683 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3684 child->issuer_raw.len ) != 0 )
3685 {
3686 trust_ca = trust_ca->next;
3687 continue;
3688 }
3689
Paul Bakker9a736322012-11-14 12:39:52 +00003690 /*
3691 * Reduce path_len to check against if top of the chain is
3692 * the same as the trusted CA
3693 */
3694 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3695 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3696 child->issuer_raw.len ) == 0 )
3697 {
3698 check_path_cnt--;
3699 }
3700
Paul Bakker915275b2012-09-28 07:10:55 +00003701 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003702 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003703 {
3704 trust_ca = trust_ca->next;
3705 continue;
3706 }
3707
Paul Bakkerc70b9822013-04-07 22:00:46 +02003708 md_info = md_info_from_type( child->sig_md );
3709 if( md_info == NULL )
3710 {
3711 /*
3712 * Cannot check 'unknown' hash
3713 */
3714 continue;
3715 }
Paul Bakker915275b2012-09-28 07:10:55 +00003716
Paul Bakkerc70b9822013-04-07 22:00:46 +02003717 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003718
Paul Bakkerc70b9822013-04-07 22:00:46 +02003719 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003720 0, hash, child->sig.p ) != 0 )
3721 {
3722 trust_ca = trust_ca->next;
3723 continue;
3724 }
3725
3726 /*
3727 * Top of chain is signed by a trusted CA
3728 */
3729 *flags &= ~BADCERT_NOT_TRUSTED;
3730 break;
3731 }
3732
Paul Bakker9a736322012-11-14 12:39:52 +00003733 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003734 * If top of chain is not the same as the trusted CA send a verify request
3735 * to the callback for any issues with validity and CRL presence for the
3736 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003737 */
3738 if( trust_ca != NULL &&
3739 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3740 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3741 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003742 {
3743 /* Check trusted CA's CRL for then chain's top crt */
3744 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3745
3746 if( x509parse_time_expired( &trust_ca->valid_to ) )
3747 ca_flags |= BADCERT_EXPIRED;
3748
Paul Bakker915275b2012-09-28 07:10:55 +00003749 if( NULL != f_vrfy )
3750 {
Paul Bakker9a736322012-11-14 12:39:52 +00003751 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003752 return( ret );
3753 }
3754 }
3755
3756 /* Call callback on top cert */
3757 if( NULL != f_vrfy )
3758 {
Paul Bakker9a736322012-11-14 12:39:52 +00003759 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003760 return( ret );
3761 }
3762
Paul Bakker915275b2012-09-28 07:10:55 +00003763 *flags |= ca_flags;
3764
3765 return( 0 );
3766}
3767
3768static int x509parse_verify_child(
3769 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003770 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003771 int (*f_vrfy)(void *, x509_cert *, int, int *),
3772 void *p_vrfy )
3773{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003774 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003775 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003776 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003777 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003778 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003779
3780 if( x509parse_time_expired( &child->valid_to ) )
3781 *flags |= BADCERT_EXPIRED;
3782
Paul Bakkerc70b9822013-04-07 22:00:46 +02003783 md_info = md_info_from_type( child->sig_md );
3784 if( md_info == NULL )
3785 {
3786 /*
3787 * Cannot check 'unknown' hash
3788 */
Paul Bakker915275b2012-09-28 07:10:55 +00003789 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003790 }
3791 else
3792 {
3793 md( md_info, child->tbs.p, child->tbs.len, hash );
3794
3795 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3796 child->sig.p ) != 0 )
3797 *flags |= BADCERT_NOT_TRUSTED;
3798 }
3799
Paul Bakker915275b2012-09-28 07:10:55 +00003800 /* Check trusted CA's CRL for the given crt */
3801 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3802
3803 grandparent = parent->next;
3804
3805 while( grandparent != NULL )
3806 {
3807 if( grandparent->version == 0 ||
3808 grandparent->ca_istrue == 0 ||
3809 parent->issuer_raw.len != grandparent->subject_raw.len ||
3810 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3811 parent->issuer_raw.len ) != 0 )
3812 {
3813 grandparent = grandparent->next;
3814 continue;
3815 }
3816 break;
3817 }
3818
Paul Bakker915275b2012-09-28 07:10:55 +00003819 if( grandparent != NULL )
3820 {
3821 /*
3822 * Part of the chain
3823 */
Paul Bakker9a736322012-11-14 12:39:52 +00003824 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 +00003825 if( ret != 0 )
3826 return( ret );
3827 }
3828 else
3829 {
Paul Bakker9a736322012-11-14 12:39:52 +00003830 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 +00003831 if( ret != 0 )
3832 return( ret );
3833 }
3834
3835 /* child is verified to be a child of the parent, call verify callback */
3836 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003837 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003838 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003839
3840 *flags |= parent_flags;
3841
3842 return( 0 );
3843}
3844
Paul Bakker76fd75a2011-01-16 21:12:10 +00003845/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003846 * Verify the certificate validity
3847 */
3848int x509parse_verify( x509_cert *crt,
3849 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003850 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003851 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003852 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003853 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003854{
Paul Bakker23986e52011-04-24 08:57:21 +00003855 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003856 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003857 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003858 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003859 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003860 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
Paul Bakker40ea7de2009-05-03 10:18:48 +00003862 *flags = 0;
3863
Paul Bakker5121ce52009-01-03 21:22:43 +00003864 if( cn != NULL )
3865 {
3866 name = &crt->subject;
3867 cn_len = strlen( cn );
3868
Paul Bakker4d2c1242012-05-10 14:12:46 +00003869 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003871 cur = &crt->subject_alt_names;
3872
3873 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003874 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003875 if( cur->buf.len == cn_len &&
3876 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003877 break;
3878
Paul Bakker535e97d2012-08-23 10:49:55 +00003879 if( cur->buf.len > 2 &&
3880 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003881 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003882 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003883
Paul Bakker4d2c1242012-05-10 14:12:46 +00003884 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003885 }
3886
3887 if( cur == NULL )
3888 *flags |= BADCERT_CN_MISMATCH;
3889 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003890 else
3891 {
3892 while( name != NULL )
3893 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003894 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003895 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003896 if( name->val.len == cn_len &&
3897 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003898 break;
3899
Paul Bakker535e97d2012-08-23 10:49:55 +00003900 if( name->val.len > 2 &&
3901 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003902 x509_wildcard_verify( cn, &name->val ) )
3903 break;
3904 }
3905
3906 name = name->next;
3907 }
3908
3909 if( name == NULL )
3910 *flags |= BADCERT_CN_MISMATCH;
3911 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003912 }
3913
Paul Bakker5121ce52009-01-03 21:22:43 +00003914 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003915 * Iterate upwards in the given cert chain, to find our crt parent.
3916 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003917 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003918 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003919
Paul Bakker76fd75a2011-01-16 21:12:10 +00003920 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003921 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003922 if( parent->ca_istrue == 0 ||
3923 crt->issuer_raw.len != parent->subject_raw.len ||
3924 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003925 crt->issuer_raw.len ) != 0 )
3926 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003927 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003928 continue;
3929 }
Paul Bakker915275b2012-09-28 07:10:55 +00003930 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003931 }
3932
Paul Bakker915275b2012-09-28 07:10:55 +00003933 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003934 {
Paul Bakker915275b2012-09-28 07:10:55 +00003935 /*
3936 * Part of the chain
3937 */
Paul Bakker9a736322012-11-14 12:39:52 +00003938 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003939 if( ret != 0 )
3940 return( ret );
3941 }
3942 else
Paul Bakker74111d32011-01-15 16:57:55 +00003943 {
Paul Bakker9a736322012-11-14 12:39:52 +00003944 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003945 if( ret != 0 )
3946 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003947 }
Paul Bakker915275b2012-09-28 07:10:55 +00003948
3949 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003950 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003951
Paul Bakker5121ce52009-01-03 21:22:43 +00003952 return( 0 );
3953}
3954
3955/*
3956 * Unallocate all certificate data
3957 */
3958void x509_free( x509_cert *crt )
3959{
3960 x509_cert *cert_cur = crt;
3961 x509_cert *cert_prv;
3962 x509_name *name_cur;
3963 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003964 x509_sequence *seq_cur;
3965 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003966
3967 if( crt == NULL )
3968 return;
3969
3970 do
3971 {
3972 rsa_free( &cert_cur->rsa );
3973
3974 name_cur = cert_cur->issuer.next;
3975 while( name_cur != NULL )
3976 {
3977 name_prv = name_cur;
3978 name_cur = name_cur->next;
3979 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003980 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003981 }
3982
3983 name_cur = cert_cur->subject.next;
3984 while( name_cur != NULL )
3985 {
3986 name_prv = name_cur;
3987 name_cur = name_cur->next;
3988 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003989 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003990 }
3991
Paul Bakker74111d32011-01-15 16:57:55 +00003992 seq_cur = cert_cur->ext_key_usage.next;
3993 while( seq_cur != NULL )
3994 {
3995 seq_prv = seq_cur;
3996 seq_cur = seq_cur->next;
3997 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003998 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003999 }
4000
Paul Bakker8afa70d2012-02-11 18:42:45 +00004001 seq_cur = cert_cur->subject_alt_names.next;
4002 while( seq_cur != NULL )
4003 {
4004 seq_prv = seq_cur;
4005 seq_cur = seq_cur->next;
4006 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004007 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004008 }
4009
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 if( cert_cur->raw.p != NULL )
4011 {
4012 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004013 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004014 }
4015
4016 cert_cur = cert_cur->next;
4017 }
4018 while( cert_cur != NULL );
4019
4020 cert_cur = crt;
4021 do
4022 {
4023 cert_prv = cert_cur;
4024 cert_cur = cert_cur->next;
4025
4026 memset( cert_prv, 0, sizeof( x509_cert ) );
4027 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004028 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004029 }
4030 while( cert_cur != NULL );
4031}
4032
Paul Bakkerd98030e2009-05-02 15:13:40 +00004033/*
4034 * Unallocate all CRL data
4035 */
4036void x509_crl_free( x509_crl *crl )
4037{
4038 x509_crl *crl_cur = crl;
4039 x509_crl *crl_prv;
4040 x509_name *name_cur;
4041 x509_name *name_prv;
4042 x509_crl_entry *entry_cur;
4043 x509_crl_entry *entry_prv;
4044
4045 if( crl == NULL )
4046 return;
4047
4048 do
4049 {
4050 name_cur = crl_cur->issuer.next;
4051 while( name_cur != NULL )
4052 {
4053 name_prv = name_cur;
4054 name_cur = name_cur->next;
4055 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004056 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004057 }
4058
4059 entry_cur = crl_cur->entry.next;
4060 while( entry_cur != NULL )
4061 {
4062 entry_prv = entry_cur;
4063 entry_cur = entry_cur->next;
4064 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004065 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004066 }
4067
4068 if( crl_cur->raw.p != NULL )
4069 {
4070 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004071 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004072 }
4073
4074 crl_cur = crl_cur->next;
4075 }
4076 while( crl_cur != NULL );
4077
4078 crl_cur = crl;
4079 do
4080 {
4081 crl_prv = crl_cur;
4082 crl_cur = crl_cur->next;
4083
4084 memset( crl_prv, 0, sizeof( x509_crl ) );
4085 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004086 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004087 }
4088 while( crl_cur != NULL );
4089}
4090
Paul Bakker40e46942009-01-03 21:51:57 +00004091#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004092
Paul Bakker40e46942009-01-03 21:51:57 +00004093#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004094
4095/*
4096 * Checkup routine
4097 */
4098int x509_self_test( int verbose )
4099{
Paul Bakker5690efc2011-05-26 13:16:06 +00004100#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004101 int ret;
4102 int flags;
4103 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004104 x509_cert cacert;
4105 x509_cert clicert;
4106 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004107#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004108 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004110
4111 if( verbose != 0 )
4112 printf( " X.509 certificate load: " );
4113
4114 memset( &clicert, 0, sizeof( x509_cert ) );
4115
Paul Bakker3c2122f2013-06-24 19:03:14 +02004116 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004117 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004118 if( ret != 0 )
4119 {
4120 if( verbose != 0 )
4121 printf( "failed\n" );
4122
4123 return( ret );
4124 }
4125
4126 memset( &cacert, 0, sizeof( x509_cert ) );
4127
Paul Bakker3c2122f2013-06-24 19:03:14 +02004128 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004129 strlen( test_ca_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 if( verbose != 0 )
4139 printf( "passed\n X.509 private key load: " );
4140
4141 i = strlen( test_ca_key );
4142 j = strlen( test_ca_pwd );
4143
Paul Bakker66b78b22011-03-25 14:22:50 +00004144 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4145
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004146 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004147 (const unsigned char *) test_ca_key, i,
4148 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004149 {
4150 if( verbose != 0 )
4151 printf( "failed\n" );
4152
4153 return( ret );
4154 }
4155
4156 if( verbose != 0 )
4157 printf( "passed\n X.509 signature verify: ");
4158
Paul Bakker23986e52011-04-24 08:57:21 +00004159 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004160 if( ret != 0 )
4161 {
Paul Bakker23986e52011-04-24 08:57:21 +00004162 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004163 if( verbose != 0 )
4164 printf( "failed\n" );
4165
4166 return( ret );
4167 }
4168
Paul Bakker5690efc2011-05-26 13:16:06 +00004169#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004170 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004171 printf( "passed\n X.509 DHM parameter load: " );
4172
4173 i = strlen( test_dhm_params );
4174 j = strlen( test_ca_pwd );
4175
Paul Bakker3c2122f2013-06-24 19:03:14 +02004176 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004177 {
4178 if( verbose != 0 )
4179 printf( "failed\n" );
4180
4181 return( ret );
4182 }
4183
4184 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004185 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004186#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004187
4188 x509_free( &cacert );
4189 x509_free( &clicert );
4190 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004191#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004192 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004193#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004194
4195 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004196#else
4197 ((void) verbose);
4198 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4199#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004200}
4201
4202#endif
4203
4204#endif