blob: f03652694dd97d7665226018f5e06fd0603cef94 [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
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200278 if( --len < 1 || *(*p)++ != 0 )
279 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200280
281 if( ( ret = ecp_point_read_binary( grp, pt,
282 (const unsigned char *) *p, len ) ) != 0 )
283 {
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200284 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
285 }
286
287 *p += len;
288
289 if( ( ret = ecp_check_pubkey( grp, pt ) ) != 0 )
290 {
291 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200292 }
293
294 return( 0 );
295}
296
297/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 * AttributeTypeAndValue ::= SEQUENCE {
299 * type AttributeType,
300 * value AttributeValue }
301 *
302 * AttributeType ::= OBJECT IDENTIFIER
303 *
304 * AttributeValue ::= ANY DEFINED BY AttributeType
305 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000306static int x509_get_attr_type_value( unsigned char **p,
307 const unsigned char *end,
308 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000309{
Paul Bakker23986e52011-04-24 08:57:21 +0000310 int ret;
311 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 x509_buf *oid;
313 x509_buf *val;
314
315 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000317 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 oid = &cur->oid;
320 oid->tag = **p;
321
322 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000323 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
325 oid->p = *p;
326 *p += oid->len;
327
328 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000329 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000330 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
332 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
333 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
334 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000335 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000336 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
338 val = &cur->val;
339 val->tag = *(*p)++;
340
341 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000342 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
344 val->p = *p;
345 *p += val->len;
346
347 cur->next = NULL;
348
Paul Bakker400ff6f2011-02-20 10:40:16 +0000349 return( 0 );
350}
351
352/*
353 * RelativeDistinguishedName ::=
354 * SET OF AttributeTypeAndValue
355 *
356 * AttributeTypeAndValue ::= SEQUENCE {
357 * type AttributeType,
358 * value AttributeValue }
359 *
360 * AttributeType ::= OBJECT IDENTIFIER
361 *
362 * AttributeValue ::= ANY DEFINED BY AttributeType
363 */
364static int x509_get_name( unsigned char **p,
365 const unsigned char *end,
366 x509_name *cur )
367{
Paul Bakker23986e52011-04-24 08:57:21 +0000368 int ret;
369 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000370 const unsigned char *end2;
371 x509_name *use;
372
373 if( ( ret = asn1_get_tag( p, end, &len,
374 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000375 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000376
377 end2 = end;
378 end = *p + len;
379 use = cur;
380
381 do
382 {
383 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
384 return( ret );
385
386 if( *p != end )
387 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200388 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000389 sizeof( x509_name ) );
390
391 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000392 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000393
394 memset( use->next, 0, sizeof( x509_name ) );
395
396 use = use->next;
397 }
398 }
399 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
401 /*
402 * recurse until end of SEQUENCE is reached
403 */
404 if( *p == end2 )
405 return( 0 );
406
Paul Bakker6e339b52013-07-03 13:37:05 +0200407 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 sizeof( x509_name ) );
409
410 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000411 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
Paul Bakker430ffbe2012-05-01 08:14:20 +0000413 memset( cur->next, 0, sizeof( x509_name ) );
414
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 return( x509_get_name( p, end2, cur->next ) );
416}
417
418/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 * Time ::= CHOICE {
420 * utcTime UTCTime,
421 * generalTime GeneralizedTime }
422 */
Paul Bakker91200182010-02-18 21:26:15 +0000423static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000424 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000425 x509_time *time )
426{
Paul Bakker23986e52011-04-24 08:57:21 +0000427 int ret;
428 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000429 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000430 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000431
Paul Bakker91200182010-02-18 21:26:15 +0000432 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000433 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
434 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000435
Paul Bakker91200182010-02-18 21:26:15 +0000436 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000437
Paul Bakker91200182010-02-18 21:26:15 +0000438 if ( tag == ASN1_UTC_TIME )
439 {
440 (*p)++;
441 ret = asn1_get_len( p, end, &len );
442
443 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000444 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000445
Paul Bakker91200182010-02-18 21:26:15 +0000446 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000447 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
448 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000449
Paul Bakker91200182010-02-18 21:26:15 +0000450 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
451 &time->year, &time->mon, &time->day,
452 &time->hour, &time->min, &time->sec ) < 5 )
453 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000454
Paul Bakker400ff6f2011-02-20 10:40:16 +0000455 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000456 time->year += 1900;
457
458 *p += len;
459
460 return( 0 );
461 }
462 else if ( tag == ASN1_GENERALIZED_TIME )
463 {
464 (*p)++;
465 ret = asn1_get_len( p, end, &len );
466
467 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000468 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000469
470 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000471 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
472 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000473
474 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
475 &time->year, &time->mon, &time->day,
476 &time->hour, &time->min, &time->sec ) < 5 )
477 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
478
479 *p += len;
480
481 return( 0 );
482 }
483 else
Paul Bakker9d781402011-05-09 16:17:09 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000485}
486
487
488/*
489 * Validity ::= SEQUENCE {
490 * notBefore Time,
491 * notAfter Time }
492 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000493static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000494 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 x509_time *from,
496 x509_time *to )
497{
Paul Bakker23986e52011-04-24 08:57:21 +0000498 int ret;
499 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501 if( ( ret = asn1_get_tag( p, end, &len,
502 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000503 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505 end = *p + len;
506
Paul Bakker91200182010-02-18 21:26:15 +0000507 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000508 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
Paul Bakker91200182010-02-18 21:26:15 +0000510 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000511 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000514 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000515 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517 return( 0 );
518}
519
520/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200521 * RSAPublicKey ::= SEQUENCE {
522 * modulus INTEGER, -- n
523 * publicExponent INTEGER -- e
524 * }
525 */
526static int x509_get_rsapubkey( unsigned char **p,
527 const unsigned char *end,
528 rsa_context *rsa )
529{
530 int ret;
531 size_t len;
532
533 if( ( ret = asn1_get_tag( p, end, &len,
534 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
535 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
536
537 if( *p + len != end )
538 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
539 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
540
541 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
542 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
543 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
544
545 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
546 return( ret );
547
548 rsa->len = mpi_size( &rsa->N );
549
550 return( 0 );
551}
552
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200553static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
554 x509_buf *alg_params, ecp_keypair *key )
555{
556 int ret;
557 ecp_group_id grp_id;
558
559 if( ( ret = x509_ecparams_get_grp_id( alg_params, &grp_id ) ) != 0 )
560 return( ret );
561
562 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
563 return( ret );
564
565 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
566 (const unsigned char *) *p, end - *p ) ) != 0 )
567 {
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200568 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
569 }
570
571 *p = (unsigned char *) end;
572
573 if( ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
574 {
575 ecp_keypair_free( key );
576 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200577 }
578
579 return( 0 );
580}
581
582/*
583 * SubjectPublicKeyInfo ::= SEQUENCE {
584 * algorithm AlgorithmIdentifier,
585 * subjectPublicKey BIT STRING }
586 */
587static int x509_get_pubkey( unsigned char **p,
588 const unsigned char *end,
589 pk_context *pk )
590{
591 int ret;
592 size_t len;
593 x509_buf alg_params;
594 pk_type_t pk_alg = POLARSSL_PK_NONE;
595
596 if( ( ret = asn1_get_tag( p, end, &len,
597 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
598 {
599 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
600 }
601
602 end = *p + len;
603
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200604 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200605 return( ret );
606
607 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
608 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
609
610 if( ( end - *p ) < 1 )
611 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
612 POLARSSL_ERR_ASN1_OUT_OF_DATA );
613
614 if( *p + len != end )
615 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
616 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
617
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200618 if( --len < 1 || *(*p)++ != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200619 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
620
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200621 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
622 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200623
624 switch( pk_alg )
625 {
626 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200627 case POLARSSL_PK_ECDSA:
628 /* Should never happen */
629 ret = POLARSSL_ERR_X509_CERT_INVALID_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200630 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200631
632 case POLARSSL_PK_RSA:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200633 ret = x509_get_rsapubkey( p, end, pk->data );
634 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200635
636 case POLARSSL_PK_ECKEY_DH:
637 ((ecp_keypair *) pk->data)->alg = POLARSSL_ECP_KEY_ALG_ECDH;
638 /* FALLTHROUGH */
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200639
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200640 case POLARSSL_PK_ECKEY:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200641 ret = x509_get_ecpubkey( p, end, &alg_params, pk->data );
642 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200643 }
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200644
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200645 if( ret == 0 && *p != end )
646 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
647 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
648
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200649 if( ret != 0 )
650 pk_free( pk );
651
652 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200653}
654
Paul Bakker5121ce52009-01-03 21:22:43 +0000655static 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é-Gonnard674b2242013-07-10 14:32:58 +02001419 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 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é-Gonnard5b18fb02013-07-10 16:07:25 +02002635 unsigned char *end2;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002636
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002637 /*
2638 * RFC 5915, orf SEC1 Appendix C.4
2639 *
2640 * ECPrivateKey ::= SEQUENCE {
2641 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2642 * privateKey OCTET STRING,
2643 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2644 * publicKey [1] BIT STRING OPTIONAL
2645 * }
2646 */
2647 if( ( ret = asn1_get_tag( &p, end, &len,
2648 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2649 {
2650 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2651 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002652
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002653 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002654
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002655 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2656 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002657
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002658 if( version != 1 )
2659 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2660
2661 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2662 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2663
2664 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2665 {
2666 ecp_keypair_free( eck );
2667 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2668 }
2669
2670 p += len;
2671
2672 /*
2673 * Is 'parameters' present?
2674 */
2675 if( ( ret = asn1_get_tag( &p, end, &len,
2676 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2677 {
2678 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2679 return( ret );
2680
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002681 /*
2682 * If we're wrapped in a bigger structure (eg PKCS#8), grp may have been
2683 * defined externally. In this case, make sure both definitions match.
2684 */
2685 if( eck->grp.id != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002686 {
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002687 if( eck->grp.id != grp_id )
2688 {
2689 ecp_keypair_free( eck );
2690 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2691 }
2692 }
2693 else
2694 {
2695 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2696 {
2697 ecp_keypair_free( eck );
2698 return( ret );
2699 }
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002700 }
2701 }
2702 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2703 {
2704 ecp_keypair_free( eck );
2705 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2706 }
2707
2708 /*
2709 * Is 'publickey' present?
2710 */
2711 if( ( ret = asn1_get_tag( &p, end, &len,
2712 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2713 {
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +02002714 end2 = p + len;
2715
2716 if( ( ret = x509_get_subpubkey_ec( &p, end2, &eck->grp, &eck->Q ) )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002717 != 0 )
2718 {
2719 ecp_keypair_free( eck );
2720 return( ret );
2721 }
2722
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +02002723 if( p != end2 )
2724 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
2725 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002726 }
2727 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2728 {
2729 ecp_keypair_free( eck );
2730 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2731 }
2732
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002733 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002734 {
2735 ecp_keypair_free( eck );
2736 return( ret );
2737 }
2738
2739 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002740}
2741
2742/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002743 * Parse an unencrypted PKCS#8 encoded private EC key
2744 */
2745static int x509parse_key_pkcs8_unencrypted_der_ec(
2746 ecp_keypair *eck,
2747 const unsigned char* key,
2748 size_t keylen )
2749{
2750 int ret, version;
2751 size_t len;
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002752 x509_buf alg_params;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002753 ecp_group_id grp_id;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002754 unsigned char *p = (unsigned char *) key;
2755 unsigned char *end = p + keylen;
2756 pk_type_t pk_alg = POLARSSL_PK_NONE;
2757
2758 /*
2759 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2760 *
2761 * PrivateKeyInfo ::= SEQUENCE {
2762 * version Version,
2763 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2764 * privateKey PrivateKey,
2765 * attributes [0] IMPLICIT Attributes OPTIONAL }
2766 *
2767 * Version ::= INTEGER
2768 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2769 * PrivateKey ::= OCTET STRING
2770 *
2771 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2772 */
2773
2774 if( ( ret = asn1_get_tag( &p, end, &len,
2775 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2776 {
2777 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2778 }
2779
2780 end = p + len;
2781
2782 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2783 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2784
2785 if( version != 0 )
2786 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2787
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +02002788 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002789 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2790
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002791 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002792 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002793
2794 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2795 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2796
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002797 if( ( ret = x509_ecparams_get_grp_id( &alg_params, &grp_id ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002798 {
2799 ecp_keypair_free( eck );
2800 return( ret );
2801 }
2802
2803 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2804 {
2805 ecp_keypair_free( eck );
2806 return( ret );
2807 }
2808
2809 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2810 {
2811 ecp_keypair_free( eck );
2812 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2813 }
2814
2815 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2816 {
2817 ecp_keypair_free( eck );
2818 return( ret );
2819 }
2820
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002821 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002822 {
2823 ecp_keypair_free( eck );
2824 return( ret );
2825 }
2826
2827 return 0;
2828}
2829
2830/*
2831 * Parse an encrypted PKCS#8 encoded private EC key
2832 */
2833static int x509parse_key_pkcs8_encrypted_der_ec(
2834 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002835 const unsigned char *key, size_t keylen,
2836 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002837{
2838 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002839 unsigned char buf[2048];
2840 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002841
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002842 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2843 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002844 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002845 return( ret );
2846 }
2847
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002848 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002849}
2850
2851/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002852 * Parse a private EC key
2853 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002854static int x509parse_key_ec( ecp_keypair *eck,
2855 const unsigned char *key, size_t keylen,
2856 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002857{
2858 int ret;
2859
2860#if defined(POLARSSL_PEM_C)
2861 size_t len;
2862 pem_context pem;
2863
2864 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002865 ret = pem_read_buffer( &pem,
2866 "-----BEGIN EC PRIVATE KEY-----",
2867 "-----END EC PRIVATE KEY-----",
2868 key, pwd, pwdlen, &len );
2869 if( ret == 0 )
2870 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002871 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002872 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002873 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002874 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002875
2876 pem_free( &pem );
2877 return( ret );
2878 }
2879 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2880 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2881 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2882 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2883 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2884 return( ret );
2885
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002886 ret = pem_read_buffer( &pem,
2887 "-----BEGIN PRIVATE KEY-----",
2888 "-----END PRIVATE KEY-----",
2889 key, NULL, 0, &len );
2890 if( ret == 0 )
2891 {
2892 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2893 pem.buf, pem.buflen ) ) != 0 )
2894 {
2895 ecp_keypair_free( eck );
2896 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002897
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002898 pem_free( &pem );
2899 return( ret );
2900 }
2901 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2902 return( ret );
2903
2904 ret = pem_read_buffer( &pem,
2905 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2906 "-----END ENCRYPTED PRIVATE KEY-----",
2907 key, NULL, 0, &len );
2908 if( ret == 0 )
2909 {
2910 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2911 pem.buf, pem.buflen,
2912 pwd, pwdlen ) ) != 0 )
2913 {
2914 ecp_keypair_free( eck );
2915 }
2916
2917 pem_free( &pem );
2918 return( ret );
2919 }
2920 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2921 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002922#else
2923 ((void) pwd);
2924 ((void) pwdlen);
2925#endif /* POLARSSL_PEM_C */
2926
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002927 /*
2928 * At this point we only know it's not a PEM formatted key. Could be any
2929 * of the known DER encoded private key formats
2930 *
2931 * We try the different DER format parsers to see if one passes without
2932 * error
2933 */
2934 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2935 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002936 {
2937 return( 0 );
2938 }
2939
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002940 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002941
2942 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2943 {
2944 return( ret );
2945 }
2946
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002947 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2948 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002949 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002950
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002951 ecp_keypair_free( eck );
2952
2953 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2954 return( 0 );
2955
2956 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002957
2958 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2959}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002960#endif /* defined(POLARSSL_ECP_C) */
2961
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002962/*
2963 * Parse a private key
2964 */
2965int x509parse_key( pk_context *ctx,
2966 const unsigned char *key, size_t keylen,
2967 const unsigned char *pwd, size_t pwdlen )
2968{
2969 int ret;
2970
2971 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
2972 return( ret );
2973
2974 if( ( ret = x509parse_key_rsa( ctx->data, key, keylen, pwd, pwdlen ) )
2975 == 0 )
2976 {
2977 return( 0 );
2978 }
2979
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002980 pk_free( ctx );
2981
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002982 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
2983 return( ret );
2984
2985 if( ( ret = x509parse_key_ec( ctx->data, key, keylen, pwd, pwdlen ) ) == 0 )
2986 {
2987 return( 0 );
2988 }
2989
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002990 pk_free( ctx );
2991
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002992 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
2993}
2994
2995/*
2996 * Parse a public key
2997 */
2998int x509parse_public_key( pk_context *ctx,
2999 const unsigned char *key, size_t keylen )
3000{
3001 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003002 unsigned char *p;
3003#if defined(POLARSSL_PEM_C)
3004 size_t len;
3005 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003006
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003007 pem_init( &pem );
3008 ret = pem_read_buffer( &pem,
3009 "-----BEGIN PUBLIC KEY-----",
3010 "-----END PUBLIC KEY-----",
3011 key, NULL, 0, &len );
3012
3013 if( ret == 0 )
3014 {
3015 /*
3016 * Was PEM encoded
3017 */
3018 key = pem.buf;
3019 keylen = pem.buflen;
3020 }
3021 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3022 {
3023 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003024 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003025 }
3026#endif
3027 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003028
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003029 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003030
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003031#if defined(POLARSSL_PEM_C)
3032 pem_free( &pem );
3033#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003034
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003035 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003036}
3037
Paul Bakkereaa89f82011-04-04 21:36:15 +00003038#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003039/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003040 * Parse DHM parameters
3041 */
Paul Bakker23986e52011-04-24 08:57:21 +00003042int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003043{
Paul Bakker23986e52011-04-24 08:57:21 +00003044 int ret;
3045 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003046 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003047#if defined(POLARSSL_PEM_C)
3048 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003049
Paul Bakker96743fc2011-02-12 14:30:57 +00003050 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003051
Paul Bakker96743fc2011-02-12 14:30:57 +00003052 ret = pem_read_buffer( &pem,
3053 "-----BEGIN DH PARAMETERS-----",
3054 "-----END DH PARAMETERS-----",
3055 dhmin, NULL, 0, &dhminlen );
3056
3057 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003058 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003059 /*
3060 * Was PEM encoded
3061 */
3062 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003063 }
Paul Bakker00b28602013-06-24 13:02:41 +02003064 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003065 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003066 pem_free( &pem );
3067 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003068 }
3069
Paul Bakker96743fc2011-02-12 14:30:57 +00003070 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3071#else
3072 p = (unsigned char *) dhmin;
3073#endif
3074 end = p + dhminlen;
3075
Paul Bakker1b57b062011-01-06 15:48:19 +00003076 memset( dhm, 0, sizeof( dhm_context ) );
3077
Paul Bakker1b57b062011-01-06 15:48:19 +00003078 /*
3079 * DHParams ::= SEQUENCE {
3080 * prime INTEGER, -- P
3081 * generator INTEGER, -- g
3082 * }
3083 */
3084 if( ( ret = asn1_get_tag( &p, end, &len,
3085 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3086 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003087#if defined(POLARSSL_PEM_C)
3088 pem_free( &pem );
3089#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003090 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003091 }
3092
3093 end = p + len;
3094
3095 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3096 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3097 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003098#if defined(POLARSSL_PEM_C)
3099 pem_free( &pem );
3100#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003101 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003102 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003103 }
3104
3105 if( p != end )
3106 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003107#if defined(POLARSSL_PEM_C)
3108 pem_free( &pem );
3109#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003110 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003111 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003112 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3113 }
3114
Paul Bakker96743fc2011-02-12 14:30:57 +00003115#if defined(POLARSSL_PEM_C)
3116 pem_free( &pem );
3117#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003118
3119 return( 0 );
3120}
3121
Paul Bakker335db3f2011-04-25 15:28:35 +00003122#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003123/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003124 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003125 */
3126int x509parse_dhmfile( dhm_context *dhm, const char *path )
3127{
3128 int ret;
3129 size_t n;
3130 unsigned char *buf;
3131
Paul Bakker69e095c2011-12-10 21:55:01 +00003132 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3133 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003134
Paul Bakker27fdf462011-06-09 13:55:13 +00003135 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003136
3137 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003138 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003139
3140 return( ret );
3141}
Paul Bakker335db3f2011-04-25 15:28:35 +00003142#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003143#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003144
Paul Bakker5121ce52009-01-03 21:22:43 +00003145#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003146#include <stdarg.h>
3147
3148#if !defined vsnprintf
3149#define vsnprintf _vsnprintf
3150#endif // vsnprintf
3151
3152/*
3153 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3154 * Result value is not size of buffer needed, but -1 if no fit is possible.
3155 *
3156 * This fuction tries to 'fix' this by at least suggesting enlarging the
3157 * size by 20.
3158 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003159static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003160{
3161 va_list ap;
3162 int res = -1;
3163
3164 va_start( ap, format );
3165
3166 res = vsnprintf( str, size, format, ap );
3167
3168 va_end( ap );
3169
3170 // No quick fix possible
3171 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003172 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003173
3174 return res;
3175}
3176
3177#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003178#endif
3179
Paul Bakkerd98030e2009-05-02 15:13:40 +00003180#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3181
3182#define SAFE_SNPRINTF() \
3183{ \
3184 if( ret == -1 ) \
3185 return( -1 ); \
3186 \
Paul Bakker23986e52011-04-24 08:57:21 +00003187 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003188 p[n - 1] = '\0'; \
3189 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3190 } \
3191 \
Paul Bakker23986e52011-04-24 08:57:21 +00003192 n -= (unsigned int) ret; \
3193 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003194}
3195
Paul Bakker5121ce52009-01-03 21:22:43 +00003196/*
3197 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003198 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003200int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003201{
Paul Bakker23986e52011-04-24 08:57:21 +00003202 int ret;
3203 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003205 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003206 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 char s[128], *p;
3208
3209 memset( s, 0, sizeof( s ) );
3210
3211 name = dn;
3212 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003213 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003214
3215 while( name != NULL )
3216 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003217 if( !name->oid.p )
3218 {
3219 name = name->next;
3220 continue;
3221 }
3222
Paul Bakker74111d32011-01-15 16:57:55 +00003223 if( name != dn )
3224 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003225 ret = snprintf( p, n, ", " );
3226 SAFE_SNPRINTF();
3227 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Paul Bakkerc70b9822013-04-07 22:00:46 +02003229 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
Paul Bakkerc70b9822013-04-07 22:00:46 +02003231 if( ret == 0 )
3232 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003234 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003235 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
3237 for( i = 0; i < name->val.len; i++ )
3238 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003239 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003240 break;
3241
3242 c = name->val.p[i];
3243 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3244 s[i] = '?';
3245 else s[i] = c;
3246 }
3247 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003248 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003249 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 name = name->next;
3251 }
3252
Paul Bakker23986e52011-04-24 08:57:21 +00003253 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254}
3255
3256/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003257 * Store the serial in printable form into buf; no more
3258 * than size characters will be written
3259 */
3260int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3261{
Paul Bakker23986e52011-04-24 08:57:21 +00003262 int ret;
3263 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003264 char *p;
3265
3266 p = buf;
3267 n = size;
3268
3269 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003270 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003271
3272 for( i = 0; i < nr; i++ )
3273 {
Paul Bakker93048802011-12-05 14:38:06 +00003274 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003275 continue;
3276
Paul Bakkerdd476992011-01-16 21:34:59 +00003277 ret = snprintf( p, n, "%02X%s",
3278 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3279 SAFE_SNPRINTF();
3280 }
3281
Paul Bakker03c7c252011-11-25 12:37:37 +00003282 if( nr != serial->len )
3283 {
3284 ret = snprintf( p, n, "...." );
3285 SAFE_SNPRINTF();
3286 }
3287
Paul Bakker23986e52011-04-24 08:57:21 +00003288 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003289}
3290
3291/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003292 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003294int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3295 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003296{
Paul Bakker23986e52011-04-24 08:57:21 +00003297 int ret;
3298 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003299 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003300 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
3302 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003303 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003304
Paul Bakkerd98030e2009-05-02 15:13:40 +00003305 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003307 SAFE_SNPRINTF();
3308 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003310 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003311
Paul Bakkerdd476992011-01-16 21:34:59 +00003312 ret = x509parse_serial_gets( p, n, &crt->serial);
3313 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Paul Bakkerd98030e2009-05-02 15:13:40 +00003315 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3316 SAFE_SNPRINTF();
3317 ret = x509parse_dn_gets( p, n, &crt->issuer );
3318 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003319
Paul Bakkerd98030e2009-05-02 15:13:40 +00003320 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3321 SAFE_SNPRINTF();
3322 ret = x509parse_dn_gets( p, n, &crt->subject );
3323 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003324
Paul Bakkerd98030e2009-05-02 15:13:40 +00003325 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3327 crt->valid_from.year, crt->valid_from.mon,
3328 crt->valid_from.day, crt->valid_from.hour,
3329 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003330 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003331
Paul Bakkerd98030e2009-05-02 15:13:40 +00003332 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3334 crt->valid_to.year, crt->valid_to.mon,
3335 crt->valid_to.day, crt->valid_to.hour,
3336 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003337 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003338
Paul Bakkerc70b9822013-04-07 22:00:46 +02003339 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003340 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003341
Paul Bakkerc70b9822013-04-07 22:00:46 +02003342 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3343 if( ret != 0 )
3344 ret = snprintf( p, n, "???" );
3345 else
3346 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003347 SAFE_SNPRINTF();
3348
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +02003349 switch( crt->pk.type )
3350 {
3351 case POLARSSL_PK_NONE:
3352 case POLARSSL_PK_ECDSA:
3353 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
3354 break;
3355
3356 case POLARSSL_PK_RSA:
3357 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3358 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3359 break;
3360
3361 case POLARSSL_PK_ECKEY:
3362 case POLARSSL_PK_ECKEY_DH:
3363 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3364 (int) pk_ec( crt->pk )->grp.pbits );
3365 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00003366 SAFE_SNPRINTF();
3367
Paul Bakker23986e52011-04-24 08:57:21 +00003368 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003369}
3370
Paul Bakker74111d32011-01-15 16:57:55 +00003371/*
3372 * Return an informational string describing the given OID
3373 */
3374const char *x509_oid_get_description( x509_buf *oid )
3375{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003376 const char *desc = NULL;
3377 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003378
Paul Bakkerc70b9822013-04-07 22:00:46 +02003379 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003380
Paul Bakkerc70b9822013-04-07 22:00:46 +02003381 if( ret != 0 )
3382 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003383
Paul Bakkerc70b9822013-04-07 22:00:46 +02003384 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003385}
3386
3387/* Return the x.y.z.... style numeric string for the given OID */
3388int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3389{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003390 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003391}
3392
Paul Bakkerd98030e2009-05-02 15:13:40 +00003393/*
3394 * Return an informational string about the CRL.
3395 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003396int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3397 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003398{
Paul Bakker23986e52011-04-24 08:57:21 +00003399 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003400 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003401 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003402 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003403 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003404
3405 p = buf;
3406 n = size;
3407
3408 ret = snprintf( p, n, "%sCRL version : %d",
3409 prefix, crl->version );
3410 SAFE_SNPRINTF();
3411
3412 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3413 SAFE_SNPRINTF();
3414 ret = x509parse_dn_gets( p, n, &crl->issuer );
3415 SAFE_SNPRINTF();
3416
3417 ret = snprintf( p, n, "\n%sthis update : " \
3418 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3419 crl->this_update.year, crl->this_update.mon,
3420 crl->this_update.day, crl->this_update.hour,
3421 crl->this_update.min, crl->this_update.sec );
3422 SAFE_SNPRINTF();
3423
3424 ret = snprintf( p, n, "\n%snext update : " \
3425 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3426 crl->next_update.year, crl->next_update.mon,
3427 crl->next_update.day, crl->next_update.hour,
3428 crl->next_update.min, crl->next_update.sec );
3429 SAFE_SNPRINTF();
3430
3431 entry = &crl->entry;
3432
3433 ret = snprintf( p, n, "\n%sRevoked certificates:",
3434 prefix );
3435 SAFE_SNPRINTF();
3436
Paul Bakker9be19372009-07-27 20:21:53 +00003437 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003438 {
3439 ret = snprintf( p, n, "\n%sserial number: ",
3440 prefix );
3441 SAFE_SNPRINTF();
3442
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003443 ret = x509parse_serial_gets( p, n, &entry->serial);
3444 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003445
Paul Bakkerd98030e2009-05-02 15:13:40 +00003446 ret = snprintf( p, n, " revocation date: " \
3447 "%04d-%02d-%02d %02d:%02d:%02d",
3448 entry->revocation_date.year, entry->revocation_date.mon,
3449 entry->revocation_date.day, entry->revocation_date.hour,
3450 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003451 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003452
3453 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 }
3455
Paul Bakkerc70b9822013-04-07 22:00:46 +02003456 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003457 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
Paul Bakkerc70b9822013-04-07 22:00:46 +02003459 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3460 if( ret != 0 )
3461 ret = snprintf( p, n, "???" );
3462 else
3463 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003464 SAFE_SNPRINTF();
3465
Paul Bakker1e27bb22009-07-19 20:25:25 +00003466 ret = snprintf( p, n, "\n" );
3467 SAFE_SNPRINTF();
3468
Paul Bakker23986e52011-04-24 08:57:21 +00003469 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003470}
3471
3472/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003473 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003475#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003476int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003477{
Paul Bakkercce9d772011-11-18 14:26:47 +00003478 int year, mon, day;
3479 int hour, min, sec;
3480
3481#if defined(_WIN32)
3482 SYSTEMTIME st;
3483
3484 GetLocalTime(&st);
3485
3486 year = st.wYear;
3487 mon = st.wMonth;
3488 day = st.wDay;
3489 hour = st.wHour;
3490 min = st.wMinute;
3491 sec = st.wSecond;
3492#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 struct tm *lt;
3494 time_t tt;
3495
3496 tt = time( NULL );
3497 lt = localtime( &tt );
3498
Paul Bakkercce9d772011-11-18 14:26:47 +00003499 year = lt->tm_year + 1900;
3500 mon = lt->tm_mon + 1;
3501 day = lt->tm_mday;
3502 hour = lt->tm_hour;
3503 min = lt->tm_min;
3504 sec = lt->tm_sec;
3505#endif
3506
3507 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003508 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Paul Bakkercce9d772011-11-18 14:26:47 +00003510 if( year == to->year &&
3511 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003512 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003513
Paul Bakkercce9d772011-11-18 14:26:47 +00003514 if( year == to->year &&
3515 mon == to->mon &&
3516 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003517 return( 1 );
3518
Paul Bakkercce9d772011-11-18 14:26:47 +00003519 if( year == to->year &&
3520 mon == to->mon &&
3521 day == to->day &&
3522 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003523 return( 1 );
3524
Paul Bakkercce9d772011-11-18 14:26:47 +00003525 if( year == to->year &&
3526 mon == to->mon &&
3527 day == to->day &&
3528 hour == to->hour &&
3529 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003530 return( 1 );
3531
Paul Bakkercce9d772011-11-18 14:26:47 +00003532 if( year == to->year &&
3533 mon == to->mon &&
3534 day == to->day &&
3535 hour == to->hour &&
3536 min == to->min &&
3537 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003538 return( 1 );
3539
Paul Bakker40ea7de2009-05-03 10:18:48 +00003540 return( 0 );
3541}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003542#else /* POLARSSL_HAVE_TIME */
3543int x509parse_time_expired( const x509_time *to )
3544{
3545 ((void) to);
3546 return( 0 );
3547}
3548#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003549
3550/*
3551 * Return 1 if the certificate is revoked, or 0 otherwise.
3552 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003553int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003554{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003555 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003556
3557 while( cur != NULL && cur->serial.len != 0 )
3558 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003559 if( crt->serial.len == cur->serial.len &&
3560 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003561 {
3562 if( x509parse_time_expired( &cur->revocation_date ) )
3563 return( 1 );
3564 }
3565
3566 cur = cur->next;
3567 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003568
3569 return( 0 );
3570}
3571
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003572/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003573 * Check that the given certificate is valid accoring to the CRL.
3574 */
3575static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3576 x509_crl *crl_list)
3577{
3578 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003579 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3580 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003581
Paul Bakker915275b2012-09-28 07:10:55 +00003582 if( ca == NULL )
3583 return( flags );
3584
Paul Bakker76fd75a2011-01-16 21:12:10 +00003585 /*
3586 * TODO: What happens if no CRL is present?
3587 * Suggestion: Revocation state should be unknown if no CRL is present.
3588 * For backwards compatibility this is not yet implemented.
3589 */
3590
Paul Bakker915275b2012-09-28 07:10:55 +00003591 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003592 {
Paul Bakker915275b2012-09-28 07:10:55 +00003593 if( crl_list->version == 0 ||
3594 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003595 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3596 crl_list->issuer_raw.len ) != 0 )
3597 {
3598 crl_list = crl_list->next;
3599 continue;
3600 }
3601
3602 /*
3603 * Check if CRL is correctly signed by the trusted CA
3604 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003605 md_info = md_info_from_type( crl_list->sig_md );
3606 if( md_info == NULL )
3607 {
3608 /*
3609 * Cannot check 'unknown' hash
3610 */
3611 flags |= BADCRL_NOT_TRUSTED;
3612 break;
3613 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003614
Paul Bakkerc70b9822013-04-07 22:00:46 +02003615 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003616
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003617 /* EC NOT IMPLEMENTED YET */
3618 if( ca->pk.type != POLARSSL_PK_RSA )
3619 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3620
3621 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003622 0, hash, crl_list->sig.p ) == 0 )
3623 {
3624 /*
3625 * CRL is not trusted
3626 */
3627 flags |= BADCRL_NOT_TRUSTED;
3628 break;
3629 }
3630
3631 /*
3632 * Check for validity of CRL (Do not drop out)
3633 */
3634 if( x509parse_time_expired( &crl_list->next_update ) )
3635 flags |= BADCRL_EXPIRED;
3636
3637 /*
3638 * Check if certificate is revoked
3639 */
3640 if( x509parse_revoked(crt, crl_list) )
3641 {
3642 flags |= BADCERT_REVOKED;
3643 break;
3644 }
3645
3646 crl_list = crl_list->next;
3647 }
3648 return flags;
3649}
3650
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003651static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003652{
3653 size_t i;
3654 size_t cn_idx = 0;
3655
Paul Bakker57b12982012-02-11 17:38:38 +00003656 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003657 return( 0 );
3658
3659 for( i = 0; i < strlen( cn ); ++i )
3660 {
3661 if( cn[i] == '.' )
3662 {
3663 cn_idx = i;
3664 break;
3665 }
3666 }
3667
3668 if( cn_idx == 0 )
3669 return( 0 );
3670
Paul Bakker535e97d2012-08-23 10:49:55 +00003671 if( strlen( cn ) - cn_idx == name->len - 1 &&
3672 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003673 {
3674 return( 1 );
3675 }
3676
3677 return( 0 );
3678}
3679
Paul Bakker915275b2012-09-28 07:10:55 +00003680static int x509parse_verify_top(
3681 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003682 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003683 int (*f_vrfy)(void *, x509_cert *, int, int *),
3684 void *p_vrfy )
3685{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003686 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003687 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003688 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3689 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003690
3691 if( x509parse_time_expired( &child->valid_to ) )
3692 *flags |= BADCERT_EXPIRED;
3693
3694 /*
3695 * Child is the top of the chain. Check against the trust_ca list.
3696 */
3697 *flags |= BADCERT_NOT_TRUSTED;
3698
3699 while( trust_ca != NULL )
3700 {
3701 if( trust_ca->version == 0 ||
3702 child->issuer_raw.len != trust_ca->subject_raw.len ||
3703 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3704 child->issuer_raw.len ) != 0 )
3705 {
3706 trust_ca = trust_ca->next;
3707 continue;
3708 }
3709
Paul Bakker9a736322012-11-14 12:39:52 +00003710 /*
3711 * Reduce path_len to check against if top of the chain is
3712 * the same as the trusted CA
3713 */
3714 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3715 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3716 child->issuer_raw.len ) == 0 )
3717 {
3718 check_path_cnt--;
3719 }
3720
Paul Bakker915275b2012-09-28 07:10:55 +00003721 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003722 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003723 {
3724 trust_ca = trust_ca->next;
3725 continue;
3726 }
3727
Paul Bakkerc70b9822013-04-07 22:00:46 +02003728 md_info = md_info_from_type( child->sig_md );
3729 if( md_info == NULL )
3730 {
3731 /*
3732 * Cannot check 'unknown' hash
3733 */
3734 continue;
3735 }
Paul Bakker915275b2012-09-28 07:10:55 +00003736
Paul Bakkerc70b9822013-04-07 22:00:46 +02003737 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003738
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003739 /* EC NOT IMPLEMENTED YET */
3740 if( trust_ca->pk.type != POLARSSL_PK_RSA )
3741 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3742
3743 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003744 0, hash, child->sig.p ) != 0 )
3745 {
3746 trust_ca = trust_ca->next;
3747 continue;
3748 }
3749
3750 /*
3751 * Top of chain is signed by a trusted CA
3752 */
3753 *flags &= ~BADCERT_NOT_TRUSTED;
3754 break;
3755 }
3756
Paul Bakker9a736322012-11-14 12:39:52 +00003757 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003758 * If top of chain is not the same as the trusted CA send a verify request
3759 * to the callback for any issues with validity and CRL presence for the
3760 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003761 */
3762 if( trust_ca != NULL &&
3763 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3764 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3765 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003766 {
3767 /* Check trusted CA's CRL for then chain's top crt */
3768 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3769
3770 if( x509parse_time_expired( &trust_ca->valid_to ) )
3771 ca_flags |= BADCERT_EXPIRED;
3772
Paul Bakker915275b2012-09-28 07:10:55 +00003773 if( NULL != f_vrfy )
3774 {
Paul Bakker9a736322012-11-14 12:39:52 +00003775 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003776 return( ret );
3777 }
3778 }
3779
3780 /* Call callback on top cert */
3781 if( NULL != f_vrfy )
3782 {
Paul Bakker9a736322012-11-14 12:39:52 +00003783 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003784 return( ret );
3785 }
3786
Paul Bakker915275b2012-09-28 07:10:55 +00003787 *flags |= ca_flags;
3788
3789 return( 0 );
3790}
3791
3792static int x509parse_verify_child(
3793 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003794 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003795 int (*f_vrfy)(void *, x509_cert *, int, int *),
3796 void *p_vrfy )
3797{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003798 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003799 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003800 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003801 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003802 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003803
3804 if( x509parse_time_expired( &child->valid_to ) )
3805 *flags |= BADCERT_EXPIRED;
3806
Paul Bakkerc70b9822013-04-07 22:00:46 +02003807 md_info = md_info_from_type( child->sig_md );
3808 if( md_info == NULL )
3809 {
3810 /*
3811 * Cannot check 'unknown' hash
3812 */
Paul Bakker915275b2012-09-28 07:10:55 +00003813 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003814 }
3815 else
3816 {
3817 md( md_info, child->tbs.p, child->tbs.len, hash );
3818
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003819 /* EC NOT IMPLEMENTED YET */
3820 if( parent->pk.type != POLARSSL_PK_RSA )
3821 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3822
3823 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
3824 0, hash, child->sig.p ) != 0 )
3825 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003826 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003827 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003828 }
3829
Paul Bakker915275b2012-09-28 07:10:55 +00003830 /* Check trusted CA's CRL for the given crt */
3831 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3832
3833 grandparent = parent->next;
3834
3835 while( grandparent != NULL )
3836 {
3837 if( grandparent->version == 0 ||
3838 grandparent->ca_istrue == 0 ||
3839 parent->issuer_raw.len != grandparent->subject_raw.len ||
3840 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3841 parent->issuer_raw.len ) != 0 )
3842 {
3843 grandparent = grandparent->next;
3844 continue;
3845 }
3846 break;
3847 }
3848
Paul Bakker915275b2012-09-28 07:10:55 +00003849 if( grandparent != NULL )
3850 {
3851 /*
3852 * Part of the chain
3853 */
Paul Bakker9a736322012-11-14 12:39:52 +00003854 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 +00003855 if( ret != 0 )
3856 return( ret );
3857 }
3858 else
3859 {
Paul Bakker9a736322012-11-14 12:39:52 +00003860 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 +00003861 if( ret != 0 )
3862 return( ret );
3863 }
3864
3865 /* child is verified to be a child of the parent, call verify callback */
3866 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003867 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003868 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003869
3870 *flags |= parent_flags;
3871
3872 return( 0 );
3873}
3874
Paul Bakker76fd75a2011-01-16 21:12:10 +00003875/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 * Verify the certificate validity
3877 */
3878int x509parse_verify( x509_cert *crt,
3879 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003880 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003881 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003882 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003883 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003884{
Paul Bakker23986e52011-04-24 08:57:21 +00003885 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003886 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003887 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003888 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003889 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003890 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003891
Paul Bakker40ea7de2009-05-03 10:18:48 +00003892 *flags = 0;
3893
Paul Bakker5121ce52009-01-03 21:22:43 +00003894 if( cn != NULL )
3895 {
3896 name = &crt->subject;
3897 cn_len = strlen( cn );
3898
Paul Bakker4d2c1242012-05-10 14:12:46 +00003899 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003900 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003901 cur = &crt->subject_alt_names;
3902
3903 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003904 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003905 if( cur->buf.len == cn_len &&
3906 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003907 break;
3908
Paul Bakker535e97d2012-08-23 10:49:55 +00003909 if( cur->buf.len > 2 &&
3910 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003911 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003912 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003913
Paul Bakker4d2c1242012-05-10 14:12:46 +00003914 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003915 }
3916
3917 if( cur == NULL )
3918 *flags |= BADCERT_CN_MISMATCH;
3919 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003920 else
3921 {
3922 while( name != NULL )
3923 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003924 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003925 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003926 if( name->val.len == cn_len &&
3927 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003928 break;
3929
Paul Bakker535e97d2012-08-23 10:49:55 +00003930 if( name->val.len > 2 &&
3931 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003932 x509_wildcard_verify( cn, &name->val ) )
3933 break;
3934 }
3935
3936 name = name->next;
3937 }
3938
3939 if( name == NULL )
3940 *flags |= BADCERT_CN_MISMATCH;
3941 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 }
3943
Paul Bakker5121ce52009-01-03 21:22:43 +00003944 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003945 * Iterate upwards in the given cert chain, to find our crt parent.
3946 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003947 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003948 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003949
Paul Bakker76fd75a2011-01-16 21:12:10 +00003950 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003951 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003952 if( parent->ca_istrue == 0 ||
3953 crt->issuer_raw.len != parent->subject_raw.len ||
3954 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003955 crt->issuer_raw.len ) != 0 )
3956 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003957 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003958 continue;
3959 }
Paul Bakker915275b2012-09-28 07:10:55 +00003960 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003961 }
3962
Paul Bakker915275b2012-09-28 07:10:55 +00003963 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 {
Paul Bakker915275b2012-09-28 07:10:55 +00003965 /*
3966 * Part of the chain
3967 */
Paul Bakker9a736322012-11-14 12:39:52 +00003968 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003969 if( ret != 0 )
3970 return( ret );
3971 }
3972 else
Paul Bakker74111d32011-01-15 16:57:55 +00003973 {
Paul Bakker9a736322012-11-14 12:39:52 +00003974 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003975 if( ret != 0 )
3976 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003977 }
Paul Bakker915275b2012-09-28 07:10:55 +00003978
3979 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003980 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003981
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 return( 0 );
3983}
3984
3985/*
3986 * Unallocate all certificate data
3987 */
3988void x509_free( x509_cert *crt )
3989{
3990 x509_cert *cert_cur = crt;
3991 x509_cert *cert_prv;
3992 x509_name *name_cur;
3993 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003994 x509_sequence *seq_cur;
3995 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003996
3997 if( crt == NULL )
3998 return;
3999
4000 do
4001 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02004002 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00004003
4004 name_cur = cert_cur->issuer.next;
4005 while( name_cur != NULL )
4006 {
4007 name_prv = name_cur;
4008 name_cur = name_cur->next;
4009 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004010 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 }
4012
4013 name_cur = cert_cur->subject.next;
4014 while( name_cur != NULL )
4015 {
4016 name_prv = name_cur;
4017 name_cur = name_cur->next;
4018 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004019 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004020 }
4021
Paul Bakker74111d32011-01-15 16:57:55 +00004022 seq_cur = cert_cur->ext_key_usage.next;
4023 while( seq_cur != NULL )
4024 {
4025 seq_prv = seq_cur;
4026 seq_cur = seq_cur->next;
4027 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004028 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004029 }
4030
Paul Bakker8afa70d2012-02-11 18:42:45 +00004031 seq_cur = cert_cur->subject_alt_names.next;
4032 while( seq_cur != NULL )
4033 {
4034 seq_prv = seq_cur;
4035 seq_cur = seq_cur->next;
4036 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004037 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004038 }
4039
Paul Bakker5121ce52009-01-03 21:22:43 +00004040 if( cert_cur->raw.p != NULL )
4041 {
4042 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004043 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 }
4045
4046 cert_cur = cert_cur->next;
4047 }
4048 while( cert_cur != NULL );
4049
4050 cert_cur = crt;
4051 do
4052 {
4053 cert_prv = cert_cur;
4054 cert_cur = cert_cur->next;
4055
4056 memset( cert_prv, 0, sizeof( x509_cert ) );
4057 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004058 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004059 }
4060 while( cert_cur != NULL );
4061}
4062
Paul Bakkerd98030e2009-05-02 15:13:40 +00004063/*
4064 * Unallocate all CRL data
4065 */
4066void x509_crl_free( x509_crl *crl )
4067{
4068 x509_crl *crl_cur = crl;
4069 x509_crl *crl_prv;
4070 x509_name *name_cur;
4071 x509_name *name_prv;
4072 x509_crl_entry *entry_cur;
4073 x509_crl_entry *entry_prv;
4074
4075 if( crl == NULL )
4076 return;
4077
4078 do
4079 {
4080 name_cur = crl_cur->issuer.next;
4081 while( name_cur != NULL )
4082 {
4083 name_prv = name_cur;
4084 name_cur = name_cur->next;
4085 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004086 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004087 }
4088
4089 entry_cur = crl_cur->entry.next;
4090 while( entry_cur != NULL )
4091 {
4092 entry_prv = entry_cur;
4093 entry_cur = entry_cur->next;
4094 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004095 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004096 }
4097
4098 if( crl_cur->raw.p != NULL )
4099 {
4100 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004101 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004102 }
4103
4104 crl_cur = crl_cur->next;
4105 }
4106 while( crl_cur != NULL );
4107
4108 crl_cur = crl;
4109 do
4110 {
4111 crl_prv = crl_cur;
4112 crl_cur = crl_cur->next;
4113
4114 memset( crl_prv, 0, sizeof( x509_crl ) );
4115 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004116 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004117 }
4118 while( crl_cur != NULL );
4119}
4120
Paul Bakker40e46942009-01-03 21:51:57 +00004121#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004122
Paul Bakker40e46942009-01-03 21:51:57 +00004123#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004124
4125/*
4126 * Checkup routine
4127 */
4128int x509_self_test( int verbose )
4129{
Paul Bakker5690efc2011-05-26 13:16:06 +00004130#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004131 int ret;
4132 int flags;
4133 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004134 x509_cert cacert;
4135 x509_cert clicert;
4136 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004137#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004138 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004139#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004140
4141 if( verbose != 0 )
4142 printf( " X.509 certificate load: " );
4143
4144 memset( &clicert, 0, sizeof( x509_cert ) );
4145
Paul Bakker3c2122f2013-06-24 19:03:14 +02004146 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004147 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004148 if( ret != 0 )
4149 {
4150 if( verbose != 0 )
4151 printf( "failed\n" );
4152
4153 return( ret );
4154 }
4155
4156 memset( &cacert, 0, sizeof( x509_cert ) );
4157
Paul Bakker3c2122f2013-06-24 19:03:14 +02004158 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004159 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004160 if( ret != 0 )
4161 {
4162 if( verbose != 0 )
4163 printf( "failed\n" );
4164
4165 return( ret );
4166 }
4167
4168 if( verbose != 0 )
4169 printf( "passed\n X.509 private key load: " );
4170
4171 i = strlen( test_ca_key );
4172 j = strlen( test_ca_pwd );
4173
Paul Bakker66b78b22011-03-25 14:22:50 +00004174 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4175
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004176 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004177 (const unsigned char *) test_ca_key, i,
4178 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004179 {
4180 if( verbose != 0 )
4181 printf( "failed\n" );
4182
4183 return( ret );
4184 }
4185
4186 if( verbose != 0 )
4187 printf( "passed\n X.509 signature verify: ");
4188
Paul Bakker23986e52011-04-24 08:57:21 +00004189 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004190 if( ret != 0 )
4191 {
Paul Bakker23986e52011-04-24 08:57:21 +00004192 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004193 if( verbose != 0 )
4194 printf( "failed\n" );
4195
4196 return( ret );
4197 }
4198
Paul Bakker5690efc2011-05-26 13:16:06 +00004199#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004200 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004201 printf( "passed\n X.509 DHM parameter load: " );
4202
4203 i = strlen( test_dhm_params );
4204 j = strlen( test_ca_pwd );
4205
Paul Bakker3c2122f2013-06-24 19:03:14 +02004206 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004207 {
4208 if( verbose != 0 )
4209 printf( "failed\n" );
4210
4211 return( ret );
4212 }
4213
4214 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004215 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004216#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004217
4218 x509_free( &cacert );
4219 x509_free( &clicert );
4220 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004221#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004222 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004223#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004224
4225 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004226#else
4227 ((void) verbose);
4228 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4229#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004230}
4231
4232#endif
4233
4234#endif