blob: 55149e48b1fcfb3ca385f3e61575164246e506a0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000045#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020046#if defined(POLARSSL_PKCS5_C)
47#include "polarssl/pkcs5.h"
48#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020049#if defined(POLARSSL_PKCS12_C)
50#include "polarssl/pkcs12.h"
51#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker6e339b52013-07-03 13:37:05 +020053#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <string.h>
61#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000062#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000063#include <windows.h>
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker335db3f2011-04-25 15:28:35 +000068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000070#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000071#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020072#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000073#include <dirent.h>
74#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000075#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
163/*
164 * AlgorithmIdentifier ::= SEQUENCE {
165 * algorithm OBJECT IDENTIFIER,
166 * parameters ANY DEFINED BY algorithm OPTIONAL }
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200167 *
168 * If params_end is NULL, then parameters must be absent or ANS.1 NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
170static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000171 const unsigned char *end,
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200172 x509_buf *alg, const unsigned char **params_end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000173{
Paul Bakker23986e52011-04-24 08:57:21 +0000174 int ret;
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200175 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200177 if( params_end == NULL ) {
178 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
179 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
180
181 return( 0 );
182 }
183
184 /* TODO: use asn1_get_alg */
185 if( ( ret = asn1_get_tag( p, end, &len,
186 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
187 {
188 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
189 }
190
191 end = *p + len;
192 alg->tag = **p;
193
194 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000195 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200197 alg->p = *p;
198 *p += alg->len;
199
200 *params_end = end;
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 return( 0 );
202}
203
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200204/* Get an EC group id from an ECParameters buffer
205 *
206 * ECParameters ::= CHOICE {
207 * namedCurve OBJECT IDENTIFIER
208 * -- implicitCurve NULL
209 * -- specifiedCurve SpecifiedECDomain
210 * }
211 */
212static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
213 ecp_group_id *grp_id )
214{
215 int ret;
216 x509_buf curve;
217
218 curve.tag = **p;
219
220 if( ( ret = asn1_get_tag( p, end, &curve.len, ASN1_OID ) ) != 0 )
221 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
222
223 curve.p = *p;
224 *p += curve.len;
225
226 if( *p != end )
227 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
228 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
229
230 if( ( ret = oid_get_ec_grp( &curve, grp_id ) ) != 0 )
231 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
232
233 return( 0 );
234}
235
Paul Bakker5121ce52009-01-03 21:22:43 +0000236/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200237 * subjectPublicKey BIT STRING
238 * -- which, in our case, contains
239 * ECPoint ::= octet string (not ASN.1)
240 */
241static int x509_get_subpubkey_ec( unsigned char **p, const unsigned char *end,
242 const ecp_group *grp, ecp_point *pt )
243{
244 int ret;
245 size_t len;
246
247 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
248 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
249
250 if( *p + len != end )
251 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
252 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
253
254 /*
255 * First byte in the content of BIT STRING is the nummber of padding bit.
256 * Here it is always 0 since ECPoint is an octet string, so skip it.
257 */
258 ++*p;
259 --len;
260
261 if( ( ret = ecp_point_read_binary( grp, pt,
262 (const unsigned char *) *p, len ) ) != 0 )
263 {
264 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
265 }
266
267 return( 0 );
268}
269
270/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 * AttributeTypeAndValue ::= SEQUENCE {
272 * type AttributeType,
273 * value AttributeValue }
274 *
275 * AttributeType ::= OBJECT IDENTIFIER
276 *
277 * AttributeValue ::= ANY DEFINED BY AttributeType
278 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000279static int x509_get_attr_type_value( unsigned char **p,
280 const unsigned char *end,
281 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000282{
Paul Bakker23986e52011-04-24 08:57:21 +0000283 int ret;
284 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 x509_buf *oid;
286 x509_buf *val;
287
288 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000290 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 oid = &cur->oid;
293 oid->tag = **p;
294
295 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000296 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298 oid->p = *p;
299 *p += oid->len;
300
301 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000302 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000303 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
306 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
307 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000308 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000309 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311 val = &cur->val;
312 val->tag = *(*p)++;
313
314 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000315 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317 val->p = *p;
318 *p += val->len;
319
320 cur->next = NULL;
321
Paul Bakker400ff6f2011-02-20 10:40:16 +0000322 return( 0 );
323}
324
325/*
326 * RelativeDistinguishedName ::=
327 * SET OF AttributeTypeAndValue
328 *
329 * AttributeTypeAndValue ::= SEQUENCE {
330 * type AttributeType,
331 * value AttributeValue }
332 *
333 * AttributeType ::= OBJECT IDENTIFIER
334 *
335 * AttributeValue ::= ANY DEFINED BY AttributeType
336 */
337static int x509_get_name( unsigned char **p,
338 const unsigned char *end,
339 x509_name *cur )
340{
Paul Bakker23986e52011-04-24 08:57:21 +0000341 int ret;
342 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000343 const unsigned char *end2;
344 x509_name *use;
345
346 if( ( ret = asn1_get_tag( p, end, &len,
347 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000348 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000349
350 end2 = end;
351 end = *p + len;
352 use = cur;
353
354 do
355 {
356 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
357 return( ret );
358
359 if( *p != end )
360 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200361 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000362 sizeof( x509_name ) );
363
364 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000365 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000366
367 memset( use->next, 0, sizeof( x509_name ) );
368
369 use = use->next;
370 }
371 }
372 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374 /*
375 * recurse until end of SEQUENCE is reached
376 */
377 if( *p == end2 )
378 return( 0 );
379
Paul Bakker6e339b52013-07-03 13:37:05 +0200380 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 sizeof( x509_name ) );
382
383 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000384 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
Paul Bakker430ffbe2012-05-01 08:14:20 +0000386 memset( cur->next, 0, sizeof( x509_name ) );
387
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 return( x509_get_name( p, end2, cur->next ) );
389}
390
391/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 * Time ::= CHOICE {
393 * utcTime UTCTime,
394 * generalTime GeneralizedTime }
395 */
Paul Bakker91200182010-02-18 21:26:15 +0000396static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000397 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000398 x509_time *time )
399{
Paul Bakker23986e52011-04-24 08:57:21 +0000400 int ret;
401 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000402 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000403 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404
Paul Bakker91200182010-02-18 21:26:15 +0000405 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000406 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
407 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000408
Paul Bakker91200182010-02-18 21:26:15 +0000409 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000410
Paul Bakker91200182010-02-18 21:26:15 +0000411 if ( tag == ASN1_UTC_TIME )
412 {
413 (*p)++;
414 ret = asn1_get_len( p, end, &len );
415
416 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000417 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000418
Paul Bakker91200182010-02-18 21:26:15 +0000419 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000420 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
421 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000422
Paul Bakker91200182010-02-18 21:26:15 +0000423 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
424 &time->year, &time->mon, &time->day,
425 &time->hour, &time->min, &time->sec ) < 5 )
426 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000427
Paul Bakker400ff6f2011-02-20 10:40:16 +0000428 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000429 time->year += 1900;
430
431 *p += len;
432
433 return( 0 );
434 }
435 else if ( tag == ASN1_GENERALIZED_TIME )
436 {
437 (*p)++;
438 ret = asn1_get_len( p, end, &len );
439
440 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000441 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000442
443 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000444 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
445 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000446
447 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
448 &time->year, &time->mon, &time->day,
449 &time->hour, &time->min, &time->sec ) < 5 )
450 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
451
452 *p += len;
453
454 return( 0 );
455 }
456 else
Paul Bakker9d781402011-05-09 16:17:09 +0000457 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000458}
459
460
461/*
462 * Validity ::= SEQUENCE {
463 * notBefore Time,
464 * notAfter Time }
465 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000466static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000467 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 x509_time *from,
469 x509_time *to )
470{
Paul Bakker23986e52011-04-24 08:57:21 +0000471 int ret;
472 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474 if( ( ret = asn1_get_tag( p, end, &len,
475 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000476 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 end = *p + len;
479
Paul Bakker91200182010-02-18 21:26:15 +0000480 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000481 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
Paul Bakker91200182010-02-18 21:26:15 +0000483 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000484 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 return( 0 );
491}
492
493/*
494 * SubjectPublicKeyInfo ::= SEQUENCE {
495 * algorithm AlgorithmIdentifier,
496 * subjectPublicKey BIT STRING }
497 */
498static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000499 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 x509_buf *pk_alg_oid,
501 mpi *N, mpi *E )
502{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200503 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000504 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 unsigned char *end2;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200506 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200508 if( ( ret = asn1_get_alg_null( p, end, pk_alg_oid ) ) != 0 )
509 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 /*
512 * only RSA public keys handled at this time
513 */
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +0200514 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 ||
515 pk_alg != POLARSSL_PK_RSA )
516 {
Paul Bakkered56b222011-07-13 11:26:43 +0000517 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +0200518 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000521 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000524 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000525 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 end2 = *p + len;
528
529 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000530 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
532 /*
533 * RSAPublicKey ::= SEQUENCE {
534 * modulus INTEGER, -- n
535 * publicExponent INTEGER -- e
536 * }
537 */
538 if( ( ret = asn1_get_tag( p, end2, &len,
539 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000540 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
542 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000543 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000544 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
546 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
547 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000548 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000551 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000552 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554 return( 0 );
555}
556
557static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000558 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 x509_buf *sig )
560{
Paul Bakker23986e52011-04-24 08:57:21 +0000561 int ret;
562 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
Paul Bakker8afa70d2012-02-11 18:42:45 +0000564 if( ( end - *p ) < 1 )
565 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
566 POLARSSL_ERR_ASN1_OUT_OF_DATA );
567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 sig->tag = **p;
569
570 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000571 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Paul Bakker74111d32011-01-15 16:57:55 +0000573
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000575 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577 sig->len = len;
578 sig->p = *p;
579
580 *p += len;
581
582 return( 0 );
583}
584
585/*
586 * X.509 v2/v3 unique identifier (not parsed)
587 */
588static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000589 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 x509_buf *uid, int n )
591{
592 int ret;
593
594 if( *p == end )
595 return( 0 );
596
597 uid->tag = **p;
598
599 if( ( ret = asn1_get_tag( p, end, &uid->len,
600 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
601 {
Paul Bakker40e46942009-01-03 21:51:57 +0000602 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 return( 0 );
604
605 return( ret );
606 }
607
608 uid->p = *p;
609 *p += uid->len;
610
611 return( 0 );
612}
613
614/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000615 * X.509 Extensions (No parsing of extensions, pointer should
616 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 */
618static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000619 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000620 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000621{
Paul Bakker23986e52011-04-24 08:57:21 +0000622 int ret;
623 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
625 if( *p == end )
626 return( 0 );
627
628 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000629
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000631 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634 ext->p = *p;
635 end = *p + ext->len;
636
637 /*
638 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
639 *
640 * Extension ::= SEQUENCE {
641 * extnID OBJECT IDENTIFIER,
642 * critical BOOLEAN DEFAULT FALSE,
643 * extnValue OCTET STRING }
644 */
645 if( ( ret = asn1_get_tag( p, end, &len,
646 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000647 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
649 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000650 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000651 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Paul Bakkerd98030e2009-05-02 15:13:40 +0000653 return( 0 );
654}
655
656/*
657 * X.509 CRL v2 extensions (no extensions parsed yet.)
658 */
659static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000660 const unsigned char *end,
661 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000662{
Paul Bakker23986e52011-04-24 08:57:21 +0000663 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000664 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000665
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000666 /* Get explicit tag */
667 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000668 {
669 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
670 return( 0 );
671
672 return( ret );
673 }
674
675 while( *p < end )
676 {
677 if( ( ret = asn1_get_tag( p, end, &len,
678 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000679 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000680
681 *p += len;
682 }
683
684 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000685 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000686 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
687
688 return( 0 );
689}
690
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000691/*
692 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
693 */
694static int x509_get_crl_entry_ext( unsigned char **p,
695 const unsigned char *end,
696 x509_buf *ext )
697{
698 int ret;
699 size_t len = 0;
700
701 /* OPTIONAL */
702 if (end <= *p)
703 return( 0 );
704
705 ext->tag = **p;
706 ext->p = *p;
707
708 /*
709 * Get CRL-entry extension sequence header
710 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
711 */
712 if( ( ret = asn1_get_tag( p, end, &ext->len,
713 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
714 {
715 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
716 {
717 ext->p = NULL;
718 return( 0 );
719 }
720 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
721 }
722
723 end = *p + ext->len;
724
725 if( end != *p + ext->len )
726 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
727 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
728
729 while( *p < end )
730 {
731 if( ( ret = asn1_get_tag( p, end, &len,
732 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
733 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
734
735 *p += len;
736 }
737
738 if( *p != end )
739 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
740 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
741
742 return( 0 );
743}
744
Paul Bakker74111d32011-01-15 16:57:55 +0000745static int x509_get_basic_constraints( unsigned char **p,
746 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000747 int *ca_istrue,
748 int *max_pathlen )
749{
Paul Bakker23986e52011-04-24 08:57:21 +0000750 int ret;
751 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000752
753 /*
754 * BasicConstraints ::= SEQUENCE {
755 * cA BOOLEAN DEFAULT FALSE,
756 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
757 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000758 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000759 *max_pathlen = 0; /* endless */
760
761 if( ( ret = asn1_get_tag( p, end, &len,
762 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000763 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000764
765 if( *p == end )
766 return 0;
767
Paul Bakker3cccddb2011-01-16 21:46:31 +0000768 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000769 {
770 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000771 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000772
773 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000774 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000775
Paul Bakker3cccddb2011-01-16 21:46:31 +0000776 if( *ca_istrue != 0 )
777 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000778 }
779
780 if( *p == end )
781 return 0;
782
783 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000784 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000785
786 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000787 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000788 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
789
790 (*max_pathlen)++;
791
Paul Bakker74111d32011-01-15 16:57:55 +0000792 return 0;
793}
794
795static int x509_get_ns_cert_type( unsigned char **p,
796 const unsigned char *end,
797 unsigned char *ns_cert_type)
798{
799 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000800 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000801
802 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000803 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000804
805 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000806 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000807 POLARSSL_ERR_ASN1_INVALID_LENGTH );
808
809 /* Get actual bitstring */
810 *ns_cert_type = *bs.p;
811 return 0;
812}
813
814static int x509_get_key_usage( unsigned char **p,
815 const unsigned char *end,
816 unsigned char *key_usage)
817{
818 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000819 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000820
821 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000823
Paul Bakker94a67962012-08-23 13:03:52 +0000824 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000825 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000826 POLARSSL_ERR_ASN1_INVALID_LENGTH );
827
828 /* Get actual bitstring */
829 *key_usage = *bs.p;
830 return 0;
831}
832
Paul Bakkerd98030e2009-05-02 15:13:40 +0000833/*
Paul Bakker74111d32011-01-15 16:57:55 +0000834 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
835 *
836 * KeyPurposeId ::= OBJECT IDENTIFIER
837 */
838static int x509_get_ext_key_usage( unsigned char **p,
839 const unsigned char *end,
840 x509_sequence *ext_key_usage)
841{
842 int ret;
843
844 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000846
847 /* Sequence length must be >= 1 */
848 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000849 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000850 POLARSSL_ERR_ASN1_INVALID_LENGTH );
851
852 return 0;
853}
854
855/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000856 * SubjectAltName ::= GeneralNames
857 *
858 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
859 *
860 * GeneralName ::= CHOICE {
861 * otherName [0] OtherName,
862 * rfc822Name [1] IA5String,
863 * dNSName [2] IA5String,
864 * x400Address [3] ORAddress,
865 * directoryName [4] Name,
866 * ediPartyName [5] EDIPartyName,
867 * uniformResourceIdentifier [6] IA5String,
868 * iPAddress [7] OCTET STRING,
869 * registeredID [8] OBJECT IDENTIFIER }
870 *
871 * OtherName ::= SEQUENCE {
872 * type-id OBJECT IDENTIFIER,
873 * value [0] EXPLICIT ANY DEFINED BY type-id }
874 *
875 * EDIPartyName ::= SEQUENCE {
876 * nameAssigner [0] DirectoryString OPTIONAL,
877 * partyName [1] DirectoryString }
878 *
879 * NOTE: PolarSSL only parses and uses dNSName at this point.
880 */
881static int x509_get_subject_alt_name( unsigned char **p,
882 const unsigned char *end,
883 x509_sequence *subject_alt_name )
884{
885 int ret;
886 size_t len, tag_len;
887 asn1_buf *buf;
888 unsigned char tag;
889 asn1_sequence *cur = subject_alt_name;
890
891 /* Get main sequence tag */
892 if( ( ret = asn1_get_tag( p, end, &len,
893 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
894 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
895
896 if( *p + len != end )
897 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
898 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
899
900 while( *p < end )
901 {
902 if( ( end - *p ) < 1 )
903 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
904 POLARSSL_ERR_ASN1_OUT_OF_DATA );
905
906 tag = **p;
907 (*p)++;
908 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
909 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
910
911 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
912 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
913 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
914
915 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
916 {
917 *p += tag_len;
918 continue;
919 }
920
921 buf = &(cur->buf);
922 buf->tag = tag;
923 buf->p = *p;
924 buf->len = tag_len;
925 *p += buf->len;
926
927 /* Allocate and assign next pointer */
928 if (*p < end)
929 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200930 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000931 sizeof( asn1_sequence ) );
932
933 if( cur->next == NULL )
934 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
935 POLARSSL_ERR_ASN1_MALLOC_FAILED );
936
Paul Bakker535e97d2012-08-23 10:49:55 +0000937 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000938 cur = cur->next;
939 }
940 }
941
942 /* Set final sequence entry's next pointer to NULL */
943 cur->next = NULL;
944
945 if( *p != end )
946 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
947 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
948
949 return( 0 );
950}
951
952/*
Paul Bakker74111d32011-01-15 16:57:55 +0000953 * X.509 v3 extensions
954 *
955 * TODO: Perform all of the basic constraints tests required by the RFC
956 * TODO: Set values for undetected extensions to a sane default?
957 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000958 */
959static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000960 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000961 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000962{
Paul Bakker23986e52011-04-24 08:57:21 +0000963 int ret;
964 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000965 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000966
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000967 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000968 {
969 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
970 return( 0 );
971
972 return( ret );
973 }
974
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 while( *p < end )
976 {
Paul Bakker74111d32011-01-15 16:57:55 +0000977 /*
978 * Extension ::= SEQUENCE {
979 * extnID OBJECT IDENTIFIER,
980 * critical BOOLEAN DEFAULT FALSE,
981 * extnValue OCTET STRING }
982 */
983 x509_buf extn_oid = {0, 0, NULL};
984 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200985 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000986
Paul Bakker5121ce52009-01-03 21:22:43 +0000987 if( ( ret = asn1_get_tag( p, end, &len,
988 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000989 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000991 end_ext_data = *p + len;
992
Paul Bakker74111d32011-01-15 16:57:55 +0000993 /* Get extension ID */
994 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
Paul Bakker74111d32011-01-15 16:57:55 +0000996 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000997 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
Paul Bakker74111d32011-01-15 16:57:55 +0000999 extn_oid.p = *p;
1000 *p += extn_oid.len;
1001
1002 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001003 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001004 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1005
1006 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001007 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001008 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001009 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Paul Bakker74111d32011-01-15 16:57:55 +00001011 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001012 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001014 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001016 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001017
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001018 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001019 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001020 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Paul Bakker74111d32011-01-15 16:57:55 +00001022 /*
1023 * Detect supported extensions
1024 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001025 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1026
1027 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001028 {
1029 /* No parser found, skip extension */
1030 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Paul Bakker5c721f92011-07-27 16:51:09 +00001032#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001033 if( is_critical )
1034 {
1035 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001036 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001037 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1038 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001039#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001040 continue;
1041 }
1042
1043 crt->ext_types |= ext_type;
1044
1045 switch( ext_type )
1046 {
1047 case EXT_BASIC_CONSTRAINTS:
1048 /* Parse basic constraints */
1049 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1050 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1051 return ( ret );
1052 break;
1053
1054 case EXT_KEY_USAGE:
1055 /* Parse key usage */
1056 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1057 &crt->key_usage ) ) != 0 )
1058 return ( ret );
1059 break;
1060
1061 case EXT_EXTENDED_KEY_USAGE:
1062 /* Parse extended key usage */
1063 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1064 &crt->ext_key_usage ) ) != 0 )
1065 return ( ret );
1066 break;
1067
1068 case EXT_SUBJECT_ALT_NAME:
1069 /* Parse subject alt name */
1070 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1071 &crt->subject_alt_names ) ) != 0 )
1072 return ( ret );
1073 break;
1074
1075 case EXT_NS_CERT_TYPE:
1076 /* Parse netscape certificate type */
1077 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1078 &crt->ns_cert_type ) ) != 0 )
1079 return ( ret );
1080 break;
1081
1082 default:
1083 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 }
1086
1087 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001088 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001089 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 return( 0 );
1092}
1093
1094/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001095 * X.509 CRL Entries
1096 */
1097static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001098 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001099 x509_crl_entry *entry )
1100{
Paul Bakker23986e52011-04-24 08:57:21 +00001101 int ret;
1102 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001103 x509_crl_entry *cur_entry = entry;
1104
1105 if( *p == end )
1106 return( 0 );
1107
Paul Bakker9be19372009-07-27 20:21:53 +00001108 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001109 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1110 {
1111 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1112 return( 0 );
1113
1114 return( ret );
1115 }
1116
Paul Bakker9be19372009-07-27 20:21:53 +00001117 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001118
1119 while( *p < end )
1120 {
Paul Bakker23986e52011-04-24 08:57:21 +00001121 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001122 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001123
1124 if( ( ret = asn1_get_tag( p, end, &len2,
1125 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1126 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001127 return( ret );
1128 }
1129
Paul Bakker9be19372009-07-27 20:21:53 +00001130 cur_entry->raw.tag = **p;
1131 cur_entry->raw.p = *p;
1132 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001133 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001134
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001135 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001136 return( ret );
1137
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001138 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001139 return( ret );
1140
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001141 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001142 return( ret );
1143
Paul Bakker74111d32011-01-15 16:57:55 +00001144 if ( *p < end )
1145 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001146 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001147
1148 if( cur_entry->next == NULL )
1149 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1150
Paul Bakkerd98030e2009-05-02 15:13:40 +00001151 cur_entry = cur_entry->next;
1152 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1153 }
1154 }
1155
1156 return( 0 );
1157}
1158
Paul Bakkerc70b9822013-04-07 22:00:46 +02001159static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1160 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001161{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001162 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001163
Paul Bakkerc70b9822013-04-07 22:00:46 +02001164 if( ret != 0 )
1165 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001166
Paul Bakkerc70b9822013-04-07 22:00:46 +02001167 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001168}
1169
Paul Bakkerd98030e2009-05-02 15:13:40 +00001170/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001171 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001173static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1174 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001175{
Paul Bakker23986e52011-04-24 08:57:21 +00001176 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001177 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001178 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
Paul Bakker320a4b52009-03-28 18:52:39 +00001180 /*
1181 * Check for valid input
1182 */
1183 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001184 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001185
Paul Bakker6e339b52013-07-03 13:37:05 +02001186 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001187
1188 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001189 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001190
1191 memcpy( p, buf, buflen );
1192
1193 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
1195 crt->raw.p = p;
1196 crt->raw.len = len;
1197 end = p + len;
1198
1199 /*
1200 * Certificate ::= SEQUENCE {
1201 * tbsCertificate TBSCertificate,
1202 * signatureAlgorithm AlgorithmIdentifier,
1203 * signatureValue BIT STRING }
1204 */
1205 if( ( ret = asn1_get_tag( &p, end, &len,
1206 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1207 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001208 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001209 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
1211
Paul Bakkerb00ca422012-09-25 12:10:00 +00001212 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001214 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001215 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001216 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001218 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001219
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 /*
1221 * TBSCertificate ::= SEQUENCE {
1222 */
1223 crt->tbs.p = p;
1224
1225 if( ( ret = asn1_get_tag( &p, end, &len,
1226 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1227 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001228 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001229 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
1231
1232 end = p + len;
1233 crt->tbs.len = end - crt->tbs.p;
1234
1235 /*
1236 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1237 *
1238 * CertificateSerialNumber ::= INTEGER
1239 *
1240 * signature AlgorithmIdentifier
1241 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001242 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1243 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1244 ( ret = x509_get_alg( &p, end, &crt->sig_oid1, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001246 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 return( ret );
1248 }
1249
1250 crt->version++;
1251
1252 if( crt->version > 3 )
1253 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001254 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001255 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 }
1257
Paul Bakkerc70b9822013-04-07 22:00:46 +02001258 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1259 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001261 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001262 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 }
1264
1265 /*
1266 * issuer Name
1267 */
1268 crt->issuer_raw.p = p;
1269
1270 if( ( ret = asn1_get_tag( &p, end, &len,
1271 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1272 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001273 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001274 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 }
1276
1277 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1278 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001279 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 return( ret );
1281 }
1282
1283 crt->issuer_raw.len = p - crt->issuer_raw.p;
1284
1285 /*
1286 * Validity ::= SEQUENCE {
1287 * notBefore Time,
1288 * notAfter Time }
1289 *
1290 */
1291 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1292 &crt->valid_to ) ) != 0 )
1293 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001294 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 return( ret );
1296 }
1297
1298 /*
1299 * subject Name
1300 */
1301 crt->subject_raw.p = p;
1302
1303 if( ( ret = asn1_get_tag( &p, end, &len,
1304 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1305 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001306 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001307 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 }
1309
Paul Bakkercefb3962012-06-27 11:51:09 +00001310 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 return( ret );
1314 }
1315
1316 crt->subject_raw.len = p - crt->subject_raw.p;
1317
1318 /*
1319 * SubjectPublicKeyInfo ::= SEQUENCE
1320 * algorithm AlgorithmIdentifier,
1321 * subjectPublicKey BIT STRING }
1322 */
1323 if( ( ret = asn1_get_tag( &p, end, &len,
1324 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1325 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001326 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001327 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 }
1329
1330 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1331 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1332 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001333 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 return( ret );
1335 }
1336
1337 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1338 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001339 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 return( ret );
1341 }
1342
1343 crt->rsa.len = mpi_size( &crt->rsa.N );
1344
1345 /*
1346 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1347 * -- If present, version shall be v2 or v3
1348 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1349 * -- If present, version shall be v2 or v3
1350 * extensions [3] EXPLICIT Extensions OPTIONAL
1351 * -- If present, version shall be v3
1352 */
1353 if( crt->version == 2 || crt->version == 3 )
1354 {
1355 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1356 if( ret != 0 )
1357 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001358 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 return( ret );
1360 }
1361 }
1362
1363 if( crt->version == 2 || crt->version == 3 )
1364 {
1365 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1366 if( ret != 0 )
1367 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001368 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 return( ret );
1370 }
1371 }
1372
1373 if( crt->version == 3 )
1374 {
Paul Bakker74111d32011-01-15 16:57:55 +00001375 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 if( ret != 0 )
1377 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001378 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 return( ret );
1380 }
1381 }
1382
1383 if( p != end )
1384 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001385 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001386 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001387 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 }
1389
Paul Bakkerb00ca422012-09-25 12:10:00 +00001390 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
1392 /*
1393 * signatureAlgorithm AlgorithmIdentifier,
1394 * signatureValue BIT STRING
1395 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001396 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001398 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 return( ret );
1400 }
1401
Paul Bakker535e97d2012-08-23 10:49:55 +00001402 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1403 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001405 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001406 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 }
1408
1409 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1410 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001411 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 return( ret );
1413 }
1414
1415 if( p != end )
1416 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001417 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001418 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001419 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 }
1421
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001422 return( 0 );
1423}
1424
1425/*
Paul Bakker42c65812013-06-24 19:21:59 +02001426 * Parse one X.509 certificate in DER format from a buffer and add them to a
1427 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001428 */
Paul Bakker42c65812013-06-24 19:21:59 +02001429int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001430{
Paul Bakker42c65812013-06-24 19:21:59 +02001431 int ret;
1432 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001433
1434 /*
1435 * Check for valid input
1436 */
1437 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001438 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001439
1440 while( crt->version != 0 && crt->next != NULL )
1441 {
1442 prev = crt;
1443 crt = crt->next;
1444 }
1445
1446 /*
1447 * Add new certificate on the end of the chain if needed.
1448 */
1449 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001450 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001451 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001452
Paul Bakker7d06ad22009-05-02 15:53:56 +00001453 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001454 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001455
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001456 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001457 crt = crt->next;
1458 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
Paul Bakker42c65812013-06-24 19:21:59 +02001461 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1462 {
1463 if( prev )
1464 prev->next = NULL;
1465
1466 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001467 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001468
1469 return( ret );
1470 }
1471
1472 return( 0 );
1473}
1474
1475/*
1476 * Parse one or more PEM certificates from a buffer and add them to the chained list
1477 */
1478int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1479{
1480 int ret, success = 0, first_error = 0, total_failed = 0;
1481 int buf_format = X509_FORMAT_DER;
1482
1483 /*
1484 * Check for valid input
1485 */
1486 if( chain == NULL || buf == NULL )
1487 return( POLARSSL_ERR_X509_INVALID_INPUT );
1488
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001489 /*
1490 * Determine buffer content. Buffer contains either one DER certificate or
1491 * one or more PEM certificates.
1492 */
1493#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001494 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001495 buf_format = X509_FORMAT_PEM;
1496#endif
1497
1498 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001499 return x509parse_crt_der( chain, buf, buflen );
1500
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001501#if defined(POLARSSL_PEM_C)
1502 if( buf_format == X509_FORMAT_PEM )
1503 {
1504 pem_context pem;
1505
1506 while( buflen > 0 )
1507 {
1508 size_t use_len;
1509 pem_init( &pem );
1510
1511 ret = pem_read_buffer( &pem,
1512 "-----BEGIN CERTIFICATE-----",
1513 "-----END CERTIFICATE-----",
1514 buf, NULL, 0, &use_len );
1515
1516 if( ret == 0 )
1517 {
1518 /*
1519 * Was PEM encoded
1520 */
1521 buflen -= use_len;
1522 buf += use_len;
1523 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001524 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1525 {
1526 return( ret );
1527 }
Paul Bakker00b28602013-06-24 13:02:41 +02001528 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001529 {
1530 pem_free( &pem );
1531
Paul Bakker5ed3b342013-06-24 19:05:46 +02001532 /*
1533 * PEM header and footer were found
1534 */
1535 buflen -= use_len;
1536 buf += use_len;
1537
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001538 if( first_error == 0 )
1539 first_error = ret;
1540
1541 continue;
1542 }
1543 else
1544 break;
1545
Paul Bakker42c65812013-06-24 19:21:59 +02001546 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001547
1548 pem_free( &pem );
1549
1550 if( ret != 0 )
1551 {
1552 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001553 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001554 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001555 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001556 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001557
1558 if( first_error == 0 )
1559 first_error = ret;
1560
Paul Bakker42c65812013-06-24 19:21:59 +02001561 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001562 continue;
1563 }
1564
1565 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001566 }
1567 }
1568#endif
1569
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001570 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001571 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001572 else if( first_error )
1573 return( first_error );
1574 else
1575 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001576}
1577
1578/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001579 * Parse one or more CRLs and add them to the chained list
1580 */
Paul Bakker23986e52011-04-24 08:57:21 +00001581int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001582{
Paul Bakker23986e52011-04-24 08:57:21 +00001583 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001584 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585 unsigned char *p, *end;
1586 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001587#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001588 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001589 pem_context pem;
1590#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001591
1592 crl = chain;
1593
1594 /*
1595 * Check for valid input
1596 */
1597 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001598 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001599
1600 while( crl->version != 0 && crl->next != NULL )
1601 crl = crl->next;
1602
1603 /*
1604 * Add new CRL on the end of the chain if needed.
1605 */
1606 if ( crl->version != 0 && crl->next == NULL)
1607 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001608 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609
Paul Bakker7d06ad22009-05-02 15:53:56 +00001610 if( crl->next == NULL )
1611 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001613 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001614 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001615
Paul Bakker7d06ad22009-05-02 15:53:56 +00001616 crl = crl->next;
1617 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001618 }
1619
Paul Bakker96743fc2011-02-12 14:30:57 +00001620#if defined(POLARSSL_PEM_C)
1621 pem_init( &pem );
1622 ret = pem_read_buffer( &pem,
1623 "-----BEGIN X509 CRL-----",
1624 "-----END X509 CRL-----",
1625 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001626
Paul Bakker96743fc2011-02-12 14:30:57 +00001627 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001628 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001629 /*
1630 * Was PEM encoded
1631 */
1632 buflen -= use_len;
1633 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634
1635 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001636 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001637 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001638 p = pem.buf;
1639 pem.buf = NULL;
1640 len = pem.buflen;
1641 pem_free( &pem );
1642 }
Paul Bakker00b28602013-06-24 13:02:41 +02001643 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001644 {
1645 pem_free( &pem );
1646 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001647 }
1648 else
1649 {
1650 /*
1651 * nope, copy the raw DER data
1652 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001653 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001654
1655 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001656 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001657
1658 memcpy( p, buf, buflen );
1659
1660 buflen = 0;
1661 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001662#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001663 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001664
1665 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001666 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001667
1668 memcpy( p, buf, buflen );
1669
1670 buflen = 0;
1671#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672
1673 crl->raw.p = p;
1674 crl->raw.len = len;
1675 end = p + len;
1676
1677 /*
1678 * CertificateList ::= SEQUENCE {
1679 * tbsCertList TBSCertList,
1680 * signatureAlgorithm AlgorithmIdentifier,
1681 * signatureValue BIT STRING }
1682 */
1683 if( ( ret = asn1_get_tag( &p, end, &len,
1684 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1685 {
1686 x509_crl_free( crl );
1687 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1688 }
1689
Paul Bakker23986e52011-04-24 08:57:21 +00001690 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 {
1692 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001693 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001694 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1695 }
1696
1697 /*
1698 * TBSCertList ::= SEQUENCE {
1699 */
1700 crl->tbs.p = p;
1701
1702 if( ( ret = asn1_get_tag( &p, end, &len,
1703 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1704 {
1705 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001706 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001707 }
1708
1709 end = p + len;
1710 crl->tbs.len = end - crl->tbs.p;
1711
1712 /*
1713 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1714 * -- if present, MUST be v2
1715 *
1716 * signature AlgorithmIdentifier
1717 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001718 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001719 ( ret = x509_get_alg( &p, end, &crl->sig_oid1, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001720 {
1721 x509_crl_free( crl );
1722 return( ret );
1723 }
1724
1725 crl->version++;
1726
1727 if( crl->version > 2 )
1728 {
1729 x509_crl_free( crl );
1730 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1731 }
1732
Paul Bakkerc70b9822013-04-07 22:00:46 +02001733 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1734 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 {
1736 x509_crl_free( crl );
1737 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1738 }
1739
1740 /*
1741 * issuer Name
1742 */
1743 crl->issuer_raw.p = p;
1744
1745 if( ( ret = asn1_get_tag( &p, end, &len,
1746 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1747 {
1748 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001749 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001750 }
1751
1752 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1753 {
1754 x509_crl_free( crl );
1755 return( ret );
1756 }
1757
1758 crl->issuer_raw.len = p - crl->issuer_raw.p;
1759
1760 /*
1761 * thisUpdate Time
1762 * nextUpdate Time OPTIONAL
1763 */
Paul Bakker91200182010-02-18 21:26:15 +00001764 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001765 {
1766 x509_crl_free( crl );
1767 return( ret );
1768 }
1769
Paul Bakker91200182010-02-18 21:26:15 +00001770 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001771 {
Paul Bakker9d781402011-05-09 16:17:09 +00001772 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001773 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001774 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001775 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001776 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001777 x509_crl_free( crl );
1778 return( ret );
1779 }
1780 }
1781
1782 /*
1783 * revokedCertificates SEQUENCE OF SEQUENCE {
1784 * userCertificate CertificateSerialNumber,
1785 * revocationDate Time,
1786 * crlEntryExtensions Extensions OPTIONAL
1787 * -- if present, MUST be v2
1788 * } OPTIONAL
1789 */
1790 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1791 {
1792 x509_crl_free( crl );
1793 return( ret );
1794 }
1795
1796 /*
1797 * crlExtensions EXPLICIT Extensions OPTIONAL
1798 * -- if present, MUST be v2
1799 */
1800 if( crl->version == 2 )
1801 {
1802 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1803
1804 if( ret != 0 )
1805 {
1806 x509_crl_free( crl );
1807 return( ret );
1808 }
1809 }
1810
1811 if( p != end )
1812 {
1813 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001814 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1816 }
1817
1818 end = crl->raw.p + crl->raw.len;
1819
1820 /*
1821 * signatureAlgorithm AlgorithmIdentifier,
1822 * signatureValue BIT STRING
1823 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001824 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001825 {
1826 x509_crl_free( crl );
1827 return( ret );
1828 }
1829
Paul Bakker535e97d2012-08-23 10:49:55 +00001830 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1831 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001832 {
1833 x509_crl_free( crl );
1834 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1835 }
1836
1837 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1838 {
1839 x509_crl_free( crl );
1840 return( ret );
1841 }
1842
1843 if( p != end )
1844 {
1845 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001846 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001847 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1848 }
1849
1850 if( buflen > 0 )
1851 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001852 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001853
Paul Bakker7d06ad22009-05-02 15:53:56 +00001854 if( crl->next == NULL )
1855 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001856 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001857 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001858 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001859
Paul Bakker7d06ad22009-05-02 15:53:56 +00001860 crl = crl->next;
1861 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001862
1863 return( x509parse_crl( crl, buf, buflen ) );
1864 }
1865
1866 return( 0 );
1867}
1868
Paul Bakker335db3f2011-04-25 15:28:35 +00001869#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001870/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001871 * Load all data from a file into a given buffer.
1872 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001873static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001874{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001875 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001876
Paul Bakkerd98030e2009-05-02 15:13:40 +00001877 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001878 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001879
Paul Bakkerd98030e2009-05-02 15:13:40 +00001880 fseek( f, 0, SEEK_END );
1881 *n = (size_t) ftell( f );
1882 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001883
Paul Bakker6e339b52013-07-03 13:37:05 +02001884 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001885 {
1886 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001887 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001888 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001889
Paul Bakkerd98030e2009-05-02 15:13:40 +00001890 if( fread( *buf, 1, *n, f ) != *n )
1891 {
1892 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001893 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001894 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001895 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001896
Paul Bakkerd98030e2009-05-02 15:13:40 +00001897 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001898
Paul Bakkerd98030e2009-05-02 15:13:40 +00001899 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001900
Paul Bakkerd98030e2009-05-02 15:13:40 +00001901 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001902}
1903
1904/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 * Load one or more certificates and add them to the chained list
1906 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001907int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001908{
1909 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 size_t n;
1911 unsigned char *buf;
1912
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001913 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001914 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Paul Bakker69e095c2011-12-10 21:55:01 +00001916 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
1918 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001919 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
1921 return( ret );
1922}
1923
Paul Bakker8d914582012-06-04 12:46:42 +00001924int x509parse_crtpath( x509_cert *chain, const char *path )
1925{
1926 int ret = 0;
1927#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001928 int w_ret;
1929 WCHAR szDir[MAX_PATH];
1930 char filename[MAX_PATH];
1931 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001932 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001933
Paul Bakker97872ac2012-11-02 12:53:26 +00001934 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001935 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001936
1937 if( len > MAX_PATH - 3 )
1938 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001939
Paul Bakker3338b792012-10-01 21:13:10 +00001940 memset( szDir, 0, sizeof(szDir) );
1941 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001942 memcpy( filename, path, len );
1943 filename[len++] = '\\';
1944 p = filename + len;
1945 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001946
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001947 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001948
Paul Bakker97872ac2012-11-02 12:53:26 +00001949 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001950 if (hFind == INVALID_HANDLE_VALUE)
1951 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1952
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001953 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001954 do
1955 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001956 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001957
Paul Bakkere4791f32012-06-04 21:29:15 +00001958 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001959 continue;
1960
Paul Bakker3338b792012-10-01 21:13:10 +00001961 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1962 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001963 p, len - 1,
1964 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001965
Paul Bakker3338b792012-10-01 21:13:10 +00001966 w_ret = x509parse_crtfile( chain, filename );
1967 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001968 ret++;
1969 else
1970 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001971 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001972 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001973
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001974 if (GetLastError() != ERROR_NO_MORE_FILES)
1975 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001976
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001977cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001978 FindClose( hFind );
1979#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001980 int t_ret, i;
1981 struct stat sb;
1982 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001983 char entry_name[255];
1984 DIR *dir = opendir( path );
1985
1986 if( dir == NULL)
1987 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1988
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001989 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001990 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001991 if( result == NULL )
1992 break;
1993
1994 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1995
1996 i = stat( entry_name, &sb );
1997
1998 if( i == -1 )
1999 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2000
2001 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002002 continue;
2003
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002004 // Ignore parse errors
2005 //
Paul Bakker8d914582012-06-04 12:46:42 +00002006 t_ret = x509parse_crtfile( chain, entry_name );
2007 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002008 ret++;
2009 else
2010 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002011 }
2012 closedir( dir );
2013#endif
2014
2015 return( ret );
2016}
2017
Paul Bakkerd98030e2009-05-02 15:13:40 +00002018/*
2019 * Load one or more CRLs and add them to the chained list
2020 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002021int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002022{
2023 int ret;
2024 size_t n;
2025 unsigned char *buf;
2026
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002027 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002028 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002029
Paul Bakker27fdf462011-06-09 13:55:13 +00002030 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002031
2032 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002033 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002034
2035 return( ret );
2036}
2037
Paul Bakker5121ce52009-01-03 21:22:43 +00002038/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002039 * Load and parse a private RSA key
2040 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002041int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002042{
2043 int ret;
2044 size_t n;
2045 unsigned char *buf;
2046
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002047 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002048 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002049
2050 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002051 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002052 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002053 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002054 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002055
2056 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002057 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002058
2059 return( ret );
2060}
2061
2062/*
2063 * Load and parse a public RSA key
2064 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002065int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002066{
2067 int ret;
2068 size_t n;
2069 unsigned char *buf;
2070
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002071 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002072 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002073
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002074 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002075
2076 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002077 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002078
2079 return( ret );
2080}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002081
2082#if defined(POLARSSL_ECP_C)
2083/*
2084 * Load and parse a private EC key
2085 */
2086int x509parse_keyfile_ec( ecp_keypair *eckey,
2087 const char *path, const char *pwd )
2088{
2089 int ret;
2090 size_t n;
2091 unsigned char *buf;
2092
2093 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2094 return( ret );
2095
2096 if( pwd == NULL )
2097 ret = x509parse_key_ec( eckey, buf, n, NULL, 0 );
2098 else
2099 ret = x509parse_key_ec( eckey, buf, n,
2100 (const unsigned char *) pwd, strlen( pwd ) );
2101
2102 memset( buf, 0, n + 1 );
2103 free( buf );
2104
2105 return( ret );
2106}
2107
2108/*
2109 * Load and parse a public EC key
2110 */
2111int x509parse_public_keyfile_ec( ecp_keypair *eckey, const char *path )
2112{
2113 int ret;
2114 size_t n;
2115 unsigned char *buf;
2116
2117 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2118 return( ret );
2119
2120 ret = x509parse_public_key_ec( eckey, buf, n );
2121
2122 memset( buf, 0, n + 1 );
2123 free( buf );
2124
2125 return( ret );
2126}
2127#endif /* defined(POLARSSL_ECP_C) */
Paul Bakker335db3f2011-04-25 15:28:35 +00002128#endif /* POLARSSL_FS_IO */
2129
2130/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002131 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002133static int x509parse_key_pkcs1_der( rsa_context *rsa,
2134 const unsigned char *key,
2135 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002136{
Paul Bakker23986e52011-04-24 08:57:21 +00002137 int ret;
2138 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002140
Paul Bakker96743fc2011-02-12 14:30:57 +00002141 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002142 end = p + keylen;
2143
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002145 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002146 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 * RSAPrivateKey ::= SEQUENCE {
2148 * version Version,
2149 * modulus INTEGER, -- n
2150 * publicExponent INTEGER, -- e
2151 * privateExponent INTEGER, -- d
2152 * prime1 INTEGER, -- p
2153 * prime2 INTEGER, -- q
2154 * exponent1 INTEGER, -- d mod (p-1)
2155 * exponent2 INTEGER, -- d mod (q-1)
2156 * coefficient INTEGER, -- (inverse of q) mod p
2157 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2158 * }
2159 */
2160 if( ( ret = asn1_get_tag( &p, end, &len,
2161 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2162 {
Paul Bakker9d781402011-05-09 16:17:09 +00002163 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 }
2165
2166 end = p + len;
2167
2168 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2169 {
Paul Bakker9d781402011-05-09 16:17:09 +00002170 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 }
2172
2173 if( rsa->ver != 0 )
2174 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002175 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 }
2177
2178 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2179 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2180 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2181 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2182 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2183 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2184 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2185 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2186 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002188 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 }
2190
2191 rsa->len = mpi_size( &rsa->N );
2192
2193 if( p != end )
2194 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002196 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002197 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
2199
2200 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2201 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 rsa_free( rsa );
2203 return( ret );
2204 }
2205
Paul Bakkere2f50402013-06-24 19:00:59 +02002206 return( 0 );
2207}
2208
2209/*
2210 * Parse an unencrypted PKCS#8 encoded private RSA key
2211 */
2212static int x509parse_key_pkcs8_unencrypted_der(
2213 rsa_context *rsa,
2214 const unsigned char *key,
2215 size_t keylen )
2216{
2217 int ret;
2218 size_t len;
2219 unsigned char *p, *end;
2220 x509_buf pk_alg_oid;
2221 pk_type_t pk_alg = POLARSSL_PK_NONE;
2222
2223 p = (unsigned char *) key;
2224 end = p + keylen;
2225
2226 /*
2227 * This function parses the PrivatKeyInfo object (PKCS#8)
2228 *
2229 * PrivateKeyInfo ::= SEQUENCE {
2230 * version Version,
2231 * algorithm AlgorithmIdentifier,
2232 * PrivateKey BIT STRING
2233 * }
2234 *
2235 * AlgorithmIdentifier ::= SEQUENCE {
2236 * algorithm OBJECT IDENTIFIER,
2237 * parameters ANY DEFINED BY algorithm OPTIONAL
2238 * }
2239 *
2240 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2241 */
2242 if( ( ret = asn1_get_tag( &p, end, &len,
2243 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2244 {
2245 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2246 }
2247
2248 end = p + len;
2249
2250 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2251 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2252
2253 if( rsa->ver != 0 )
2254 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2255
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002256 if( ( ret = asn1_get_alg_null( &p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002257 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2258
2259 /*
2260 * only RSA keys handled at this time
2261 */
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +02002262 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 ||
2263 pk_alg != POLARSSL_PK_RSA )
2264 {
Paul Bakkere2f50402013-06-24 19:00:59 +02002265 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +02002266 }
Paul Bakkere2f50402013-06-24 19:00:59 +02002267
2268 /*
2269 * Get the OCTET STRING and parse the PKCS#1 format inside
2270 */
2271 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2272 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2273
2274 if( ( end - p ) < 1 )
2275 {
2276 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2277 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2278 }
2279
2280 end = p + len;
2281
2282 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2283 return( ret );
2284
2285 return( 0 );
2286}
2287
2288/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002289 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002290 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002291static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2292 size_t *used_len,
2293 const unsigned char *key, size_t keylen,
2294 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002295{
2296 int ret;
2297 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002298 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002299 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002300#if defined(POLARSSL_PKCS12_C)
2301 cipher_type_t cipher_alg;
2302 md_type_t md_alg;
2303#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002304
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002305 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002306
2307 p = (unsigned char *) key;
2308 end = p + keylen;
2309
Paul Bakker28144de2013-06-24 19:28:55 +02002310 if( pwdlen == 0 )
2311 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2312
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002313 /*
2314 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2315 *
2316 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2317 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2318 * encryptedData EncryptedData
2319 * }
2320 *
2321 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2322 *
2323 * EncryptedData ::= OCTET STRING
2324 *
2325 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2326 */
2327 if( ( ret = asn1_get_tag( &p, end, &len,
2328 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2329 {
2330 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2331 }
2332
2333 end = p + len;
2334
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002335 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002336 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002337
2338 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2339 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2340
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002341 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002342 return( POLARSSL_ERR_X509_INVALID_INPUT );
2343
2344 /*
2345 * Decrypt EncryptedData with appropriate PDE
2346 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002347#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002348 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002349 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002350 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002351 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002352 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002353 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002354 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2355 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2356
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002357 return( ret );
2358 }
2359 }
2360 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2361 {
2362 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2363 PKCS12_PBE_DECRYPT,
2364 pwd, pwdlen,
2365 p, len, buf ) ) != 0 )
2366 {
2367 return( ret );
2368 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002369
2370 // Best guess for password mismatch when using RC4. If first tag is
2371 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2372 //
2373 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2374 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002375 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002376 else
2377#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002378#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002379 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002380 {
2381 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2382 p, len, buf ) ) != 0 )
2383 {
2384 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2385 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2386
2387 return( ret );
2388 }
2389 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002390 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002391#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002392 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2393
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002394 *used_len = len;
2395 return( 0 );
2396}
2397
2398/*
2399 * Parse an encrypted PKCS#8 encoded private RSA key
2400 */
2401static int x509parse_key_pkcs8_encrypted_der(
2402 rsa_context *rsa,
2403 const unsigned char *key, size_t keylen,
2404 const unsigned char *pwd, size_t pwdlen )
2405{
2406 int ret;
2407 unsigned char buf[2048];
2408 size_t len = 0;
2409
2410 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2411 key, keylen, pwd, pwdlen ) ) != 0 )
2412 {
2413 return( ret );
2414 }
2415
2416 return( x509parse_key_pkcs8_unencrypted_der( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002417}
2418
2419/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002420 * Parse a private RSA key
2421 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002422int x509parse_key_rsa( rsa_context *rsa,
2423 const unsigned char *key, size_t keylen,
2424 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002425{
2426 int ret;
2427
Paul Bakker96743fc2011-02-12 14:30:57 +00002428#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002429 size_t len;
2430 pem_context pem;
2431
2432 pem_init( &pem );
2433 ret = pem_read_buffer( &pem,
2434 "-----BEGIN RSA PRIVATE KEY-----",
2435 "-----END RSA PRIVATE KEY-----",
2436 key, pwd, pwdlen, &len );
2437 if( ret == 0 )
2438 {
2439 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2440 {
2441 rsa_free( rsa );
2442 }
2443
2444 pem_free( &pem );
2445 return( ret );
2446 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002447 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2448 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2449 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2450 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002451 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002452 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002453
2454 ret = pem_read_buffer( &pem,
2455 "-----BEGIN PRIVATE KEY-----",
2456 "-----END PRIVATE KEY-----",
2457 key, NULL, 0, &len );
2458 if( ret == 0 )
2459 {
2460 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2461 pem.buf, pem.buflen ) ) != 0 )
2462 {
2463 rsa_free( rsa );
2464 }
2465
2466 pem_free( &pem );
2467 return( ret );
2468 }
2469 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002470 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002471
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002472 ret = pem_read_buffer( &pem,
2473 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2474 "-----END ENCRYPTED PRIVATE KEY-----",
2475 key, NULL, 0, &len );
2476 if( ret == 0 )
2477 {
2478 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2479 pem.buf, pem.buflen,
2480 pwd, pwdlen ) ) != 0 )
2481 {
2482 rsa_free( rsa );
2483 }
2484
2485 pem_free( &pem );
2486 return( ret );
2487 }
2488 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002489 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002490#else
2491 ((void) pwd);
2492 ((void) pwdlen);
2493#endif /* POLARSSL_PEM_C */
2494
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002495 /*
2496 * At this point we only know it's not a PEM formatted key. Could be any
2497 * of the known DER encoded private key formats
2498 *
2499 * We try the different DER format parsers to see if one passes without
2500 * error
2501 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002502 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2503 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002504 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002505 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002508 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002509
2510 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2511 {
2512 return( ret );
2513 }
2514
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002515 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2516 return( 0 );
2517
2518 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002519
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002520 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2521 return( 0 );
2522
2523 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002524
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002525 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526}
2527
2528/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002529 * Parse a public RSA key
2530 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002531int x509parse_public_key_rsa( rsa_context *rsa,
2532 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002533{
Paul Bakker23986e52011-04-24 08:57:21 +00002534 int ret;
2535 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002536 unsigned char *p, *end;
2537 x509_buf alg_oid;
2538#if defined(POLARSSL_PEM_C)
2539 pem_context pem;
2540
2541 pem_init( &pem );
2542 ret = pem_read_buffer( &pem,
2543 "-----BEGIN PUBLIC KEY-----",
2544 "-----END PUBLIC KEY-----",
2545 key, NULL, 0, &len );
2546
2547 if( ret == 0 )
2548 {
2549 /*
2550 * Was PEM encoded
2551 */
2552 keylen = pem.buflen;
2553 }
Paul Bakker00b28602013-06-24 13:02:41 +02002554 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002555 {
2556 pem_free( &pem );
2557 return( ret );
2558 }
2559
2560 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2561#else
2562 p = (unsigned char *) key;
2563#endif
2564 end = p + keylen;
2565
2566 /*
2567 * PublicKeyInfo ::= SEQUENCE {
2568 * algorithm AlgorithmIdentifier,
2569 * PublicKey BIT STRING
2570 * }
2571 *
2572 * AlgorithmIdentifier ::= SEQUENCE {
2573 * algorithm OBJECT IDENTIFIER,
2574 * parameters ANY DEFINED BY algorithm OPTIONAL
2575 * }
2576 *
2577 * RSAPublicKey ::= SEQUENCE {
2578 * modulus INTEGER, -- n
2579 * publicExponent INTEGER -- e
2580 * }
2581 */
2582
2583 if( ( ret = asn1_get_tag( &p, end, &len,
2584 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2585 {
2586#if defined(POLARSSL_PEM_C)
2587 pem_free( &pem );
2588#endif
2589 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002590 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002591 }
2592
2593 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2594 {
2595#if defined(POLARSSL_PEM_C)
2596 pem_free( &pem );
2597#endif
2598 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002599 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002600 }
2601
2602 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2603 {
2604#if defined(POLARSSL_PEM_C)
2605 pem_free( &pem );
2606#endif
2607 rsa_free( rsa );
2608 return( ret );
2609 }
2610
2611 rsa->len = mpi_size( &rsa->N );
2612
2613#if defined(POLARSSL_PEM_C)
2614 pem_free( &pem );
2615#endif
2616
2617 return( 0 );
2618}
2619
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002620#if defined(POLARSSL_ECP_C)
2621/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002622 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002623 */
2624static int x509parse_key_sec1_der( ecp_keypair *eck,
2625 const unsigned char *key,
2626 size_t keylen )
2627{
2628 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002629 int version;
2630 size_t len;
2631 ecp_group_id grp_id;
2632 unsigned char *p = (unsigned char *) key;
2633 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002634
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002635 /*
2636 * RFC 5915, orf SEC1 Appendix C.4
2637 *
2638 * ECPrivateKey ::= SEQUENCE {
2639 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2640 * privateKey OCTET STRING,
2641 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2642 * publicKey [1] BIT STRING OPTIONAL
2643 * }
2644 */
2645 if( ( ret = asn1_get_tag( &p, end, &len,
2646 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2647 {
2648 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2649 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002650
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002651 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002652
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002653 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2654 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002655
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002656 if( version != 1 )
2657 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2658
2659 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2660 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2661
2662 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2663 {
2664 ecp_keypair_free( eck );
2665 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2666 }
2667
2668 p += len;
2669
2670 /*
2671 * Is 'parameters' present?
2672 */
2673 if( ( ret = asn1_get_tag( &p, end, &len,
2674 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2675 {
2676 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2677 return( ret );
2678
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002679 /*
2680 * If we're wrapped in a bigger structure (eg PKCS#8), grp may have been
2681 * defined externally. In this case, make sure both definitions match.
2682 */
2683 if( eck->grp.id != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002684 {
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002685 if( eck->grp.id != grp_id )
2686 {
2687 ecp_keypair_free( eck );
2688 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2689 }
2690 }
2691 else
2692 {
2693 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2694 {
2695 ecp_keypair_free( eck );
2696 return( ret );
2697 }
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002698 }
2699 }
2700 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2701 {
2702 ecp_keypair_free( eck );
2703 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2704 }
2705
2706 /*
2707 * Is 'publickey' present?
2708 */
2709 if( ( ret = asn1_get_tag( &p, end, &len,
2710 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2711 {
2712 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2713 != 0 )
2714 {
2715 ecp_keypair_free( eck );
2716 return( ret );
2717 }
2718
2719 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2720 {
2721 ecp_keypair_free( eck );
2722 return( ret );
2723 }
2724 }
2725 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2726 {
2727 ecp_keypair_free( eck );
2728 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2729 }
2730
2731 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2732 {
2733 ecp_keypair_free( eck );
2734 return( ret );
2735 }
2736
2737 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002738}
2739
2740/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002741 * Parse an unencrypted PKCS#8 encoded private EC key
2742 */
2743static int x509parse_key_pkcs8_unencrypted_der_ec(
2744 ecp_keypair *eck,
2745 const unsigned char* key,
2746 size_t keylen )
2747{
2748 int ret, version;
2749 size_t len;
2750 x509_buf pk_alg_oid;
2751 ecp_group_id grp_id;
2752 const unsigned char *params_end;
2753 unsigned char *p = (unsigned char *) key;
2754 unsigned char *end = p + keylen;
2755 pk_type_t pk_alg = POLARSSL_PK_NONE;
2756
2757 /*
2758 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2759 *
2760 * PrivateKeyInfo ::= SEQUENCE {
2761 * version Version,
2762 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2763 * privateKey PrivateKey,
2764 * attributes [0] IMPLICIT Attributes OPTIONAL }
2765 *
2766 * Version ::= INTEGER
2767 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2768 * PrivateKey ::= OCTET STRING
2769 *
2770 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2771 */
2772
2773 if( ( ret = asn1_get_tag( &p, end, &len,
2774 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2775 {
2776 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2777 }
2778
2779 end = p + len;
2780
2781 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2782 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2783
2784 if( version != 0 )
2785 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2786
2787 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid, &params_end ) ) != 0 )
2788 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2789
2790 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2791 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2792
2793 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
2794 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2795
2796 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2797 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2798
2799 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2800 {
2801 ecp_keypair_free( eck );
2802 return( ret );
2803 }
2804
2805 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2806 {
2807 ecp_keypair_free( eck );
2808 return( ret );
2809 }
2810
2811 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2812 {
2813 ecp_keypair_free( eck );
2814 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2815 }
2816
2817 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2818 {
2819 ecp_keypair_free( eck );
2820 return( ret );
2821 }
2822
2823 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2824 {
2825 ecp_keypair_free( eck );
2826 return( ret );
2827 }
2828
2829 return 0;
2830}
2831
2832/*
2833 * Parse an encrypted PKCS#8 encoded private EC key
2834 */
2835static int x509parse_key_pkcs8_encrypted_der_ec(
2836 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002837 const unsigned char *key, size_t keylen,
2838 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002839{
2840 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002841 unsigned char buf[2048];
2842 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002843
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002844 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2845 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002846 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002847 return( ret );
2848 }
2849
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002850 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002851}
2852
2853/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002854 * Parse a private EC key
2855 */
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002856int x509parse_key_ec( ecp_keypair *eck,
2857 const unsigned char *key, size_t keylen,
2858 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002859{
2860 int ret;
2861
2862#if defined(POLARSSL_PEM_C)
2863 size_t len;
2864 pem_context pem;
2865
2866 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002867 ret = pem_read_buffer( &pem,
2868 "-----BEGIN EC PRIVATE KEY-----",
2869 "-----END EC PRIVATE KEY-----",
2870 key, pwd, pwdlen, &len );
2871 if( ret == 0 )
2872 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002873 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002874 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002875 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002876 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002877
2878 pem_free( &pem );
2879 return( ret );
2880 }
2881 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2882 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2883 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2884 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2885 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2886 return( ret );
2887
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002888 ret = pem_read_buffer( &pem,
2889 "-----BEGIN PRIVATE KEY-----",
2890 "-----END PRIVATE KEY-----",
2891 key, NULL, 0, &len );
2892 if( ret == 0 )
2893 {
2894 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2895 pem.buf, pem.buflen ) ) != 0 )
2896 {
2897 ecp_keypair_free( eck );
2898 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002899
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002900 pem_free( &pem );
2901 return( ret );
2902 }
2903 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2904 return( ret );
2905
2906 ret = pem_read_buffer( &pem,
2907 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2908 "-----END ENCRYPTED PRIVATE KEY-----",
2909 key, NULL, 0, &len );
2910 if( ret == 0 )
2911 {
2912 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2913 pem.buf, pem.buflen,
2914 pwd, pwdlen ) ) != 0 )
2915 {
2916 ecp_keypair_free( eck );
2917 }
2918
2919 pem_free( &pem );
2920 return( ret );
2921 }
2922 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2923 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002924#else
2925 ((void) pwd);
2926 ((void) pwdlen);
2927#endif /* POLARSSL_PEM_C */
2928
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002929 /*
2930 * At this point we only know it's not a PEM formatted key. Could be any
2931 * of the known DER encoded private key formats
2932 *
2933 * We try the different DER format parsers to see if one passes without
2934 * error
2935 */
2936 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2937 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002938 {
2939 return( 0 );
2940 }
2941
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002942 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002943
2944 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2945 {
2946 return( ret );
2947 }
2948
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002949 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2950 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002951 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002952
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002953 ecp_keypair_free( eck );
2954
2955 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2956 return( 0 );
2957
2958 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002959
2960 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2961}
2962
2963/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002964 * Parse a public EC key in RFC 5480 format, der-encoded
2965 */
2966static int x509parse_public_key_ec_der( ecp_keypair *key,
2967 const unsigned char *buf, size_t len )
2968{
2969 int ret;
2970 ecp_group_id grp_id;
2971 x509_buf alg_oid;
2972 pk_type_t alg = POLARSSL_PK_NONE;
2973 unsigned char *p = (unsigned char *) buf;
2974 unsigned char *end = p + len;
2975 const unsigned char *params_end;
2976 /*
2977 * SubjectPublicKeyInfo ::= SEQUENCE {
2978 * algorithm AlgorithmIdentifier,
2979 * subjectPublicKey BIT STRING
2980 * }
2981 * -- algorithm parameters are ECParameters
2982 * -- subjectPublicKey is an ECPoint
2983 */
2984 if( ( ret = asn1_get_tag( &p, end, &len,
2985 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2986 {
2987 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
2988 }
2989
2990 if( ( ret = x509_get_alg( &p, end, &alg_oid, &params_end ) ) != 0 )
2991 return( ret );
2992
2993 if( oid_get_pk_alg( &alg_oid, &alg ) != 0 )
2994 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2995
2996 if( alg != POLARSSL_PK_ECKEY && alg != POLARSSL_PK_ECKEY_DH )
2997 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2998
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002999 if( alg == POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003000 key->alg = POLARSSL_ECP_KEY_ALG_ECDH;
3001
3002 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
3003 return( ret );
3004
3005 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
3006 return( ret );
3007
3008 if( ( ret = x509_get_subpubkey_ec( &p, end, &key->grp, &key->Q ) ) != 0 )
3009 {
3010 return( ret );
3011 }
3012
3013 return( 0 );
3014}
3015
3016/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003017 * Parse a public EC key
3018 */
3019int x509parse_public_key_ec( ecp_keypair *eckey,
3020 const unsigned char *key, size_t keylen )
3021{
3022 int ret;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003023#if defined(POLARSSL_PEM_C)
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003024 size_t len;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003025 pem_context pem;
3026
3027 pem_init( &pem );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003028 ret = pem_read_buffer( &pem,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003029 "-----BEGIN PUBLIC KEY-----",
3030 "-----END PUBLIC KEY-----",
3031 key, NULL, 0, &len );
3032
3033 if( ret == 0 )
3034 {
3035 /*
3036 * Was PEM encoded
3037 */
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003038 key = pem.buf;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003039 keylen = pem.buflen;
3040 }
3041 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3042 {
3043 pem_free( &pem );
3044 return( ret );
3045 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003046#endif
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003047
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003048 if( ( ret = x509parse_public_key_ec_der ( eckey, key, keylen ) ) != 0 ||
3049 ( ret = ecp_check_pubkey( &eckey->grp, &eckey->Q ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003050 {
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003051 ecp_keypair_free( eckey );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003052 }
3053
3054#if defined(POLARSSL_PEM_C)
3055 pem_free( &pem );
3056#endif
3057
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003058 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003059}
3060#endif /* defined(POLARSSL_ECP_C) */
3061
Paul Bakkereaa89f82011-04-04 21:36:15 +00003062#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003063/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003064 * Parse DHM parameters
3065 */
Paul Bakker23986e52011-04-24 08:57:21 +00003066int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003067{
Paul Bakker23986e52011-04-24 08:57:21 +00003068 int ret;
3069 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003070 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003071#if defined(POLARSSL_PEM_C)
3072 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003073
Paul Bakker96743fc2011-02-12 14:30:57 +00003074 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003075
Paul Bakker96743fc2011-02-12 14:30:57 +00003076 ret = pem_read_buffer( &pem,
3077 "-----BEGIN DH PARAMETERS-----",
3078 "-----END DH PARAMETERS-----",
3079 dhmin, NULL, 0, &dhminlen );
3080
3081 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003082 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003083 /*
3084 * Was PEM encoded
3085 */
3086 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003087 }
Paul Bakker00b28602013-06-24 13:02:41 +02003088 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003089 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003090 pem_free( &pem );
3091 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003092 }
3093
Paul Bakker96743fc2011-02-12 14:30:57 +00003094 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3095#else
3096 p = (unsigned char *) dhmin;
3097#endif
3098 end = p + dhminlen;
3099
Paul Bakker1b57b062011-01-06 15:48:19 +00003100 memset( dhm, 0, sizeof( dhm_context ) );
3101
Paul Bakker1b57b062011-01-06 15:48:19 +00003102 /*
3103 * DHParams ::= SEQUENCE {
3104 * prime INTEGER, -- P
3105 * generator INTEGER, -- g
3106 * }
3107 */
3108 if( ( ret = asn1_get_tag( &p, end, &len,
3109 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3110 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003111#if defined(POLARSSL_PEM_C)
3112 pem_free( &pem );
3113#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003114 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003115 }
3116
3117 end = p + len;
3118
3119 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3120 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3121 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003122#if defined(POLARSSL_PEM_C)
3123 pem_free( &pem );
3124#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003125 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003126 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003127 }
3128
3129 if( p != end )
3130 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003131#if defined(POLARSSL_PEM_C)
3132 pem_free( &pem );
3133#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003134 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003135 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003136 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3137 }
3138
Paul Bakker96743fc2011-02-12 14:30:57 +00003139#if defined(POLARSSL_PEM_C)
3140 pem_free( &pem );
3141#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003142
3143 return( 0 );
3144}
3145
Paul Bakker335db3f2011-04-25 15:28:35 +00003146#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003147/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003148 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003149 */
3150int x509parse_dhmfile( dhm_context *dhm, const char *path )
3151{
3152 int ret;
3153 size_t n;
3154 unsigned char *buf;
3155
Paul Bakker69e095c2011-12-10 21:55:01 +00003156 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3157 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003158
Paul Bakker27fdf462011-06-09 13:55:13 +00003159 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003160
3161 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003162 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003163
3164 return( ret );
3165}
Paul Bakker335db3f2011-04-25 15:28:35 +00003166#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003167#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003168
Paul Bakker5121ce52009-01-03 21:22:43 +00003169#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003170#include <stdarg.h>
3171
3172#if !defined vsnprintf
3173#define vsnprintf _vsnprintf
3174#endif // vsnprintf
3175
3176/*
3177 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3178 * Result value is not size of buffer needed, but -1 if no fit is possible.
3179 *
3180 * This fuction tries to 'fix' this by at least suggesting enlarging the
3181 * size by 20.
3182 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003183static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003184{
3185 va_list ap;
3186 int res = -1;
3187
3188 va_start( ap, format );
3189
3190 res = vsnprintf( str, size, format, ap );
3191
3192 va_end( ap );
3193
3194 // No quick fix possible
3195 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003196 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003197
3198 return res;
3199}
3200
3201#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003202#endif
3203
Paul Bakkerd98030e2009-05-02 15:13:40 +00003204#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3205
3206#define SAFE_SNPRINTF() \
3207{ \
3208 if( ret == -1 ) \
3209 return( -1 ); \
3210 \
Paul Bakker23986e52011-04-24 08:57:21 +00003211 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003212 p[n - 1] = '\0'; \
3213 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3214 } \
3215 \
Paul Bakker23986e52011-04-24 08:57:21 +00003216 n -= (unsigned int) ret; \
3217 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003218}
3219
Paul Bakker5121ce52009-01-03 21:22:43 +00003220/*
3221 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003222 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003224int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003225{
Paul Bakker23986e52011-04-24 08:57:21 +00003226 int ret;
3227 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003229 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003230 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 char s[128], *p;
3232
3233 memset( s, 0, sizeof( s ) );
3234
3235 name = dn;
3236 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003237 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
3239 while( name != NULL )
3240 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003241 if( !name->oid.p )
3242 {
3243 name = name->next;
3244 continue;
3245 }
3246
Paul Bakker74111d32011-01-15 16:57:55 +00003247 if( name != dn )
3248 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003249 ret = snprintf( p, n, ", " );
3250 SAFE_SNPRINTF();
3251 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003252
Paul Bakkerc70b9822013-04-07 22:00:46 +02003253 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254
Paul Bakkerc70b9822013-04-07 22:00:46 +02003255 if( ret == 0 )
3256 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003258 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003259 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003260
3261 for( i = 0; i < name->val.len; i++ )
3262 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003263 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 break;
3265
3266 c = name->val.p[i];
3267 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3268 s[i] = '?';
3269 else s[i] = c;
3270 }
3271 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003272 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003273 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 name = name->next;
3275 }
3276
Paul Bakker23986e52011-04-24 08:57:21 +00003277 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278}
3279
3280/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003281 * Store the serial in printable form into buf; no more
3282 * than size characters will be written
3283 */
3284int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3285{
Paul Bakker23986e52011-04-24 08:57:21 +00003286 int ret;
3287 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003288 char *p;
3289
3290 p = buf;
3291 n = size;
3292
3293 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003294 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003295
3296 for( i = 0; i < nr; i++ )
3297 {
Paul Bakker93048802011-12-05 14:38:06 +00003298 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003299 continue;
3300
Paul Bakkerdd476992011-01-16 21:34:59 +00003301 ret = snprintf( p, n, "%02X%s",
3302 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3303 SAFE_SNPRINTF();
3304 }
3305
Paul Bakker03c7c252011-11-25 12:37:37 +00003306 if( nr != serial->len )
3307 {
3308 ret = snprintf( p, n, "...." );
3309 SAFE_SNPRINTF();
3310 }
3311
Paul Bakker23986e52011-04-24 08:57:21 +00003312 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003313}
3314
3315/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003316 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003318int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3319 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003320{
Paul Bakker23986e52011-04-24 08:57:21 +00003321 int ret;
3322 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003323 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003324 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003325
3326 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003327 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003328
Paul Bakkerd98030e2009-05-02 15:13:40 +00003329 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003331 SAFE_SNPRINTF();
3332 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003334 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Paul Bakkerdd476992011-01-16 21:34:59 +00003336 ret = x509parse_serial_gets( p, n, &crt->serial);
3337 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003338
Paul Bakkerd98030e2009-05-02 15:13:40 +00003339 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3340 SAFE_SNPRINTF();
3341 ret = x509parse_dn_gets( p, n, &crt->issuer );
3342 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003343
Paul Bakkerd98030e2009-05-02 15:13:40 +00003344 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3345 SAFE_SNPRINTF();
3346 ret = x509parse_dn_gets( p, n, &crt->subject );
3347 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003348
Paul Bakkerd98030e2009-05-02 15:13:40 +00003349 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3351 crt->valid_from.year, crt->valid_from.mon,
3352 crt->valid_from.day, crt->valid_from.hour,
3353 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003354 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
Paul Bakkerd98030e2009-05-02 15:13:40 +00003356 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3358 crt->valid_to.year, crt->valid_to.mon,
3359 crt->valid_to.day, crt->valid_to.hour,
3360 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003361 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
Paul Bakkerc70b9822013-04-07 22:00:46 +02003363 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003364 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003365
Paul Bakkerc70b9822013-04-07 22:00:46 +02003366 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3367 if( ret != 0 )
3368 ret = snprintf( p, n, "???" );
3369 else
3370 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003371 SAFE_SNPRINTF();
3372
3373 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003374 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003375 SAFE_SNPRINTF();
3376
Paul Bakker23986e52011-04-24 08:57:21 +00003377 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003378}
3379
Paul Bakker74111d32011-01-15 16:57:55 +00003380/*
3381 * Return an informational string describing the given OID
3382 */
3383const char *x509_oid_get_description( x509_buf *oid )
3384{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003385 const char *desc = NULL;
3386 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003387
Paul Bakkerc70b9822013-04-07 22:00:46 +02003388 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003389
Paul Bakkerc70b9822013-04-07 22:00:46 +02003390 if( ret != 0 )
3391 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003392
Paul Bakkerc70b9822013-04-07 22:00:46 +02003393 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003394}
3395
3396/* Return the x.y.z.... style numeric string for the given OID */
3397int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3398{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003399 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003400}
3401
Paul Bakkerd98030e2009-05-02 15:13:40 +00003402/*
3403 * Return an informational string about the CRL.
3404 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003405int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3406 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003407{
Paul Bakker23986e52011-04-24 08:57:21 +00003408 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003409 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003410 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003411 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003412 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003413
3414 p = buf;
3415 n = size;
3416
3417 ret = snprintf( p, n, "%sCRL version : %d",
3418 prefix, crl->version );
3419 SAFE_SNPRINTF();
3420
3421 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3422 SAFE_SNPRINTF();
3423 ret = x509parse_dn_gets( p, n, &crl->issuer );
3424 SAFE_SNPRINTF();
3425
3426 ret = snprintf( p, n, "\n%sthis update : " \
3427 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3428 crl->this_update.year, crl->this_update.mon,
3429 crl->this_update.day, crl->this_update.hour,
3430 crl->this_update.min, crl->this_update.sec );
3431 SAFE_SNPRINTF();
3432
3433 ret = snprintf( p, n, "\n%snext update : " \
3434 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3435 crl->next_update.year, crl->next_update.mon,
3436 crl->next_update.day, crl->next_update.hour,
3437 crl->next_update.min, crl->next_update.sec );
3438 SAFE_SNPRINTF();
3439
3440 entry = &crl->entry;
3441
3442 ret = snprintf( p, n, "\n%sRevoked certificates:",
3443 prefix );
3444 SAFE_SNPRINTF();
3445
Paul Bakker9be19372009-07-27 20:21:53 +00003446 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003447 {
3448 ret = snprintf( p, n, "\n%sserial number: ",
3449 prefix );
3450 SAFE_SNPRINTF();
3451
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003452 ret = x509parse_serial_gets( p, n, &entry->serial);
3453 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003454
Paul Bakkerd98030e2009-05-02 15:13:40 +00003455 ret = snprintf( p, n, " revocation date: " \
3456 "%04d-%02d-%02d %02d:%02d:%02d",
3457 entry->revocation_date.year, entry->revocation_date.mon,
3458 entry->revocation_date.day, entry->revocation_date.hour,
3459 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003460 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003461
3462 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 }
3464
Paul Bakkerc70b9822013-04-07 22:00:46 +02003465 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003466 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003467
Paul Bakkerc70b9822013-04-07 22:00:46 +02003468 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3469 if( ret != 0 )
3470 ret = snprintf( p, n, "???" );
3471 else
3472 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003473 SAFE_SNPRINTF();
3474
Paul Bakker1e27bb22009-07-19 20:25:25 +00003475 ret = snprintf( p, n, "\n" );
3476 SAFE_SNPRINTF();
3477
Paul Bakker23986e52011-04-24 08:57:21 +00003478 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003479}
3480
3481/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003482 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003484#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003485int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003486{
Paul Bakkercce9d772011-11-18 14:26:47 +00003487 int year, mon, day;
3488 int hour, min, sec;
3489
3490#if defined(_WIN32)
3491 SYSTEMTIME st;
3492
3493 GetLocalTime(&st);
3494
3495 year = st.wYear;
3496 mon = st.wMonth;
3497 day = st.wDay;
3498 hour = st.wHour;
3499 min = st.wMinute;
3500 sec = st.wSecond;
3501#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 struct tm *lt;
3503 time_t tt;
3504
3505 tt = time( NULL );
3506 lt = localtime( &tt );
3507
Paul Bakkercce9d772011-11-18 14:26:47 +00003508 year = lt->tm_year + 1900;
3509 mon = lt->tm_mon + 1;
3510 day = lt->tm_mday;
3511 hour = lt->tm_hour;
3512 min = lt->tm_min;
3513 sec = lt->tm_sec;
3514#endif
3515
3516 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003517 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003518
Paul Bakkercce9d772011-11-18 14:26:47 +00003519 if( year == to->year &&
3520 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003521 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003522
Paul Bakkercce9d772011-11-18 14:26:47 +00003523 if( year == to->year &&
3524 mon == to->mon &&
3525 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003526 return( 1 );
3527
Paul Bakkercce9d772011-11-18 14:26:47 +00003528 if( year == to->year &&
3529 mon == to->mon &&
3530 day == to->day &&
3531 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003532 return( 1 );
3533
Paul Bakkercce9d772011-11-18 14:26:47 +00003534 if( year == to->year &&
3535 mon == to->mon &&
3536 day == to->day &&
3537 hour == to->hour &&
3538 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003539 return( 1 );
3540
Paul Bakkercce9d772011-11-18 14:26:47 +00003541 if( year == to->year &&
3542 mon == to->mon &&
3543 day == to->day &&
3544 hour == to->hour &&
3545 min == to->min &&
3546 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003547 return( 1 );
3548
Paul Bakker40ea7de2009-05-03 10:18:48 +00003549 return( 0 );
3550}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003551#else /* POLARSSL_HAVE_TIME */
3552int x509parse_time_expired( const x509_time *to )
3553{
3554 ((void) to);
3555 return( 0 );
3556}
3557#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003558
3559/*
3560 * Return 1 if the certificate is revoked, or 0 otherwise.
3561 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003562int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003563{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003564 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003565
3566 while( cur != NULL && cur->serial.len != 0 )
3567 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003568 if( crt->serial.len == cur->serial.len &&
3569 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003570 {
3571 if( x509parse_time_expired( &cur->revocation_date ) )
3572 return( 1 );
3573 }
3574
3575 cur = cur->next;
3576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003577
3578 return( 0 );
3579}
3580
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003581/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003582 * Check that the given certificate is valid accoring to the CRL.
3583 */
3584static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3585 x509_crl *crl_list)
3586{
3587 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003588 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3589 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003590
Paul Bakker915275b2012-09-28 07:10:55 +00003591 if( ca == NULL )
3592 return( flags );
3593
Paul Bakker76fd75a2011-01-16 21:12:10 +00003594 /*
3595 * TODO: What happens if no CRL is present?
3596 * Suggestion: Revocation state should be unknown if no CRL is present.
3597 * For backwards compatibility this is not yet implemented.
3598 */
3599
Paul Bakker915275b2012-09-28 07:10:55 +00003600 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003601 {
Paul Bakker915275b2012-09-28 07:10:55 +00003602 if( crl_list->version == 0 ||
3603 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003604 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3605 crl_list->issuer_raw.len ) != 0 )
3606 {
3607 crl_list = crl_list->next;
3608 continue;
3609 }
3610
3611 /*
3612 * Check if CRL is correctly signed by the trusted CA
3613 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003614 md_info = md_info_from_type( crl_list->sig_md );
3615 if( md_info == NULL )
3616 {
3617 /*
3618 * Cannot check 'unknown' hash
3619 */
3620 flags |= BADCRL_NOT_TRUSTED;
3621 break;
3622 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003623
Paul Bakkerc70b9822013-04-07 22:00:46 +02003624 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003625
Paul Bakkerc70b9822013-04-07 22:00:46 +02003626 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003627 0, hash, crl_list->sig.p ) == 0 )
3628 {
3629 /*
3630 * CRL is not trusted
3631 */
3632 flags |= BADCRL_NOT_TRUSTED;
3633 break;
3634 }
3635
3636 /*
3637 * Check for validity of CRL (Do not drop out)
3638 */
3639 if( x509parse_time_expired( &crl_list->next_update ) )
3640 flags |= BADCRL_EXPIRED;
3641
3642 /*
3643 * Check if certificate is revoked
3644 */
3645 if( x509parse_revoked(crt, crl_list) )
3646 {
3647 flags |= BADCERT_REVOKED;
3648 break;
3649 }
3650
3651 crl_list = crl_list->next;
3652 }
3653 return flags;
3654}
3655
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003656static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003657{
3658 size_t i;
3659 size_t cn_idx = 0;
3660
Paul Bakker57b12982012-02-11 17:38:38 +00003661 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003662 return( 0 );
3663
3664 for( i = 0; i < strlen( cn ); ++i )
3665 {
3666 if( cn[i] == '.' )
3667 {
3668 cn_idx = i;
3669 break;
3670 }
3671 }
3672
3673 if( cn_idx == 0 )
3674 return( 0 );
3675
Paul Bakker535e97d2012-08-23 10:49:55 +00003676 if( strlen( cn ) - cn_idx == name->len - 1 &&
3677 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003678 {
3679 return( 1 );
3680 }
3681
3682 return( 0 );
3683}
3684
Paul Bakker915275b2012-09-28 07:10:55 +00003685static int x509parse_verify_top(
3686 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003687 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003688 int (*f_vrfy)(void *, x509_cert *, int, int *),
3689 void *p_vrfy )
3690{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003691 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003692 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003693 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3694 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003695
3696 if( x509parse_time_expired( &child->valid_to ) )
3697 *flags |= BADCERT_EXPIRED;
3698
3699 /*
3700 * Child is the top of the chain. Check against the trust_ca list.
3701 */
3702 *flags |= BADCERT_NOT_TRUSTED;
3703
3704 while( trust_ca != NULL )
3705 {
3706 if( trust_ca->version == 0 ||
3707 child->issuer_raw.len != trust_ca->subject_raw.len ||
3708 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3709 child->issuer_raw.len ) != 0 )
3710 {
3711 trust_ca = trust_ca->next;
3712 continue;
3713 }
3714
Paul Bakker9a736322012-11-14 12:39:52 +00003715 /*
3716 * Reduce path_len to check against if top of the chain is
3717 * the same as the trusted CA
3718 */
3719 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3720 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3721 child->issuer_raw.len ) == 0 )
3722 {
3723 check_path_cnt--;
3724 }
3725
Paul Bakker915275b2012-09-28 07:10:55 +00003726 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003727 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003728 {
3729 trust_ca = trust_ca->next;
3730 continue;
3731 }
3732
Paul Bakkerc70b9822013-04-07 22:00:46 +02003733 md_info = md_info_from_type( child->sig_md );
3734 if( md_info == NULL )
3735 {
3736 /*
3737 * Cannot check 'unknown' hash
3738 */
3739 continue;
3740 }
Paul Bakker915275b2012-09-28 07:10:55 +00003741
Paul Bakkerc70b9822013-04-07 22:00:46 +02003742 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003743
Paul Bakkerc70b9822013-04-07 22:00:46 +02003744 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003745 0, hash, child->sig.p ) != 0 )
3746 {
3747 trust_ca = trust_ca->next;
3748 continue;
3749 }
3750
3751 /*
3752 * Top of chain is signed by a trusted CA
3753 */
3754 *flags &= ~BADCERT_NOT_TRUSTED;
3755 break;
3756 }
3757
Paul Bakker9a736322012-11-14 12:39:52 +00003758 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003759 * If top of chain is not the same as the trusted CA send a verify request
3760 * to the callback for any issues with validity and CRL presence for the
3761 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003762 */
3763 if( trust_ca != NULL &&
3764 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3765 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3766 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003767 {
3768 /* Check trusted CA's CRL for then chain's top crt */
3769 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3770
3771 if( x509parse_time_expired( &trust_ca->valid_to ) )
3772 ca_flags |= BADCERT_EXPIRED;
3773
Paul Bakker915275b2012-09-28 07:10:55 +00003774 if( NULL != f_vrfy )
3775 {
Paul Bakker9a736322012-11-14 12:39:52 +00003776 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003777 return( ret );
3778 }
3779 }
3780
3781 /* Call callback on top cert */
3782 if( NULL != f_vrfy )
3783 {
Paul Bakker9a736322012-11-14 12:39:52 +00003784 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003785 return( ret );
3786 }
3787
Paul Bakker915275b2012-09-28 07:10:55 +00003788 *flags |= ca_flags;
3789
3790 return( 0 );
3791}
3792
3793static int x509parse_verify_child(
3794 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003795 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003796 int (*f_vrfy)(void *, x509_cert *, int, int *),
3797 void *p_vrfy )
3798{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003799 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003800 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003801 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003802 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003803 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003804
3805 if( x509parse_time_expired( &child->valid_to ) )
3806 *flags |= BADCERT_EXPIRED;
3807
Paul Bakkerc70b9822013-04-07 22:00:46 +02003808 md_info = md_info_from_type( child->sig_md );
3809 if( md_info == NULL )
3810 {
3811 /*
3812 * Cannot check 'unknown' hash
3813 */
Paul Bakker915275b2012-09-28 07:10:55 +00003814 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003815 }
3816 else
3817 {
3818 md( md_info, child->tbs.p, child->tbs.len, hash );
3819
3820 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3821 child->sig.p ) != 0 )
3822 *flags |= BADCERT_NOT_TRUSTED;
3823 }
3824
Paul Bakker915275b2012-09-28 07:10:55 +00003825 /* Check trusted CA's CRL for the given crt */
3826 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3827
3828 grandparent = parent->next;
3829
3830 while( grandparent != NULL )
3831 {
3832 if( grandparent->version == 0 ||
3833 grandparent->ca_istrue == 0 ||
3834 parent->issuer_raw.len != grandparent->subject_raw.len ||
3835 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3836 parent->issuer_raw.len ) != 0 )
3837 {
3838 grandparent = grandparent->next;
3839 continue;
3840 }
3841 break;
3842 }
3843
Paul Bakker915275b2012-09-28 07:10:55 +00003844 if( grandparent != NULL )
3845 {
3846 /*
3847 * Part of the chain
3848 */
Paul Bakker9a736322012-11-14 12:39:52 +00003849 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 +00003850 if( ret != 0 )
3851 return( ret );
3852 }
3853 else
3854 {
Paul Bakker9a736322012-11-14 12:39:52 +00003855 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 +00003856 if( ret != 0 )
3857 return( ret );
3858 }
3859
3860 /* child is verified to be a child of the parent, call verify callback */
3861 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003862 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003863 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003864
3865 *flags |= parent_flags;
3866
3867 return( 0 );
3868}
3869
Paul Bakker76fd75a2011-01-16 21:12:10 +00003870/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003871 * Verify the certificate validity
3872 */
3873int x509parse_verify( x509_cert *crt,
3874 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003875 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003876 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003877 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003878 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003879{
Paul Bakker23986e52011-04-24 08:57:21 +00003880 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003881 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003882 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003883 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003885 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003886
Paul Bakker40ea7de2009-05-03 10:18:48 +00003887 *flags = 0;
3888
Paul Bakker5121ce52009-01-03 21:22:43 +00003889 if( cn != NULL )
3890 {
3891 name = &crt->subject;
3892 cn_len = strlen( cn );
3893
Paul Bakker4d2c1242012-05-10 14:12:46 +00003894 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003895 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003896 cur = &crt->subject_alt_names;
3897
3898 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003899 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003900 if( cur->buf.len == cn_len &&
3901 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003902 break;
3903
Paul Bakker535e97d2012-08-23 10:49:55 +00003904 if( cur->buf.len > 2 &&
3905 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003906 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003907 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003908
Paul Bakker4d2c1242012-05-10 14:12:46 +00003909 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003910 }
3911
3912 if( cur == NULL )
3913 *flags |= BADCERT_CN_MISMATCH;
3914 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003915 else
3916 {
3917 while( name != NULL )
3918 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003919 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003920 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003921 if( name->val.len == cn_len &&
3922 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003923 break;
3924
Paul Bakker535e97d2012-08-23 10:49:55 +00003925 if( name->val.len > 2 &&
3926 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003927 x509_wildcard_verify( cn, &name->val ) )
3928 break;
3929 }
3930
3931 name = name->next;
3932 }
3933
3934 if( name == NULL )
3935 *flags |= BADCERT_CN_MISMATCH;
3936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003937 }
3938
Paul Bakker5121ce52009-01-03 21:22:43 +00003939 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003940 * Iterate upwards in the given cert chain, to find our crt parent.
3941 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003943 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003944
Paul Bakker76fd75a2011-01-16 21:12:10 +00003945 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003946 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003947 if( parent->ca_istrue == 0 ||
3948 crt->issuer_raw.len != parent->subject_raw.len ||
3949 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003950 crt->issuer_raw.len ) != 0 )
3951 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003952 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 continue;
3954 }
Paul Bakker915275b2012-09-28 07:10:55 +00003955 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003956 }
3957
Paul Bakker915275b2012-09-28 07:10:55 +00003958 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 {
Paul Bakker915275b2012-09-28 07:10:55 +00003960 /*
3961 * Part of the chain
3962 */
Paul Bakker9a736322012-11-14 12:39:52 +00003963 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003964 if( ret != 0 )
3965 return( ret );
3966 }
3967 else
Paul Bakker74111d32011-01-15 16:57:55 +00003968 {
Paul Bakker9a736322012-11-14 12:39:52 +00003969 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003970 if( ret != 0 )
3971 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003972 }
Paul Bakker915275b2012-09-28 07:10:55 +00003973
3974 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003975 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003976
Paul Bakker5121ce52009-01-03 21:22:43 +00003977 return( 0 );
3978}
3979
3980/*
3981 * Unallocate all certificate data
3982 */
3983void x509_free( x509_cert *crt )
3984{
3985 x509_cert *cert_cur = crt;
3986 x509_cert *cert_prv;
3987 x509_name *name_cur;
3988 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003989 x509_sequence *seq_cur;
3990 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003991
3992 if( crt == NULL )
3993 return;
3994
3995 do
3996 {
3997 rsa_free( &cert_cur->rsa );
3998
3999 name_cur = cert_cur->issuer.next;
4000 while( name_cur != NULL )
4001 {
4002 name_prv = name_cur;
4003 name_cur = name_cur->next;
4004 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004005 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 }
4007
4008 name_cur = cert_cur->subject.next;
4009 while( name_cur != NULL )
4010 {
4011 name_prv = name_cur;
4012 name_cur = name_cur->next;
4013 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004014 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 }
4016
Paul Bakker74111d32011-01-15 16:57:55 +00004017 seq_cur = cert_cur->ext_key_usage.next;
4018 while( seq_cur != NULL )
4019 {
4020 seq_prv = seq_cur;
4021 seq_cur = seq_cur->next;
4022 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004023 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004024 }
4025
Paul Bakker8afa70d2012-02-11 18:42:45 +00004026 seq_cur = cert_cur->subject_alt_names.next;
4027 while( seq_cur != NULL )
4028 {
4029 seq_prv = seq_cur;
4030 seq_cur = seq_cur->next;
4031 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004032 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004033 }
4034
Paul Bakker5121ce52009-01-03 21:22:43 +00004035 if( cert_cur->raw.p != NULL )
4036 {
4037 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004038 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004039 }
4040
4041 cert_cur = cert_cur->next;
4042 }
4043 while( cert_cur != NULL );
4044
4045 cert_cur = crt;
4046 do
4047 {
4048 cert_prv = cert_cur;
4049 cert_cur = cert_cur->next;
4050
4051 memset( cert_prv, 0, sizeof( x509_cert ) );
4052 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004053 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004054 }
4055 while( cert_cur != NULL );
4056}
4057
Paul Bakkerd98030e2009-05-02 15:13:40 +00004058/*
4059 * Unallocate all CRL data
4060 */
4061void x509_crl_free( x509_crl *crl )
4062{
4063 x509_crl *crl_cur = crl;
4064 x509_crl *crl_prv;
4065 x509_name *name_cur;
4066 x509_name *name_prv;
4067 x509_crl_entry *entry_cur;
4068 x509_crl_entry *entry_prv;
4069
4070 if( crl == NULL )
4071 return;
4072
4073 do
4074 {
4075 name_cur = crl_cur->issuer.next;
4076 while( name_cur != NULL )
4077 {
4078 name_prv = name_cur;
4079 name_cur = name_cur->next;
4080 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004081 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004082 }
4083
4084 entry_cur = crl_cur->entry.next;
4085 while( entry_cur != NULL )
4086 {
4087 entry_prv = entry_cur;
4088 entry_cur = entry_cur->next;
4089 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004090 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004091 }
4092
4093 if( crl_cur->raw.p != NULL )
4094 {
4095 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004096 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004097 }
4098
4099 crl_cur = crl_cur->next;
4100 }
4101 while( crl_cur != NULL );
4102
4103 crl_cur = crl;
4104 do
4105 {
4106 crl_prv = crl_cur;
4107 crl_cur = crl_cur->next;
4108
4109 memset( crl_prv, 0, sizeof( x509_crl ) );
4110 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004111 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004112 }
4113 while( crl_cur != NULL );
4114}
4115
Paul Bakker40e46942009-01-03 21:51:57 +00004116#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004117
Paul Bakker40e46942009-01-03 21:51:57 +00004118#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004119
4120/*
4121 * Checkup routine
4122 */
4123int x509_self_test( int verbose )
4124{
Paul Bakker5690efc2011-05-26 13:16:06 +00004125#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004126 int ret;
4127 int flags;
4128 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004129 x509_cert cacert;
4130 x509_cert clicert;
4131 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004132#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004133 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004134#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004135
4136 if( verbose != 0 )
4137 printf( " X.509 certificate load: " );
4138
4139 memset( &clicert, 0, sizeof( x509_cert ) );
4140
Paul Bakker3c2122f2013-06-24 19:03:14 +02004141 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004142 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004143 if( ret != 0 )
4144 {
4145 if( verbose != 0 )
4146 printf( "failed\n" );
4147
4148 return( ret );
4149 }
4150
4151 memset( &cacert, 0, sizeof( x509_cert ) );
4152
Paul Bakker3c2122f2013-06-24 19:03:14 +02004153 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004154 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004155 if( ret != 0 )
4156 {
4157 if( verbose != 0 )
4158 printf( "failed\n" );
4159
4160 return( ret );
4161 }
4162
4163 if( verbose != 0 )
4164 printf( "passed\n X.509 private key load: " );
4165
4166 i = strlen( test_ca_key );
4167 j = strlen( test_ca_pwd );
4168
Paul Bakker66b78b22011-03-25 14:22:50 +00004169 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4170
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004171 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004172 (const unsigned char *) test_ca_key, i,
4173 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004174 {
4175 if( verbose != 0 )
4176 printf( "failed\n" );
4177
4178 return( ret );
4179 }
4180
4181 if( verbose != 0 )
4182 printf( "passed\n X.509 signature verify: ");
4183
Paul Bakker23986e52011-04-24 08:57:21 +00004184 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004185 if( ret != 0 )
4186 {
Paul Bakker23986e52011-04-24 08:57:21 +00004187 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004188 if( verbose != 0 )
4189 printf( "failed\n" );
4190
4191 return( ret );
4192 }
4193
Paul Bakker5690efc2011-05-26 13:16:06 +00004194#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004196 printf( "passed\n X.509 DHM parameter load: " );
4197
4198 i = strlen( test_dhm_params );
4199 j = strlen( test_ca_pwd );
4200
Paul Bakker3c2122f2013-06-24 19:03:14 +02004201 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004202 {
4203 if( verbose != 0 )
4204 printf( "failed\n" );
4205
4206 return( ret );
4207 }
4208
4209 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004210 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004212
4213 x509_free( &cacert );
4214 x509_free( &clicert );
4215 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004216#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004217 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004218#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004219
4220 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004221#else
4222 ((void) verbose);
4223 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4224#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004225}
4226
4227#endif
4228
4229#endif