blob: a2ab08500880ec7f582f9db16f37e7da128f5625 [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
2679 /* TODO: grp may not be empty at this point,
2680 * if we're wrapped inside a PKCS#8 structure: check consistency */
2681 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2682 {
2683 ecp_keypair_free( eck );
2684 return( ret );
2685 }
2686 }
2687 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2688 {
2689 ecp_keypair_free( eck );
2690 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2691 }
2692
2693 /*
2694 * Is 'publickey' present?
2695 */
2696 if( ( ret = asn1_get_tag( &p, end, &len,
2697 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2698 {
2699 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2700 != 0 )
2701 {
2702 ecp_keypair_free( eck );
2703 return( ret );
2704 }
2705
2706 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2707 {
2708 ecp_keypair_free( eck );
2709 return( ret );
2710 }
2711 }
2712 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2713 {
2714 ecp_keypair_free( eck );
2715 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2716 }
2717
2718 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2719 {
2720 ecp_keypair_free( eck );
2721 return( ret );
2722 }
2723
2724 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002725}
2726
2727/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002728 * Parse an unencrypted PKCS#8 encoded private EC key
2729 */
2730static int x509parse_key_pkcs8_unencrypted_der_ec(
2731 ecp_keypair *eck,
2732 const unsigned char* key,
2733 size_t keylen )
2734{
2735 int ret, version;
2736 size_t len;
2737 x509_buf pk_alg_oid;
2738 ecp_group_id grp_id;
2739 const unsigned char *params_end;
2740 unsigned char *p = (unsigned char *) key;
2741 unsigned char *end = p + keylen;
2742 pk_type_t pk_alg = POLARSSL_PK_NONE;
2743
2744 /*
2745 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2746 *
2747 * PrivateKeyInfo ::= SEQUENCE {
2748 * version Version,
2749 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2750 * privateKey PrivateKey,
2751 * attributes [0] IMPLICIT Attributes OPTIONAL }
2752 *
2753 * Version ::= INTEGER
2754 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2755 * PrivateKey ::= OCTET STRING
2756 *
2757 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2758 */
2759
2760 if( ( ret = asn1_get_tag( &p, end, &len,
2761 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2762 {
2763 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2764 }
2765
2766 end = p + len;
2767
2768 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2769 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2770
2771 if( version != 0 )
2772 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2773
2774 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid, &params_end ) ) != 0 )
2775 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2776
2777 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2778 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2779
2780 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
2781 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2782
2783 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2784 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2785
2786 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2787 {
2788 ecp_keypair_free( eck );
2789 return( ret );
2790 }
2791
2792 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2793 {
2794 ecp_keypair_free( eck );
2795 return( ret );
2796 }
2797
2798 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2799 {
2800 ecp_keypair_free( eck );
2801 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2802 }
2803
2804 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2805 {
2806 ecp_keypair_free( eck );
2807 return( ret );
2808 }
2809
2810 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2811 {
2812 ecp_keypair_free( eck );
2813 return( ret );
2814 }
2815
2816 return 0;
2817}
2818
2819/*
2820 * Parse an encrypted PKCS#8 encoded private EC key
2821 */
2822static int x509parse_key_pkcs8_encrypted_der_ec(
2823 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002824 const unsigned char *key, size_t keylen,
2825 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002826{
2827 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002828 unsigned char buf[2048];
2829 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002830
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002831 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2832 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002833 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002834 return( ret );
2835 }
2836
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002837 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002838}
2839
2840/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002841 * Parse a private EC key
2842 */
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002843int x509parse_key_ec( ecp_keypair *eck,
2844 const unsigned char *key, size_t keylen,
2845 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002846{
2847 int ret;
2848
2849#if defined(POLARSSL_PEM_C)
2850 size_t len;
2851 pem_context pem;
2852
2853 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002854 ret = pem_read_buffer( &pem,
2855 "-----BEGIN EC PRIVATE KEY-----",
2856 "-----END EC PRIVATE KEY-----",
2857 key, pwd, pwdlen, &len );
2858 if( ret == 0 )
2859 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002860 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002861 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002862 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002863 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002864
2865 pem_free( &pem );
2866 return( ret );
2867 }
2868 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2869 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2870 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2871 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2872 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2873 return( ret );
2874
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002875 ret = pem_read_buffer( &pem,
2876 "-----BEGIN PRIVATE KEY-----",
2877 "-----END PRIVATE KEY-----",
2878 key, NULL, 0, &len );
2879 if( ret == 0 )
2880 {
2881 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2882 pem.buf, pem.buflen ) ) != 0 )
2883 {
2884 ecp_keypair_free( eck );
2885 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002886
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002887 pem_free( &pem );
2888 return( ret );
2889 }
2890 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2891 return( ret );
2892
2893 ret = pem_read_buffer( &pem,
2894 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2895 "-----END ENCRYPTED PRIVATE KEY-----",
2896 key, NULL, 0, &len );
2897 if( ret == 0 )
2898 {
2899 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2900 pem.buf, pem.buflen,
2901 pwd, pwdlen ) ) != 0 )
2902 {
2903 ecp_keypair_free( eck );
2904 }
2905
2906 pem_free( &pem );
2907 return( ret );
2908 }
2909 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2910 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002911#else
2912 ((void) pwd);
2913 ((void) pwdlen);
2914#endif /* POLARSSL_PEM_C */
2915
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002916 /*
2917 * At this point we only know it's not a PEM formatted key. Could be any
2918 * of the known DER encoded private key formats
2919 *
2920 * We try the different DER format parsers to see if one passes without
2921 * error
2922 */
2923 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2924 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002925 {
2926 return( 0 );
2927 }
2928
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002929 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002930
2931 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2932 {
2933 return( ret );
2934 }
2935
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002936 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2937 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002938 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002939
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002940 ecp_keypair_free( eck );
2941
2942 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2943 return( 0 );
2944
2945 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002946
2947 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2948}
2949
2950/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002951 * Parse a public EC key in RFC 5480 format, der-encoded
2952 */
2953static int x509parse_public_key_ec_der( ecp_keypair *key,
2954 const unsigned char *buf, size_t len )
2955{
2956 int ret;
2957 ecp_group_id grp_id;
2958 x509_buf alg_oid;
2959 pk_type_t alg = POLARSSL_PK_NONE;
2960 unsigned char *p = (unsigned char *) buf;
2961 unsigned char *end = p + len;
2962 const unsigned char *params_end;
2963 /*
2964 * SubjectPublicKeyInfo ::= SEQUENCE {
2965 * algorithm AlgorithmIdentifier,
2966 * subjectPublicKey BIT STRING
2967 * }
2968 * -- algorithm parameters are ECParameters
2969 * -- subjectPublicKey is an ECPoint
2970 */
2971 if( ( ret = asn1_get_tag( &p, end, &len,
2972 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2973 {
2974 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
2975 }
2976
2977 if( ( ret = x509_get_alg( &p, end, &alg_oid, &params_end ) ) != 0 )
2978 return( ret );
2979
2980 if( oid_get_pk_alg( &alg_oid, &alg ) != 0 )
2981 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2982
2983 if( alg != POLARSSL_PK_ECKEY && alg != POLARSSL_PK_ECKEY_DH )
2984 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2985
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002986 if( alg == POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002987 key->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2988
2989 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2990 return( ret );
2991
2992 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
2993 return( ret );
2994
2995 if( ( ret = x509_get_subpubkey_ec( &p, end, &key->grp, &key->Q ) ) != 0 )
2996 {
2997 return( ret );
2998 }
2999
3000 return( 0 );
3001}
3002
3003/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003004 * Parse a public EC key
3005 */
3006int x509parse_public_key_ec( ecp_keypair *eckey,
3007 const unsigned char *key, size_t keylen )
3008{
3009 int ret;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003010#if defined(POLARSSL_PEM_C)
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003011 size_t len;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003012 pem_context pem;
3013
3014 pem_init( &pem );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003015 ret = pem_read_buffer( &pem,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003016 "-----BEGIN PUBLIC KEY-----",
3017 "-----END PUBLIC KEY-----",
3018 key, NULL, 0, &len );
3019
3020 if( ret == 0 )
3021 {
3022 /*
3023 * Was PEM encoded
3024 */
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003025 key = pem.buf;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003026 keylen = pem.buflen;
3027 }
3028 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3029 {
3030 pem_free( &pem );
3031 return( ret );
3032 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003033#endif
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003034
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003035 if( ( ret = x509parse_public_key_ec_der ( eckey, key, keylen ) ) != 0 ||
3036 ( ret = ecp_check_pubkey( &eckey->grp, &eckey->Q ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003037 {
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003038 ecp_keypair_free( eckey );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003039 }
3040
3041#if defined(POLARSSL_PEM_C)
3042 pem_free( &pem );
3043#endif
3044
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003045 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003046}
3047#endif /* defined(POLARSSL_ECP_C) */
3048
Paul Bakkereaa89f82011-04-04 21:36:15 +00003049#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003050/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003051 * Parse DHM parameters
3052 */
Paul Bakker23986e52011-04-24 08:57:21 +00003053int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003054{
Paul Bakker23986e52011-04-24 08:57:21 +00003055 int ret;
3056 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003057 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003058#if defined(POLARSSL_PEM_C)
3059 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003060
Paul Bakker96743fc2011-02-12 14:30:57 +00003061 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003062
Paul Bakker96743fc2011-02-12 14:30:57 +00003063 ret = pem_read_buffer( &pem,
3064 "-----BEGIN DH PARAMETERS-----",
3065 "-----END DH PARAMETERS-----",
3066 dhmin, NULL, 0, &dhminlen );
3067
3068 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003069 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003070 /*
3071 * Was PEM encoded
3072 */
3073 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003074 }
Paul Bakker00b28602013-06-24 13:02:41 +02003075 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003076 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003077 pem_free( &pem );
3078 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003079 }
3080
Paul Bakker96743fc2011-02-12 14:30:57 +00003081 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3082#else
3083 p = (unsigned char *) dhmin;
3084#endif
3085 end = p + dhminlen;
3086
Paul Bakker1b57b062011-01-06 15:48:19 +00003087 memset( dhm, 0, sizeof( dhm_context ) );
3088
Paul Bakker1b57b062011-01-06 15:48:19 +00003089 /*
3090 * DHParams ::= SEQUENCE {
3091 * prime INTEGER, -- P
3092 * generator INTEGER, -- g
3093 * }
3094 */
3095 if( ( ret = asn1_get_tag( &p, end, &len,
3096 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3097 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003098#if defined(POLARSSL_PEM_C)
3099 pem_free( &pem );
3100#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003101 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003102 }
3103
3104 end = p + len;
3105
3106 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3107 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3108 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003109#if defined(POLARSSL_PEM_C)
3110 pem_free( &pem );
3111#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003112 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003113 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003114 }
3115
3116 if( p != end )
3117 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003118#if defined(POLARSSL_PEM_C)
3119 pem_free( &pem );
3120#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003121 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003122 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003123 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3124 }
3125
Paul Bakker96743fc2011-02-12 14:30:57 +00003126#if defined(POLARSSL_PEM_C)
3127 pem_free( &pem );
3128#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003129
3130 return( 0 );
3131}
3132
Paul Bakker335db3f2011-04-25 15:28:35 +00003133#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003134/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003135 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003136 */
3137int x509parse_dhmfile( dhm_context *dhm, const char *path )
3138{
3139 int ret;
3140 size_t n;
3141 unsigned char *buf;
3142
Paul Bakker69e095c2011-12-10 21:55:01 +00003143 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3144 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003145
Paul Bakker27fdf462011-06-09 13:55:13 +00003146 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003147
3148 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003149 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003150
3151 return( ret );
3152}
Paul Bakker335db3f2011-04-25 15:28:35 +00003153#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003154#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003155
Paul Bakker5121ce52009-01-03 21:22:43 +00003156#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003157#include <stdarg.h>
3158
3159#if !defined vsnprintf
3160#define vsnprintf _vsnprintf
3161#endif // vsnprintf
3162
3163/*
3164 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3165 * Result value is not size of buffer needed, but -1 if no fit is possible.
3166 *
3167 * This fuction tries to 'fix' this by at least suggesting enlarging the
3168 * size by 20.
3169 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003170static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003171{
3172 va_list ap;
3173 int res = -1;
3174
3175 va_start( ap, format );
3176
3177 res = vsnprintf( str, size, format, ap );
3178
3179 va_end( ap );
3180
3181 // No quick fix possible
3182 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003183 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003184
3185 return res;
3186}
3187
3188#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003189#endif
3190
Paul Bakkerd98030e2009-05-02 15:13:40 +00003191#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3192
3193#define SAFE_SNPRINTF() \
3194{ \
3195 if( ret == -1 ) \
3196 return( -1 ); \
3197 \
Paul Bakker23986e52011-04-24 08:57:21 +00003198 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003199 p[n - 1] = '\0'; \
3200 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3201 } \
3202 \
Paul Bakker23986e52011-04-24 08:57:21 +00003203 n -= (unsigned int) ret; \
3204 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003205}
3206
Paul Bakker5121ce52009-01-03 21:22:43 +00003207/*
3208 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003209 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003211int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003212{
Paul Bakker23986e52011-04-24 08:57:21 +00003213 int ret;
3214 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003216 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003217 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 char s[128], *p;
3219
3220 memset( s, 0, sizeof( s ) );
3221
3222 name = dn;
3223 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003224 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
3226 while( name != NULL )
3227 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003228 if( !name->oid.p )
3229 {
3230 name = name->next;
3231 continue;
3232 }
3233
Paul Bakker74111d32011-01-15 16:57:55 +00003234 if( name != dn )
3235 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003236 ret = snprintf( p, n, ", " );
3237 SAFE_SNPRINTF();
3238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
Paul Bakkerc70b9822013-04-07 22:00:46 +02003240 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003241
Paul Bakkerc70b9822013-04-07 22:00:46 +02003242 if( ret == 0 )
3243 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003245 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003246 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003247
3248 for( i = 0; i < name->val.len; i++ )
3249 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003250 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 break;
3252
3253 c = name->val.p[i];
3254 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3255 s[i] = '?';
3256 else s[i] = c;
3257 }
3258 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003259 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003260 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 name = name->next;
3262 }
3263
Paul Bakker23986e52011-04-24 08:57:21 +00003264 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003265}
3266
3267/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003268 * Store the serial in printable form into buf; no more
3269 * than size characters will be written
3270 */
3271int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3272{
Paul Bakker23986e52011-04-24 08:57:21 +00003273 int ret;
3274 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003275 char *p;
3276
3277 p = buf;
3278 n = size;
3279
3280 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003281 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003282
3283 for( i = 0; i < nr; i++ )
3284 {
Paul Bakker93048802011-12-05 14:38:06 +00003285 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003286 continue;
3287
Paul Bakkerdd476992011-01-16 21:34:59 +00003288 ret = snprintf( p, n, "%02X%s",
3289 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3290 SAFE_SNPRINTF();
3291 }
3292
Paul Bakker03c7c252011-11-25 12:37:37 +00003293 if( nr != serial->len )
3294 {
3295 ret = snprintf( p, n, "...." );
3296 SAFE_SNPRINTF();
3297 }
3298
Paul Bakker23986e52011-04-24 08:57:21 +00003299 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003300}
3301
3302/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003303 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003305int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3306 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003307{
Paul Bakker23986e52011-04-24 08:57:21 +00003308 int ret;
3309 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003310 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003311 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
3313 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003314 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003315
Paul Bakkerd98030e2009-05-02 15:13:40 +00003316 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003318 SAFE_SNPRINTF();
3319 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003321 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003322
Paul Bakkerdd476992011-01-16 21:34:59 +00003323 ret = x509parse_serial_gets( p, n, &crt->serial);
3324 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003325
Paul Bakkerd98030e2009-05-02 15:13:40 +00003326 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3327 SAFE_SNPRINTF();
3328 ret = x509parse_dn_gets( p, n, &crt->issuer );
3329 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003330
Paul Bakkerd98030e2009-05-02 15:13:40 +00003331 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3332 SAFE_SNPRINTF();
3333 ret = x509parse_dn_gets( p, n, &crt->subject );
3334 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Paul Bakkerd98030e2009-05-02 15:13:40 +00003336 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3338 crt->valid_from.year, crt->valid_from.mon,
3339 crt->valid_from.day, crt->valid_from.hour,
3340 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003341 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003342
Paul Bakkerd98030e2009-05-02 15:13:40 +00003343 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3345 crt->valid_to.year, crt->valid_to.mon,
3346 crt->valid_to.day, crt->valid_to.hour,
3347 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003348 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003349
Paul Bakkerc70b9822013-04-07 22:00:46 +02003350 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003351 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003352
Paul Bakkerc70b9822013-04-07 22:00:46 +02003353 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3354 if( ret != 0 )
3355 ret = snprintf( p, n, "???" );
3356 else
3357 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003358 SAFE_SNPRINTF();
3359
3360 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003361 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003362 SAFE_SNPRINTF();
3363
Paul Bakker23986e52011-04-24 08:57:21 +00003364 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003365}
3366
Paul Bakker74111d32011-01-15 16:57:55 +00003367/*
3368 * Return an informational string describing the given OID
3369 */
3370const char *x509_oid_get_description( x509_buf *oid )
3371{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003372 const char *desc = NULL;
3373 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003374
Paul Bakkerc70b9822013-04-07 22:00:46 +02003375 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003376
Paul Bakkerc70b9822013-04-07 22:00:46 +02003377 if( ret != 0 )
3378 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003379
Paul Bakkerc70b9822013-04-07 22:00:46 +02003380 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003381}
3382
3383/* Return the x.y.z.... style numeric string for the given OID */
3384int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3385{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003386 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003387}
3388
Paul Bakkerd98030e2009-05-02 15:13:40 +00003389/*
3390 * Return an informational string about the CRL.
3391 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003392int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3393 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003394{
Paul Bakker23986e52011-04-24 08:57:21 +00003395 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003396 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003397 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003398 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003399 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003400
3401 p = buf;
3402 n = size;
3403
3404 ret = snprintf( p, n, "%sCRL version : %d",
3405 prefix, crl->version );
3406 SAFE_SNPRINTF();
3407
3408 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3409 SAFE_SNPRINTF();
3410 ret = x509parse_dn_gets( p, n, &crl->issuer );
3411 SAFE_SNPRINTF();
3412
3413 ret = snprintf( p, n, "\n%sthis update : " \
3414 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3415 crl->this_update.year, crl->this_update.mon,
3416 crl->this_update.day, crl->this_update.hour,
3417 crl->this_update.min, crl->this_update.sec );
3418 SAFE_SNPRINTF();
3419
3420 ret = snprintf( p, n, "\n%snext update : " \
3421 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3422 crl->next_update.year, crl->next_update.mon,
3423 crl->next_update.day, crl->next_update.hour,
3424 crl->next_update.min, crl->next_update.sec );
3425 SAFE_SNPRINTF();
3426
3427 entry = &crl->entry;
3428
3429 ret = snprintf( p, n, "\n%sRevoked certificates:",
3430 prefix );
3431 SAFE_SNPRINTF();
3432
Paul Bakker9be19372009-07-27 20:21:53 +00003433 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003434 {
3435 ret = snprintf( p, n, "\n%sserial number: ",
3436 prefix );
3437 SAFE_SNPRINTF();
3438
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003439 ret = x509parse_serial_gets( p, n, &entry->serial);
3440 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003441
Paul Bakkerd98030e2009-05-02 15:13:40 +00003442 ret = snprintf( p, n, " revocation date: " \
3443 "%04d-%02d-%02d %02d:%02d:%02d",
3444 entry->revocation_date.year, entry->revocation_date.mon,
3445 entry->revocation_date.day, entry->revocation_date.hour,
3446 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003447 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003448
3449 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 }
3451
Paul Bakkerc70b9822013-04-07 22:00:46 +02003452 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003453 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003454
Paul Bakkerc70b9822013-04-07 22:00:46 +02003455 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3456 if( ret != 0 )
3457 ret = snprintf( p, n, "???" );
3458 else
3459 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003460 SAFE_SNPRINTF();
3461
Paul Bakker1e27bb22009-07-19 20:25:25 +00003462 ret = snprintf( p, n, "\n" );
3463 SAFE_SNPRINTF();
3464
Paul Bakker23986e52011-04-24 08:57:21 +00003465 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003466}
3467
3468/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003469 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003471#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003472int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003473{
Paul Bakkercce9d772011-11-18 14:26:47 +00003474 int year, mon, day;
3475 int hour, min, sec;
3476
3477#if defined(_WIN32)
3478 SYSTEMTIME st;
3479
3480 GetLocalTime(&st);
3481
3482 year = st.wYear;
3483 mon = st.wMonth;
3484 day = st.wDay;
3485 hour = st.wHour;
3486 min = st.wMinute;
3487 sec = st.wSecond;
3488#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 struct tm *lt;
3490 time_t tt;
3491
3492 tt = time( NULL );
3493 lt = localtime( &tt );
3494
Paul Bakkercce9d772011-11-18 14:26:47 +00003495 year = lt->tm_year + 1900;
3496 mon = lt->tm_mon + 1;
3497 day = lt->tm_mday;
3498 hour = lt->tm_hour;
3499 min = lt->tm_min;
3500 sec = lt->tm_sec;
3501#endif
3502
3503 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003504 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003505
Paul Bakkercce9d772011-11-18 14:26:47 +00003506 if( year == to->year &&
3507 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003508 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Paul Bakkercce9d772011-11-18 14:26:47 +00003510 if( year == to->year &&
3511 mon == to->mon &&
3512 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003513 return( 1 );
3514
Paul Bakkercce9d772011-11-18 14:26:47 +00003515 if( year == to->year &&
3516 mon == to->mon &&
3517 day == to->day &&
3518 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003519 return( 1 );
3520
Paul Bakkercce9d772011-11-18 14:26:47 +00003521 if( year == to->year &&
3522 mon == to->mon &&
3523 day == to->day &&
3524 hour == to->hour &&
3525 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +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 &&
3532 min == to->min &&
3533 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003534 return( 1 );
3535
Paul Bakker40ea7de2009-05-03 10:18:48 +00003536 return( 0 );
3537}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003538#else /* POLARSSL_HAVE_TIME */
3539int x509parse_time_expired( const x509_time *to )
3540{
3541 ((void) to);
3542 return( 0 );
3543}
3544#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003545
3546/*
3547 * Return 1 if the certificate is revoked, or 0 otherwise.
3548 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003549int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003550{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003551 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003552
3553 while( cur != NULL && cur->serial.len != 0 )
3554 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003555 if( crt->serial.len == cur->serial.len &&
3556 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003557 {
3558 if( x509parse_time_expired( &cur->revocation_date ) )
3559 return( 1 );
3560 }
3561
3562 cur = cur->next;
3563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003564
3565 return( 0 );
3566}
3567
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003568/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003569 * Check that the given certificate is valid accoring to the CRL.
3570 */
3571static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3572 x509_crl *crl_list)
3573{
3574 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003575 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3576 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003577
Paul Bakker915275b2012-09-28 07:10:55 +00003578 if( ca == NULL )
3579 return( flags );
3580
Paul Bakker76fd75a2011-01-16 21:12:10 +00003581 /*
3582 * TODO: What happens if no CRL is present?
3583 * Suggestion: Revocation state should be unknown if no CRL is present.
3584 * For backwards compatibility this is not yet implemented.
3585 */
3586
Paul Bakker915275b2012-09-28 07:10:55 +00003587 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003588 {
Paul Bakker915275b2012-09-28 07:10:55 +00003589 if( crl_list->version == 0 ||
3590 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003591 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3592 crl_list->issuer_raw.len ) != 0 )
3593 {
3594 crl_list = crl_list->next;
3595 continue;
3596 }
3597
3598 /*
3599 * Check if CRL is correctly signed by the trusted CA
3600 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003601 md_info = md_info_from_type( crl_list->sig_md );
3602 if( md_info == NULL )
3603 {
3604 /*
3605 * Cannot check 'unknown' hash
3606 */
3607 flags |= BADCRL_NOT_TRUSTED;
3608 break;
3609 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003610
Paul Bakkerc70b9822013-04-07 22:00:46 +02003611 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003612
Paul Bakkerc70b9822013-04-07 22:00:46 +02003613 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003614 0, hash, crl_list->sig.p ) == 0 )
3615 {
3616 /*
3617 * CRL is not trusted
3618 */
3619 flags |= BADCRL_NOT_TRUSTED;
3620 break;
3621 }
3622
3623 /*
3624 * Check for validity of CRL (Do not drop out)
3625 */
3626 if( x509parse_time_expired( &crl_list->next_update ) )
3627 flags |= BADCRL_EXPIRED;
3628
3629 /*
3630 * Check if certificate is revoked
3631 */
3632 if( x509parse_revoked(crt, crl_list) )
3633 {
3634 flags |= BADCERT_REVOKED;
3635 break;
3636 }
3637
3638 crl_list = crl_list->next;
3639 }
3640 return flags;
3641}
3642
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003643static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003644{
3645 size_t i;
3646 size_t cn_idx = 0;
3647
Paul Bakker57b12982012-02-11 17:38:38 +00003648 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003649 return( 0 );
3650
3651 for( i = 0; i < strlen( cn ); ++i )
3652 {
3653 if( cn[i] == '.' )
3654 {
3655 cn_idx = i;
3656 break;
3657 }
3658 }
3659
3660 if( cn_idx == 0 )
3661 return( 0 );
3662
Paul Bakker535e97d2012-08-23 10:49:55 +00003663 if( strlen( cn ) - cn_idx == name->len - 1 &&
3664 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003665 {
3666 return( 1 );
3667 }
3668
3669 return( 0 );
3670}
3671
Paul Bakker915275b2012-09-28 07:10:55 +00003672static int x509parse_verify_top(
3673 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003674 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003675 int (*f_vrfy)(void *, x509_cert *, int, int *),
3676 void *p_vrfy )
3677{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003678 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003679 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003680 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3681 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003682
3683 if( x509parse_time_expired( &child->valid_to ) )
3684 *flags |= BADCERT_EXPIRED;
3685
3686 /*
3687 * Child is the top of the chain. Check against the trust_ca list.
3688 */
3689 *flags |= BADCERT_NOT_TRUSTED;
3690
3691 while( trust_ca != NULL )
3692 {
3693 if( trust_ca->version == 0 ||
3694 child->issuer_raw.len != trust_ca->subject_raw.len ||
3695 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3696 child->issuer_raw.len ) != 0 )
3697 {
3698 trust_ca = trust_ca->next;
3699 continue;
3700 }
3701
Paul Bakker9a736322012-11-14 12:39:52 +00003702 /*
3703 * Reduce path_len to check against if top of the chain is
3704 * the same as the trusted CA
3705 */
3706 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3707 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3708 child->issuer_raw.len ) == 0 )
3709 {
3710 check_path_cnt--;
3711 }
3712
Paul Bakker915275b2012-09-28 07:10:55 +00003713 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003714 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003715 {
3716 trust_ca = trust_ca->next;
3717 continue;
3718 }
3719
Paul Bakkerc70b9822013-04-07 22:00:46 +02003720 md_info = md_info_from_type( child->sig_md );
3721 if( md_info == NULL )
3722 {
3723 /*
3724 * Cannot check 'unknown' hash
3725 */
3726 continue;
3727 }
Paul Bakker915275b2012-09-28 07:10:55 +00003728
Paul Bakkerc70b9822013-04-07 22:00:46 +02003729 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003730
Paul Bakkerc70b9822013-04-07 22:00:46 +02003731 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003732 0, hash, child->sig.p ) != 0 )
3733 {
3734 trust_ca = trust_ca->next;
3735 continue;
3736 }
3737
3738 /*
3739 * Top of chain is signed by a trusted CA
3740 */
3741 *flags &= ~BADCERT_NOT_TRUSTED;
3742 break;
3743 }
3744
Paul Bakker9a736322012-11-14 12:39:52 +00003745 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003746 * If top of chain is not the same as the trusted CA send a verify request
3747 * to the callback for any issues with validity and CRL presence for the
3748 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003749 */
3750 if( trust_ca != NULL &&
3751 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3752 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3753 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003754 {
3755 /* Check trusted CA's CRL for then chain's top crt */
3756 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3757
3758 if( x509parse_time_expired( &trust_ca->valid_to ) )
3759 ca_flags |= BADCERT_EXPIRED;
3760
Paul Bakker915275b2012-09-28 07:10:55 +00003761 if( NULL != f_vrfy )
3762 {
Paul Bakker9a736322012-11-14 12:39:52 +00003763 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003764 return( ret );
3765 }
3766 }
3767
3768 /* Call callback on top cert */
3769 if( NULL != f_vrfy )
3770 {
Paul Bakker9a736322012-11-14 12:39:52 +00003771 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003772 return( ret );
3773 }
3774
Paul Bakker915275b2012-09-28 07:10:55 +00003775 *flags |= ca_flags;
3776
3777 return( 0 );
3778}
3779
3780static int x509parse_verify_child(
3781 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003782 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003783 int (*f_vrfy)(void *, x509_cert *, int, int *),
3784 void *p_vrfy )
3785{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003786 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003787 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003788 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003789 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003790 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003791
3792 if( x509parse_time_expired( &child->valid_to ) )
3793 *flags |= BADCERT_EXPIRED;
3794
Paul Bakkerc70b9822013-04-07 22:00:46 +02003795 md_info = md_info_from_type( child->sig_md );
3796 if( md_info == NULL )
3797 {
3798 /*
3799 * Cannot check 'unknown' hash
3800 */
Paul Bakker915275b2012-09-28 07:10:55 +00003801 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003802 }
3803 else
3804 {
3805 md( md_info, child->tbs.p, child->tbs.len, hash );
3806
3807 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3808 child->sig.p ) != 0 )
3809 *flags |= BADCERT_NOT_TRUSTED;
3810 }
3811
Paul Bakker915275b2012-09-28 07:10:55 +00003812 /* Check trusted CA's CRL for the given crt */
3813 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3814
3815 grandparent = parent->next;
3816
3817 while( grandparent != NULL )
3818 {
3819 if( grandparent->version == 0 ||
3820 grandparent->ca_istrue == 0 ||
3821 parent->issuer_raw.len != grandparent->subject_raw.len ||
3822 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3823 parent->issuer_raw.len ) != 0 )
3824 {
3825 grandparent = grandparent->next;
3826 continue;
3827 }
3828 break;
3829 }
3830
Paul Bakker915275b2012-09-28 07:10:55 +00003831 if( grandparent != NULL )
3832 {
3833 /*
3834 * Part of the chain
3835 */
Paul Bakker9a736322012-11-14 12:39:52 +00003836 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 +00003837 if( ret != 0 )
3838 return( ret );
3839 }
3840 else
3841 {
Paul Bakker9a736322012-11-14 12:39:52 +00003842 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 +00003843 if( ret != 0 )
3844 return( ret );
3845 }
3846
3847 /* child is verified to be a child of the parent, call verify callback */
3848 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003849 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003850 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003851
3852 *flags |= parent_flags;
3853
3854 return( 0 );
3855}
3856
Paul Bakker76fd75a2011-01-16 21:12:10 +00003857/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 * Verify the certificate validity
3859 */
3860int x509parse_verify( x509_cert *crt,
3861 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003862 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003863 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003864 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003865 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003866{
Paul Bakker23986e52011-04-24 08:57:21 +00003867 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003868 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003869 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003870 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003871 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003872 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003873
Paul Bakker40ea7de2009-05-03 10:18:48 +00003874 *flags = 0;
3875
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 if( cn != NULL )
3877 {
3878 name = &crt->subject;
3879 cn_len = strlen( cn );
3880
Paul Bakker4d2c1242012-05-10 14:12:46 +00003881 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003882 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003883 cur = &crt->subject_alt_names;
3884
3885 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003886 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003887 if( cur->buf.len == cn_len &&
3888 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003889 break;
3890
Paul Bakker535e97d2012-08-23 10:49:55 +00003891 if( cur->buf.len > 2 &&
3892 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003893 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003894 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003895
Paul Bakker4d2c1242012-05-10 14:12:46 +00003896 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003897 }
3898
3899 if( cur == NULL )
3900 *flags |= BADCERT_CN_MISMATCH;
3901 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003902 else
3903 {
3904 while( name != NULL )
3905 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003906 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003907 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003908 if( name->val.len == cn_len &&
3909 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003910 break;
3911
Paul Bakker535e97d2012-08-23 10:49:55 +00003912 if( name->val.len > 2 &&
3913 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003914 x509_wildcard_verify( cn, &name->val ) )
3915 break;
3916 }
3917
3918 name = name->next;
3919 }
3920
3921 if( name == NULL )
3922 *flags |= BADCERT_CN_MISMATCH;
3923 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003924 }
3925
Paul Bakker5121ce52009-01-03 21:22:43 +00003926 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003927 * Iterate upwards in the given cert chain, to find our crt parent.
3928 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003929 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003930 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003931
Paul Bakker76fd75a2011-01-16 21:12:10 +00003932 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003933 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003934 if( parent->ca_istrue == 0 ||
3935 crt->issuer_raw.len != parent->subject_raw.len ||
3936 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003937 crt->issuer_raw.len ) != 0 )
3938 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003939 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003940 continue;
3941 }
Paul Bakker915275b2012-09-28 07:10:55 +00003942 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003943 }
3944
Paul Bakker915275b2012-09-28 07:10:55 +00003945 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003946 {
Paul Bakker915275b2012-09-28 07:10:55 +00003947 /*
3948 * Part of the chain
3949 */
Paul Bakker9a736322012-11-14 12:39:52 +00003950 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003951 if( ret != 0 )
3952 return( ret );
3953 }
3954 else
Paul Bakker74111d32011-01-15 16:57:55 +00003955 {
Paul Bakker9a736322012-11-14 12:39:52 +00003956 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003957 if( ret != 0 )
3958 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003959 }
Paul Bakker915275b2012-09-28 07:10:55 +00003960
3961 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003962 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003963
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 return( 0 );
3965}
3966
3967/*
3968 * Unallocate all certificate data
3969 */
3970void x509_free( x509_cert *crt )
3971{
3972 x509_cert *cert_cur = crt;
3973 x509_cert *cert_prv;
3974 x509_name *name_cur;
3975 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003976 x509_sequence *seq_cur;
3977 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003978
3979 if( crt == NULL )
3980 return;
3981
3982 do
3983 {
3984 rsa_free( &cert_cur->rsa );
3985
3986 name_cur = cert_cur->issuer.next;
3987 while( name_cur != NULL )
3988 {
3989 name_prv = name_cur;
3990 name_cur = name_cur->next;
3991 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003992 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003993 }
3994
3995 name_cur = cert_cur->subject.next;
3996 while( name_cur != NULL )
3997 {
3998 name_prv = name_cur;
3999 name_cur = name_cur->next;
4000 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004001 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004002 }
4003
Paul Bakker74111d32011-01-15 16:57:55 +00004004 seq_cur = cert_cur->ext_key_usage.next;
4005 while( seq_cur != NULL )
4006 {
4007 seq_prv = seq_cur;
4008 seq_cur = seq_cur->next;
4009 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004010 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004011 }
4012
Paul Bakker8afa70d2012-02-11 18:42:45 +00004013 seq_cur = cert_cur->subject_alt_names.next;
4014 while( seq_cur != NULL )
4015 {
4016 seq_prv = seq_cur;
4017 seq_cur = seq_cur->next;
4018 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004019 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004020 }
4021
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 if( cert_cur->raw.p != NULL )
4023 {
4024 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004025 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004026 }
4027
4028 cert_cur = cert_cur->next;
4029 }
4030 while( cert_cur != NULL );
4031
4032 cert_cur = crt;
4033 do
4034 {
4035 cert_prv = cert_cur;
4036 cert_cur = cert_cur->next;
4037
4038 memset( cert_prv, 0, sizeof( x509_cert ) );
4039 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004040 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004041 }
4042 while( cert_cur != NULL );
4043}
4044
Paul Bakkerd98030e2009-05-02 15:13:40 +00004045/*
4046 * Unallocate all CRL data
4047 */
4048void x509_crl_free( x509_crl *crl )
4049{
4050 x509_crl *crl_cur = crl;
4051 x509_crl *crl_prv;
4052 x509_name *name_cur;
4053 x509_name *name_prv;
4054 x509_crl_entry *entry_cur;
4055 x509_crl_entry *entry_prv;
4056
4057 if( crl == NULL )
4058 return;
4059
4060 do
4061 {
4062 name_cur = crl_cur->issuer.next;
4063 while( name_cur != NULL )
4064 {
4065 name_prv = name_cur;
4066 name_cur = name_cur->next;
4067 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004068 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004069 }
4070
4071 entry_cur = crl_cur->entry.next;
4072 while( entry_cur != NULL )
4073 {
4074 entry_prv = entry_cur;
4075 entry_cur = entry_cur->next;
4076 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004077 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004078 }
4079
4080 if( crl_cur->raw.p != NULL )
4081 {
4082 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004083 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004084 }
4085
4086 crl_cur = crl_cur->next;
4087 }
4088 while( crl_cur != NULL );
4089
4090 crl_cur = crl;
4091 do
4092 {
4093 crl_prv = crl_cur;
4094 crl_cur = crl_cur->next;
4095
4096 memset( crl_prv, 0, sizeof( x509_crl ) );
4097 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004098 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004099 }
4100 while( crl_cur != NULL );
4101}
4102
Paul Bakker40e46942009-01-03 21:51:57 +00004103#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004104
Paul Bakker40e46942009-01-03 21:51:57 +00004105#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004106
4107/*
4108 * Checkup routine
4109 */
4110int x509_self_test( int verbose )
4111{
Paul Bakker5690efc2011-05-26 13:16:06 +00004112#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004113 int ret;
4114 int flags;
4115 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004116 x509_cert cacert;
4117 x509_cert clicert;
4118 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004119#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004120 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004122
4123 if( verbose != 0 )
4124 printf( " X.509 certificate load: " );
4125
4126 memset( &clicert, 0, sizeof( x509_cert ) );
4127
Paul Bakker3c2122f2013-06-24 19:03:14 +02004128 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004129 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004130 if( ret != 0 )
4131 {
4132 if( verbose != 0 )
4133 printf( "failed\n" );
4134
4135 return( ret );
4136 }
4137
4138 memset( &cacert, 0, sizeof( x509_cert ) );
4139
Paul Bakker3c2122f2013-06-24 19:03:14 +02004140 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004141 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004142 if( ret != 0 )
4143 {
4144 if( verbose != 0 )
4145 printf( "failed\n" );
4146
4147 return( ret );
4148 }
4149
4150 if( verbose != 0 )
4151 printf( "passed\n X.509 private key load: " );
4152
4153 i = strlen( test_ca_key );
4154 j = strlen( test_ca_pwd );
4155
Paul Bakker66b78b22011-03-25 14:22:50 +00004156 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4157
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004158 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004159 (const unsigned char *) test_ca_key, i,
4160 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004161 {
4162 if( verbose != 0 )
4163 printf( "failed\n" );
4164
4165 return( ret );
4166 }
4167
4168 if( verbose != 0 )
4169 printf( "passed\n X.509 signature verify: ");
4170
Paul Bakker23986e52011-04-24 08:57:21 +00004171 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004172 if( ret != 0 )
4173 {
Paul Bakker23986e52011-04-24 08:57:21 +00004174 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004175 if( verbose != 0 )
4176 printf( "failed\n" );
4177
4178 return( ret );
4179 }
4180
Paul Bakker5690efc2011-05-26 13:16:06 +00004181#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004182 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004183 printf( "passed\n X.509 DHM parameter load: " );
4184
4185 i = strlen( test_dhm_params );
4186 j = strlen( test_ca_pwd );
4187
Paul Bakker3c2122f2013-06-24 19:03:14 +02004188 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004189 {
4190 if( verbose != 0 )
4191 printf( "failed\n" );
4192
4193 return( ret );
4194 }
4195
4196 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004197 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004198#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004199
4200 x509_free( &cacert );
4201 x509_free( &clicert );
4202 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004203#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004204 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004205#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004206
4207 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004208#else
4209 ((void) verbose);
4210 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004212}
4213
4214#endif
4215
4216#endif