blob: 605e40c42f238c03a9bdbf1ad82609dfa243ecb8 [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é-Gonnard0a64e8f2013-07-08 18:26:18 +0200163/* Get an algorithm identifier and its parameters
164 *
165 * AlgorithmIdentifier ::= SEQUENCE {
166 * algorithm OBJECT IDENTIFIER,
167 * parameters ANY DEFINED BY algorithm OPTIONAL }
168 */
169static int x509_get_algid( unsigned char **p,
170 const unsigned char *end,
171 pk_type_t *pk_alg, x509_buf *params )
172{
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
Paul Bakker5121ce52009-01-03 21:22:43 +0000197/*
198 * AlgorithmIdentifier ::= SEQUENCE {
199 * algorithm OBJECT IDENTIFIER,
200 * parameters ANY DEFINED BY algorithm OPTIONAL }
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200201 *
202 * If params_end is NULL, then parameters must be absent or ANS.1 NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 */
204static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000205 const unsigned char *end,
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200206 x509_buf *alg, const unsigned char **params_end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207{
Paul Bakker23986e52011-04-24 08:57:21 +0000208 int ret;
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200209 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200211 if( params_end == NULL ) {
212 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
213 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
214
215 return( 0 );
216 }
217
218 /* TODO: use asn1_get_alg */
219 if( ( ret = asn1_get_tag( p, end, &len,
220 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
221 {
222 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
223 }
224
225 end = *p + len;
226 alg->tag = **p;
227
228 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000229 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200231 alg->p = *p;
232 *p += alg->len;
233
234 *params_end = end;
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 return( 0 );
236}
237
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200238/* Get an EC group id from an ECParameters buffer
239 *
240 * ECParameters ::= CHOICE {
241 * namedCurve OBJECT IDENTIFIER
242 * -- implicitCurve NULL
243 * -- specifiedCurve SpecifiedECDomain
244 * }
245 */
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200246static int x509_ecparams_get_grp_id( const x509_buf *params,
247 ecp_group_id *grp_id )
248{
249 if( oid_get_ec_grp( params, grp_id ) != 0 )
250 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
251
252 return( 0 );
253}
254
255/* Get an EC group id from an ECParameters buffer
256 *
257 * ECParameters ::= CHOICE {
258 * namedCurve OBJECT IDENTIFIER
259 * -- implicitCurve NULL
260 * -- specifiedCurve SpecifiedECDomain
261 * }
262 */
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200263static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
264 ecp_group_id *grp_id )
265{
266 int ret;
267 x509_buf curve;
268
269 curve.tag = **p;
270
271 if( ( ret = asn1_get_tag( p, end, &curve.len, ASN1_OID ) ) != 0 )
272 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
273
274 curve.p = *p;
275 *p += curve.len;
276
277 if( *p != end )
278 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
279 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
280
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200281 return( x509_ecparams_get_grp_id( &curve, grp_id ) );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200282}
283
Paul Bakker5121ce52009-01-03 21:22:43 +0000284/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200285 * subjectPublicKey BIT STRING
286 * -- which, in our case, contains
287 * ECPoint ::= octet string (not ASN.1)
288 */
289static int x509_get_subpubkey_ec( unsigned char **p, const unsigned char *end,
290 const ecp_group *grp, ecp_point *pt )
291{
292 int ret;
293 size_t len;
294
295 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
296 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
297
298 if( *p + len != end )
299 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
300 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
301
302 /*
303 * First byte in the content of BIT STRING is the nummber of padding bit.
304 * Here it is always 0 since ECPoint is an octet string, so skip it.
305 */
306 ++*p;
307 --len;
308
309 if( ( ret = ecp_point_read_binary( grp, pt,
310 (const unsigned char *) *p, len ) ) != 0 )
311 {
312 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
313 }
314
315 return( 0 );
316}
317
318/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 * AttributeTypeAndValue ::= SEQUENCE {
320 * type AttributeType,
321 * value AttributeValue }
322 *
323 * AttributeType ::= OBJECT IDENTIFIER
324 *
325 * AttributeValue ::= ANY DEFINED BY AttributeType
326 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000327static int x509_get_attr_type_value( unsigned char **p,
328 const unsigned char *end,
329 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000330{
Paul Bakker23986e52011-04-24 08:57:21 +0000331 int ret;
332 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 x509_buf *oid;
334 x509_buf *val;
335
336 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000338 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 oid = &cur->oid;
341 oid->tag = **p;
342
343 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000344 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346 oid->p = *p;
347 *p += oid->len;
348
349 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000350 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000351 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
354 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
355 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000356 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000357 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
359 val = &cur->val;
360 val->tag = *(*p)++;
361
362 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000363 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
365 val->p = *p;
366 *p += val->len;
367
368 cur->next = NULL;
369
Paul Bakker400ff6f2011-02-20 10:40:16 +0000370 return( 0 );
371}
372
373/*
374 * RelativeDistinguishedName ::=
375 * SET OF AttributeTypeAndValue
376 *
377 * AttributeTypeAndValue ::= SEQUENCE {
378 * type AttributeType,
379 * value AttributeValue }
380 *
381 * AttributeType ::= OBJECT IDENTIFIER
382 *
383 * AttributeValue ::= ANY DEFINED BY AttributeType
384 */
385static int x509_get_name( unsigned char **p,
386 const unsigned char *end,
387 x509_name *cur )
388{
Paul Bakker23986e52011-04-24 08:57:21 +0000389 int ret;
390 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000391 const unsigned char *end2;
392 x509_name *use;
393
394 if( ( ret = asn1_get_tag( p, end, &len,
395 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000396 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000397
398 end2 = end;
399 end = *p + len;
400 use = cur;
401
402 do
403 {
404 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
405 return( ret );
406
407 if( *p != end )
408 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200409 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000410 sizeof( x509_name ) );
411
412 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000413 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000414
415 memset( use->next, 0, sizeof( x509_name ) );
416
417 use = use->next;
418 }
419 }
420 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 /*
423 * recurse until end of SEQUENCE is reached
424 */
425 if( *p == end2 )
426 return( 0 );
427
Paul Bakker6e339b52013-07-03 13:37:05 +0200428 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 sizeof( x509_name ) );
430
431 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000432 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
Paul Bakker430ffbe2012-05-01 08:14:20 +0000434 memset( cur->next, 0, sizeof( x509_name ) );
435
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 return( x509_get_name( p, end2, cur->next ) );
437}
438
439/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 * Time ::= CHOICE {
441 * utcTime UTCTime,
442 * generalTime GeneralizedTime }
443 */
Paul Bakker91200182010-02-18 21:26:15 +0000444static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000445 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000446 x509_time *time )
447{
Paul Bakker23986e52011-04-24 08:57:21 +0000448 int ret;
449 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000450 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000451 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000452
Paul Bakker91200182010-02-18 21:26:15 +0000453 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000454 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
455 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000456
Paul Bakker91200182010-02-18 21:26:15 +0000457 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000458
Paul Bakker91200182010-02-18 21:26:15 +0000459 if ( tag == ASN1_UTC_TIME )
460 {
461 (*p)++;
462 ret = asn1_get_len( p, end, &len );
463
464 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000465 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000466
Paul Bakker91200182010-02-18 21:26:15 +0000467 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000468 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
469 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000470
Paul Bakker91200182010-02-18 21:26:15 +0000471 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
472 &time->year, &time->mon, &time->day,
473 &time->hour, &time->min, &time->sec ) < 5 )
474 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000475
Paul Bakker400ff6f2011-02-20 10:40:16 +0000476 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000477 time->year += 1900;
478
479 *p += len;
480
481 return( 0 );
482 }
483 else if ( tag == ASN1_GENERALIZED_TIME )
484 {
485 (*p)++;
486 ret = asn1_get_len( p, end, &len );
487
488 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000490
491 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000492 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
493 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000494
495 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
496 &time->year, &time->mon, &time->day,
497 &time->hour, &time->min, &time->sec ) < 5 )
498 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
499
500 *p += len;
501
502 return( 0 );
503 }
504 else
Paul Bakker9d781402011-05-09 16:17:09 +0000505 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000506}
507
508
509/*
510 * Validity ::= SEQUENCE {
511 * notBefore Time,
512 * notAfter Time }
513 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000514static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000515 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 x509_time *from,
517 x509_time *to )
518{
Paul Bakker23986e52011-04-24 08:57:21 +0000519 int ret;
520 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 if( ( ret = asn1_get_tag( p, end, &len,
523 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000524 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 end = *p + len;
527
Paul Bakker91200182010-02-18 21:26:15 +0000528 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000529 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
Paul Bakker91200182010-02-18 21:26:15 +0000531 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000532 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000535 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000536 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
538 return( 0 );
539}
540
541/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200542 * RSAPublicKey ::= SEQUENCE {
543 * modulus INTEGER, -- n
544 * publicExponent INTEGER -- e
545 * }
546 */
547static int x509_get_rsapubkey( unsigned char **p,
548 const unsigned char *end,
549 rsa_context *rsa )
550{
551 int ret;
552 size_t len;
553
554 if( ( ret = asn1_get_tag( p, end, &len,
555 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
556 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
557
558 if( *p + len != end )
559 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
560 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
561
562 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
563 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
564 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
565
566 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
567 return( ret );
568
569 rsa->len = mpi_size( &rsa->N );
570
571 return( 0 );
572}
573
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200574static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
575 x509_buf *alg_params, ecp_keypair *key )
576{
577 int ret;
578 ecp_group_id grp_id;
579
580 if( ( ret = x509_ecparams_get_grp_id( alg_params, &grp_id ) ) != 0 )
581 return( ret );
582
583 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
584 return( ret );
585
586 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
587 (const unsigned char *) *p, end - *p ) ) != 0 )
588 {
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200589 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200590 }
591
592 return( 0 );
593}
594
595/*
596 * SubjectPublicKeyInfo ::= SEQUENCE {
597 * algorithm AlgorithmIdentifier,
598 * subjectPublicKey BIT STRING }
599 */
600static int x509_get_pubkey( unsigned char **p,
601 const unsigned char *end,
602 pk_context *pk )
603{
604 int ret;
605 size_t len;
606 x509_buf alg_params;
607 pk_type_t pk_alg = POLARSSL_PK_NONE;
608
609 if( ( ret = asn1_get_tag( p, end, &len,
610 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
611 {
612 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
613 }
614
615 end = *p + len;
616
617 if( ( ret = x509_get_algid( p, end, &pk_alg, &alg_params ) ) != 0 )
618 return( ret );
619
620 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
621 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
622
623 if( ( end - *p ) < 1 )
624 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
625 POLARSSL_ERR_ASN1_OUT_OF_DATA );
626
627 if( *p + len != end )
628 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
629 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
630
631 if( *(*p)++ != 0 )
632 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
633
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200634 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
635 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200636
637 switch( pk_alg )
638 {
639 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200640 ret = POLARSSL_ERR_X509_UNKNOWN_PK_ALG;
641 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200642
643 case POLARSSL_PK_RSA:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200644 ret = x509_get_rsapubkey( p, end, pk->data );
645 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200646
647 case POLARSSL_PK_ECKEY_DH:
648 ((ecp_keypair *) pk->data)->alg = POLARSSL_ECP_KEY_ALG_ECDH;
649 /* FALLTHROUGH */
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200650
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200651 case POLARSSL_PK_ECKEY:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200652 ret = x509_get_ecpubkey( p, end, &alg_params, pk->data );
653 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200654 }
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200655
656 if( ret != 0 )
657 pk_free( pk );
658
659 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200660}
661
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200662/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 * SubjectPublicKeyInfo ::= SEQUENCE {
664 * algorithm AlgorithmIdentifier,
665 * subjectPublicKey BIT STRING }
666 */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200667static int x509_get_pubkey_rsa( unsigned char **p,
668 const unsigned char *end,
669 rsa_context *rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000670{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200671 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000672 size_t len;
Manuel Pégourié-Gonnard788db112013-07-09 11:26:17 +0200673 x509_buf pk_alg_oid;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200674 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +0200676 if( ( ret = asn1_get_tag( p, end, &len,
677 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
678 {
679 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
680 }
681
682 end = *p + len;
683
Manuel Pégourié-Gonnard788db112013-07-09 11:26:17 +0200684 if( ( ret = asn1_get_alg_null( p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200685 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686
687 /*
688 * only RSA public keys handled at this time
689 */
Manuel Pégourié-Gonnard788db112013-07-09 11:26:17 +0200690 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +0200691 {
Paul Bakkered56b222011-07-13 11:26:43 +0000692 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +0200693 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200695 if (pk_alg != POLARSSL_PK_RSA )
696 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
697
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000699 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000702 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000703 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Manuel Pégourié-Gonnardf16ac762013-07-09 12:26:00 +0200705 if( *p + len != end )
706 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
707 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
709 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000710 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200712 return( x509_get_rsapubkey( p, end, rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000713}
714
715static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000716 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 x509_buf *sig )
718{
Paul Bakker23986e52011-04-24 08:57:21 +0000719 int ret;
720 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000721
Paul Bakker8afa70d2012-02-11 18:42:45 +0000722 if( ( end - *p ) < 1 )
723 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
724 POLARSSL_ERR_ASN1_OUT_OF_DATA );
725
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 sig->tag = **p;
727
728 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000729 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Paul Bakker74111d32011-01-15 16:57:55 +0000731
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000733 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 sig->len = len;
736 sig->p = *p;
737
738 *p += len;
739
740 return( 0 );
741}
742
743/*
744 * X.509 v2/v3 unique identifier (not parsed)
745 */
746static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000747 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 x509_buf *uid, int n )
749{
750 int ret;
751
752 if( *p == end )
753 return( 0 );
754
755 uid->tag = **p;
756
757 if( ( ret = asn1_get_tag( p, end, &uid->len,
758 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
759 {
Paul Bakker40e46942009-01-03 21:51:57 +0000760 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 return( 0 );
762
763 return( ret );
764 }
765
766 uid->p = *p;
767 *p += uid->len;
768
769 return( 0 );
770}
771
772/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000773 * X.509 Extensions (No parsing of extensions, pointer should
774 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 */
776static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000777 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000778 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000779{
Paul Bakker23986e52011-04-24 08:57:21 +0000780 int ret;
781 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
783 if( *p == end )
784 return( 0 );
785
786 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000787
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000789 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000790 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
792 ext->p = *p;
793 end = *p + ext->len;
794
795 /*
796 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
797 *
798 * Extension ::= SEQUENCE {
799 * extnID OBJECT IDENTIFIER,
800 * critical BOOLEAN DEFAULT FALSE,
801 * extnValue OCTET STRING }
802 */
803 if( ( ret = asn1_get_tag( p, end, &len,
804 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000805 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
807 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000808 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000809 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000810
Paul Bakkerd98030e2009-05-02 15:13:40 +0000811 return( 0 );
812}
813
814/*
815 * X.509 CRL v2 extensions (no extensions parsed yet.)
816 */
817static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000818 const unsigned char *end,
819 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000820{
Paul Bakker23986e52011-04-24 08:57:21 +0000821 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000822 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000823
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000824 /* Get explicit tag */
825 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000826 {
827 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
828 return( 0 );
829
830 return( ret );
831 }
832
833 while( *p < end )
834 {
835 if( ( ret = asn1_get_tag( p, end, &len,
836 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000837 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000838
839 *p += len;
840 }
841
842 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000843 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000844 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
845
846 return( 0 );
847}
848
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000849/*
850 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
851 */
852static int x509_get_crl_entry_ext( unsigned char **p,
853 const unsigned char *end,
854 x509_buf *ext )
855{
856 int ret;
857 size_t len = 0;
858
859 /* OPTIONAL */
860 if (end <= *p)
861 return( 0 );
862
863 ext->tag = **p;
864 ext->p = *p;
865
866 /*
867 * Get CRL-entry extension sequence header
868 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
869 */
870 if( ( ret = asn1_get_tag( p, end, &ext->len,
871 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
872 {
873 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
874 {
875 ext->p = NULL;
876 return( 0 );
877 }
878 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
879 }
880
881 end = *p + ext->len;
882
883 if( end != *p + ext->len )
884 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
885 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
886
887 while( *p < end )
888 {
889 if( ( ret = asn1_get_tag( p, end, &len,
890 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
891 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
892
893 *p += len;
894 }
895
896 if( *p != end )
897 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
898 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
899
900 return( 0 );
901}
902
Paul Bakker74111d32011-01-15 16:57:55 +0000903static int x509_get_basic_constraints( unsigned char **p,
904 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000905 int *ca_istrue,
906 int *max_pathlen )
907{
Paul Bakker23986e52011-04-24 08:57:21 +0000908 int ret;
909 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000910
911 /*
912 * BasicConstraints ::= SEQUENCE {
913 * cA BOOLEAN DEFAULT FALSE,
914 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
915 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000916 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000917 *max_pathlen = 0; /* endless */
918
919 if( ( ret = asn1_get_tag( p, end, &len,
920 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000921 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000922
923 if( *p == end )
924 return 0;
925
Paul Bakker3cccddb2011-01-16 21:46:31 +0000926 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000927 {
928 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000929 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000930
931 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000932 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000933
Paul Bakker3cccddb2011-01-16 21:46:31 +0000934 if( *ca_istrue != 0 )
935 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000936 }
937
938 if( *p == end )
939 return 0;
940
941 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000942 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000943
944 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000945 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000946 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
947
948 (*max_pathlen)++;
949
Paul Bakker74111d32011-01-15 16:57:55 +0000950 return 0;
951}
952
953static int x509_get_ns_cert_type( unsigned char **p,
954 const unsigned char *end,
955 unsigned char *ns_cert_type)
956{
957 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000958 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000959
960 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000961 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000962
963 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000964 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000965 POLARSSL_ERR_ASN1_INVALID_LENGTH );
966
967 /* Get actual bitstring */
968 *ns_cert_type = *bs.p;
969 return 0;
970}
971
972static int x509_get_key_usage( unsigned char **p,
973 const unsigned char *end,
974 unsigned char *key_usage)
975{
976 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000977 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000978
979 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000980 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000981
Paul Bakker94a67962012-08-23 13:03:52 +0000982 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000983 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000984 POLARSSL_ERR_ASN1_INVALID_LENGTH );
985
986 /* Get actual bitstring */
987 *key_usage = *bs.p;
988 return 0;
989}
990
Paul Bakkerd98030e2009-05-02 15:13:40 +0000991/*
Paul Bakker74111d32011-01-15 16:57:55 +0000992 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
993 *
994 * KeyPurposeId ::= OBJECT IDENTIFIER
995 */
996static int x509_get_ext_key_usage( unsigned char **p,
997 const unsigned char *end,
998 x509_sequence *ext_key_usage)
999{
1000 int ret;
1001
1002 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001003 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +00001004
1005 /* Sequence length must be >= 1 */
1006 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +00001007 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001008 POLARSSL_ERR_ASN1_INVALID_LENGTH );
1009
1010 return 0;
1011}
1012
1013/*
Paul Bakkera8cd2392012-02-11 16:09:32 +00001014 * SubjectAltName ::= GeneralNames
1015 *
1016 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
1017 *
1018 * GeneralName ::= CHOICE {
1019 * otherName [0] OtherName,
1020 * rfc822Name [1] IA5String,
1021 * dNSName [2] IA5String,
1022 * x400Address [3] ORAddress,
1023 * directoryName [4] Name,
1024 * ediPartyName [5] EDIPartyName,
1025 * uniformResourceIdentifier [6] IA5String,
1026 * iPAddress [7] OCTET STRING,
1027 * registeredID [8] OBJECT IDENTIFIER }
1028 *
1029 * OtherName ::= SEQUENCE {
1030 * type-id OBJECT IDENTIFIER,
1031 * value [0] EXPLICIT ANY DEFINED BY type-id }
1032 *
1033 * EDIPartyName ::= SEQUENCE {
1034 * nameAssigner [0] DirectoryString OPTIONAL,
1035 * partyName [1] DirectoryString }
1036 *
1037 * NOTE: PolarSSL only parses and uses dNSName at this point.
1038 */
1039static int x509_get_subject_alt_name( unsigned char **p,
1040 const unsigned char *end,
1041 x509_sequence *subject_alt_name )
1042{
1043 int ret;
1044 size_t len, tag_len;
1045 asn1_buf *buf;
1046 unsigned char tag;
1047 asn1_sequence *cur = subject_alt_name;
1048
1049 /* Get main sequence tag */
1050 if( ( ret = asn1_get_tag( p, end, &len,
1051 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1052 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
1053
1054 if( *p + len != end )
1055 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1056 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1057
1058 while( *p < end )
1059 {
1060 if( ( end - *p ) < 1 )
1061 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1062 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1063
1064 tag = **p;
1065 (*p)++;
1066 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
1067 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
1068
1069 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
1070 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1071 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1072
1073 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
1074 {
1075 *p += tag_len;
1076 continue;
1077 }
1078
1079 buf = &(cur->buf);
1080 buf->tag = tag;
1081 buf->p = *p;
1082 buf->len = tag_len;
1083 *p += buf->len;
1084
1085 /* Allocate and assign next pointer */
1086 if (*p < end)
1087 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001088 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +00001089 sizeof( asn1_sequence ) );
1090
1091 if( cur->next == NULL )
1092 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1093 POLARSSL_ERR_ASN1_MALLOC_FAILED );
1094
Paul Bakker535e97d2012-08-23 10:49:55 +00001095 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +00001096 cur = cur->next;
1097 }
1098 }
1099
1100 /* Set final sequence entry's next pointer to NULL */
1101 cur->next = NULL;
1102
1103 if( *p != end )
1104 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1105 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1106
1107 return( 0 );
1108}
1109
1110/*
Paul Bakker74111d32011-01-15 16:57:55 +00001111 * X.509 v3 extensions
1112 *
1113 * TODO: Perform all of the basic constraints tests required by the RFC
1114 * TODO: Set values for undetected extensions to a sane default?
1115 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001116 */
1117static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001118 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001119 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001120{
Paul Bakker23986e52011-04-24 08:57:21 +00001121 int ret;
1122 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001123 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001124
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001125 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001126 {
1127 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1128 return( 0 );
1129
1130 return( ret );
1131 }
1132
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 while( *p < end )
1134 {
Paul Bakker74111d32011-01-15 16:57:55 +00001135 /*
1136 * Extension ::= SEQUENCE {
1137 * extnID OBJECT IDENTIFIER,
1138 * critical BOOLEAN DEFAULT FALSE,
1139 * extnValue OCTET STRING }
1140 */
1141 x509_buf extn_oid = {0, 0, NULL};
1142 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001143 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001144
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 if( ( ret = asn1_get_tag( p, end, &len,
1146 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001147 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001149 end_ext_data = *p + len;
1150
Paul Bakker74111d32011-01-15 16:57:55 +00001151 /* Get extension ID */
1152 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Paul Bakker74111d32011-01-15 16:57:55 +00001154 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001155 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Paul Bakker74111d32011-01-15 16:57:55 +00001157 extn_oid.p = *p;
1158 *p += extn_oid.len;
1159
1160 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001161 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001162 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1163
1164 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001165 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001166 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001167 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Paul Bakker74111d32011-01-15 16:57:55 +00001169 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001170 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001172 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001174 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001175
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001176 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001177 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001178 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
Paul Bakker74111d32011-01-15 16:57:55 +00001180 /*
1181 * Detect supported extensions
1182 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001183 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1184
1185 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001186 {
1187 /* No parser found, skip extension */
1188 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001189
Paul Bakker5c721f92011-07-27 16:51:09 +00001190#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001191 if( is_critical )
1192 {
1193 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001194 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001195 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1196 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001197#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001198 continue;
1199 }
1200
1201 crt->ext_types |= ext_type;
1202
1203 switch( ext_type )
1204 {
1205 case EXT_BASIC_CONSTRAINTS:
1206 /* Parse basic constraints */
1207 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1208 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1209 return ( ret );
1210 break;
1211
1212 case EXT_KEY_USAGE:
1213 /* Parse key usage */
1214 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1215 &crt->key_usage ) ) != 0 )
1216 return ( ret );
1217 break;
1218
1219 case EXT_EXTENDED_KEY_USAGE:
1220 /* Parse extended key usage */
1221 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1222 &crt->ext_key_usage ) ) != 0 )
1223 return ( ret );
1224 break;
1225
1226 case EXT_SUBJECT_ALT_NAME:
1227 /* Parse subject alt name */
1228 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1229 &crt->subject_alt_names ) ) != 0 )
1230 return ( ret );
1231 break;
1232
1233 case EXT_NS_CERT_TYPE:
1234 /* Parse netscape certificate type */
1235 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1236 &crt->ns_cert_type ) ) != 0 )
1237 return ( ret );
1238 break;
1239
1240 default:
1241 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 }
1244
1245 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001246 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001247 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 return( 0 );
1250}
1251
1252/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001253 * X.509 CRL Entries
1254 */
1255static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001256 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001257 x509_crl_entry *entry )
1258{
Paul Bakker23986e52011-04-24 08:57:21 +00001259 int ret;
1260 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001261 x509_crl_entry *cur_entry = entry;
1262
1263 if( *p == end )
1264 return( 0 );
1265
Paul Bakker9be19372009-07-27 20:21:53 +00001266 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001267 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1268 {
1269 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1270 return( 0 );
1271
1272 return( ret );
1273 }
1274
Paul Bakker9be19372009-07-27 20:21:53 +00001275 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001276
1277 while( *p < end )
1278 {
Paul Bakker23986e52011-04-24 08:57:21 +00001279 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001280 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001281
1282 if( ( ret = asn1_get_tag( p, end, &len2,
1283 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1284 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001285 return( ret );
1286 }
1287
Paul Bakker9be19372009-07-27 20:21:53 +00001288 cur_entry->raw.tag = **p;
1289 cur_entry->raw.p = *p;
1290 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001291 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001292
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001293 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001294 return( ret );
1295
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001296 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001297 return( ret );
1298
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001299 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001300 return( ret );
1301
Paul Bakker74111d32011-01-15 16:57:55 +00001302 if ( *p < end )
1303 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001304 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001305
1306 if( cur_entry->next == NULL )
1307 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1308
Paul Bakkerd98030e2009-05-02 15:13:40 +00001309 cur_entry = cur_entry->next;
1310 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1311 }
1312 }
1313
1314 return( 0 );
1315}
1316
Paul Bakkerc70b9822013-04-07 22:00:46 +02001317static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1318 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001319{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001320 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001321
Paul Bakkerc70b9822013-04-07 22:00:46 +02001322 if( ret != 0 )
1323 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001324
Paul Bakkerc70b9822013-04-07 22:00:46 +02001325 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001326}
1327
Paul Bakkerd98030e2009-05-02 15:13:40 +00001328/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001329 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001331static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1332 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001333{
Paul Bakker23986e52011-04-24 08:57:21 +00001334 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001335 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001336 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Paul Bakker320a4b52009-03-28 18:52:39 +00001338 /*
1339 * Check for valid input
1340 */
1341 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001342 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001343
Paul Bakker6e339b52013-07-03 13:37:05 +02001344 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001345
1346 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001347 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001348
1349 memcpy( p, buf, buflen );
1350
1351 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
1353 crt->raw.p = p;
1354 crt->raw.len = len;
1355 end = p + len;
1356
1357 /*
1358 * Certificate ::= SEQUENCE {
1359 * tbsCertificate TBSCertificate,
1360 * signatureAlgorithm AlgorithmIdentifier,
1361 * signatureValue BIT STRING }
1362 */
1363 if( ( ret = asn1_get_tag( &p, end, &len,
1364 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1365 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001366 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001367 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 }
1369
Paul Bakkerb00ca422012-09-25 12:10:00 +00001370 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001372 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001373 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001374 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001376 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001377
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 /*
1379 * TBSCertificate ::= SEQUENCE {
1380 */
1381 crt->tbs.p = p;
1382
1383 if( ( ret = asn1_get_tag( &p, end, &len,
1384 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1385 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001386 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001387 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 }
1389
1390 end = p + len;
1391 crt->tbs.len = end - crt->tbs.p;
1392
1393 /*
1394 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1395 *
1396 * CertificateSerialNumber ::= INTEGER
1397 *
1398 * signature AlgorithmIdentifier
1399 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001400 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1401 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1402 ( ret = x509_get_alg( &p, end, &crt->sig_oid1, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001404 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 return( ret );
1406 }
1407
1408 crt->version++;
1409
1410 if( crt->version > 3 )
1411 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001412 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001413 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 }
1415
Paul Bakkerc70b9822013-04-07 22:00:46 +02001416 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1417 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001419 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001420 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 }
1422
1423 /*
1424 * issuer Name
1425 */
1426 crt->issuer_raw.p = p;
1427
1428 if( ( ret = asn1_get_tag( &p, end, &len,
1429 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1430 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001431 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001432 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 }
1434
1435 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1436 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001437 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 return( ret );
1439 }
1440
1441 crt->issuer_raw.len = p - crt->issuer_raw.p;
1442
1443 /*
1444 * Validity ::= SEQUENCE {
1445 * notBefore Time,
1446 * notAfter Time }
1447 *
1448 */
1449 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1450 &crt->valid_to ) ) != 0 )
1451 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001452 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 return( ret );
1454 }
1455
1456 /*
1457 * subject Name
1458 */
1459 crt->subject_raw.p = p;
1460
1461 if( ( ret = asn1_get_tag( &p, end, &len,
1462 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1463 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001464 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001465 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 }
1467
Paul Bakkercefb3962012-06-27 11:51:09 +00001468 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001470 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 return( ret );
1472 }
1473
1474 crt->subject_raw.len = p - crt->subject_raw.p;
1475
1476 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001477 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +02001479 if( ( ret = x509_get_pubkey_rsa( &p, end, &crt->rsa ) ) != 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 Bakker5121ce52009-01-03 21:22:43 +00001485 /*
1486 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1487 * -- If present, version shall be v2 or v3
1488 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1489 * -- If present, version shall be v2 or v3
1490 * extensions [3] EXPLICIT Extensions OPTIONAL
1491 * -- If present, version shall be v3
1492 */
1493 if( crt->version == 2 || crt->version == 3 )
1494 {
1495 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1496 if( ret != 0 )
1497 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001498 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 return( ret );
1500 }
1501 }
1502
1503 if( crt->version == 2 || crt->version == 3 )
1504 {
1505 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1506 if( ret != 0 )
1507 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001508 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 return( ret );
1510 }
1511 }
1512
1513 if( crt->version == 3 )
1514 {
Paul Bakker74111d32011-01-15 16:57:55 +00001515 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 if( ret != 0 )
1517 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001518 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001519 return( ret );
1520 }
1521 }
1522
1523 if( p != end )
1524 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001525 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001526 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001527 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 }
1529
Paul Bakkerb00ca422012-09-25 12:10:00 +00001530 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
1532 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001533 * }
1534 * -- end of TBSCertificate
1535 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 * signatureAlgorithm AlgorithmIdentifier,
1537 * signatureValue BIT STRING
1538 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001539 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001541 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 return( ret );
1543 }
1544
Paul Bakker535e97d2012-08-23 10:49:55 +00001545 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1546 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001548 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001549 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 }
1551
1552 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1553 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001554 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 return( ret );
1556 }
1557
1558 if( p != end )
1559 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001560 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001561 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001562 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 }
1564
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001565 return( 0 );
1566}
1567
1568/*
Paul Bakker42c65812013-06-24 19:21:59 +02001569 * Parse one X.509 certificate in DER format from a buffer and add them to a
1570 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001571 */
Paul Bakker42c65812013-06-24 19:21:59 +02001572int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001573{
Paul Bakker42c65812013-06-24 19:21:59 +02001574 int ret;
1575 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001576
1577 /*
1578 * Check for valid input
1579 */
1580 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001581 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001582
1583 while( crt->version != 0 && crt->next != NULL )
1584 {
1585 prev = crt;
1586 crt = crt->next;
1587 }
1588
1589 /*
1590 * Add new certificate on the end of the chain if needed.
1591 */
1592 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001593 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001594 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001595
Paul Bakker7d06ad22009-05-02 15:53:56 +00001596 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001597 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001598
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001599 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001600 crt = crt->next;
1601 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
Paul Bakker42c65812013-06-24 19:21:59 +02001604 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1605 {
1606 if( prev )
1607 prev->next = NULL;
1608
1609 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001610 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001611
1612 return( ret );
1613 }
1614
1615 return( 0 );
1616}
1617
1618/*
1619 * Parse one or more PEM certificates from a buffer and add them to the chained list
1620 */
1621int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1622{
1623 int ret, success = 0, first_error = 0, total_failed = 0;
1624 int buf_format = X509_FORMAT_DER;
1625
1626 /*
1627 * Check for valid input
1628 */
1629 if( chain == NULL || buf == NULL )
1630 return( POLARSSL_ERR_X509_INVALID_INPUT );
1631
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001632 /*
1633 * Determine buffer content. Buffer contains either one DER certificate or
1634 * one or more PEM certificates.
1635 */
1636#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001637 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001638 buf_format = X509_FORMAT_PEM;
1639#endif
1640
1641 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001642 return x509parse_crt_der( chain, buf, buflen );
1643
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001644#if defined(POLARSSL_PEM_C)
1645 if( buf_format == X509_FORMAT_PEM )
1646 {
1647 pem_context pem;
1648
1649 while( buflen > 0 )
1650 {
1651 size_t use_len;
1652 pem_init( &pem );
1653
1654 ret = pem_read_buffer( &pem,
1655 "-----BEGIN CERTIFICATE-----",
1656 "-----END CERTIFICATE-----",
1657 buf, NULL, 0, &use_len );
1658
1659 if( ret == 0 )
1660 {
1661 /*
1662 * Was PEM encoded
1663 */
1664 buflen -= use_len;
1665 buf += use_len;
1666 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001667 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1668 {
1669 return( ret );
1670 }
Paul Bakker00b28602013-06-24 13:02:41 +02001671 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001672 {
1673 pem_free( &pem );
1674
Paul Bakker5ed3b342013-06-24 19:05:46 +02001675 /*
1676 * PEM header and footer were found
1677 */
1678 buflen -= use_len;
1679 buf += use_len;
1680
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001681 if( first_error == 0 )
1682 first_error = ret;
1683
1684 continue;
1685 }
1686 else
1687 break;
1688
Paul Bakker42c65812013-06-24 19:21:59 +02001689 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001690
1691 pem_free( &pem );
1692
1693 if( ret != 0 )
1694 {
1695 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001696 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001697 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001698 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001699 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001700
1701 if( first_error == 0 )
1702 first_error = ret;
1703
Paul Bakker42c65812013-06-24 19:21:59 +02001704 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001705 continue;
1706 }
1707
1708 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001709 }
1710 }
1711#endif
1712
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001713 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001714 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001715 else if( first_error )
1716 return( first_error );
1717 else
1718 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001719}
1720
1721/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001722 * Parse one or more CRLs and add them to the chained list
1723 */
Paul Bakker23986e52011-04-24 08:57:21 +00001724int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725{
Paul Bakker23986e52011-04-24 08:57:21 +00001726 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001727 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001728 unsigned char *p, *end;
1729 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001730#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001731 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001732 pem_context pem;
1733#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001734
1735 crl = chain;
1736
1737 /*
1738 * Check for valid input
1739 */
1740 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001741 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001742
1743 while( crl->version != 0 && crl->next != NULL )
1744 crl = crl->next;
1745
1746 /*
1747 * Add new CRL on the end of the chain if needed.
1748 */
1749 if ( crl->version != 0 && crl->next == NULL)
1750 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001751 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752
Paul Bakker7d06ad22009-05-02 15:53:56 +00001753 if( crl->next == NULL )
1754 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001756 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001757 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001758
Paul Bakker7d06ad22009-05-02 15:53:56 +00001759 crl = crl->next;
1760 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001761 }
1762
Paul Bakker96743fc2011-02-12 14:30:57 +00001763#if defined(POLARSSL_PEM_C)
1764 pem_init( &pem );
1765 ret = pem_read_buffer( &pem,
1766 "-----BEGIN X509 CRL-----",
1767 "-----END X509 CRL-----",
1768 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001769
Paul Bakker96743fc2011-02-12 14:30:57 +00001770 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001771 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001772 /*
1773 * Was PEM encoded
1774 */
1775 buflen -= use_len;
1776 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001777
1778 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001779 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001780 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001781 p = pem.buf;
1782 pem.buf = NULL;
1783 len = pem.buflen;
1784 pem_free( &pem );
1785 }
Paul Bakker00b28602013-06-24 13:02:41 +02001786 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001787 {
1788 pem_free( &pem );
1789 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790 }
1791 else
1792 {
1793 /*
1794 * nope, copy the raw DER data
1795 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001796 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001797
1798 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001799 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001800
1801 memcpy( p, buf, buflen );
1802
1803 buflen = 0;
1804 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001805#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001806 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001807
1808 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001809 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001810
1811 memcpy( p, buf, buflen );
1812
1813 buflen = 0;
1814#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815
1816 crl->raw.p = p;
1817 crl->raw.len = len;
1818 end = p + len;
1819
1820 /*
1821 * CertificateList ::= SEQUENCE {
1822 * tbsCertList TBSCertList,
1823 * signatureAlgorithm AlgorithmIdentifier,
1824 * signatureValue BIT STRING }
1825 */
1826 if( ( ret = asn1_get_tag( &p, end, &len,
1827 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1828 {
1829 x509_crl_free( crl );
1830 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1831 }
1832
Paul Bakker23986e52011-04-24 08:57:21 +00001833 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001834 {
1835 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001836 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001837 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1838 }
1839
1840 /*
1841 * TBSCertList ::= SEQUENCE {
1842 */
1843 crl->tbs.p = p;
1844
1845 if( ( ret = asn1_get_tag( &p, end, &len,
1846 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1847 {
1848 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001849 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850 }
1851
1852 end = p + len;
1853 crl->tbs.len = end - crl->tbs.p;
1854
1855 /*
1856 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1857 * -- if present, MUST be v2
1858 *
1859 * signature AlgorithmIdentifier
1860 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001861 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001862 ( ret = x509_get_alg( &p, end, &crl->sig_oid1, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001863 {
1864 x509_crl_free( crl );
1865 return( ret );
1866 }
1867
1868 crl->version++;
1869
1870 if( crl->version > 2 )
1871 {
1872 x509_crl_free( crl );
1873 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1874 }
1875
Paul Bakkerc70b9822013-04-07 22:00:46 +02001876 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1877 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001878 {
1879 x509_crl_free( crl );
1880 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1881 }
1882
1883 /*
1884 * issuer Name
1885 */
1886 crl->issuer_raw.p = p;
1887
1888 if( ( ret = asn1_get_tag( &p, end, &len,
1889 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1890 {
1891 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001892 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001893 }
1894
1895 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1896 {
1897 x509_crl_free( crl );
1898 return( ret );
1899 }
1900
1901 crl->issuer_raw.len = p - crl->issuer_raw.p;
1902
1903 /*
1904 * thisUpdate Time
1905 * nextUpdate Time OPTIONAL
1906 */
Paul Bakker91200182010-02-18 21:26:15 +00001907 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001908 {
1909 x509_crl_free( crl );
1910 return( ret );
1911 }
1912
Paul Bakker91200182010-02-18 21:26:15 +00001913 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001914 {
Paul Bakker9d781402011-05-09 16:17:09 +00001915 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001916 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001917 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001918 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001919 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001920 x509_crl_free( crl );
1921 return( ret );
1922 }
1923 }
1924
1925 /*
1926 * revokedCertificates SEQUENCE OF SEQUENCE {
1927 * userCertificate CertificateSerialNumber,
1928 * revocationDate Time,
1929 * crlEntryExtensions Extensions OPTIONAL
1930 * -- if present, MUST be v2
1931 * } OPTIONAL
1932 */
1933 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1934 {
1935 x509_crl_free( crl );
1936 return( ret );
1937 }
1938
1939 /*
1940 * crlExtensions EXPLICIT Extensions OPTIONAL
1941 * -- if present, MUST be v2
1942 */
1943 if( crl->version == 2 )
1944 {
1945 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1946
1947 if( ret != 0 )
1948 {
1949 x509_crl_free( crl );
1950 return( ret );
1951 }
1952 }
1953
1954 if( p != end )
1955 {
1956 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001957 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001958 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1959 }
1960
1961 end = crl->raw.p + crl->raw.len;
1962
1963 /*
1964 * signatureAlgorithm AlgorithmIdentifier,
1965 * signatureValue BIT STRING
1966 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001967 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001968 {
1969 x509_crl_free( crl );
1970 return( ret );
1971 }
1972
Paul Bakker535e97d2012-08-23 10:49:55 +00001973 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1974 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001975 {
1976 x509_crl_free( crl );
1977 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1978 }
1979
1980 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1981 {
1982 x509_crl_free( crl );
1983 return( ret );
1984 }
1985
1986 if( p != end )
1987 {
1988 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001989 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001990 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1991 }
1992
1993 if( buflen > 0 )
1994 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001995 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001996
Paul Bakker7d06ad22009-05-02 15:53:56 +00001997 if( crl->next == NULL )
1998 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001999 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00002000 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00002001 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002002
Paul Bakker7d06ad22009-05-02 15:53:56 +00002003 crl = crl->next;
2004 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002005
2006 return( x509parse_crl( crl, buf, buflen ) );
2007 }
2008
2009 return( 0 );
2010}
2011
Paul Bakker335db3f2011-04-25 15:28:35 +00002012#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002013/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00002014 * Load all data from a file into a given buffer.
2015 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002016static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00002017{
Paul Bakkerd98030e2009-05-02 15:13:40 +00002018 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00002019
Paul Bakkerd98030e2009-05-02 15:13:40 +00002020 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00002021 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002022
Paul Bakkerd98030e2009-05-02 15:13:40 +00002023 fseek( f, 0, SEEK_END );
2024 *n = (size_t) ftell( f );
2025 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002026
Paul Bakker6e339b52013-07-03 13:37:05 +02002027 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02002028 {
2029 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00002030 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02002031 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00002032
Paul Bakkerd98030e2009-05-02 15:13:40 +00002033 if( fread( *buf, 1, *n, f ) != *n )
2034 {
2035 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02002036 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00002037 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002038 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00002039
Paul Bakkerd98030e2009-05-02 15:13:40 +00002040 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002041
Paul Bakkerd98030e2009-05-02 15:13:40 +00002042 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00002043
Paul Bakkerd98030e2009-05-02 15:13:40 +00002044 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002045}
2046
2047/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 * Load one or more certificates and add them to the chained list
2049 */
Paul Bakker69e095c2011-12-10 21:55:01 +00002050int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051{
2052 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 size_t n;
2054 unsigned char *buf;
2055
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002056 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002057 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Paul Bakker69e095c2011-12-10 21:55:01 +00002059 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002060
2061 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002062 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
2064 return( ret );
2065}
2066
Paul Bakker8d914582012-06-04 12:46:42 +00002067int x509parse_crtpath( x509_cert *chain, const char *path )
2068{
2069 int ret = 0;
2070#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00002071 int w_ret;
2072 WCHAR szDir[MAX_PATH];
2073 char filename[MAX_PATH];
2074 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002075 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00002076
Paul Bakker97872ac2012-11-02 12:53:26 +00002077 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00002078 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002079
2080 if( len > MAX_PATH - 3 )
2081 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00002082
Paul Bakker3338b792012-10-01 21:13:10 +00002083 memset( szDir, 0, sizeof(szDir) );
2084 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002085 memcpy( filename, path, len );
2086 filename[len++] = '\\';
2087 p = filename + len;
2088 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002089
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002090 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002091
Paul Bakker97872ac2012-11-02 12:53:26 +00002092 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002093 if (hFind == INVALID_HANDLE_VALUE)
2094 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2095
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002096 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002097 do
2098 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002099 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002100
Paul Bakkere4791f32012-06-04 21:29:15 +00002101 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002102 continue;
2103
Paul Bakker3338b792012-10-01 21:13:10 +00002104 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2105 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002106 p, len - 1,
2107 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002108
Paul Bakker3338b792012-10-01 21:13:10 +00002109 w_ret = x509parse_crtfile( chain, filename );
2110 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002111 ret++;
2112 else
2113 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002114 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002115 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002116
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002117 if (GetLastError() != ERROR_NO_MORE_FILES)
2118 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002119
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002120cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002121 FindClose( hFind );
2122#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002123 int t_ret, i;
2124 struct stat sb;
2125 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002126 char entry_name[255];
2127 DIR *dir = opendir( path );
2128
2129 if( dir == NULL)
2130 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2131
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002132 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002133 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002134 if( result == NULL )
2135 break;
2136
2137 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2138
2139 i = stat( entry_name, &sb );
2140
2141 if( i == -1 )
2142 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2143
2144 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002145 continue;
2146
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002147 // Ignore parse errors
2148 //
Paul Bakker8d914582012-06-04 12:46:42 +00002149 t_ret = x509parse_crtfile( chain, entry_name );
2150 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002151 ret++;
2152 else
2153 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002154 }
2155 closedir( dir );
2156#endif
2157
2158 return( ret );
2159}
2160
Paul Bakkerd98030e2009-05-02 15:13:40 +00002161/*
2162 * Load one or more CRLs and add them to the chained list
2163 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002164int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002165{
2166 int ret;
2167 size_t n;
2168 unsigned char *buf;
2169
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002170 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002171 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002172
Paul Bakker27fdf462011-06-09 13:55:13 +00002173 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002174
2175 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002176 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002177
2178 return( ret );
2179}
2180
Paul Bakker5121ce52009-01-03 21:22:43 +00002181/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002182 * Load and parse a private RSA key
2183 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002184int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002185{
2186 int ret;
2187 size_t n;
2188 unsigned char *buf;
2189
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002190 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002191 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002192
2193 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002194 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002195 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002196 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002197 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002198
2199 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002200 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002201
2202 return( ret );
2203}
2204
2205/*
2206 * Load and parse a public RSA key
2207 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002208int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002209{
2210 int ret;
2211 size_t n;
2212 unsigned char *buf;
2213
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002214 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002215 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002216
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002217 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002218
2219 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002220 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002221
2222 return( ret );
2223}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002224
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002225/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002226 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002227 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002228int x509parse_keyfile( pk_context *ctx,
2229 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002230{
2231 int ret;
2232 size_t n;
2233 unsigned char *buf;
2234
2235 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2236 return( ret );
2237
2238 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002239 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002240 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002241 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002242 (const unsigned char *) pwd, strlen( pwd ) );
2243
2244 memset( buf, 0, n + 1 );
2245 free( buf );
2246
2247 return( ret );
2248}
2249
2250/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002251 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002252 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002253int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002254{
2255 int ret;
2256 size_t n;
2257 unsigned char *buf;
2258
2259 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2260 return( ret );
2261
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002262 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002263
2264 memset( buf, 0, n + 1 );
2265 free( buf );
2266
2267 return( ret );
2268}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002269
Paul Bakker335db3f2011-04-25 15:28:35 +00002270#endif /* POLARSSL_FS_IO */
2271
2272/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002273 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002275static int x509parse_key_pkcs1_der( rsa_context *rsa,
2276 const unsigned char *key,
2277 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002278{
Paul Bakker23986e52011-04-24 08:57:21 +00002279 int ret;
2280 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002282
Paul Bakker96743fc2011-02-12 14:30:57 +00002283 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002284 end = p + keylen;
2285
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002287 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002288 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 * RSAPrivateKey ::= SEQUENCE {
2290 * version Version,
2291 * modulus INTEGER, -- n
2292 * publicExponent INTEGER, -- e
2293 * privateExponent INTEGER, -- d
2294 * prime1 INTEGER, -- p
2295 * prime2 INTEGER, -- q
2296 * exponent1 INTEGER, -- d mod (p-1)
2297 * exponent2 INTEGER, -- d mod (q-1)
2298 * coefficient INTEGER, -- (inverse of q) mod p
2299 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2300 * }
2301 */
2302 if( ( ret = asn1_get_tag( &p, end, &len,
2303 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2304 {
Paul Bakker9d781402011-05-09 16:17:09 +00002305 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 }
2307
2308 end = p + len;
2309
2310 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2311 {
Paul Bakker9d781402011-05-09 16:17:09 +00002312 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 }
2314
2315 if( rsa->ver != 0 )
2316 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002317 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 }
2319
2320 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2321 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2322 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2323 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2324 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2325 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2326 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2327 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2328 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002330 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 }
2332
2333 rsa->len = mpi_size( &rsa->N );
2334
2335 if( p != end )
2336 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002337 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002338 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002339 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 }
2341
2342 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2343 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 rsa_free( rsa );
2345 return( ret );
2346 }
2347
Paul Bakkere2f50402013-06-24 19:00:59 +02002348 return( 0 );
2349}
2350
2351/*
2352 * Parse an unencrypted PKCS#8 encoded private RSA key
2353 */
2354static int x509parse_key_pkcs8_unencrypted_der(
2355 rsa_context *rsa,
2356 const unsigned char *key,
2357 size_t keylen )
2358{
2359 int ret;
2360 size_t len;
2361 unsigned char *p, *end;
2362 x509_buf pk_alg_oid;
2363 pk_type_t pk_alg = POLARSSL_PK_NONE;
2364
2365 p = (unsigned char *) key;
2366 end = p + keylen;
2367
2368 /*
2369 * This function parses the PrivatKeyInfo object (PKCS#8)
2370 *
2371 * PrivateKeyInfo ::= SEQUENCE {
2372 * version Version,
2373 * algorithm AlgorithmIdentifier,
2374 * PrivateKey BIT STRING
2375 * }
2376 *
2377 * AlgorithmIdentifier ::= SEQUENCE {
2378 * algorithm OBJECT IDENTIFIER,
2379 * parameters ANY DEFINED BY algorithm OPTIONAL
2380 * }
2381 *
2382 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2383 */
2384 if( ( ret = asn1_get_tag( &p, end, &len,
2385 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2386 {
2387 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2388 }
2389
2390 end = p + len;
2391
2392 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2393 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2394
2395 if( rsa->ver != 0 )
2396 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2397
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002398 if( ( ret = asn1_get_alg_null( &p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002399 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2400
2401 /*
2402 * only RSA keys handled at this time
2403 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002404 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +02002405 {
Paul Bakkere2f50402013-06-24 19:00:59 +02002406 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +02002407 }
Paul Bakkere2f50402013-06-24 19:00:59 +02002408
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002409 if (pk_alg != POLARSSL_PK_RSA )
2410 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
2411
Paul Bakkere2f50402013-06-24 19:00:59 +02002412 /*
2413 * Get the OCTET STRING and parse the PKCS#1 format inside
2414 */
2415 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2416 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2417
2418 if( ( end - p ) < 1 )
2419 {
2420 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2421 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2422 }
2423
2424 end = p + len;
2425
2426 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2427 return( ret );
2428
2429 return( 0 );
2430}
2431
2432/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002433 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002434 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002435static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2436 size_t *used_len,
2437 const unsigned char *key, size_t keylen,
2438 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002439{
2440 int ret;
2441 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002442 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002443 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002444#if defined(POLARSSL_PKCS12_C)
2445 cipher_type_t cipher_alg;
2446 md_type_t md_alg;
2447#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002448
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002449 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002450
2451 p = (unsigned char *) key;
2452 end = p + keylen;
2453
Paul Bakker28144de2013-06-24 19:28:55 +02002454 if( pwdlen == 0 )
2455 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2456
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002457 /*
2458 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2459 *
2460 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2461 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2462 * encryptedData EncryptedData
2463 * }
2464 *
2465 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2466 *
2467 * EncryptedData ::= OCTET STRING
2468 *
2469 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2470 */
2471 if( ( ret = asn1_get_tag( &p, end, &len,
2472 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2473 {
2474 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2475 }
2476
2477 end = p + len;
2478
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002479 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002480 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002481
2482 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2483 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2484
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002485 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002486 return( POLARSSL_ERR_X509_INVALID_INPUT );
2487
2488 /*
2489 * Decrypt EncryptedData with appropriate PDE
2490 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002491#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002492 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002493 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002494 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002495 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002496 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002497 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002498 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2499 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2500
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002501 return( ret );
2502 }
2503 }
2504 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2505 {
2506 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2507 PKCS12_PBE_DECRYPT,
2508 pwd, pwdlen,
2509 p, len, buf ) ) != 0 )
2510 {
2511 return( ret );
2512 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002513
2514 // Best guess for password mismatch when using RC4. If first tag is
2515 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2516 //
2517 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2518 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002519 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002520 else
2521#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002522#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002523 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002524 {
2525 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2526 p, len, buf ) ) != 0 )
2527 {
2528 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2529 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2530
2531 return( ret );
2532 }
2533 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002534 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002535#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002536 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2537
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002538 *used_len = len;
2539 return( 0 );
2540}
2541
2542/*
2543 * Parse an encrypted PKCS#8 encoded private RSA key
2544 */
2545static int x509parse_key_pkcs8_encrypted_der(
2546 rsa_context *rsa,
2547 const unsigned char *key, size_t keylen,
2548 const unsigned char *pwd, size_t pwdlen )
2549{
2550 int ret;
2551 unsigned char buf[2048];
2552 size_t len = 0;
2553
2554 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2555 key, keylen, pwd, pwdlen ) ) != 0 )
2556 {
2557 return( ret );
2558 }
2559
2560 return( x509parse_key_pkcs8_unencrypted_der( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002561}
2562
2563/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002564 * Parse a private RSA key
2565 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002566int x509parse_key_rsa( rsa_context *rsa,
2567 const unsigned char *key, size_t keylen,
2568 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002569{
2570 int ret;
2571
Paul Bakker96743fc2011-02-12 14:30:57 +00002572#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002573 size_t len;
2574 pem_context pem;
2575
2576 pem_init( &pem );
2577 ret = pem_read_buffer( &pem,
2578 "-----BEGIN RSA PRIVATE KEY-----",
2579 "-----END RSA PRIVATE KEY-----",
2580 key, pwd, pwdlen, &len );
2581 if( ret == 0 )
2582 {
2583 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2584 {
2585 rsa_free( rsa );
2586 }
2587
2588 pem_free( &pem );
2589 return( ret );
2590 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002591 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2592 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2593 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2594 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002595 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002596 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002597
2598 ret = pem_read_buffer( &pem,
2599 "-----BEGIN PRIVATE KEY-----",
2600 "-----END PRIVATE KEY-----",
2601 key, NULL, 0, &len );
2602 if( ret == 0 )
2603 {
2604 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2605 pem.buf, pem.buflen ) ) != 0 )
2606 {
2607 rsa_free( rsa );
2608 }
2609
2610 pem_free( &pem );
2611 return( ret );
2612 }
2613 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002614 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002615
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002616 ret = pem_read_buffer( &pem,
2617 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2618 "-----END ENCRYPTED PRIVATE KEY-----",
2619 key, NULL, 0, &len );
2620 if( ret == 0 )
2621 {
2622 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2623 pem.buf, pem.buflen,
2624 pwd, pwdlen ) ) != 0 )
2625 {
2626 rsa_free( rsa );
2627 }
2628
2629 pem_free( &pem );
2630 return( ret );
2631 }
2632 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002633 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002634#else
2635 ((void) pwd);
2636 ((void) pwdlen);
2637#endif /* POLARSSL_PEM_C */
2638
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002639 /*
2640 * At this point we only know it's not a PEM formatted key. Could be any
2641 * of the known DER encoded private key formats
2642 *
2643 * We try the different DER format parsers to see if one passes without
2644 * error
2645 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002646 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2647 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002648 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002649 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002650 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002652 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002653
2654 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2655 {
2656 return( ret );
2657 }
2658
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002659 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2660 return( 0 );
2661
2662 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002663
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002664 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2665 return( 0 );
2666
2667 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002668
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002669 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002670}
2671
2672/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002673 * Parse a public RSA key
2674 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002675int x509parse_public_key_rsa( rsa_context *rsa,
2676 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002677{
Paul Bakker23986e52011-04-24 08:57:21 +00002678 int ret;
2679 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002680 unsigned char *p, *end;
Paul Bakker53019ae2011-03-25 13:58:48 +00002681#if defined(POLARSSL_PEM_C)
2682 pem_context pem;
2683
2684 pem_init( &pem );
2685 ret = pem_read_buffer( &pem,
2686 "-----BEGIN PUBLIC KEY-----",
2687 "-----END PUBLIC KEY-----",
2688 key, NULL, 0, &len );
2689
2690 if( ret == 0 )
2691 {
2692 /*
2693 * Was PEM encoded
2694 */
2695 keylen = pem.buflen;
2696 }
Paul Bakker00b28602013-06-24 13:02:41 +02002697 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002698 {
2699 pem_free( &pem );
2700 return( ret );
2701 }
2702
2703 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2704#else
2705 p = (unsigned char *) key;
2706#endif
2707 end = p + keylen;
2708
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +02002709 if( ( ret = x509_get_pubkey_rsa( &p, end, rsa ) ) != 0 )
Paul Bakker53019ae2011-03-25 13:58:48 +00002710 {
2711#if defined(POLARSSL_PEM_C)
2712 pem_free( &pem );
2713#endif
2714 rsa_free( rsa );
2715 return( ret );
2716 }
2717
Paul Bakker53019ae2011-03-25 13:58:48 +00002718#if defined(POLARSSL_PEM_C)
2719 pem_free( &pem );
2720#endif
2721
2722 return( 0 );
2723}
2724
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002725#if defined(POLARSSL_ECP_C)
2726/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002727 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002728 */
2729static int x509parse_key_sec1_der( ecp_keypair *eck,
2730 const unsigned char *key,
2731 size_t keylen )
2732{
2733 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002734 int version;
2735 size_t len;
2736 ecp_group_id grp_id;
2737 unsigned char *p = (unsigned char *) key;
2738 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002739
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002740 /*
2741 * RFC 5915, orf SEC1 Appendix C.4
2742 *
2743 * ECPrivateKey ::= SEQUENCE {
2744 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2745 * privateKey OCTET STRING,
2746 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2747 * publicKey [1] BIT STRING OPTIONAL
2748 * }
2749 */
2750 if( ( ret = asn1_get_tag( &p, end, &len,
2751 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2752 {
2753 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2754 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002755
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002756 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002757
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002758 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2759 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002760
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002761 if( version != 1 )
2762 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2763
2764 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2765 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2766
2767 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2768 {
2769 ecp_keypair_free( eck );
2770 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2771 }
2772
2773 p += len;
2774
2775 /*
2776 * Is 'parameters' present?
2777 */
2778 if( ( ret = asn1_get_tag( &p, end, &len,
2779 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2780 {
2781 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2782 return( ret );
2783
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002784 /*
2785 * If we're wrapped in a bigger structure (eg PKCS#8), grp may have been
2786 * defined externally. In this case, make sure both definitions match.
2787 */
2788 if( eck->grp.id != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002789 {
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002790 if( eck->grp.id != grp_id )
2791 {
2792 ecp_keypair_free( eck );
2793 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2794 }
2795 }
2796 else
2797 {
2798 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2799 {
2800 ecp_keypair_free( eck );
2801 return( ret );
2802 }
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002803 }
2804 }
2805 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2806 {
2807 ecp_keypair_free( eck );
2808 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2809 }
2810
2811 /*
2812 * Is 'publickey' present?
2813 */
2814 if( ( ret = asn1_get_tag( &p, end, &len,
2815 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2816 {
2817 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2818 != 0 )
2819 {
2820 ecp_keypair_free( eck );
2821 return( ret );
2822 }
2823
2824 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2825 {
2826 ecp_keypair_free( eck );
2827 return( ret );
2828 }
2829 }
2830 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2831 {
2832 ecp_keypair_free( eck );
2833 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2834 }
2835
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002836 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002837 {
2838 ecp_keypair_free( eck );
2839 return( ret );
2840 }
2841
2842 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002843}
2844
2845/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002846 * Parse an unencrypted PKCS#8 encoded private EC key
2847 */
2848static int x509parse_key_pkcs8_unencrypted_der_ec(
2849 ecp_keypair *eck,
2850 const unsigned char* key,
2851 size_t keylen )
2852{
2853 int ret, version;
2854 size_t len;
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002855 x509_buf alg_params;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002856 ecp_group_id grp_id;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002857 unsigned char *p = (unsigned char *) key;
2858 unsigned char *end = p + keylen;
2859 pk_type_t pk_alg = POLARSSL_PK_NONE;
2860
2861 /*
2862 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2863 *
2864 * PrivateKeyInfo ::= SEQUENCE {
2865 * version Version,
2866 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2867 * privateKey PrivateKey,
2868 * attributes [0] IMPLICIT Attributes OPTIONAL }
2869 *
2870 * Version ::= INTEGER
2871 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2872 * PrivateKey ::= OCTET STRING
2873 *
2874 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2875 */
2876
2877 if( ( ret = asn1_get_tag( &p, end, &len,
2878 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2879 {
2880 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2881 }
2882
2883 end = p + len;
2884
2885 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2886 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2887
2888 if( version != 0 )
2889 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2890
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002891 if( ( ret = x509_get_algid( &p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002892 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2893
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002894 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002895 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002896
2897 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2898 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2899
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002900 if( ( ret = x509_ecparams_get_grp_id( &alg_params, &grp_id ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002901 {
2902 ecp_keypair_free( eck );
2903 return( ret );
2904 }
2905
2906 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2907 {
2908 ecp_keypair_free( eck );
2909 return( ret );
2910 }
2911
2912 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2913 {
2914 ecp_keypair_free( eck );
2915 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2916 }
2917
2918 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2919 {
2920 ecp_keypair_free( eck );
2921 return( ret );
2922 }
2923
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002924 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002925 {
2926 ecp_keypair_free( eck );
2927 return( ret );
2928 }
2929
2930 return 0;
2931}
2932
2933/*
2934 * Parse an encrypted PKCS#8 encoded private EC key
2935 */
2936static int x509parse_key_pkcs8_encrypted_der_ec(
2937 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002938 const unsigned char *key, size_t keylen,
2939 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002940{
2941 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002942 unsigned char buf[2048];
2943 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002944
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002945 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2946 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002947 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002948 return( ret );
2949 }
2950
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002951 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002952}
2953
2954/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002955 * Parse a private EC key
2956 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002957static int x509parse_key_ec( ecp_keypair *eck,
2958 const unsigned char *key, size_t keylen,
2959 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002960{
2961 int ret;
2962
2963#if defined(POLARSSL_PEM_C)
2964 size_t len;
2965 pem_context pem;
2966
2967 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002968 ret = pem_read_buffer( &pem,
2969 "-----BEGIN EC PRIVATE KEY-----",
2970 "-----END EC PRIVATE KEY-----",
2971 key, pwd, pwdlen, &len );
2972 if( ret == 0 )
2973 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002974 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002975 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002976 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002977 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002978
2979 pem_free( &pem );
2980 return( ret );
2981 }
2982 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2983 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2984 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2985 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2986 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2987 return( ret );
2988
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002989 ret = pem_read_buffer( &pem,
2990 "-----BEGIN PRIVATE KEY-----",
2991 "-----END PRIVATE KEY-----",
2992 key, NULL, 0, &len );
2993 if( ret == 0 )
2994 {
2995 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2996 pem.buf, pem.buflen ) ) != 0 )
2997 {
2998 ecp_keypair_free( eck );
2999 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003000
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02003001 pem_free( &pem );
3002 return( ret );
3003 }
3004 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3005 return( ret );
3006
3007 ret = pem_read_buffer( &pem,
3008 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
3009 "-----END ENCRYPTED PRIVATE KEY-----",
3010 key, NULL, 0, &len );
3011 if( ret == 0 )
3012 {
3013 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
3014 pem.buf, pem.buflen,
3015 pwd, pwdlen ) ) != 0 )
3016 {
3017 ecp_keypair_free( eck );
3018 }
3019
3020 pem_free( &pem );
3021 return( ret );
3022 }
3023 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3024 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003025#else
3026 ((void) pwd);
3027 ((void) pwdlen);
3028#endif /* POLARSSL_PEM_C */
3029
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02003030 /*
3031 * At this point we only know it's not a PEM formatted key. Could be any
3032 * of the known DER encoded private key formats
3033 *
3034 * We try the different DER format parsers to see if one passes without
3035 * error
3036 */
3037 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
3038 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003039 {
3040 return( 0 );
3041 }
3042
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02003043 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003044
3045 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
3046 {
3047 return( ret );
3048 }
3049
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02003050 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
3051 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003052 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003053
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02003054 ecp_keypair_free( eck );
3055
3056 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
3057 return( 0 );
3058
3059 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003060
3061 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
3062}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003063#endif /* defined(POLARSSL_ECP_C) */
3064
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003065/*
3066 * Parse a private key
3067 */
3068int x509parse_key( pk_context *ctx,
3069 const unsigned char *key, size_t keylen,
3070 const unsigned char *pwd, size_t pwdlen )
3071{
3072 int ret;
3073
3074 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
3075 return( ret );
3076
3077 if( ( ret = x509parse_key_rsa( ctx->data, key, keylen, pwd, pwdlen ) )
3078 == 0 )
3079 {
3080 return( 0 );
3081 }
3082
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003083 pk_free( ctx );
3084
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003085 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
3086 return( ret );
3087
3088 if( ( ret = x509parse_key_ec( ctx->data, key, keylen, pwd, pwdlen ) ) == 0 )
3089 {
3090 return( 0 );
3091 }
3092
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003093 pk_free( ctx );
3094
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003095 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
3096}
3097
3098/*
3099 * Parse a public key
3100 */
3101int x509parse_public_key( pk_context *ctx,
3102 const unsigned char *key, size_t keylen )
3103{
3104 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003105 unsigned char *p;
3106#if defined(POLARSSL_PEM_C)
3107 size_t len;
3108 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003109
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003110 pem_init( &pem );
3111 ret = pem_read_buffer( &pem,
3112 "-----BEGIN PUBLIC KEY-----",
3113 "-----END PUBLIC KEY-----",
3114 key, NULL, 0, &len );
3115
3116 if( ret == 0 )
3117 {
3118 /*
3119 * Was PEM encoded
3120 */
3121 key = pem.buf;
3122 keylen = pem.buflen;
3123 }
3124 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3125 {
3126 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003127 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003128 }
3129#endif
3130 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003131
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003132 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003133
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003134#if defined(POLARSSL_PEM_C)
3135 pem_free( &pem );
3136#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003137
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003138 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003139}
3140
Paul Bakkereaa89f82011-04-04 21:36:15 +00003141#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003142/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003143 * Parse DHM parameters
3144 */
Paul Bakker23986e52011-04-24 08:57:21 +00003145int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003146{
Paul Bakker23986e52011-04-24 08:57:21 +00003147 int ret;
3148 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003149 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003150#if defined(POLARSSL_PEM_C)
3151 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003152
Paul Bakker96743fc2011-02-12 14:30:57 +00003153 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003154
Paul Bakker96743fc2011-02-12 14:30:57 +00003155 ret = pem_read_buffer( &pem,
3156 "-----BEGIN DH PARAMETERS-----",
3157 "-----END DH PARAMETERS-----",
3158 dhmin, NULL, 0, &dhminlen );
3159
3160 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003161 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003162 /*
3163 * Was PEM encoded
3164 */
3165 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003166 }
Paul Bakker00b28602013-06-24 13:02:41 +02003167 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003168 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003169 pem_free( &pem );
3170 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003171 }
3172
Paul Bakker96743fc2011-02-12 14:30:57 +00003173 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3174#else
3175 p = (unsigned char *) dhmin;
3176#endif
3177 end = p + dhminlen;
3178
Paul Bakker1b57b062011-01-06 15:48:19 +00003179 memset( dhm, 0, sizeof( dhm_context ) );
3180
Paul Bakker1b57b062011-01-06 15:48:19 +00003181 /*
3182 * DHParams ::= SEQUENCE {
3183 * prime INTEGER, -- P
3184 * generator INTEGER, -- g
3185 * }
3186 */
3187 if( ( ret = asn1_get_tag( &p, end, &len,
3188 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3189 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003190#if defined(POLARSSL_PEM_C)
3191 pem_free( &pem );
3192#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003193 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003194 }
3195
3196 end = p + len;
3197
3198 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3199 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3200 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003201#if defined(POLARSSL_PEM_C)
3202 pem_free( &pem );
3203#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003204 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003205 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003206 }
3207
3208 if( p != end )
3209 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003210#if defined(POLARSSL_PEM_C)
3211 pem_free( &pem );
3212#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003213 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003214 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003215 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3216 }
3217
Paul Bakker96743fc2011-02-12 14:30:57 +00003218#if defined(POLARSSL_PEM_C)
3219 pem_free( &pem );
3220#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003221
3222 return( 0 );
3223}
3224
Paul Bakker335db3f2011-04-25 15:28:35 +00003225#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003226/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003227 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003228 */
3229int x509parse_dhmfile( dhm_context *dhm, const char *path )
3230{
3231 int ret;
3232 size_t n;
3233 unsigned char *buf;
3234
Paul Bakker69e095c2011-12-10 21:55:01 +00003235 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3236 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003237
Paul Bakker27fdf462011-06-09 13:55:13 +00003238 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003239
3240 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003241 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003242
3243 return( ret );
3244}
Paul Bakker335db3f2011-04-25 15:28:35 +00003245#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003246#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003247
Paul Bakker5121ce52009-01-03 21:22:43 +00003248#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003249#include <stdarg.h>
3250
3251#if !defined vsnprintf
3252#define vsnprintf _vsnprintf
3253#endif // vsnprintf
3254
3255/*
3256 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3257 * Result value is not size of buffer needed, but -1 if no fit is possible.
3258 *
3259 * This fuction tries to 'fix' this by at least suggesting enlarging the
3260 * size by 20.
3261 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003262static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003263{
3264 va_list ap;
3265 int res = -1;
3266
3267 va_start( ap, format );
3268
3269 res = vsnprintf( str, size, format, ap );
3270
3271 va_end( ap );
3272
3273 // No quick fix possible
3274 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003275 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003276
3277 return res;
3278}
3279
3280#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003281#endif
3282
Paul Bakkerd98030e2009-05-02 15:13:40 +00003283#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3284
3285#define SAFE_SNPRINTF() \
3286{ \
3287 if( ret == -1 ) \
3288 return( -1 ); \
3289 \
Paul Bakker23986e52011-04-24 08:57:21 +00003290 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003291 p[n - 1] = '\0'; \
3292 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3293 } \
3294 \
Paul Bakker23986e52011-04-24 08:57:21 +00003295 n -= (unsigned int) ret; \
3296 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003297}
3298
Paul Bakker5121ce52009-01-03 21:22:43 +00003299/*
3300 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003301 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003303int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003304{
Paul Bakker23986e52011-04-24 08:57:21 +00003305 int ret;
3306 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003308 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003309 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 char s[128], *p;
3311
3312 memset( s, 0, sizeof( s ) );
3313
3314 name = dn;
3315 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003316 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003317
3318 while( name != NULL )
3319 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003320 if( !name->oid.p )
3321 {
3322 name = name->next;
3323 continue;
3324 }
3325
Paul Bakker74111d32011-01-15 16:57:55 +00003326 if( name != dn )
3327 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003328 ret = snprintf( p, n, ", " );
3329 SAFE_SNPRINTF();
3330 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003331
Paul Bakkerc70b9822013-04-07 22:00:46 +02003332 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333
Paul Bakkerc70b9822013-04-07 22:00:46 +02003334 if( ret == 0 )
3335 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003337 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003338 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003339
3340 for( i = 0; i < name->val.len; i++ )
3341 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003342 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 break;
3344
3345 c = name->val.p[i];
3346 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3347 s[i] = '?';
3348 else s[i] = c;
3349 }
3350 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003351 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003352 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 name = name->next;
3354 }
3355
Paul Bakker23986e52011-04-24 08:57:21 +00003356 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003357}
3358
3359/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003360 * Store the serial in printable form into buf; no more
3361 * than size characters will be written
3362 */
3363int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3364{
Paul Bakker23986e52011-04-24 08:57:21 +00003365 int ret;
3366 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003367 char *p;
3368
3369 p = buf;
3370 n = size;
3371
3372 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003373 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003374
3375 for( i = 0; i < nr; i++ )
3376 {
Paul Bakker93048802011-12-05 14:38:06 +00003377 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003378 continue;
3379
Paul Bakkerdd476992011-01-16 21:34:59 +00003380 ret = snprintf( p, n, "%02X%s",
3381 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3382 SAFE_SNPRINTF();
3383 }
3384
Paul Bakker03c7c252011-11-25 12:37:37 +00003385 if( nr != serial->len )
3386 {
3387 ret = snprintf( p, n, "...." );
3388 SAFE_SNPRINTF();
3389 }
3390
Paul Bakker23986e52011-04-24 08:57:21 +00003391 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003392}
3393
3394/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003395 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003397int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3398 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003399{
Paul Bakker23986e52011-04-24 08:57:21 +00003400 int ret;
3401 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003402 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003403 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003404
3405 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003406 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003407
Paul Bakkerd98030e2009-05-02 15:13:40 +00003408 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003410 SAFE_SNPRINTF();
3411 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003413 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003414
Paul Bakkerdd476992011-01-16 21:34:59 +00003415 ret = x509parse_serial_gets( p, n, &crt->serial);
3416 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003417
Paul Bakkerd98030e2009-05-02 15:13:40 +00003418 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3419 SAFE_SNPRINTF();
3420 ret = x509parse_dn_gets( p, n, &crt->issuer );
3421 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003422
Paul Bakkerd98030e2009-05-02 15:13:40 +00003423 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3424 SAFE_SNPRINTF();
3425 ret = x509parse_dn_gets( p, n, &crt->subject );
3426 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003427
Paul Bakkerd98030e2009-05-02 15:13:40 +00003428 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3430 crt->valid_from.year, crt->valid_from.mon,
3431 crt->valid_from.day, crt->valid_from.hour,
3432 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003433 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003434
Paul Bakkerd98030e2009-05-02 15:13:40 +00003435 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3437 crt->valid_to.year, crt->valid_to.mon,
3438 crt->valid_to.day, crt->valid_to.hour,
3439 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003440 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003441
Paul Bakkerc70b9822013-04-07 22:00:46 +02003442 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003443 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003444
Paul Bakkerc70b9822013-04-07 22:00:46 +02003445 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3446 if( ret != 0 )
3447 ret = snprintf( p, n, "???" );
3448 else
3449 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003450 SAFE_SNPRINTF();
3451
3452 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003453 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003454 SAFE_SNPRINTF();
3455
Paul Bakker23986e52011-04-24 08:57:21 +00003456 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003457}
3458
Paul Bakker74111d32011-01-15 16:57:55 +00003459/*
3460 * Return an informational string describing the given OID
3461 */
3462const char *x509_oid_get_description( x509_buf *oid )
3463{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003464 const char *desc = NULL;
3465 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003466
Paul Bakkerc70b9822013-04-07 22:00:46 +02003467 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003468
Paul Bakkerc70b9822013-04-07 22:00:46 +02003469 if( ret != 0 )
3470 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003471
Paul Bakkerc70b9822013-04-07 22:00:46 +02003472 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003473}
3474
3475/* Return the x.y.z.... style numeric string for the given OID */
3476int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3477{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003478 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003479}
3480
Paul Bakkerd98030e2009-05-02 15:13:40 +00003481/*
3482 * Return an informational string about the CRL.
3483 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003484int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3485 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003486{
Paul Bakker23986e52011-04-24 08:57:21 +00003487 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003488 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003489 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003490 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003491 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003492
3493 p = buf;
3494 n = size;
3495
3496 ret = snprintf( p, n, "%sCRL version : %d",
3497 prefix, crl->version );
3498 SAFE_SNPRINTF();
3499
3500 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3501 SAFE_SNPRINTF();
3502 ret = x509parse_dn_gets( p, n, &crl->issuer );
3503 SAFE_SNPRINTF();
3504
3505 ret = snprintf( p, n, "\n%sthis update : " \
3506 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3507 crl->this_update.year, crl->this_update.mon,
3508 crl->this_update.day, crl->this_update.hour,
3509 crl->this_update.min, crl->this_update.sec );
3510 SAFE_SNPRINTF();
3511
3512 ret = snprintf( p, n, "\n%snext update : " \
3513 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3514 crl->next_update.year, crl->next_update.mon,
3515 crl->next_update.day, crl->next_update.hour,
3516 crl->next_update.min, crl->next_update.sec );
3517 SAFE_SNPRINTF();
3518
3519 entry = &crl->entry;
3520
3521 ret = snprintf( p, n, "\n%sRevoked certificates:",
3522 prefix );
3523 SAFE_SNPRINTF();
3524
Paul Bakker9be19372009-07-27 20:21:53 +00003525 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003526 {
3527 ret = snprintf( p, n, "\n%sserial number: ",
3528 prefix );
3529 SAFE_SNPRINTF();
3530
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003531 ret = x509parse_serial_gets( p, n, &entry->serial);
3532 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003533
Paul Bakkerd98030e2009-05-02 15:13:40 +00003534 ret = snprintf( p, n, " revocation date: " \
3535 "%04d-%02d-%02d %02d:%02d:%02d",
3536 entry->revocation_date.year, entry->revocation_date.mon,
3537 entry->revocation_date.day, entry->revocation_date.hour,
3538 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003539 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003540
3541 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 }
3543
Paul Bakkerc70b9822013-04-07 22:00:46 +02003544 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003545 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003546
Paul Bakkerc70b9822013-04-07 22:00:46 +02003547 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3548 if( ret != 0 )
3549 ret = snprintf( p, n, "???" );
3550 else
3551 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003552 SAFE_SNPRINTF();
3553
Paul Bakker1e27bb22009-07-19 20:25:25 +00003554 ret = snprintf( p, n, "\n" );
3555 SAFE_SNPRINTF();
3556
Paul Bakker23986e52011-04-24 08:57:21 +00003557 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003558}
3559
3560/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003561 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003563#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003564int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003565{
Paul Bakkercce9d772011-11-18 14:26:47 +00003566 int year, mon, day;
3567 int hour, min, sec;
3568
3569#if defined(_WIN32)
3570 SYSTEMTIME st;
3571
3572 GetLocalTime(&st);
3573
3574 year = st.wYear;
3575 mon = st.wMonth;
3576 day = st.wDay;
3577 hour = st.wHour;
3578 min = st.wMinute;
3579 sec = st.wSecond;
3580#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003581 struct tm *lt;
3582 time_t tt;
3583
3584 tt = time( NULL );
3585 lt = localtime( &tt );
3586
Paul Bakkercce9d772011-11-18 14:26:47 +00003587 year = lt->tm_year + 1900;
3588 mon = lt->tm_mon + 1;
3589 day = lt->tm_mday;
3590 hour = lt->tm_hour;
3591 min = lt->tm_min;
3592 sec = lt->tm_sec;
3593#endif
3594
3595 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003596 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003597
Paul Bakkercce9d772011-11-18 14:26:47 +00003598 if( year == to->year &&
3599 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003600 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003601
Paul Bakkercce9d772011-11-18 14:26:47 +00003602 if( year == to->year &&
3603 mon == to->mon &&
3604 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003605 return( 1 );
3606
Paul Bakkercce9d772011-11-18 14:26:47 +00003607 if( year == to->year &&
3608 mon == to->mon &&
3609 day == to->day &&
3610 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003611 return( 1 );
3612
Paul Bakkercce9d772011-11-18 14:26:47 +00003613 if( year == to->year &&
3614 mon == to->mon &&
3615 day == to->day &&
3616 hour == to->hour &&
3617 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003618 return( 1 );
3619
Paul Bakkercce9d772011-11-18 14:26:47 +00003620 if( year == to->year &&
3621 mon == to->mon &&
3622 day == to->day &&
3623 hour == to->hour &&
3624 min == to->min &&
3625 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003626 return( 1 );
3627
Paul Bakker40ea7de2009-05-03 10:18:48 +00003628 return( 0 );
3629}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003630#else /* POLARSSL_HAVE_TIME */
3631int x509parse_time_expired( const x509_time *to )
3632{
3633 ((void) to);
3634 return( 0 );
3635}
3636#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003637
3638/*
3639 * Return 1 if the certificate is revoked, or 0 otherwise.
3640 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003641int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003642{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003643 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003644
3645 while( cur != NULL && cur->serial.len != 0 )
3646 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003647 if( crt->serial.len == cur->serial.len &&
3648 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003649 {
3650 if( x509parse_time_expired( &cur->revocation_date ) )
3651 return( 1 );
3652 }
3653
3654 cur = cur->next;
3655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003656
3657 return( 0 );
3658}
3659
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003660/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003661 * Check that the given certificate is valid accoring to the CRL.
3662 */
3663static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3664 x509_crl *crl_list)
3665{
3666 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003667 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3668 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003669
Paul Bakker915275b2012-09-28 07:10:55 +00003670 if( ca == NULL )
3671 return( flags );
3672
Paul Bakker76fd75a2011-01-16 21:12:10 +00003673 /*
3674 * TODO: What happens if no CRL is present?
3675 * Suggestion: Revocation state should be unknown if no CRL is present.
3676 * For backwards compatibility this is not yet implemented.
3677 */
3678
Paul Bakker915275b2012-09-28 07:10:55 +00003679 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003680 {
Paul Bakker915275b2012-09-28 07:10:55 +00003681 if( crl_list->version == 0 ||
3682 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003683 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3684 crl_list->issuer_raw.len ) != 0 )
3685 {
3686 crl_list = crl_list->next;
3687 continue;
3688 }
3689
3690 /*
3691 * Check if CRL is correctly signed by the trusted CA
3692 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003693 md_info = md_info_from_type( crl_list->sig_md );
3694 if( md_info == NULL )
3695 {
3696 /*
3697 * Cannot check 'unknown' hash
3698 */
3699 flags |= BADCRL_NOT_TRUSTED;
3700 break;
3701 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003702
Paul Bakkerc70b9822013-04-07 22:00:46 +02003703 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003704
Paul Bakkerc70b9822013-04-07 22:00:46 +02003705 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003706 0, hash, crl_list->sig.p ) == 0 )
3707 {
3708 /*
3709 * CRL is not trusted
3710 */
3711 flags |= BADCRL_NOT_TRUSTED;
3712 break;
3713 }
3714
3715 /*
3716 * Check for validity of CRL (Do not drop out)
3717 */
3718 if( x509parse_time_expired( &crl_list->next_update ) )
3719 flags |= BADCRL_EXPIRED;
3720
3721 /*
3722 * Check if certificate is revoked
3723 */
3724 if( x509parse_revoked(crt, crl_list) )
3725 {
3726 flags |= BADCERT_REVOKED;
3727 break;
3728 }
3729
3730 crl_list = crl_list->next;
3731 }
3732 return flags;
3733}
3734
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003735static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003736{
3737 size_t i;
3738 size_t cn_idx = 0;
3739
Paul Bakker57b12982012-02-11 17:38:38 +00003740 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003741 return( 0 );
3742
3743 for( i = 0; i < strlen( cn ); ++i )
3744 {
3745 if( cn[i] == '.' )
3746 {
3747 cn_idx = i;
3748 break;
3749 }
3750 }
3751
3752 if( cn_idx == 0 )
3753 return( 0 );
3754
Paul Bakker535e97d2012-08-23 10:49:55 +00003755 if( strlen( cn ) - cn_idx == name->len - 1 &&
3756 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003757 {
3758 return( 1 );
3759 }
3760
3761 return( 0 );
3762}
3763
Paul Bakker915275b2012-09-28 07:10:55 +00003764static int x509parse_verify_top(
3765 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003766 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003767 int (*f_vrfy)(void *, x509_cert *, int, int *),
3768 void *p_vrfy )
3769{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003770 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003771 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003772 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3773 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003774
3775 if( x509parse_time_expired( &child->valid_to ) )
3776 *flags |= BADCERT_EXPIRED;
3777
3778 /*
3779 * Child is the top of the chain. Check against the trust_ca list.
3780 */
3781 *flags |= BADCERT_NOT_TRUSTED;
3782
3783 while( trust_ca != NULL )
3784 {
3785 if( trust_ca->version == 0 ||
3786 child->issuer_raw.len != trust_ca->subject_raw.len ||
3787 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3788 child->issuer_raw.len ) != 0 )
3789 {
3790 trust_ca = trust_ca->next;
3791 continue;
3792 }
3793
Paul Bakker9a736322012-11-14 12:39:52 +00003794 /*
3795 * Reduce path_len to check against if top of the chain is
3796 * the same as the trusted CA
3797 */
3798 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3799 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3800 child->issuer_raw.len ) == 0 )
3801 {
3802 check_path_cnt--;
3803 }
3804
Paul Bakker915275b2012-09-28 07:10:55 +00003805 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003806 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003807 {
3808 trust_ca = trust_ca->next;
3809 continue;
3810 }
3811
Paul Bakkerc70b9822013-04-07 22:00:46 +02003812 md_info = md_info_from_type( child->sig_md );
3813 if( md_info == NULL )
3814 {
3815 /*
3816 * Cannot check 'unknown' hash
3817 */
3818 continue;
3819 }
Paul Bakker915275b2012-09-28 07:10:55 +00003820
Paul Bakkerc70b9822013-04-07 22:00:46 +02003821 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003822
Paul Bakkerc70b9822013-04-07 22:00:46 +02003823 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003824 0, hash, child->sig.p ) != 0 )
3825 {
3826 trust_ca = trust_ca->next;
3827 continue;
3828 }
3829
3830 /*
3831 * Top of chain is signed by a trusted CA
3832 */
3833 *flags &= ~BADCERT_NOT_TRUSTED;
3834 break;
3835 }
3836
Paul Bakker9a736322012-11-14 12:39:52 +00003837 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003838 * If top of chain is not the same as the trusted CA send a verify request
3839 * to the callback for any issues with validity and CRL presence for the
3840 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003841 */
3842 if( trust_ca != NULL &&
3843 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3844 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3845 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003846 {
3847 /* Check trusted CA's CRL for then chain's top crt */
3848 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3849
3850 if( x509parse_time_expired( &trust_ca->valid_to ) )
3851 ca_flags |= BADCERT_EXPIRED;
3852
Paul Bakker915275b2012-09-28 07:10:55 +00003853 if( NULL != f_vrfy )
3854 {
Paul Bakker9a736322012-11-14 12:39:52 +00003855 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003856 return( ret );
3857 }
3858 }
3859
3860 /* Call callback on top cert */
3861 if( NULL != f_vrfy )
3862 {
Paul Bakker9a736322012-11-14 12:39:52 +00003863 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003864 return( ret );
3865 }
3866
Paul Bakker915275b2012-09-28 07:10:55 +00003867 *flags |= ca_flags;
3868
3869 return( 0 );
3870}
3871
3872static int x509parse_verify_child(
3873 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003874 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003875 int (*f_vrfy)(void *, x509_cert *, int, int *),
3876 void *p_vrfy )
3877{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003878 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003879 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003880 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003881 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003882 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003883
3884 if( x509parse_time_expired( &child->valid_to ) )
3885 *flags |= BADCERT_EXPIRED;
3886
Paul Bakkerc70b9822013-04-07 22:00:46 +02003887 md_info = md_info_from_type( child->sig_md );
3888 if( md_info == NULL )
3889 {
3890 /*
3891 * Cannot check 'unknown' hash
3892 */
Paul Bakker915275b2012-09-28 07:10:55 +00003893 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003894 }
3895 else
3896 {
3897 md( md_info, child->tbs.p, child->tbs.len, hash );
3898
3899 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3900 child->sig.p ) != 0 )
3901 *flags |= BADCERT_NOT_TRUSTED;
3902 }
3903
Paul Bakker915275b2012-09-28 07:10:55 +00003904 /* Check trusted CA's CRL for the given crt */
3905 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3906
3907 grandparent = parent->next;
3908
3909 while( grandparent != NULL )
3910 {
3911 if( grandparent->version == 0 ||
3912 grandparent->ca_istrue == 0 ||
3913 parent->issuer_raw.len != grandparent->subject_raw.len ||
3914 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3915 parent->issuer_raw.len ) != 0 )
3916 {
3917 grandparent = grandparent->next;
3918 continue;
3919 }
3920 break;
3921 }
3922
Paul Bakker915275b2012-09-28 07:10:55 +00003923 if( grandparent != NULL )
3924 {
3925 /*
3926 * Part of the chain
3927 */
Paul Bakker9a736322012-11-14 12:39:52 +00003928 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 +00003929 if( ret != 0 )
3930 return( ret );
3931 }
3932 else
3933 {
Paul Bakker9a736322012-11-14 12:39:52 +00003934 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 +00003935 if( ret != 0 )
3936 return( ret );
3937 }
3938
3939 /* child is verified to be a child of the parent, call verify callback */
3940 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003941 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003942 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003943
3944 *flags |= parent_flags;
3945
3946 return( 0 );
3947}
3948
Paul Bakker76fd75a2011-01-16 21:12:10 +00003949/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003950 * Verify the certificate validity
3951 */
3952int x509parse_verify( x509_cert *crt,
3953 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003954 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003955 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003956 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003957 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003958{
Paul Bakker23986e52011-04-24 08:57:21 +00003959 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003960 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003961 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003962 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003964 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003965
Paul Bakker40ea7de2009-05-03 10:18:48 +00003966 *flags = 0;
3967
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 if( cn != NULL )
3969 {
3970 name = &crt->subject;
3971 cn_len = strlen( cn );
3972
Paul Bakker4d2c1242012-05-10 14:12:46 +00003973 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003974 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003975 cur = &crt->subject_alt_names;
3976
3977 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003978 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003979 if( cur->buf.len == cn_len &&
3980 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003981 break;
3982
Paul Bakker535e97d2012-08-23 10:49:55 +00003983 if( cur->buf.len > 2 &&
3984 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003985 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003986 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003987
Paul Bakker4d2c1242012-05-10 14:12:46 +00003988 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003989 }
3990
3991 if( cur == NULL )
3992 *flags |= BADCERT_CN_MISMATCH;
3993 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003994 else
3995 {
3996 while( name != NULL )
3997 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003998 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003999 {
Paul Bakker535e97d2012-08-23 10:49:55 +00004000 if( name->val.len == cn_len &&
4001 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00004002 break;
4003
Paul Bakker535e97d2012-08-23 10:49:55 +00004004 if( name->val.len > 2 &&
4005 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00004006 x509_wildcard_verify( cn, &name->val ) )
4007 break;
4008 }
4009
4010 name = name->next;
4011 }
4012
4013 if( name == NULL )
4014 *flags |= BADCERT_CN_MISMATCH;
4015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004016 }
4017
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 /*
Paul Bakker915275b2012-09-28 07:10:55 +00004019 * Iterate upwards in the given cert chain, to find our crt parent.
4020 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00004021 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00004022 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004023
Paul Bakker76fd75a2011-01-16 21:12:10 +00004024 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004025 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004026 if( parent->ca_istrue == 0 ||
4027 crt->issuer_raw.len != parent->subject_raw.len ||
4028 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00004029 crt->issuer_raw.len ) != 0 )
4030 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004031 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004032 continue;
4033 }
Paul Bakker915275b2012-09-28 07:10:55 +00004034 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004035 }
4036
Paul Bakker915275b2012-09-28 07:10:55 +00004037 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004038 {
Paul Bakker915275b2012-09-28 07:10:55 +00004039 /*
4040 * Part of the chain
4041 */
Paul Bakker9a736322012-11-14 12:39:52 +00004042 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004043 if( ret != 0 )
4044 return( ret );
4045 }
4046 else
Paul Bakker74111d32011-01-15 16:57:55 +00004047 {
Paul Bakker9a736322012-11-14 12:39:52 +00004048 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004049 if( ret != 0 )
4050 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004051 }
Paul Bakker915275b2012-09-28 07:10:55 +00004052
4053 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00004054 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004055
Paul Bakker5121ce52009-01-03 21:22:43 +00004056 return( 0 );
4057}
4058
4059/*
4060 * Unallocate all certificate data
4061 */
4062void x509_free( x509_cert *crt )
4063{
4064 x509_cert *cert_cur = crt;
4065 x509_cert *cert_prv;
4066 x509_name *name_cur;
4067 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00004068 x509_sequence *seq_cur;
4069 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00004070
4071 if( crt == NULL )
4072 return;
4073
4074 do
4075 {
4076 rsa_free( &cert_cur->rsa );
4077
4078 name_cur = cert_cur->issuer.next;
4079 while( name_cur != NULL )
4080 {
4081 name_prv = name_cur;
4082 name_cur = name_cur->next;
4083 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004084 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004085 }
4086
4087 name_cur = cert_cur->subject.next;
4088 while( name_cur != NULL )
4089 {
4090 name_prv = name_cur;
4091 name_cur = name_cur->next;
4092 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004093 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004094 }
4095
Paul Bakker74111d32011-01-15 16:57:55 +00004096 seq_cur = cert_cur->ext_key_usage.next;
4097 while( seq_cur != NULL )
4098 {
4099 seq_prv = seq_cur;
4100 seq_cur = seq_cur->next;
4101 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004102 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004103 }
4104
Paul Bakker8afa70d2012-02-11 18:42:45 +00004105 seq_cur = cert_cur->subject_alt_names.next;
4106 while( seq_cur != NULL )
4107 {
4108 seq_prv = seq_cur;
4109 seq_cur = seq_cur->next;
4110 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004111 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004112 }
4113
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 if( cert_cur->raw.p != NULL )
4115 {
4116 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004117 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004118 }
4119
4120 cert_cur = cert_cur->next;
4121 }
4122 while( cert_cur != NULL );
4123
4124 cert_cur = crt;
4125 do
4126 {
4127 cert_prv = cert_cur;
4128 cert_cur = cert_cur->next;
4129
4130 memset( cert_prv, 0, sizeof( x509_cert ) );
4131 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004132 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004133 }
4134 while( cert_cur != NULL );
4135}
4136
Paul Bakkerd98030e2009-05-02 15:13:40 +00004137/*
4138 * Unallocate all CRL data
4139 */
4140void x509_crl_free( x509_crl *crl )
4141{
4142 x509_crl *crl_cur = crl;
4143 x509_crl *crl_prv;
4144 x509_name *name_cur;
4145 x509_name *name_prv;
4146 x509_crl_entry *entry_cur;
4147 x509_crl_entry *entry_prv;
4148
4149 if( crl == NULL )
4150 return;
4151
4152 do
4153 {
4154 name_cur = crl_cur->issuer.next;
4155 while( name_cur != NULL )
4156 {
4157 name_prv = name_cur;
4158 name_cur = name_cur->next;
4159 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004160 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004161 }
4162
4163 entry_cur = crl_cur->entry.next;
4164 while( entry_cur != NULL )
4165 {
4166 entry_prv = entry_cur;
4167 entry_cur = entry_cur->next;
4168 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004169 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004170 }
4171
4172 if( crl_cur->raw.p != NULL )
4173 {
4174 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004175 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004176 }
4177
4178 crl_cur = crl_cur->next;
4179 }
4180 while( crl_cur != NULL );
4181
4182 crl_cur = crl;
4183 do
4184 {
4185 crl_prv = crl_cur;
4186 crl_cur = crl_cur->next;
4187
4188 memset( crl_prv, 0, sizeof( x509_crl ) );
4189 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004190 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004191 }
4192 while( crl_cur != NULL );
4193}
4194
Paul Bakker40e46942009-01-03 21:51:57 +00004195#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004196
Paul Bakker40e46942009-01-03 21:51:57 +00004197#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004198
4199/*
4200 * Checkup routine
4201 */
4202int x509_self_test( int verbose )
4203{
Paul Bakker5690efc2011-05-26 13:16:06 +00004204#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004205 int ret;
4206 int flags;
4207 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004208 x509_cert cacert;
4209 x509_cert clicert;
4210 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004211#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004212 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004213#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004214
4215 if( verbose != 0 )
4216 printf( " X.509 certificate load: " );
4217
4218 memset( &clicert, 0, sizeof( x509_cert ) );
4219
Paul Bakker3c2122f2013-06-24 19:03:14 +02004220 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004221 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004222 if( ret != 0 )
4223 {
4224 if( verbose != 0 )
4225 printf( "failed\n" );
4226
4227 return( ret );
4228 }
4229
4230 memset( &cacert, 0, sizeof( x509_cert ) );
4231
Paul Bakker3c2122f2013-06-24 19:03:14 +02004232 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004233 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 if( ret != 0 )
4235 {
4236 if( verbose != 0 )
4237 printf( "failed\n" );
4238
4239 return( ret );
4240 }
4241
4242 if( verbose != 0 )
4243 printf( "passed\n X.509 private key load: " );
4244
4245 i = strlen( test_ca_key );
4246 j = strlen( test_ca_pwd );
4247
Paul Bakker66b78b22011-03-25 14:22:50 +00004248 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4249
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004250 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004251 (const unsigned char *) test_ca_key, i,
4252 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004253 {
4254 if( verbose != 0 )
4255 printf( "failed\n" );
4256
4257 return( ret );
4258 }
4259
4260 if( verbose != 0 )
4261 printf( "passed\n X.509 signature verify: ");
4262
Paul Bakker23986e52011-04-24 08:57:21 +00004263 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004264 if( ret != 0 )
4265 {
Paul Bakker23986e52011-04-24 08:57:21 +00004266 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 if( verbose != 0 )
4268 printf( "failed\n" );
4269
4270 return( ret );
4271 }
4272
Paul Bakker5690efc2011-05-26 13:16:06 +00004273#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004274 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004275 printf( "passed\n X.509 DHM parameter load: " );
4276
4277 i = strlen( test_dhm_params );
4278 j = strlen( test_ca_pwd );
4279
Paul Bakker3c2122f2013-06-24 19:03:14 +02004280 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004281 {
4282 if( verbose != 0 )
4283 printf( "failed\n" );
4284
4285 return( ret );
4286 }
4287
4288 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004289 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004291
4292 x509_free( &cacert );
4293 x509_free( &clicert );
4294 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004295#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004296 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004297#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004298
4299 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004300#else
4301 ((void) verbose);
4302 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4303#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004304}
4305
4306#endif
4307
4308#endif