blob: e704c48db223d9f806aafc330ebab449cdd9d994 [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 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200514 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000515 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000518 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000521 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000522 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 end2 = *p + len;
525
526 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000527 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 /*
530 * RSAPublicKey ::= SEQUENCE {
531 * modulus INTEGER, -- n
532 * publicExponent INTEGER -- e
533 * }
534 */
535 if( ( ret = asn1_get_tag( p, end2, &len,
536 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000537 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000540 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000541 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
544 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000545 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000548 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000549 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
551 return( 0 );
552}
553
554static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000555 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 x509_buf *sig )
557{
Paul Bakker23986e52011-04-24 08:57:21 +0000558 int ret;
559 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Paul Bakker8afa70d2012-02-11 18:42:45 +0000561 if( ( end - *p ) < 1 )
562 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
563 POLARSSL_ERR_ASN1_OUT_OF_DATA );
564
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 sig->tag = **p;
566
567 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000568 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Paul Bakker74111d32011-01-15 16:57:55 +0000570
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000572 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 sig->len = len;
575 sig->p = *p;
576
577 *p += len;
578
579 return( 0 );
580}
581
582/*
583 * X.509 v2/v3 unique identifier (not parsed)
584 */
585static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000586 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 x509_buf *uid, int n )
588{
589 int ret;
590
591 if( *p == end )
592 return( 0 );
593
594 uid->tag = **p;
595
596 if( ( ret = asn1_get_tag( p, end, &uid->len,
597 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
598 {
Paul Bakker40e46942009-01-03 21:51:57 +0000599 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 return( 0 );
601
602 return( ret );
603 }
604
605 uid->p = *p;
606 *p += uid->len;
607
608 return( 0 );
609}
610
611/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000612 * X.509 Extensions (No parsing of extensions, pointer should
613 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 */
615static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000616 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000617 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000618{
Paul Bakker23986e52011-04-24 08:57:21 +0000619 int ret;
620 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622 if( *p == end )
623 return( 0 );
624
625 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000626
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000628 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
631 ext->p = *p;
632 end = *p + ext->len;
633
634 /*
635 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
636 *
637 * Extension ::= SEQUENCE {
638 * extnID OBJECT IDENTIFIER,
639 * critical BOOLEAN DEFAULT FALSE,
640 * extnValue OCTET STRING }
641 */
642 if( ( ret = asn1_get_tag( p, end, &len,
643 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000644 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000647 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000648 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Paul Bakkerd98030e2009-05-02 15:13:40 +0000650 return( 0 );
651}
652
653/*
654 * X.509 CRL v2 extensions (no extensions parsed yet.)
655 */
656static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000657 const unsigned char *end,
658 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000659{
Paul Bakker23986e52011-04-24 08:57:21 +0000660 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000661 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000662
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000663 /* Get explicit tag */
664 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000665 {
666 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
667 return( 0 );
668
669 return( ret );
670 }
671
672 while( *p < end )
673 {
674 if( ( ret = asn1_get_tag( p, end, &len,
675 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000676 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000677
678 *p += len;
679 }
680
681 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000682 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000683 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
684
685 return( 0 );
686}
687
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000688/*
689 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
690 */
691static int x509_get_crl_entry_ext( unsigned char **p,
692 const unsigned char *end,
693 x509_buf *ext )
694{
695 int ret;
696 size_t len = 0;
697
698 /* OPTIONAL */
699 if (end <= *p)
700 return( 0 );
701
702 ext->tag = **p;
703 ext->p = *p;
704
705 /*
706 * Get CRL-entry extension sequence header
707 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
708 */
709 if( ( ret = asn1_get_tag( p, end, &ext->len,
710 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
711 {
712 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
713 {
714 ext->p = NULL;
715 return( 0 );
716 }
717 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
718 }
719
720 end = *p + ext->len;
721
722 if( end != *p + ext->len )
723 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
724 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
725
726 while( *p < end )
727 {
728 if( ( ret = asn1_get_tag( p, end, &len,
729 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
730 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
731
732 *p += len;
733 }
734
735 if( *p != end )
736 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
737 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
738
739 return( 0 );
740}
741
Paul Bakker74111d32011-01-15 16:57:55 +0000742static int x509_get_basic_constraints( unsigned char **p,
743 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000744 int *ca_istrue,
745 int *max_pathlen )
746{
Paul Bakker23986e52011-04-24 08:57:21 +0000747 int ret;
748 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000749
750 /*
751 * BasicConstraints ::= SEQUENCE {
752 * cA BOOLEAN DEFAULT FALSE,
753 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
754 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000755 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000756 *max_pathlen = 0; /* endless */
757
758 if( ( ret = asn1_get_tag( p, end, &len,
759 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000760 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000761
762 if( *p == end )
763 return 0;
764
Paul Bakker3cccddb2011-01-16 21:46:31 +0000765 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000766 {
767 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000768 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000769
770 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000771 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000772
Paul Bakker3cccddb2011-01-16 21:46:31 +0000773 if( *ca_istrue != 0 )
774 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000775 }
776
777 if( *p == end )
778 return 0;
779
780 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000782
783 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000784 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000785 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
786
787 (*max_pathlen)++;
788
Paul Bakker74111d32011-01-15 16:57:55 +0000789 return 0;
790}
791
792static int x509_get_ns_cert_type( unsigned char **p,
793 const unsigned char *end,
794 unsigned char *ns_cert_type)
795{
796 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000797 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000798
799 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000800 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000801
802 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000803 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000804 POLARSSL_ERR_ASN1_INVALID_LENGTH );
805
806 /* Get actual bitstring */
807 *ns_cert_type = *bs.p;
808 return 0;
809}
810
811static int x509_get_key_usage( unsigned char **p,
812 const unsigned char *end,
813 unsigned char *key_usage)
814{
815 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000816 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000817
818 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000819 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000820
Paul Bakker94a67962012-08-23 13:03:52 +0000821 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000823 POLARSSL_ERR_ASN1_INVALID_LENGTH );
824
825 /* Get actual bitstring */
826 *key_usage = *bs.p;
827 return 0;
828}
829
Paul Bakkerd98030e2009-05-02 15:13:40 +0000830/*
Paul Bakker74111d32011-01-15 16:57:55 +0000831 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
832 *
833 * KeyPurposeId ::= OBJECT IDENTIFIER
834 */
835static int x509_get_ext_key_usage( unsigned char **p,
836 const unsigned char *end,
837 x509_sequence *ext_key_usage)
838{
839 int ret;
840
841 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000842 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000843
844 /* Sequence length must be >= 1 */
845 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000847 POLARSSL_ERR_ASN1_INVALID_LENGTH );
848
849 return 0;
850}
851
852/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000853 * SubjectAltName ::= GeneralNames
854 *
855 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
856 *
857 * GeneralName ::= CHOICE {
858 * otherName [0] OtherName,
859 * rfc822Name [1] IA5String,
860 * dNSName [2] IA5String,
861 * x400Address [3] ORAddress,
862 * directoryName [4] Name,
863 * ediPartyName [5] EDIPartyName,
864 * uniformResourceIdentifier [6] IA5String,
865 * iPAddress [7] OCTET STRING,
866 * registeredID [8] OBJECT IDENTIFIER }
867 *
868 * OtherName ::= SEQUENCE {
869 * type-id OBJECT IDENTIFIER,
870 * value [0] EXPLICIT ANY DEFINED BY type-id }
871 *
872 * EDIPartyName ::= SEQUENCE {
873 * nameAssigner [0] DirectoryString OPTIONAL,
874 * partyName [1] DirectoryString }
875 *
876 * NOTE: PolarSSL only parses and uses dNSName at this point.
877 */
878static int x509_get_subject_alt_name( unsigned char **p,
879 const unsigned char *end,
880 x509_sequence *subject_alt_name )
881{
882 int ret;
883 size_t len, tag_len;
884 asn1_buf *buf;
885 unsigned char tag;
886 asn1_sequence *cur = subject_alt_name;
887
888 /* Get main sequence tag */
889 if( ( ret = asn1_get_tag( p, end, &len,
890 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
891 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
892
893 if( *p + len != end )
894 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
895 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
896
897 while( *p < end )
898 {
899 if( ( end - *p ) < 1 )
900 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
901 POLARSSL_ERR_ASN1_OUT_OF_DATA );
902
903 tag = **p;
904 (*p)++;
905 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
906 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
907
908 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
909 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
910 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
911
912 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
913 {
914 *p += tag_len;
915 continue;
916 }
917
918 buf = &(cur->buf);
919 buf->tag = tag;
920 buf->p = *p;
921 buf->len = tag_len;
922 *p += buf->len;
923
924 /* Allocate and assign next pointer */
925 if (*p < end)
926 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200927 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000928 sizeof( asn1_sequence ) );
929
930 if( cur->next == NULL )
931 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
932 POLARSSL_ERR_ASN1_MALLOC_FAILED );
933
Paul Bakker535e97d2012-08-23 10:49:55 +0000934 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000935 cur = cur->next;
936 }
937 }
938
939 /* Set final sequence entry's next pointer to NULL */
940 cur->next = NULL;
941
942 if( *p != end )
943 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
944 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
945
946 return( 0 );
947}
948
949/*
Paul Bakker74111d32011-01-15 16:57:55 +0000950 * X.509 v3 extensions
951 *
952 * TODO: Perform all of the basic constraints tests required by the RFC
953 * TODO: Set values for undetected extensions to a sane default?
954 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000955 */
956static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000957 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000958 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000959{
Paul Bakker23986e52011-04-24 08:57:21 +0000960 int ret;
961 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000962 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000963
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000964 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000965 {
966 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
967 return( 0 );
968
969 return( ret );
970 }
971
Paul Bakker5121ce52009-01-03 21:22:43 +0000972 while( *p < end )
973 {
Paul Bakker74111d32011-01-15 16:57:55 +0000974 /*
975 * Extension ::= SEQUENCE {
976 * extnID OBJECT IDENTIFIER,
977 * critical BOOLEAN DEFAULT FALSE,
978 * extnValue OCTET STRING }
979 */
980 x509_buf extn_oid = {0, 0, NULL};
981 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200982 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000983
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 if( ( ret = asn1_get_tag( p, end, &len,
985 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000986 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000988 end_ext_data = *p + len;
989
Paul Bakker74111d32011-01-15 16:57:55 +0000990 /* Get extension ID */
991 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
Paul Bakker74111d32011-01-15 16:57:55 +0000993 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000994 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
Paul Bakker74111d32011-01-15 16:57:55 +0000996 extn_oid.p = *p;
997 *p += extn_oid.len;
998
999 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001000 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001001 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1002
1003 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001004 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001005 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001006 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Paul Bakker74111d32011-01-15 16:57:55 +00001008 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001009 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001011 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001013 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001014
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001015 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001016 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001017 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Paul Bakker74111d32011-01-15 16:57:55 +00001019 /*
1020 * Detect supported extensions
1021 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001022 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1023
1024 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001025 {
1026 /* No parser found, skip extension */
1027 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker5c721f92011-07-27 16:51:09 +00001029#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001030 if( is_critical )
1031 {
1032 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001033 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001034 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1035 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001036#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001037 continue;
1038 }
1039
1040 crt->ext_types |= ext_type;
1041
1042 switch( ext_type )
1043 {
1044 case EXT_BASIC_CONSTRAINTS:
1045 /* Parse basic constraints */
1046 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1047 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1048 return ( ret );
1049 break;
1050
1051 case EXT_KEY_USAGE:
1052 /* Parse key usage */
1053 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1054 &crt->key_usage ) ) != 0 )
1055 return ( ret );
1056 break;
1057
1058 case EXT_EXTENDED_KEY_USAGE:
1059 /* Parse extended key usage */
1060 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1061 &crt->ext_key_usage ) ) != 0 )
1062 return ( ret );
1063 break;
1064
1065 case EXT_SUBJECT_ALT_NAME:
1066 /* Parse subject alt name */
1067 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1068 &crt->subject_alt_names ) ) != 0 )
1069 return ( ret );
1070 break;
1071
1072 case EXT_NS_CERT_TYPE:
1073 /* Parse netscape certificate type */
1074 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1075 &crt->ns_cert_type ) ) != 0 )
1076 return ( ret );
1077 break;
1078
1079 default:
1080 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001081 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 }
1083
1084 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001085 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001086 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 return( 0 );
1089}
1090
1091/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001092 * X.509 CRL Entries
1093 */
1094static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001095 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001096 x509_crl_entry *entry )
1097{
Paul Bakker23986e52011-04-24 08:57:21 +00001098 int ret;
1099 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001100 x509_crl_entry *cur_entry = entry;
1101
1102 if( *p == end )
1103 return( 0 );
1104
Paul Bakker9be19372009-07-27 20:21:53 +00001105 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001106 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1107 {
1108 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1109 return( 0 );
1110
1111 return( ret );
1112 }
1113
Paul Bakker9be19372009-07-27 20:21:53 +00001114 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001115
1116 while( *p < end )
1117 {
Paul Bakker23986e52011-04-24 08:57:21 +00001118 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001119 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001120
1121 if( ( ret = asn1_get_tag( p, end, &len2,
1122 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1123 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001124 return( ret );
1125 }
1126
Paul Bakker9be19372009-07-27 20:21:53 +00001127 cur_entry->raw.tag = **p;
1128 cur_entry->raw.p = *p;
1129 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001130 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001131
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001132 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001133 return( ret );
1134
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001135 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001136 return( ret );
1137
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001138 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001139 return( ret );
1140
Paul Bakker74111d32011-01-15 16:57:55 +00001141 if ( *p < end )
1142 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001143 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001144
1145 if( cur_entry->next == NULL )
1146 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1147
Paul Bakkerd98030e2009-05-02 15:13:40 +00001148 cur_entry = cur_entry->next;
1149 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1150 }
1151 }
1152
1153 return( 0 );
1154}
1155
Paul Bakkerc70b9822013-04-07 22:00:46 +02001156static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1157 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001158{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001159 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001160
Paul Bakkerc70b9822013-04-07 22:00:46 +02001161 if( ret != 0 )
1162 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001163
Paul Bakkerc70b9822013-04-07 22:00:46 +02001164 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001165}
1166
Paul Bakkerd98030e2009-05-02 15:13:40 +00001167/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001168 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001170static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1171 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001172{
Paul Bakker23986e52011-04-24 08:57:21 +00001173 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001174 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001175 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
Paul Bakker320a4b52009-03-28 18:52:39 +00001177 /*
1178 * Check for valid input
1179 */
1180 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001181 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001182
Paul Bakker6e339b52013-07-03 13:37:05 +02001183 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001184
1185 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001186 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001187
1188 memcpy( p, buf, buflen );
1189
1190 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
1192 crt->raw.p = p;
1193 crt->raw.len = len;
1194 end = p + len;
1195
1196 /*
1197 * Certificate ::= SEQUENCE {
1198 * tbsCertificate TBSCertificate,
1199 * signatureAlgorithm AlgorithmIdentifier,
1200 * signatureValue BIT STRING }
1201 */
1202 if( ( ret = asn1_get_tag( &p, end, &len,
1203 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1204 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001205 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001206 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
1208
Paul Bakkerb00ca422012-09-25 12:10:00 +00001209 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001211 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001212 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001213 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001215 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001216
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 /*
1218 * TBSCertificate ::= SEQUENCE {
1219 */
1220 crt->tbs.p = p;
1221
1222 if( ( ret = asn1_get_tag( &p, end, &len,
1223 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1224 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001225 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001226 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 }
1228
1229 end = p + len;
1230 crt->tbs.len = end - crt->tbs.p;
1231
1232 /*
1233 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1234 *
1235 * CertificateSerialNumber ::= INTEGER
1236 *
1237 * signature AlgorithmIdentifier
1238 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001239 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1240 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1241 ( ret = x509_get_alg( &p, end, &crt->sig_oid1, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001243 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 return( ret );
1245 }
1246
1247 crt->version++;
1248
1249 if( crt->version > 3 )
1250 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001251 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001252 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 }
1254
Paul Bakkerc70b9822013-04-07 22:00:46 +02001255 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1256 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001258 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001259 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 }
1261
1262 /*
1263 * issuer Name
1264 */
1265 crt->issuer_raw.p = p;
1266
1267 if( ( ret = asn1_get_tag( &p, end, &len,
1268 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1269 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001270 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001271 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 }
1273
1274 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1275 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001276 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 return( ret );
1278 }
1279
1280 crt->issuer_raw.len = p - crt->issuer_raw.p;
1281
1282 /*
1283 * Validity ::= SEQUENCE {
1284 * notBefore Time,
1285 * notAfter Time }
1286 *
1287 */
1288 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1289 &crt->valid_to ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 return( ret );
1293 }
1294
1295 /*
1296 * subject Name
1297 */
1298 crt->subject_raw.p = p;
1299
1300 if( ( ret = asn1_get_tag( &p, end, &len,
1301 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1302 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001303 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001304 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 }
1306
Paul Bakkercefb3962012-06-27 11:51:09 +00001307 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001309 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 return( ret );
1311 }
1312
1313 crt->subject_raw.len = p - crt->subject_raw.p;
1314
1315 /*
1316 * SubjectPublicKeyInfo ::= SEQUENCE
1317 * algorithm AlgorithmIdentifier,
1318 * subjectPublicKey BIT STRING }
1319 */
1320 if( ( ret = asn1_get_tag( &p, end, &len,
1321 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1322 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001323 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001324 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
1327 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1328 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1329 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 return( ret );
1332 }
1333
1334 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 return( ret );
1338 }
1339
1340 crt->rsa.len = mpi_size( &crt->rsa.N );
1341
1342 /*
1343 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1344 * -- If present, version shall be v2 or v3
1345 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1346 * -- If present, version shall be v2 or v3
1347 * extensions [3] EXPLICIT Extensions OPTIONAL
1348 * -- If present, version shall be v3
1349 */
1350 if( crt->version == 2 || crt->version == 3 )
1351 {
1352 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1353 if( ret != 0 )
1354 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001355 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 return( ret );
1357 }
1358 }
1359
1360 if( crt->version == 2 || crt->version == 3 )
1361 {
1362 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1363 if( ret != 0 )
1364 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001365 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 return( ret );
1367 }
1368 }
1369
1370 if( crt->version == 3 )
1371 {
Paul Bakker74111d32011-01-15 16:57:55 +00001372 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 if( ret != 0 )
1374 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001375 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 return( ret );
1377 }
1378 }
1379
1380 if( p != end )
1381 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001382 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001383 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001384 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 }
1386
Paul Bakkerb00ca422012-09-25 12:10:00 +00001387 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
1389 /*
1390 * signatureAlgorithm AlgorithmIdentifier,
1391 * signatureValue BIT STRING
1392 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001393 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001395 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 return( ret );
1397 }
1398
Paul Bakker535e97d2012-08-23 10:49:55 +00001399 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1400 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001402 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001403 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 }
1405
1406 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1407 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001408 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 return( ret );
1410 }
1411
1412 if( p != end )
1413 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001414 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001415 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001416 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 }
1418
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001419 return( 0 );
1420}
1421
1422/*
Paul Bakker42c65812013-06-24 19:21:59 +02001423 * Parse one X.509 certificate in DER format from a buffer and add them to a
1424 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001425 */
Paul Bakker42c65812013-06-24 19:21:59 +02001426int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001427{
Paul Bakker42c65812013-06-24 19:21:59 +02001428 int ret;
1429 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001430
1431 /*
1432 * Check for valid input
1433 */
1434 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001435 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001436
1437 while( crt->version != 0 && crt->next != NULL )
1438 {
1439 prev = crt;
1440 crt = crt->next;
1441 }
1442
1443 /*
1444 * Add new certificate on the end of the chain if needed.
1445 */
1446 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001447 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001448 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001449
Paul Bakker7d06ad22009-05-02 15:53:56 +00001450 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001451 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001452
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001453 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001454 crt = crt->next;
1455 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001456 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Paul Bakker42c65812013-06-24 19:21:59 +02001458 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1459 {
1460 if( prev )
1461 prev->next = NULL;
1462
1463 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001464 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001465
1466 return( ret );
1467 }
1468
1469 return( 0 );
1470}
1471
1472/*
1473 * Parse one or more PEM certificates from a buffer and add them to the chained list
1474 */
1475int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1476{
1477 int ret, success = 0, first_error = 0, total_failed = 0;
1478 int buf_format = X509_FORMAT_DER;
1479
1480 /*
1481 * Check for valid input
1482 */
1483 if( chain == NULL || buf == NULL )
1484 return( POLARSSL_ERR_X509_INVALID_INPUT );
1485
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001486 /*
1487 * Determine buffer content. Buffer contains either one DER certificate or
1488 * one or more PEM certificates.
1489 */
1490#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001491 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001492 buf_format = X509_FORMAT_PEM;
1493#endif
1494
1495 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001496 return x509parse_crt_der( chain, buf, buflen );
1497
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001498#if defined(POLARSSL_PEM_C)
1499 if( buf_format == X509_FORMAT_PEM )
1500 {
1501 pem_context pem;
1502
1503 while( buflen > 0 )
1504 {
1505 size_t use_len;
1506 pem_init( &pem );
1507
1508 ret = pem_read_buffer( &pem,
1509 "-----BEGIN CERTIFICATE-----",
1510 "-----END CERTIFICATE-----",
1511 buf, NULL, 0, &use_len );
1512
1513 if( ret == 0 )
1514 {
1515 /*
1516 * Was PEM encoded
1517 */
1518 buflen -= use_len;
1519 buf += use_len;
1520 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001521 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1522 {
1523 return( ret );
1524 }
Paul Bakker00b28602013-06-24 13:02:41 +02001525 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001526 {
1527 pem_free( &pem );
1528
Paul Bakker5ed3b342013-06-24 19:05:46 +02001529 /*
1530 * PEM header and footer were found
1531 */
1532 buflen -= use_len;
1533 buf += use_len;
1534
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001535 if( first_error == 0 )
1536 first_error = ret;
1537
1538 continue;
1539 }
1540 else
1541 break;
1542
Paul Bakker42c65812013-06-24 19:21:59 +02001543 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001544
1545 pem_free( &pem );
1546
1547 if( ret != 0 )
1548 {
1549 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001550 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001551 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001552 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001553 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001554
1555 if( first_error == 0 )
1556 first_error = ret;
1557
Paul Bakker42c65812013-06-24 19:21:59 +02001558 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001559 continue;
1560 }
1561
1562 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001563 }
1564 }
1565#endif
1566
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001567 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001568 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001569 else if( first_error )
1570 return( first_error );
1571 else
1572 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001573}
1574
1575/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001576 * Parse one or more CRLs and add them to the chained list
1577 */
Paul Bakker23986e52011-04-24 08:57:21 +00001578int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001579{
Paul Bakker23986e52011-04-24 08:57:21 +00001580 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001581 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001582 unsigned char *p, *end;
1583 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001584#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001585 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001586 pem_context pem;
1587#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001588
1589 crl = chain;
1590
1591 /*
1592 * Check for valid input
1593 */
1594 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001595 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001596
1597 while( crl->version != 0 && crl->next != NULL )
1598 crl = crl->next;
1599
1600 /*
1601 * Add new CRL on the end of the chain if needed.
1602 */
1603 if ( crl->version != 0 && crl->next == NULL)
1604 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001605 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001606
Paul Bakker7d06ad22009-05-02 15:53:56 +00001607 if( crl->next == NULL )
1608 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001610 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001611 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612
Paul Bakker7d06ad22009-05-02 15:53:56 +00001613 crl = crl->next;
1614 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001615 }
1616
Paul Bakker96743fc2011-02-12 14:30:57 +00001617#if defined(POLARSSL_PEM_C)
1618 pem_init( &pem );
1619 ret = pem_read_buffer( &pem,
1620 "-----BEGIN X509 CRL-----",
1621 "-----END X509 CRL-----",
1622 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001623
Paul Bakker96743fc2011-02-12 14:30:57 +00001624 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001625 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001626 /*
1627 * Was PEM encoded
1628 */
1629 buflen -= use_len;
1630 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631
1632 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001633 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001635 p = pem.buf;
1636 pem.buf = NULL;
1637 len = pem.buflen;
1638 pem_free( &pem );
1639 }
Paul Bakker00b28602013-06-24 13:02:41 +02001640 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001641 {
1642 pem_free( &pem );
1643 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001644 }
1645 else
1646 {
1647 /*
1648 * nope, copy the raw DER data
1649 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001650 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001651
1652 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001653 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001654
1655 memcpy( p, buf, buflen );
1656
1657 buflen = 0;
1658 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001659#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001660 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001661
1662 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001663 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001664
1665 memcpy( p, buf, buflen );
1666
1667 buflen = 0;
1668#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001669
1670 crl->raw.p = p;
1671 crl->raw.len = len;
1672 end = p + len;
1673
1674 /*
1675 * CertificateList ::= SEQUENCE {
1676 * tbsCertList TBSCertList,
1677 * signatureAlgorithm AlgorithmIdentifier,
1678 * signatureValue BIT STRING }
1679 */
1680 if( ( ret = asn1_get_tag( &p, end, &len,
1681 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1682 {
1683 x509_crl_free( crl );
1684 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1685 }
1686
Paul Bakker23986e52011-04-24 08:57:21 +00001687 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001688 {
1689 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001690 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1692 }
1693
1694 /*
1695 * TBSCertList ::= SEQUENCE {
1696 */
1697 crl->tbs.p = p;
1698
1699 if( ( ret = asn1_get_tag( &p, end, &len,
1700 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1701 {
1702 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001703 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001704 }
1705
1706 end = p + len;
1707 crl->tbs.len = end - crl->tbs.p;
1708
1709 /*
1710 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1711 * -- if present, MUST be v2
1712 *
1713 * signature AlgorithmIdentifier
1714 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001715 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001716 ( ret = x509_get_alg( &p, end, &crl->sig_oid1, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001717 {
1718 x509_crl_free( crl );
1719 return( ret );
1720 }
1721
1722 crl->version++;
1723
1724 if( crl->version > 2 )
1725 {
1726 x509_crl_free( crl );
1727 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1728 }
1729
Paul Bakkerc70b9822013-04-07 22:00:46 +02001730 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1731 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001732 {
1733 x509_crl_free( crl );
1734 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1735 }
1736
1737 /*
1738 * issuer Name
1739 */
1740 crl->issuer_raw.p = p;
1741
1742 if( ( ret = asn1_get_tag( &p, end, &len,
1743 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1744 {
1745 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001746 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001747 }
1748
1749 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1750 {
1751 x509_crl_free( crl );
1752 return( ret );
1753 }
1754
1755 crl->issuer_raw.len = p - crl->issuer_raw.p;
1756
1757 /*
1758 * thisUpdate Time
1759 * nextUpdate Time OPTIONAL
1760 */
Paul Bakker91200182010-02-18 21:26:15 +00001761 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001762 {
1763 x509_crl_free( crl );
1764 return( ret );
1765 }
1766
Paul Bakker91200182010-02-18 21:26:15 +00001767 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001768 {
Paul Bakker9d781402011-05-09 16:17:09 +00001769 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001770 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001771 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001772 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001773 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001774 x509_crl_free( crl );
1775 return( ret );
1776 }
1777 }
1778
1779 /*
1780 * revokedCertificates SEQUENCE OF SEQUENCE {
1781 * userCertificate CertificateSerialNumber,
1782 * revocationDate Time,
1783 * crlEntryExtensions Extensions OPTIONAL
1784 * -- if present, MUST be v2
1785 * } OPTIONAL
1786 */
1787 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1788 {
1789 x509_crl_free( crl );
1790 return( ret );
1791 }
1792
1793 /*
1794 * crlExtensions EXPLICIT Extensions OPTIONAL
1795 * -- if present, MUST be v2
1796 */
1797 if( crl->version == 2 )
1798 {
1799 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1800
1801 if( ret != 0 )
1802 {
1803 x509_crl_free( crl );
1804 return( ret );
1805 }
1806 }
1807
1808 if( p != end )
1809 {
1810 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001811 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001812 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1813 }
1814
1815 end = crl->raw.p + crl->raw.len;
1816
1817 /*
1818 * signatureAlgorithm AlgorithmIdentifier,
1819 * signatureValue BIT STRING
1820 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001821 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001822 {
1823 x509_crl_free( crl );
1824 return( ret );
1825 }
1826
Paul Bakker535e97d2012-08-23 10:49:55 +00001827 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1828 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001829 {
1830 x509_crl_free( crl );
1831 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1832 }
1833
1834 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1835 {
1836 x509_crl_free( crl );
1837 return( ret );
1838 }
1839
1840 if( p != end )
1841 {
1842 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001843 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001844 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1845 }
1846
1847 if( buflen > 0 )
1848 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001849 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850
Paul Bakker7d06ad22009-05-02 15:53:56 +00001851 if( crl->next == NULL )
1852 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001853 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001854 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001855 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001856
Paul Bakker7d06ad22009-05-02 15:53:56 +00001857 crl = crl->next;
1858 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001859
1860 return( x509parse_crl( crl, buf, buflen ) );
1861 }
1862
1863 return( 0 );
1864}
1865
Paul Bakker335db3f2011-04-25 15:28:35 +00001866#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001867/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001868 * Load all data from a file into a given buffer.
1869 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001870static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001871{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001872 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001873
Paul Bakkerd98030e2009-05-02 15:13:40 +00001874 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001875 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001876
Paul Bakkerd98030e2009-05-02 15:13:40 +00001877 fseek( f, 0, SEEK_END );
1878 *n = (size_t) ftell( f );
1879 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001880
Paul Bakker6e339b52013-07-03 13:37:05 +02001881 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001882 {
1883 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001884 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001885 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001886
Paul Bakkerd98030e2009-05-02 15:13:40 +00001887 if( fread( *buf, 1, *n, f ) != *n )
1888 {
1889 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001890 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001891 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001892 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001893
Paul Bakkerd98030e2009-05-02 15:13:40 +00001894 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001895
Paul Bakkerd98030e2009-05-02 15:13:40 +00001896 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001897
Paul Bakkerd98030e2009-05-02 15:13:40 +00001898 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001899}
1900
1901/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 * Load one or more certificates and add them to the chained list
1903 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001904int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001905{
1906 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 size_t n;
1908 unsigned char *buf;
1909
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001910 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001911 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
Paul Bakker69e095c2011-12-10 21:55:01 +00001913 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
1915 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001916 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
1918 return( ret );
1919}
1920
Paul Bakker8d914582012-06-04 12:46:42 +00001921int x509parse_crtpath( x509_cert *chain, const char *path )
1922{
1923 int ret = 0;
1924#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001925 int w_ret;
1926 WCHAR szDir[MAX_PATH];
1927 char filename[MAX_PATH];
1928 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001929 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001930
Paul Bakker97872ac2012-11-02 12:53:26 +00001931 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001932 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001933
1934 if( len > MAX_PATH - 3 )
1935 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001936
Paul Bakker3338b792012-10-01 21:13:10 +00001937 memset( szDir, 0, sizeof(szDir) );
1938 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001939 memcpy( filename, path, len );
1940 filename[len++] = '\\';
1941 p = filename + len;
1942 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001943
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001944 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001945
Paul Bakker97872ac2012-11-02 12:53:26 +00001946 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001947 if (hFind == INVALID_HANDLE_VALUE)
1948 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1949
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001950 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001951 do
1952 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001953 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001954
Paul Bakkere4791f32012-06-04 21:29:15 +00001955 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001956 continue;
1957
Paul Bakker3338b792012-10-01 21:13:10 +00001958 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1959 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001960 p, len - 1,
1961 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001962
Paul Bakker3338b792012-10-01 21:13:10 +00001963 w_ret = x509parse_crtfile( chain, filename );
1964 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001965 ret++;
1966 else
1967 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001968 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001969 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001970
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001971 if (GetLastError() != ERROR_NO_MORE_FILES)
1972 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001973
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001974cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001975 FindClose( hFind );
1976#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001977 int t_ret, i;
1978 struct stat sb;
1979 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001980 char entry_name[255];
1981 DIR *dir = opendir( path );
1982
1983 if( dir == NULL)
1984 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1985
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001986 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001987 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001988 if( result == NULL )
1989 break;
1990
1991 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1992
1993 i = stat( entry_name, &sb );
1994
1995 if( i == -1 )
1996 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1997
1998 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00001999 continue;
2000
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002001 // Ignore parse errors
2002 //
Paul Bakker8d914582012-06-04 12:46:42 +00002003 t_ret = x509parse_crtfile( chain, entry_name );
2004 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002005 ret++;
2006 else
2007 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002008 }
2009 closedir( dir );
2010#endif
2011
2012 return( ret );
2013}
2014
Paul Bakkerd98030e2009-05-02 15:13:40 +00002015/*
2016 * Load one or more CRLs and add them to the chained list
2017 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002018int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002019{
2020 int ret;
2021 size_t n;
2022 unsigned char *buf;
2023
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002024 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002025 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002026
Paul Bakker27fdf462011-06-09 13:55:13 +00002027 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002028
2029 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002030 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002031
2032 return( ret );
2033}
2034
Paul Bakker5121ce52009-01-03 21:22:43 +00002035/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002036 * Load and parse a private RSA key
2037 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002038int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002039{
2040 int ret;
2041 size_t n;
2042 unsigned char *buf;
2043
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002044 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002045 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002046
2047 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002048 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002049 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002050 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002051 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002052
2053 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002054 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002055
2056 return( ret );
2057}
2058
2059/*
2060 * Load and parse a public RSA key
2061 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002062int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002063{
2064 int ret;
2065 size_t n;
2066 unsigned char *buf;
2067
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002068 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002069 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002070
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002071 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002072
2073 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002074 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002075
2076 return( ret );
2077}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002078
2079#if defined(POLARSSL_ECP_C)
2080/*
2081 * Load and parse a private EC key
2082 */
2083int x509parse_keyfile_ec( ecp_keypair *eckey,
2084 const char *path, const char *pwd )
2085{
2086 int ret;
2087 size_t n;
2088 unsigned char *buf;
2089
2090 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2091 return( ret );
2092
2093 if( pwd == NULL )
2094 ret = x509parse_key_ec( eckey, buf, n, NULL, 0 );
2095 else
2096 ret = x509parse_key_ec( eckey, buf, n,
2097 (const unsigned char *) pwd, strlen( pwd ) );
2098
2099 memset( buf, 0, n + 1 );
2100 free( buf );
2101
2102 return( ret );
2103}
2104
2105/*
2106 * Load and parse a public EC key
2107 */
2108int x509parse_public_keyfile_ec( ecp_keypair *eckey, const char *path )
2109{
2110 int ret;
2111 size_t n;
2112 unsigned char *buf;
2113
2114 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2115 return( ret );
2116
2117 ret = x509parse_public_key_ec( eckey, buf, n );
2118
2119 memset( buf, 0, n + 1 );
2120 free( buf );
2121
2122 return( ret );
2123}
2124#endif /* defined(POLARSSL_ECP_C) */
Paul Bakker335db3f2011-04-25 15:28:35 +00002125#endif /* POLARSSL_FS_IO */
2126
2127/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002128 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002130static int x509parse_key_pkcs1_der( rsa_context *rsa,
2131 const unsigned char *key,
2132 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002133{
Paul Bakker23986e52011-04-24 08:57:21 +00002134 int ret;
2135 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002137
Paul Bakker96743fc2011-02-12 14:30:57 +00002138 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002139 end = p + keylen;
2140
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002142 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002143 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 * RSAPrivateKey ::= SEQUENCE {
2145 * version Version,
2146 * modulus INTEGER, -- n
2147 * publicExponent INTEGER, -- e
2148 * privateExponent INTEGER, -- d
2149 * prime1 INTEGER, -- p
2150 * prime2 INTEGER, -- q
2151 * exponent1 INTEGER, -- d mod (p-1)
2152 * exponent2 INTEGER, -- d mod (q-1)
2153 * coefficient INTEGER, -- (inverse of q) mod p
2154 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2155 * }
2156 */
2157 if( ( ret = asn1_get_tag( &p, end, &len,
2158 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2159 {
Paul Bakker9d781402011-05-09 16:17:09 +00002160 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 }
2162
2163 end = p + len;
2164
2165 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2166 {
Paul Bakker9d781402011-05-09 16:17:09 +00002167 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 }
2169
2170 if( rsa->ver != 0 )
2171 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002172 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 }
2174
2175 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2176 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2177 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2178 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2179 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2180 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2181 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2182 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2183 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002185 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 }
2187
2188 rsa->len = mpi_size( &rsa->N );
2189
2190 if( p != end )
2191 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002193 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002194 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 }
2196
2197 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2198 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 rsa_free( rsa );
2200 return( ret );
2201 }
2202
Paul Bakkere2f50402013-06-24 19:00:59 +02002203 return( 0 );
2204}
2205
2206/*
2207 * Parse an unencrypted PKCS#8 encoded private RSA key
2208 */
2209static int x509parse_key_pkcs8_unencrypted_der(
2210 rsa_context *rsa,
2211 const unsigned char *key,
2212 size_t keylen )
2213{
2214 int ret;
2215 size_t len;
2216 unsigned char *p, *end;
2217 x509_buf pk_alg_oid;
2218 pk_type_t pk_alg = POLARSSL_PK_NONE;
2219
2220 p = (unsigned char *) key;
2221 end = p + keylen;
2222
2223 /*
2224 * This function parses the PrivatKeyInfo object (PKCS#8)
2225 *
2226 * PrivateKeyInfo ::= SEQUENCE {
2227 * version Version,
2228 * algorithm AlgorithmIdentifier,
2229 * PrivateKey BIT STRING
2230 * }
2231 *
2232 * AlgorithmIdentifier ::= SEQUENCE {
2233 * algorithm OBJECT IDENTIFIER,
2234 * parameters ANY DEFINED BY algorithm OPTIONAL
2235 * }
2236 *
2237 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2238 */
2239 if( ( ret = asn1_get_tag( &p, end, &len,
2240 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2241 {
2242 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2243 }
2244
2245 end = p + len;
2246
2247 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2248 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2249
2250 if( rsa->ver != 0 )
2251 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2252
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002253 if( ( ret = asn1_get_alg_null( &p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002254 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2255
2256 /*
2257 * only RSA keys handled at this time
2258 */
2259 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2260 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2261
2262 /*
2263 * Get the OCTET STRING and parse the PKCS#1 format inside
2264 */
2265 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2266 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2267
2268 if( ( end - p ) < 1 )
2269 {
2270 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2271 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2272 }
2273
2274 end = p + len;
2275
2276 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2277 return( ret );
2278
2279 return( 0 );
2280}
2281
2282/*
Paul Bakkerbda7cb72013-06-24 19:34:25 +02002283 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002284 */
2285static int x509parse_key_pkcs8_encrypted_der(
2286 rsa_context *rsa,
2287 const unsigned char *key,
2288 size_t keylen,
2289 const unsigned char *pwd,
2290 size_t pwdlen )
2291{
2292 int ret;
2293 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002294 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002295 x509_buf pbe_alg_oid, pbe_params;
2296 unsigned char buf[2048];
Paul Bakker7749a222013-06-28 17:28:20 +02002297#if defined(POLARSSL_PKCS12_C)
2298 cipher_type_t cipher_alg;
2299 md_type_t md_alg;
2300#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002301
2302 memset(buf, 0, 2048);
2303
2304 p = (unsigned char *) key;
2305 end = p + keylen;
2306
Paul Bakker28144de2013-06-24 19:28:55 +02002307 if( pwdlen == 0 )
2308 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2309
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002310 /*
2311 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2312 *
2313 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2314 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2315 * encryptedData EncryptedData
2316 * }
2317 *
2318 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2319 *
2320 * EncryptedData ::= OCTET STRING
2321 *
2322 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2323 */
2324 if( ( ret = asn1_get_tag( &p, end, &len,
2325 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2326 {
2327 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2328 }
2329
2330 end = p + len;
2331
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002332 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002333 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002334
2335 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2336 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2337
2338 // buf has been sized to 2048 bytes
2339 if( len > 2048 )
2340 return( POLARSSL_ERR_X509_INVALID_INPUT );
2341
2342 /*
2343 * Decrypt EncryptedData with appropriate PDE
2344 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002345#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002346 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002347 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002348 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002349 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002350 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002351 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002352 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2353 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2354
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002355 return( ret );
2356 }
2357 }
2358 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2359 {
2360 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2361 PKCS12_PBE_DECRYPT,
2362 pwd, pwdlen,
2363 p, len, buf ) ) != 0 )
2364 {
2365 return( ret );
2366 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002367
2368 // Best guess for password mismatch when using RC4. If first tag is
2369 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2370 //
2371 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2372 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002373 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002374 else
2375#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002376#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002377 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002378 {
2379 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2380 p, len, buf ) ) != 0 )
2381 {
2382 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2383 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2384
2385 return( ret );
2386 }
2387 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002388 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002389#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002390 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2391
2392 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2393}
2394
2395/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002396 * Parse a private RSA key
2397 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002398int x509parse_key_rsa( rsa_context *rsa,
2399 const unsigned char *key, size_t keylen,
2400 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002401{
2402 int ret;
2403
Paul Bakker96743fc2011-02-12 14:30:57 +00002404#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002405 size_t len;
2406 pem_context pem;
2407
2408 pem_init( &pem );
2409 ret = pem_read_buffer( &pem,
2410 "-----BEGIN RSA PRIVATE KEY-----",
2411 "-----END RSA PRIVATE KEY-----",
2412 key, pwd, pwdlen, &len );
2413 if( ret == 0 )
2414 {
2415 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2416 {
2417 rsa_free( rsa );
2418 }
2419
2420 pem_free( &pem );
2421 return( ret );
2422 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002423 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2424 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2425 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2426 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002427 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002428 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002429
2430 ret = pem_read_buffer( &pem,
2431 "-----BEGIN PRIVATE KEY-----",
2432 "-----END PRIVATE KEY-----",
2433 key, NULL, 0, &len );
2434 if( ret == 0 )
2435 {
2436 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2437 pem.buf, pem.buflen ) ) != 0 )
2438 {
2439 rsa_free( rsa );
2440 }
2441
2442 pem_free( &pem );
2443 return( ret );
2444 }
2445 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002446 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002447
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002448 ret = pem_read_buffer( &pem,
2449 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2450 "-----END ENCRYPTED PRIVATE KEY-----",
2451 key, NULL, 0, &len );
2452 if( ret == 0 )
2453 {
2454 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2455 pem.buf, pem.buflen,
2456 pwd, pwdlen ) ) != 0 )
2457 {
2458 rsa_free( rsa );
2459 }
2460
2461 pem_free( &pem );
2462 return( ret );
2463 }
2464 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002465 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002466#else
2467 ((void) pwd);
2468 ((void) pwdlen);
2469#endif /* POLARSSL_PEM_C */
2470
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002471 /*
2472 * At this point we only know it's not a PEM formatted key. Could be any
2473 * of the known DER encoded private key formats
2474 *
2475 * We try the different DER format parsers to see if one passes without
2476 * error
2477 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002478 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2479 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002480 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002481 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002484 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002485
2486 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2487 {
2488 return( ret );
2489 }
2490
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002491 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2492 return( 0 );
2493
2494 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002495
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002496 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2497 return( 0 );
2498
2499 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002500
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002501 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502}
2503
2504/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002505 * Parse a public RSA key
2506 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002507int x509parse_public_key_rsa( rsa_context *rsa,
2508 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002509{
Paul Bakker23986e52011-04-24 08:57:21 +00002510 int ret;
2511 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002512 unsigned char *p, *end;
2513 x509_buf alg_oid;
2514#if defined(POLARSSL_PEM_C)
2515 pem_context pem;
2516
2517 pem_init( &pem );
2518 ret = pem_read_buffer( &pem,
2519 "-----BEGIN PUBLIC KEY-----",
2520 "-----END PUBLIC KEY-----",
2521 key, NULL, 0, &len );
2522
2523 if( ret == 0 )
2524 {
2525 /*
2526 * Was PEM encoded
2527 */
2528 keylen = pem.buflen;
2529 }
Paul Bakker00b28602013-06-24 13:02:41 +02002530 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002531 {
2532 pem_free( &pem );
2533 return( ret );
2534 }
2535
2536 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2537#else
2538 p = (unsigned char *) key;
2539#endif
2540 end = p + keylen;
2541
2542 /*
2543 * PublicKeyInfo ::= SEQUENCE {
2544 * algorithm AlgorithmIdentifier,
2545 * PublicKey BIT STRING
2546 * }
2547 *
2548 * AlgorithmIdentifier ::= SEQUENCE {
2549 * algorithm OBJECT IDENTIFIER,
2550 * parameters ANY DEFINED BY algorithm OPTIONAL
2551 * }
2552 *
2553 * RSAPublicKey ::= SEQUENCE {
2554 * modulus INTEGER, -- n
2555 * publicExponent INTEGER -- e
2556 * }
2557 */
2558
2559 if( ( ret = asn1_get_tag( &p, end, &len,
2560 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2561 {
2562#if defined(POLARSSL_PEM_C)
2563 pem_free( &pem );
2564#endif
2565 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002566 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002567 }
2568
2569 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2570 {
2571#if defined(POLARSSL_PEM_C)
2572 pem_free( &pem );
2573#endif
2574 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002575 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002576 }
2577
2578 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2579 {
2580#if defined(POLARSSL_PEM_C)
2581 pem_free( &pem );
2582#endif
2583 rsa_free( rsa );
2584 return( ret );
2585 }
2586
2587 rsa->len = mpi_size( &rsa->N );
2588
2589#if defined(POLARSSL_PEM_C)
2590 pem_free( &pem );
2591#endif
2592
2593 return( 0 );
2594}
2595
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002596#if defined(POLARSSL_ECP_C)
2597/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002598 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002599 */
2600static int x509parse_key_sec1_der( ecp_keypair *eck,
2601 const unsigned char *key,
2602 size_t keylen )
2603{
2604 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002605 int version;
2606 size_t len;
2607 ecp_group_id grp_id;
2608 unsigned char *p = (unsigned char *) key;
2609 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002610
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002611 /*
2612 * RFC 5915, orf SEC1 Appendix C.4
2613 *
2614 * ECPrivateKey ::= SEQUENCE {
2615 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2616 * privateKey OCTET STRING,
2617 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2618 * publicKey [1] BIT STRING OPTIONAL
2619 * }
2620 */
2621 if( ( ret = asn1_get_tag( &p, end, &len,
2622 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2623 {
2624 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2625 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002626
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002627 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002628
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002629 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2630 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002631
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002632 if( version != 1 )
2633 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2634
2635 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2636 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2637
2638 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2639 {
2640 ecp_keypair_free( eck );
2641 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2642 }
2643
2644 p += len;
2645
2646 /*
2647 * Is 'parameters' present?
2648 */
2649 if( ( ret = asn1_get_tag( &p, end, &len,
2650 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2651 {
2652 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2653 return( ret );
2654
2655 /* TODO: grp may not be empty at this point,
2656 * if we're wrapped inside a PKCS#8 structure: check consistency */
2657 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2658 {
2659 ecp_keypair_free( eck );
2660 return( ret );
2661 }
2662 }
2663 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2664 {
2665 ecp_keypair_free( eck );
2666 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2667 }
2668
2669 /*
2670 * Is 'publickey' present?
2671 */
2672 if( ( ret = asn1_get_tag( &p, end, &len,
2673 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2674 {
2675 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2676 != 0 )
2677 {
2678 ecp_keypair_free( eck );
2679 return( ret );
2680 }
2681
2682 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2683 {
2684 ecp_keypair_free( eck );
2685 return( ret );
2686 }
2687 }
2688 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2689 {
2690 ecp_keypair_free( eck );
2691 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2692 }
2693
2694 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2695 {
2696 ecp_keypair_free( eck );
2697 return( ret );
2698 }
2699
2700 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002701}
2702
2703/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002704 * Parse an unencrypted PKCS#8 encoded private EC key
2705 */
2706static int x509parse_key_pkcs8_unencrypted_der_ec(
2707 ecp_keypair *eck,
2708 const unsigned char* key,
2709 size_t keylen )
2710{
2711 int ret, version;
2712 size_t len;
2713 x509_buf pk_alg_oid;
2714 ecp_group_id grp_id;
2715 const unsigned char *params_end;
2716 unsigned char *p = (unsigned char *) key;
2717 unsigned char *end = p + keylen;
2718 pk_type_t pk_alg = POLARSSL_PK_NONE;
2719
2720 /*
2721 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2722 *
2723 * PrivateKeyInfo ::= SEQUENCE {
2724 * version Version,
2725 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2726 * privateKey PrivateKey,
2727 * attributes [0] IMPLICIT Attributes OPTIONAL }
2728 *
2729 * Version ::= INTEGER
2730 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2731 * PrivateKey ::= OCTET STRING
2732 *
2733 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2734 */
2735
2736 if( ( ret = asn1_get_tag( &p, end, &len,
2737 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2738 {
2739 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2740 }
2741
2742 end = p + len;
2743
2744 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2745 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2746
2747 if( version != 0 )
2748 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2749
2750 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid, &params_end ) ) != 0 )
2751 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2752
2753 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2754 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2755
2756 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
2757 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2758
2759 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2760 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2761
2762 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2763 {
2764 ecp_keypair_free( eck );
2765 return( ret );
2766 }
2767
2768 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2769 {
2770 ecp_keypair_free( eck );
2771 return( ret );
2772 }
2773
2774 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2775 {
2776 ecp_keypair_free( eck );
2777 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2778 }
2779
2780 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2781 {
2782 ecp_keypair_free( eck );
2783 return( ret );
2784 }
2785
2786 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2787 {
2788 ecp_keypair_free( eck );
2789 return( ret );
2790 }
2791
2792 return 0;
2793}
2794
2795/*
2796 * Parse an encrypted PKCS#8 encoded private EC key
2797 */
2798static int x509parse_key_pkcs8_encrypted_der_ec(
2799 ecp_keypair *eck,
2800 const unsigned char *key,
2801 size_t keylen,
2802 const unsigned char *pwd,
2803 size_t pwdlen )
2804{
2805 int ret;
2806
2807 (void) key;
2808 (void) keylen;
2809 (void) pwd;
2810 (void) pwdlen;
2811
2812 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2813 {
2814 ecp_keypair_free( eck );
2815 return( ret );
2816 }
2817
2818 return 0;
2819}
2820
2821/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002822 * Parse a private EC key
2823 */
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002824int x509parse_key_ec( ecp_keypair *eck,
2825 const unsigned char *key, size_t keylen,
2826 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002827{
2828 int ret;
2829
2830#if defined(POLARSSL_PEM_C)
2831 size_t len;
2832 pem_context pem;
2833
2834 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002835 ret = pem_read_buffer( &pem,
2836 "-----BEGIN EC PRIVATE KEY-----",
2837 "-----END EC PRIVATE KEY-----",
2838 key, pwd, pwdlen, &len );
2839 if( ret == 0 )
2840 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002841 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002842 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002843 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002844 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002845
2846 pem_free( &pem );
2847 return( ret );
2848 }
2849 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2850 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2851 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2852 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2853 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2854 return( ret );
2855
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002856 ret = pem_read_buffer( &pem,
2857 "-----BEGIN PRIVATE KEY-----",
2858 "-----END PRIVATE KEY-----",
2859 key, NULL, 0, &len );
2860 if( ret == 0 )
2861 {
2862 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2863 pem.buf, pem.buflen ) ) != 0 )
2864 {
2865 ecp_keypair_free( eck );
2866 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002867
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002868 pem_free( &pem );
2869 return( ret );
2870 }
2871 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2872 return( ret );
2873
2874 ret = pem_read_buffer( &pem,
2875 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2876 "-----END ENCRYPTED PRIVATE KEY-----",
2877 key, NULL, 0, &len );
2878 if( ret == 0 )
2879 {
2880 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2881 pem.buf, pem.buflen,
2882 pwd, pwdlen ) ) != 0 )
2883 {
2884 ecp_keypair_free( eck );
2885 }
2886
2887 pem_free( &pem );
2888 return( ret );
2889 }
2890 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2891 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002892#else
2893 ((void) pwd);
2894 ((void) pwdlen);
2895#endif /* POLARSSL_PEM_C */
2896
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002897 /*
2898 * At this point we only know it's not a PEM formatted key. Could be any
2899 * of the known DER encoded private key formats
2900 *
2901 * We try the different DER format parsers to see if one passes without
2902 * error
2903 */
2904 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2905 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002906 {
2907 return( 0 );
2908 }
2909
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002910 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002911
2912 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2913 {
2914 return( ret );
2915 }
2916
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002917 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2918 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002919 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002920
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002921 ecp_keypair_free( eck );
2922
2923 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2924 return( 0 );
2925
2926 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002927
2928 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2929}
2930
2931/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002932 * Parse a public EC key in RFC 5480 format, der-encoded
2933 */
2934static int x509parse_public_key_ec_der( ecp_keypair *key,
2935 const unsigned char *buf, size_t len )
2936{
2937 int ret;
2938 ecp_group_id grp_id;
2939 x509_buf alg_oid;
2940 pk_type_t alg = POLARSSL_PK_NONE;
2941 unsigned char *p = (unsigned char *) buf;
2942 unsigned char *end = p + len;
2943 const unsigned char *params_end;
2944 /*
2945 * SubjectPublicKeyInfo ::= SEQUENCE {
2946 * algorithm AlgorithmIdentifier,
2947 * subjectPublicKey BIT STRING
2948 * }
2949 * -- algorithm parameters are ECParameters
2950 * -- subjectPublicKey is an ECPoint
2951 */
2952 if( ( ret = asn1_get_tag( &p, end, &len,
2953 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2954 {
2955 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
2956 }
2957
2958 if( ( ret = x509_get_alg( &p, end, &alg_oid, &params_end ) ) != 0 )
2959 return( ret );
2960
2961 if( oid_get_pk_alg( &alg_oid, &alg ) != 0 )
2962 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2963
2964 if( alg != POLARSSL_PK_ECKEY && alg != POLARSSL_PK_ECKEY_DH )
2965 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2966
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002967 if( alg == POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002968 key->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2969
2970 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2971 return( ret );
2972
2973 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
2974 return( ret );
2975
2976 if( ( ret = x509_get_subpubkey_ec( &p, end, &key->grp, &key->Q ) ) != 0 )
2977 {
2978 return( ret );
2979 }
2980
2981 return( 0 );
2982}
2983
2984/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002985 * Parse a public EC key
2986 */
2987int x509parse_public_key_ec( ecp_keypair *eckey,
2988 const unsigned char *key, size_t keylen )
2989{
2990 int ret;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002991#if defined(POLARSSL_PEM_C)
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002992 size_t len;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002993 pem_context pem;
2994
2995 pem_init( &pem );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002996 ret = pem_read_buffer( &pem,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002997 "-----BEGIN PUBLIC KEY-----",
2998 "-----END PUBLIC KEY-----",
2999 key, NULL, 0, &len );
3000
3001 if( ret == 0 )
3002 {
3003 /*
3004 * Was PEM encoded
3005 */
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003006 key = pem.buf;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003007 keylen = pem.buflen;
3008 }
3009 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3010 {
3011 pem_free( &pem );
3012 return( ret );
3013 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003014#endif
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003015
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003016 if( ( ret = x509parse_public_key_ec_der ( eckey, key, keylen ) ) != 0 ||
3017 ( ret = ecp_check_pubkey( &eckey->grp, &eckey->Q ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003018 {
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003019 ecp_keypair_free( eckey );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003020 }
3021
3022#if defined(POLARSSL_PEM_C)
3023 pem_free( &pem );
3024#endif
3025
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003026 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003027}
3028#endif /* defined(POLARSSL_ECP_C) */
3029
Paul Bakkereaa89f82011-04-04 21:36:15 +00003030#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003031/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003032 * Parse DHM parameters
3033 */
Paul Bakker23986e52011-04-24 08:57:21 +00003034int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003035{
Paul Bakker23986e52011-04-24 08:57:21 +00003036 int ret;
3037 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003038 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003039#if defined(POLARSSL_PEM_C)
3040 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003041
Paul Bakker96743fc2011-02-12 14:30:57 +00003042 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003043
Paul Bakker96743fc2011-02-12 14:30:57 +00003044 ret = pem_read_buffer( &pem,
3045 "-----BEGIN DH PARAMETERS-----",
3046 "-----END DH PARAMETERS-----",
3047 dhmin, NULL, 0, &dhminlen );
3048
3049 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003050 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003051 /*
3052 * Was PEM encoded
3053 */
3054 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003055 }
Paul Bakker00b28602013-06-24 13:02:41 +02003056 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003057 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003058 pem_free( &pem );
3059 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003060 }
3061
Paul Bakker96743fc2011-02-12 14:30:57 +00003062 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3063#else
3064 p = (unsigned char *) dhmin;
3065#endif
3066 end = p + dhminlen;
3067
Paul Bakker1b57b062011-01-06 15:48:19 +00003068 memset( dhm, 0, sizeof( dhm_context ) );
3069
Paul Bakker1b57b062011-01-06 15:48:19 +00003070 /*
3071 * DHParams ::= SEQUENCE {
3072 * prime INTEGER, -- P
3073 * generator INTEGER, -- g
3074 * }
3075 */
3076 if( ( ret = asn1_get_tag( &p, end, &len,
3077 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3078 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003079#if defined(POLARSSL_PEM_C)
3080 pem_free( &pem );
3081#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003082 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003083 }
3084
3085 end = p + len;
3086
3087 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3088 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3089 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003090#if defined(POLARSSL_PEM_C)
3091 pem_free( &pem );
3092#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003093 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003094 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003095 }
3096
3097 if( p != end )
3098 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003099#if defined(POLARSSL_PEM_C)
3100 pem_free( &pem );
3101#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003102 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003103 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003104 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3105 }
3106
Paul Bakker96743fc2011-02-12 14:30:57 +00003107#if defined(POLARSSL_PEM_C)
3108 pem_free( &pem );
3109#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003110
3111 return( 0 );
3112}
3113
Paul Bakker335db3f2011-04-25 15:28:35 +00003114#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003115/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003116 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003117 */
3118int x509parse_dhmfile( dhm_context *dhm, const char *path )
3119{
3120 int ret;
3121 size_t n;
3122 unsigned char *buf;
3123
Paul Bakker69e095c2011-12-10 21:55:01 +00003124 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3125 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003126
Paul Bakker27fdf462011-06-09 13:55:13 +00003127 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003128
3129 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003130 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003131
3132 return( ret );
3133}
Paul Bakker335db3f2011-04-25 15:28:35 +00003134#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003135#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003136
Paul Bakker5121ce52009-01-03 21:22:43 +00003137#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003138#include <stdarg.h>
3139
3140#if !defined vsnprintf
3141#define vsnprintf _vsnprintf
3142#endif // vsnprintf
3143
3144/*
3145 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3146 * Result value is not size of buffer needed, but -1 if no fit is possible.
3147 *
3148 * This fuction tries to 'fix' this by at least suggesting enlarging the
3149 * size by 20.
3150 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003151static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003152{
3153 va_list ap;
3154 int res = -1;
3155
3156 va_start( ap, format );
3157
3158 res = vsnprintf( str, size, format, ap );
3159
3160 va_end( ap );
3161
3162 // No quick fix possible
3163 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003164 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003165
3166 return res;
3167}
3168
3169#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003170#endif
3171
Paul Bakkerd98030e2009-05-02 15:13:40 +00003172#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3173
3174#define SAFE_SNPRINTF() \
3175{ \
3176 if( ret == -1 ) \
3177 return( -1 ); \
3178 \
Paul Bakker23986e52011-04-24 08:57:21 +00003179 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003180 p[n - 1] = '\0'; \
3181 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3182 } \
3183 \
Paul Bakker23986e52011-04-24 08:57:21 +00003184 n -= (unsigned int) ret; \
3185 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003186}
3187
Paul Bakker5121ce52009-01-03 21:22:43 +00003188/*
3189 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003190 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003192int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003193{
Paul Bakker23986e52011-04-24 08:57:21 +00003194 int ret;
3195 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003197 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003198 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 char s[128], *p;
3200
3201 memset( s, 0, sizeof( s ) );
3202
3203 name = dn;
3204 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003205 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003206
3207 while( name != NULL )
3208 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003209 if( !name->oid.p )
3210 {
3211 name = name->next;
3212 continue;
3213 }
3214
Paul Bakker74111d32011-01-15 16:57:55 +00003215 if( name != dn )
3216 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003217 ret = snprintf( p, n, ", " );
3218 SAFE_SNPRINTF();
3219 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Paul Bakkerc70b9822013-04-07 22:00:46 +02003221 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Paul Bakkerc70b9822013-04-07 22:00:46 +02003223 if( ret == 0 )
3224 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003226 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003227 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
3229 for( i = 0; i < name->val.len; i++ )
3230 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003231 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 break;
3233
3234 c = name->val.p[i];
3235 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3236 s[i] = '?';
3237 else s[i] = c;
3238 }
3239 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003240 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003241 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 name = name->next;
3243 }
3244
Paul Bakker23986e52011-04-24 08:57:21 +00003245 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246}
3247
3248/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003249 * Store the serial in printable form into buf; no more
3250 * than size characters will be written
3251 */
3252int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3253{
Paul Bakker23986e52011-04-24 08:57:21 +00003254 int ret;
3255 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003256 char *p;
3257
3258 p = buf;
3259 n = size;
3260
3261 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003262 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003263
3264 for( i = 0; i < nr; i++ )
3265 {
Paul Bakker93048802011-12-05 14:38:06 +00003266 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003267 continue;
3268
Paul Bakkerdd476992011-01-16 21:34:59 +00003269 ret = snprintf( p, n, "%02X%s",
3270 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3271 SAFE_SNPRINTF();
3272 }
3273
Paul Bakker03c7c252011-11-25 12:37:37 +00003274 if( nr != serial->len )
3275 {
3276 ret = snprintf( p, n, "...." );
3277 SAFE_SNPRINTF();
3278 }
3279
Paul Bakker23986e52011-04-24 08:57:21 +00003280 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003281}
3282
3283/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003284 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003286int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3287 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003288{
Paul Bakker23986e52011-04-24 08:57:21 +00003289 int ret;
3290 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003291 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003292 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003293
3294 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003295 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Paul Bakkerd98030e2009-05-02 15:13:40 +00003297 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003299 SAFE_SNPRINTF();
3300 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003302 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
Paul Bakkerdd476992011-01-16 21:34:59 +00003304 ret = x509parse_serial_gets( p, n, &crt->serial);
3305 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Paul Bakkerd98030e2009-05-02 15:13:40 +00003307 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3308 SAFE_SNPRINTF();
3309 ret = x509parse_dn_gets( p, n, &crt->issuer );
3310 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003311
Paul Bakkerd98030e2009-05-02 15:13:40 +00003312 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3313 SAFE_SNPRINTF();
3314 ret = x509parse_dn_gets( p, n, &crt->subject );
3315 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
Paul Bakkerd98030e2009-05-02 15:13:40 +00003317 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3319 crt->valid_from.year, crt->valid_from.mon,
3320 crt->valid_from.day, crt->valid_from.hour,
3321 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003322 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
Paul Bakkerd98030e2009-05-02 15:13:40 +00003324 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3326 crt->valid_to.year, crt->valid_to.mon,
3327 crt->valid_to.day, crt->valid_to.hour,
3328 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003329 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003330
Paul Bakkerc70b9822013-04-07 22:00:46 +02003331 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003332 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003333
Paul Bakkerc70b9822013-04-07 22:00:46 +02003334 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3335 if( ret != 0 )
3336 ret = snprintf( p, n, "???" );
3337 else
3338 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003339 SAFE_SNPRINTF();
3340
3341 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003342 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003343 SAFE_SNPRINTF();
3344
Paul Bakker23986e52011-04-24 08:57:21 +00003345 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003346}
3347
Paul Bakker74111d32011-01-15 16:57:55 +00003348/*
3349 * Return an informational string describing the given OID
3350 */
3351const char *x509_oid_get_description( x509_buf *oid )
3352{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003353 const char *desc = NULL;
3354 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003355
Paul Bakkerc70b9822013-04-07 22:00:46 +02003356 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003357
Paul Bakkerc70b9822013-04-07 22:00:46 +02003358 if( ret != 0 )
3359 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003360
Paul Bakkerc70b9822013-04-07 22:00:46 +02003361 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003362}
3363
3364/* Return the x.y.z.... style numeric string for the given OID */
3365int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3366{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003367 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003368}
3369
Paul Bakkerd98030e2009-05-02 15:13:40 +00003370/*
3371 * Return an informational string about the CRL.
3372 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003373int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3374 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003375{
Paul Bakker23986e52011-04-24 08:57:21 +00003376 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003377 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003378 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003379 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003380 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003381
3382 p = buf;
3383 n = size;
3384
3385 ret = snprintf( p, n, "%sCRL version : %d",
3386 prefix, crl->version );
3387 SAFE_SNPRINTF();
3388
3389 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3390 SAFE_SNPRINTF();
3391 ret = x509parse_dn_gets( p, n, &crl->issuer );
3392 SAFE_SNPRINTF();
3393
3394 ret = snprintf( p, n, "\n%sthis update : " \
3395 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3396 crl->this_update.year, crl->this_update.mon,
3397 crl->this_update.day, crl->this_update.hour,
3398 crl->this_update.min, crl->this_update.sec );
3399 SAFE_SNPRINTF();
3400
3401 ret = snprintf( p, n, "\n%snext update : " \
3402 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3403 crl->next_update.year, crl->next_update.mon,
3404 crl->next_update.day, crl->next_update.hour,
3405 crl->next_update.min, crl->next_update.sec );
3406 SAFE_SNPRINTF();
3407
3408 entry = &crl->entry;
3409
3410 ret = snprintf( p, n, "\n%sRevoked certificates:",
3411 prefix );
3412 SAFE_SNPRINTF();
3413
Paul Bakker9be19372009-07-27 20:21:53 +00003414 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003415 {
3416 ret = snprintf( p, n, "\n%sserial number: ",
3417 prefix );
3418 SAFE_SNPRINTF();
3419
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003420 ret = x509parse_serial_gets( p, n, &entry->serial);
3421 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003422
Paul Bakkerd98030e2009-05-02 15:13:40 +00003423 ret = snprintf( p, n, " revocation date: " \
3424 "%04d-%02d-%02d %02d:%02d:%02d",
3425 entry->revocation_date.year, entry->revocation_date.mon,
3426 entry->revocation_date.day, entry->revocation_date.hour,
3427 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003428 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003429
3430 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 }
3432
Paul Bakkerc70b9822013-04-07 22:00:46 +02003433 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003434 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003435
Paul Bakkerc70b9822013-04-07 22:00:46 +02003436 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3437 if( ret != 0 )
3438 ret = snprintf( p, n, "???" );
3439 else
3440 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003441 SAFE_SNPRINTF();
3442
Paul Bakker1e27bb22009-07-19 20:25:25 +00003443 ret = snprintf( p, n, "\n" );
3444 SAFE_SNPRINTF();
3445
Paul Bakker23986e52011-04-24 08:57:21 +00003446 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003447}
3448
3449/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003450 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003452#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003453int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003454{
Paul Bakkercce9d772011-11-18 14:26:47 +00003455 int year, mon, day;
3456 int hour, min, sec;
3457
3458#if defined(_WIN32)
3459 SYSTEMTIME st;
3460
3461 GetLocalTime(&st);
3462
3463 year = st.wYear;
3464 mon = st.wMonth;
3465 day = st.wDay;
3466 hour = st.wHour;
3467 min = st.wMinute;
3468 sec = st.wSecond;
3469#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 struct tm *lt;
3471 time_t tt;
3472
3473 tt = time( NULL );
3474 lt = localtime( &tt );
3475
Paul Bakkercce9d772011-11-18 14:26:47 +00003476 year = lt->tm_year + 1900;
3477 mon = lt->tm_mon + 1;
3478 day = lt->tm_mday;
3479 hour = lt->tm_hour;
3480 min = lt->tm_min;
3481 sec = lt->tm_sec;
3482#endif
3483
3484 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003485 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003486
Paul Bakkercce9d772011-11-18 14:26:47 +00003487 if( year == to->year &&
3488 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003489 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490
Paul Bakkercce9d772011-11-18 14:26:47 +00003491 if( year == to->year &&
3492 mon == to->mon &&
3493 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003494 return( 1 );
3495
Paul Bakkercce9d772011-11-18 14:26:47 +00003496 if( year == to->year &&
3497 mon == to->mon &&
3498 day == to->day &&
3499 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003500 return( 1 );
3501
Paul Bakkercce9d772011-11-18 14:26:47 +00003502 if( year == to->year &&
3503 mon == to->mon &&
3504 day == to->day &&
3505 hour == to->hour &&
3506 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003507 return( 1 );
3508
Paul Bakkercce9d772011-11-18 14:26:47 +00003509 if( year == to->year &&
3510 mon == to->mon &&
3511 day == to->day &&
3512 hour == to->hour &&
3513 min == to->min &&
3514 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003515 return( 1 );
3516
Paul Bakker40ea7de2009-05-03 10:18:48 +00003517 return( 0 );
3518}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003519#else /* POLARSSL_HAVE_TIME */
3520int x509parse_time_expired( const x509_time *to )
3521{
3522 ((void) to);
3523 return( 0 );
3524}
3525#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003526
3527/*
3528 * Return 1 if the certificate is revoked, or 0 otherwise.
3529 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003530int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003531{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003532 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003533
3534 while( cur != NULL && cur->serial.len != 0 )
3535 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003536 if( crt->serial.len == cur->serial.len &&
3537 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003538 {
3539 if( x509parse_time_expired( &cur->revocation_date ) )
3540 return( 1 );
3541 }
3542
3543 cur = cur->next;
3544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003545
3546 return( 0 );
3547}
3548
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003549/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003550 * Check that the given certificate is valid accoring to the CRL.
3551 */
3552static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3553 x509_crl *crl_list)
3554{
3555 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003556 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3557 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003558
Paul Bakker915275b2012-09-28 07:10:55 +00003559 if( ca == NULL )
3560 return( flags );
3561
Paul Bakker76fd75a2011-01-16 21:12:10 +00003562 /*
3563 * TODO: What happens if no CRL is present?
3564 * Suggestion: Revocation state should be unknown if no CRL is present.
3565 * For backwards compatibility this is not yet implemented.
3566 */
3567
Paul Bakker915275b2012-09-28 07:10:55 +00003568 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003569 {
Paul Bakker915275b2012-09-28 07:10:55 +00003570 if( crl_list->version == 0 ||
3571 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003572 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3573 crl_list->issuer_raw.len ) != 0 )
3574 {
3575 crl_list = crl_list->next;
3576 continue;
3577 }
3578
3579 /*
3580 * Check if CRL is correctly signed by the trusted CA
3581 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003582 md_info = md_info_from_type( crl_list->sig_md );
3583 if( md_info == NULL )
3584 {
3585 /*
3586 * Cannot check 'unknown' hash
3587 */
3588 flags |= BADCRL_NOT_TRUSTED;
3589 break;
3590 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003591
Paul Bakkerc70b9822013-04-07 22:00:46 +02003592 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003593
Paul Bakkerc70b9822013-04-07 22:00:46 +02003594 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003595 0, hash, crl_list->sig.p ) == 0 )
3596 {
3597 /*
3598 * CRL is not trusted
3599 */
3600 flags |= BADCRL_NOT_TRUSTED;
3601 break;
3602 }
3603
3604 /*
3605 * Check for validity of CRL (Do not drop out)
3606 */
3607 if( x509parse_time_expired( &crl_list->next_update ) )
3608 flags |= BADCRL_EXPIRED;
3609
3610 /*
3611 * Check if certificate is revoked
3612 */
3613 if( x509parse_revoked(crt, crl_list) )
3614 {
3615 flags |= BADCERT_REVOKED;
3616 break;
3617 }
3618
3619 crl_list = crl_list->next;
3620 }
3621 return flags;
3622}
3623
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003624static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003625{
3626 size_t i;
3627 size_t cn_idx = 0;
3628
Paul Bakker57b12982012-02-11 17:38:38 +00003629 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003630 return( 0 );
3631
3632 for( i = 0; i < strlen( cn ); ++i )
3633 {
3634 if( cn[i] == '.' )
3635 {
3636 cn_idx = i;
3637 break;
3638 }
3639 }
3640
3641 if( cn_idx == 0 )
3642 return( 0 );
3643
Paul Bakker535e97d2012-08-23 10:49:55 +00003644 if( strlen( cn ) - cn_idx == name->len - 1 &&
3645 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003646 {
3647 return( 1 );
3648 }
3649
3650 return( 0 );
3651}
3652
Paul Bakker915275b2012-09-28 07:10:55 +00003653static int x509parse_verify_top(
3654 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003655 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003656 int (*f_vrfy)(void *, x509_cert *, int, int *),
3657 void *p_vrfy )
3658{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003659 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003660 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003661 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3662 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003663
3664 if( x509parse_time_expired( &child->valid_to ) )
3665 *flags |= BADCERT_EXPIRED;
3666
3667 /*
3668 * Child is the top of the chain. Check against the trust_ca list.
3669 */
3670 *flags |= BADCERT_NOT_TRUSTED;
3671
3672 while( trust_ca != NULL )
3673 {
3674 if( trust_ca->version == 0 ||
3675 child->issuer_raw.len != trust_ca->subject_raw.len ||
3676 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3677 child->issuer_raw.len ) != 0 )
3678 {
3679 trust_ca = trust_ca->next;
3680 continue;
3681 }
3682
Paul Bakker9a736322012-11-14 12:39:52 +00003683 /*
3684 * Reduce path_len to check against if top of the chain is
3685 * the same as the trusted CA
3686 */
3687 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3688 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3689 child->issuer_raw.len ) == 0 )
3690 {
3691 check_path_cnt--;
3692 }
3693
Paul Bakker915275b2012-09-28 07:10:55 +00003694 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003695 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003696 {
3697 trust_ca = trust_ca->next;
3698 continue;
3699 }
3700
Paul Bakkerc70b9822013-04-07 22:00:46 +02003701 md_info = md_info_from_type( child->sig_md );
3702 if( md_info == NULL )
3703 {
3704 /*
3705 * Cannot check 'unknown' hash
3706 */
3707 continue;
3708 }
Paul Bakker915275b2012-09-28 07:10:55 +00003709
Paul Bakkerc70b9822013-04-07 22:00:46 +02003710 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003711
Paul Bakkerc70b9822013-04-07 22:00:46 +02003712 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003713 0, hash, child->sig.p ) != 0 )
3714 {
3715 trust_ca = trust_ca->next;
3716 continue;
3717 }
3718
3719 /*
3720 * Top of chain is signed by a trusted CA
3721 */
3722 *flags &= ~BADCERT_NOT_TRUSTED;
3723 break;
3724 }
3725
Paul Bakker9a736322012-11-14 12:39:52 +00003726 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003727 * If top of chain is not the same as the trusted CA send a verify request
3728 * to the callback for any issues with validity and CRL presence for the
3729 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003730 */
3731 if( trust_ca != NULL &&
3732 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3733 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3734 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003735 {
3736 /* Check trusted CA's CRL for then chain's top crt */
3737 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3738
3739 if( x509parse_time_expired( &trust_ca->valid_to ) )
3740 ca_flags |= BADCERT_EXPIRED;
3741
Paul Bakker915275b2012-09-28 07:10:55 +00003742 if( NULL != f_vrfy )
3743 {
Paul Bakker9a736322012-11-14 12:39:52 +00003744 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003745 return( ret );
3746 }
3747 }
3748
3749 /* Call callback on top cert */
3750 if( NULL != f_vrfy )
3751 {
Paul Bakker9a736322012-11-14 12:39:52 +00003752 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003753 return( ret );
3754 }
3755
Paul Bakker915275b2012-09-28 07:10:55 +00003756 *flags |= ca_flags;
3757
3758 return( 0 );
3759}
3760
3761static int x509parse_verify_child(
3762 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003763 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003764 int (*f_vrfy)(void *, x509_cert *, int, int *),
3765 void *p_vrfy )
3766{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003767 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003768 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003769 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003770 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003771 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003772
3773 if( x509parse_time_expired( &child->valid_to ) )
3774 *flags |= BADCERT_EXPIRED;
3775
Paul Bakkerc70b9822013-04-07 22:00:46 +02003776 md_info = md_info_from_type( child->sig_md );
3777 if( md_info == NULL )
3778 {
3779 /*
3780 * Cannot check 'unknown' hash
3781 */
Paul Bakker915275b2012-09-28 07:10:55 +00003782 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003783 }
3784 else
3785 {
3786 md( md_info, child->tbs.p, child->tbs.len, hash );
3787
3788 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3789 child->sig.p ) != 0 )
3790 *flags |= BADCERT_NOT_TRUSTED;
3791 }
3792
Paul Bakker915275b2012-09-28 07:10:55 +00003793 /* Check trusted CA's CRL for the given crt */
3794 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3795
3796 grandparent = parent->next;
3797
3798 while( grandparent != NULL )
3799 {
3800 if( grandparent->version == 0 ||
3801 grandparent->ca_istrue == 0 ||
3802 parent->issuer_raw.len != grandparent->subject_raw.len ||
3803 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3804 parent->issuer_raw.len ) != 0 )
3805 {
3806 grandparent = grandparent->next;
3807 continue;
3808 }
3809 break;
3810 }
3811
Paul Bakker915275b2012-09-28 07:10:55 +00003812 if( grandparent != NULL )
3813 {
3814 /*
3815 * Part of the chain
3816 */
Paul Bakker9a736322012-11-14 12:39:52 +00003817 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 +00003818 if( ret != 0 )
3819 return( ret );
3820 }
3821 else
3822 {
Paul Bakker9a736322012-11-14 12:39:52 +00003823 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 +00003824 if( ret != 0 )
3825 return( ret );
3826 }
3827
3828 /* child is verified to be a child of the parent, call verify callback */
3829 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003830 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003831 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003832
3833 *flags |= parent_flags;
3834
3835 return( 0 );
3836}
3837
Paul Bakker76fd75a2011-01-16 21:12:10 +00003838/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003839 * Verify the certificate validity
3840 */
3841int x509parse_verify( x509_cert *crt,
3842 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003843 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003844 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003845 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003846 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003847{
Paul Bakker23986e52011-04-24 08:57:21 +00003848 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003849 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003850 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003851 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003853 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003854
Paul Bakker40ea7de2009-05-03 10:18:48 +00003855 *flags = 0;
3856
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 if( cn != NULL )
3858 {
3859 name = &crt->subject;
3860 cn_len = strlen( cn );
3861
Paul Bakker4d2c1242012-05-10 14:12:46 +00003862 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003863 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003864 cur = &crt->subject_alt_names;
3865
3866 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003867 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003868 if( cur->buf.len == cn_len &&
3869 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003870 break;
3871
Paul Bakker535e97d2012-08-23 10:49:55 +00003872 if( cur->buf.len > 2 &&
3873 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003874 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003875 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003876
Paul Bakker4d2c1242012-05-10 14:12:46 +00003877 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003878 }
3879
3880 if( cur == NULL )
3881 *flags |= BADCERT_CN_MISMATCH;
3882 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003883 else
3884 {
3885 while( name != NULL )
3886 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003887 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003888 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003889 if( name->val.len == cn_len &&
3890 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003891 break;
3892
Paul Bakker535e97d2012-08-23 10:49:55 +00003893 if( name->val.len > 2 &&
3894 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003895 x509_wildcard_verify( cn, &name->val ) )
3896 break;
3897 }
3898
3899 name = name->next;
3900 }
3901
3902 if( name == NULL )
3903 *flags |= BADCERT_CN_MISMATCH;
3904 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003905 }
3906
Paul Bakker5121ce52009-01-03 21:22:43 +00003907 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003908 * Iterate upwards in the given cert chain, to find our crt parent.
3909 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003910 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003911 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003912
Paul Bakker76fd75a2011-01-16 21:12:10 +00003913 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003914 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003915 if( parent->ca_istrue == 0 ||
3916 crt->issuer_raw.len != parent->subject_raw.len ||
3917 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003918 crt->issuer_raw.len ) != 0 )
3919 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003920 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003921 continue;
3922 }
Paul Bakker915275b2012-09-28 07:10:55 +00003923 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003924 }
3925
Paul Bakker915275b2012-09-28 07:10:55 +00003926 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 {
Paul Bakker915275b2012-09-28 07:10:55 +00003928 /*
3929 * Part of the chain
3930 */
Paul Bakker9a736322012-11-14 12:39:52 +00003931 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003932 if( ret != 0 )
3933 return( ret );
3934 }
3935 else
Paul Bakker74111d32011-01-15 16:57:55 +00003936 {
Paul Bakker9a736322012-11-14 12:39:52 +00003937 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003938 if( ret != 0 )
3939 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003940 }
Paul Bakker915275b2012-09-28 07:10:55 +00003941
3942 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003943 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003944
Paul Bakker5121ce52009-01-03 21:22:43 +00003945 return( 0 );
3946}
3947
3948/*
3949 * Unallocate all certificate data
3950 */
3951void x509_free( x509_cert *crt )
3952{
3953 x509_cert *cert_cur = crt;
3954 x509_cert *cert_prv;
3955 x509_name *name_cur;
3956 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003957 x509_sequence *seq_cur;
3958 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003959
3960 if( crt == NULL )
3961 return;
3962
3963 do
3964 {
3965 rsa_free( &cert_cur->rsa );
3966
3967 name_cur = cert_cur->issuer.next;
3968 while( name_cur != NULL )
3969 {
3970 name_prv = name_cur;
3971 name_cur = name_cur->next;
3972 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003973 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003974 }
3975
3976 name_cur = cert_cur->subject.next;
3977 while( name_cur != NULL )
3978 {
3979 name_prv = name_cur;
3980 name_cur = name_cur->next;
3981 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003982 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 }
3984
Paul Bakker74111d32011-01-15 16:57:55 +00003985 seq_cur = cert_cur->ext_key_usage.next;
3986 while( seq_cur != NULL )
3987 {
3988 seq_prv = seq_cur;
3989 seq_cur = seq_cur->next;
3990 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003991 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003992 }
3993
Paul Bakker8afa70d2012-02-11 18:42:45 +00003994 seq_cur = cert_cur->subject_alt_names.next;
3995 while( seq_cur != NULL )
3996 {
3997 seq_prv = seq_cur;
3998 seq_cur = seq_cur->next;
3999 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004000 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004001 }
4002
Paul Bakker5121ce52009-01-03 21:22:43 +00004003 if( cert_cur->raw.p != NULL )
4004 {
4005 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004006 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 }
4008
4009 cert_cur = cert_cur->next;
4010 }
4011 while( cert_cur != NULL );
4012
4013 cert_cur = crt;
4014 do
4015 {
4016 cert_prv = cert_cur;
4017 cert_cur = cert_cur->next;
4018
4019 memset( cert_prv, 0, sizeof( x509_cert ) );
4020 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004021 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 }
4023 while( cert_cur != NULL );
4024}
4025
Paul Bakkerd98030e2009-05-02 15:13:40 +00004026/*
4027 * Unallocate all CRL data
4028 */
4029void x509_crl_free( x509_crl *crl )
4030{
4031 x509_crl *crl_cur = crl;
4032 x509_crl *crl_prv;
4033 x509_name *name_cur;
4034 x509_name *name_prv;
4035 x509_crl_entry *entry_cur;
4036 x509_crl_entry *entry_prv;
4037
4038 if( crl == NULL )
4039 return;
4040
4041 do
4042 {
4043 name_cur = crl_cur->issuer.next;
4044 while( name_cur != NULL )
4045 {
4046 name_prv = name_cur;
4047 name_cur = name_cur->next;
4048 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004049 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004050 }
4051
4052 entry_cur = crl_cur->entry.next;
4053 while( entry_cur != NULL )
4054 {
4055 entry_prv = entry_cur;
4056 entry_cur = entry_cur->next;
4057 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004058 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004059 }
4060
4061 if( crl_cur->raw.p != NULL )
4062 {
4063 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004064 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004065 }
4066
4067 crl_cur = crl_cur->next;
4068 }
4069 while( crl_cur != NULL );
4070
4071 crl_cur = crl;
4072 do
4073 {
4074 crl_prv = crl_cur;
4075 crl_cur = crl_cur->next;
4076
4077 memset( crl_prv, 0, sizeof( x509_crl ) );
4078 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004079 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004080 }
4081 while( crl_cur != NULL );
4082}
4083
Paul Bakker40e46942009-01-03 21:51:57 +00004084#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004085
Paul Bakker40e46942009-01-03 21:51:57 +00004086#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004087
4088/*
4089 * Checkup routine
4090 */
4091int x509_self_test( int verbose )
4092{
Paul Bakker5690efc2011-05-26 13:16:06 +00004093#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004094 int ret;
4095 int flags;
4096 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 x509_cert cacert;
4098 x509_cert clicert;
4099 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004100#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004101 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004102#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004103
4104 if( verbose != 0 )
4105 printf( " X.509 certificate load: " );
4106
4107 memset( &clicert, 0, sizeof( x509_cert ) );
4108
Paul Bakker3c2122f2013-06-24 19:03:14 +02004109 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004110 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004111 if( ret != 0 )
4112 {
4113 if( verbose != 0 )
4114 printf( "failed\n" );
4115
4116 return( ret );
4117 }
4118
4119 memset( &cacert, 0, sizeof( x509_cert ) );
4120
Paul Bakker3c2122f2013-06-24 19:03:14 +02004121 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004122 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004123 if( ret != 0 )
4124 {
4125 if( verbose != 0 )
4126 printf( "failed\n" );
4127
4128 return( ret );
4129 }
4130
4131 if( verbose != 0 )
4132 printf( "passed\n X.509 private key load: " );
4133
4134 i = strlen( test_ca_key );
4135 j = strlen( test_ca_pwd );
4136
Paul Bakker66b78b22011-03-25 14:22:50 +00004137 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4138
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004139 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004140 (const unsigned char *) test_ca_key, i,
4141 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004142 {
4143 if( verbose != 0 )
4144 printf( "failed\n" );
4145
4146 return( ret );
4147 }
4148
4149 if( verbose != 0 )
4150 printf( "passed\n X.509 signature verify: ");
4151
Paul Bakker23986e52011-04-24 08:57:21 +00004152 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004153 if( ret != 0 )
4154 {
Paul Bakker23986e52011-04-24 08:57:21 +00004155 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004156 if( verbose != 0 )
4157 printf( "failed\n" );
4158
4159 return( ret );
4160 }
4161
Paul Bakker5690efc2011-05-26 13:16:06 +00004162#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004163 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004164 printf( "passed\n X.509 DHM parameter load: " );
4165
4166 i = strlen( test_dhm_params );
4167 j = strlen( test_ca_pwd );
4168
Paul Bakker3c2122f2013-06-24 19:03:14 +02004169 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004170 {
4171 if( verbose != 0 )
4172 printf( "failed\n" );
4173
4174 return( ret );
4175 }
4176
4177 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004178 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004180
4181 x509_free( &cacert );
4182 x509_free( &clicert );
4183 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004184#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004185 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004186#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004187
4188 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004189#else
4190 ((void) verbose);
4191 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004193}
4194
4195#endif
4196
4197#endif