blob: b231f80011522a60de3ce6c653d36462f010456a [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/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002283 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002284 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002285static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2286 size_t *used_len,
2287 const unsigned char *key, size_t keylen,
2288 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002289{
2290 int ret;
2291 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002292 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002293 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002294#if defined(POLARSSL_PKCS12_C)
2295 cipher_type_t cipher_alg;
2296 md_type_t md_alg;
2297#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002298
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002299 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002300
2301 p = (unsigned char *) key;
2302 end = p + keylen;
2303
Paul Bakker28144de2013-06-24 19:28:55 +02002304 if( pwdlen == 0 )
2305 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2306
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002307 /*
2308 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2309 *
2310 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2311 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2312 * encryptedData EncryptedData
2313 * }
2314 *
2315 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2316 *
2317 * EncryptedData ::= OCTET STRING
2318 *
2319 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2320 */
2321 if( ( ret = asn1_get_tag( &p, end, &len,
2322 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2323 {
2324 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2325 }
2326
2327 end = p + len;
2328
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002329 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002330 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002331
2332 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2333 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2334
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002335 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002336 return( POLARSSL_ERR_X509_INVALID_INPUT );
2337
2338 /*
2339 * Decrypt EncryptedData with appropriate PDE
2340 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002341#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002342 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002343 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002344 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002345 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002346 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002347 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002348 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2349 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2350
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002351 return( ret );
2352 }
2353 }
2354 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2355 {
2356 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2357 PKCS12_PBE_DECRYPT,
2358 pwd, pwdlen,
2359 p, len, buf ) ) != 0 )
2360 {
2361 return( ret );
2362 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002363
2364 // Best guess for password mismatch when using RC4. If first tag is
2365 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2366 //
2367 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2368 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002369 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002370 else
2371#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002372#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002373 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002374 {
2375 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2376 p, len, buf ) ) != 0 )
2377 {
2378 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2379 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2380
2381 return( ret );
2382 }
2383 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002384 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002385#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002386 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2387
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002388 *used_len = len;
2389 return( 0 );
2390}
2391
2392/*
2393 * Parse an encrypted PKCS#8 encoded private RSA key
2394 */
2395static int x509parse_key_pkcs8_encrypted_der(
2396 rsa_context *rsa,
2397 const unsigned char *key, size_t keylen,
2398 const unsigned char *pwd, size_t pwdlen )
2399{
2400 int ret;
2401 unsigned char buf[2048];
2402 size_t len = 0;
2403
2404 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2405 key, keylen, pwd, pwdlen ) ) != 0 )
2406 {
2407 return( ret );
2408 }
2409
2410 return( x509parse_key_pkcs8_unencrypted_der( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002411}
2412
2413/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002414 * Parse a private RSA key
2415 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002416int x509parse_key_rsa( rsa_context *rsa,
2417 const unsigned char *key, size_t keylen,
2418 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002419{
2420 int ret;
2421
Paul Bakker96743fc2011-02-12 14:30:57 +00002422#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002423 size_t len;
2424 pem_context pem;
2425
2426 pem_init( &pem );
2427 ret = pem_read_buffer( &pem,
2428 "-----BEGIN RSA PRIVATE KEY-----",
2429 "-----END RSA PRIVATE KEY-----",
2430 key, pwd, pwdlen, &len );
2431 if( ret == 0 )
2432 {
2433 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2434 {
2435 rsa_free( rsa );
2436 }
2437
2438 pem_free( &pem );
2439 return( ret );
2440 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002441 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2442 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2443 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2444 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002445 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
2448 ret = pem_read_buffer( &pem,
2449 "-----BEGIN PRIVATE KEY-----",
2450 "-----END PRIVATE KEY-----",
2451 key, NULL, 0, &len );
2452 if( ret == 0 )
2453 {
2454 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2455 pem.buf, pem.buflen ) ) != 0 )
2456 {
2457 rsa_free( rsa );
2458 }
2459
2460 pem_free( &pem );
2461 return( ret );
2462 }
2463 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002464 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002465
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002466 ret = pem_read_buffer( &pem,
2467 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2468 "-----END ENCRYPTED PRIVATE KEY-----",
2469 key, NULL, 0, &len );
2470 if( ret == 0 )
2471 {
2472 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2473 pem.buf, pem.buflen,
2474 pwd, pwdlen ) ) != 0 )
2475 {
2476 rsa_free( rsa );
2477 }
2478
2479 pem_free( &pem );
2480 return( ret );
2481 }
2482 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002483 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002484#else
2485 ((void) pwd);
2486 ((void) pwdlen);
2487#endif /* POLARSSL_PEM_C */
2488
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002489 /*
2490 * At this point we only know it's not a PEM formatted key. Could be any
2491 * of the known DER encoded private key formats
2492 *
2493 * We try the different DER format parsers to see if one passes without
2494 * error
2495 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002496 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2497 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002498 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002499 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002502 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002503
2504 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2505 {
2506 return( ret );
2507 }
2508
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002509 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2510 return( 0 );
2511
2512 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002513
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002514 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2515 return( 0 );
2516
2517 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002518
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002519 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520}
2521
2522/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002523 * Parse a public RSA key
2524 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002525int x509parse_public_key_rsa( rsa_context *rsa,
2526 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002527{
Paul Bakker23986e52011-04-24 08:57:21 +00002528 int ret;
2529 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002530 unsigned char *p, *end;
2531 x509_buf alg_oid;
2532#if defined(POLARSSL_PEM_C)
2533 pem_context pem;
2534
2535 pem_init( &pem );
2536 ret = pem_read_buffer( &pem,
2537 "-----BEGIN PUBLIC KEY-----",
2538 "-----END PUBLIC KEY-----",
2539 key, NULL, 0, &len );
2540
2541 if( ret == 0 )
2542 {
2543 /*
2544 * Was PEM encoded
2545 */
2546 keylen = pem.buflen;
2547 }
Paul Bakker00b28602013-06-24 13:02:41 +02002548 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002549 {
2550 pem_free( &pem );
2551 return( ret );
2552 }
2553
2554 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2555#else
2556 p = (unsigned char *) key;
2557#endif
2558 end = p + keylen;
2559
2560 /*
2561 * PublicKeyInfo ::= SEQUENCE {
2562 * algorithm AlgorithmIdentifier,
2563 * PublicKey BIT STRING
2564 * }
2565 *
2566 * AlgorithmIdentifier ::= SEQUENCE {
2567 * algorithm OBJECT IDENTIFIER,
2568 * parameters ANY DEFINED BY algorithm OPTIONAL
2569 * }
2570 *
2571 * RSAPublicKey ::= SEQUENCE {
2572 * modulus INTEGER, -- n
2573 * publicExponent INTEGER -- e
2574 * }
2575 */
2576
2577 if( ( ret = asn1_get_tag( &p, end, &len,
2578 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2579 {
2580#if defined(POLARSSL_PEM_C)
2581 pem_free( &pem );
2582#endif
2583 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002584 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002585 }
2586
2587 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2588 {
2589#if defined(POLARSSL_PEM_C)
2590 pem_free( &pem );
2591#endif
2592 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002593 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002594 }
2595
2596 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2597 {
2598#if defined(POLARSSL_PEM_C)
2599 pem_free( &pem );
2600#endif
2601 rsa_free( rsa );
2602 return( ret );
2603 }
2604
2605 rsa->len = mpi_size( &rsa->N );
2606
2607#if defined(POLARSSL_PEM_C)
2608 pem_free( &pem );
2609#endif
2610
2611 return( 0 );
2612}
2613
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002614#if defined(POLARSSL_ECP_C)
2615/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002616 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002617 */
2618static int x509parse_key_sec1_der( ecp_keypair *eck,
2619 const unsigned char *key,
2620 size_t keylen )
2621{
2622 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002623 int version;
2624 size_t len;
2625 ecp_group_id grp_id;
2626 unsigned char *p = (unsigned char *) key;
2627 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002628
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002629 /*
2630 * RFC 5915, orf SEC1 Appendix C.4
2631 *
2632 * ECPrivateKey ::= SEQUENCE {
2633 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2634 * privateKey OCTET STRING,
2635 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2636 * publicKey [1] BIT STRING OPTIONAL
2637 * }
2638 */
2639 if( ( ret = asn1_get_tag( &p, end, &len,
2640 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2641 {
2642 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2643 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002644
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002645 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002646
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002647 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2648 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002649
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002650 if( version != 1 )
2651 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2652
2653 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2654 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2655
2656 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2657 {
2658 ecp_keypair_free( eck );
2659 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2660 }
2661
2662 p += len;
2663
2664 /*
2665 * Is 'parameters' present?
2666 */
2667 if( ( ret = asn1_get_tag( &p, end, &len,
2668 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2669 {
2670 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2671 return( ret );
2672
2673 /* TODO: grp may not be empty at this point,
2674 * if we're wrapped inside a PKCS#8 structure: check consistency */
2675 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2676 {
2677 ecp_keypair_free( eck );
2678 return( ret );
2679 }
2680 }
2681 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2682 {
2683 ecp_keypair_free( eck );
2684 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2685 }
2686
2687 /*
2688 * Is 'publickey' present?
2689 */
2690 if( ( ret = asn1_get_tag( &p, end, &len,
2691 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2692 {
2693 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2694 != 0 )
2695 {
2696 ecp_keypair_free( eck );
2697 return( ret );
2698 }
2699
2700 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2701 {
2702 ecp_keypair_free( eck );
2703 return( ret );
2704 }
2705 }
2706 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2707 {
2708 ecp_keypair_free( eck );
2709 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2710 }
2711
2712 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2713 {
2714 ecp_keypair_free( eck );
2715 return( ret );
2716 }
2717
2718 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002719}
2720
2721/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002722 * Parse an unencrypted PKCS#8 encoded private EC key
2723 */
2724static int x509parse_key_pkcs8_unencrypted_der_ec(
2725 ecp_keypair *eck,
2726 const unsigned char* key,
2727 size_t keylen )
2728{
2729 int ret, version;
2730 size_t len;
2731 x509_buf pk_alg_oid;
2732 ecp_group_id grp_id;
2733 const unsigned char *params_end;
2734 unsigned char *p = (unsigned char *) key;
2735 unsigned char *end = p + keylen;
2736 pk_type_t pk_alg = POLARSSL_PK_NONE;
2737
2738 /*
2739 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2740 *
2741 * PrivateKeyInfo ::= SEQUENCE {
2742 * version Version,
2743 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2744 * privateKey PrivateKey,
2745 * attributes [0] IMPLICIT Attributes OPTIONAL }
2746 *
2747 * Version ::= INTEGER
2748 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2749 * PrivateKey ::= OCTET STRING
2750 *
2751 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2752 */
2753
2754 if( ( ret = asn1_get_tag( &p, end, &len,
2755 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2756 {
2757 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2758 }
2759
2760 end = p + len;
2761
2762 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2763 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2764
2765 if( version != 0 )
2766 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2767
2768 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid, &params_end ) ) != 0 )
2769 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2770
2771 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2772 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2773
2774 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
2775 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2776
2777 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2778 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2779
2780 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2781 {
2782 ecp_keypair_free( eck );
2783 return( ret );
2784 }
2785
2786 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2787 {
2788 ecp_keypair_free( eck );
2789 return( ret );
2790 }
2791
2792 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2793 {
2794 ecp_keypair_free( eck );
2795 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2796 }
2797
2798 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2799 {
2800 ecp_keypair_free( eck );
2801 return( ret );
2802 }
2803
2804 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2805 {
2806 ecp_keypair_free( eck );
2807 return( ret );
2808 }
2809
2810 return 0;
2811}
2812
2813/*
2814 * Parse an encrypted PKCS#8 encoded private EC key
2815 */
2816static int x509parse_key_pkcs8_encrypted_der_ec(
2817 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002818 const unsigned char *key, size_t keylen,
2819 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002820{
2821 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002822 unsigned char buf[2048];
2823 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002824
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002825 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2826 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002827 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002828 return( ret );
2829 }
2830
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002831 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002832}
2833
2834/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002835 * Parse a private EC key
2836 */
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002837int x509parse_key_ec( ecp_keypair *eck,
2838 const unsigned char *key, size_t keylen,
2839 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002840{
2841 int ret;
2842
2843#if defined(POLARSSL_PEM_C)
2844 size_t len;
2845 pem_context pem;
2846
2847 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002848 ret = pem_read_buffer( &pem,
2849 "-----BEGIN EC PRIVATE KEY-----",
2850 "-----END EC PRIVATE KEY-----",
2851 key, pwd, pwdlen, &len );
2852 if( ret == 0 )
2853 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002854 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002855 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002856 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002857 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002858
2859 pem_free( &pem );
2860 return( ret );
2861 }
2862 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2863 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2864 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2865 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2866 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2867 return( ret );
2868
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002869 ret = pem_read_buffer( &pem,
2870 "-----BEGIN PRIVATE KEY-----",
2871 "-----END PRIVATE KEY-----",
2872 key, NULL, 0, &len );
2873 if( ret == 0 )
2874 {
2875 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2876 pem.buf, pem.buflen ) ) != 0 )
2877 {
2878 ecp_keypair_free( eck );
2879 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002880
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002881 pem_free( &pem );
2882 return( ret );
2883 }
2884 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2885 return( ret );
2886
2887 ret = pem_read_buffer( &pem,
2888 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2889 "-----END ENCRYPTED PRIVATE KEY-----",
2890 key, NULL, 0, &len );
2891 if( ret == 0 )
2892 {
2893 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2894 pem.buf, pem.buflen,
2895 pwd, pwdlen ) ) != 0 )
2896 {
2897 ecp_keypair_free( eck );
2898 }
2899
2900 pem_free( &pem );
2901 return( ret );
2902 }
2903 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2904 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002905#else
2906 ((void) pwd);
2907 ((void) pwdlen);
2908#endif /* POLARSSL_PEM_C */
2909
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002910 /*
2911 * At this point we only know it's not a PEM formatted key. Could be any
2912 * of the known DER encoded private key formats
2913 *
2914 * We try the different DER format parsers to see if one passes without
2915 * error
2916 */
2917 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2918 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002919 {
2920 return( 0 );
2921 }
2922
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002923 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002924
2925 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2926 {
2927 return( ret );
2928 }
2929
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002930 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2931 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002932 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002933
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002934 ecp_keypair_free( eck );
2935
2936 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2937 return( 0 );
2938
2939 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002940
2941 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2942}
2943
2944/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002945 * Parse a public EC key in RFC 5480 format, der-encoded
2946 */
2947static int x509parse_public_key_ec_der( ecp_keypair *key,
2948 const unsigned char *buf, size_t len )
2949{
2950 int ret;
2951 ecp_group_id grp_id;
2952 x509_buf alg_oid;
2953 pk_type_t alg = POLARSSL_PK_NONE;
2954 unsigned char *p = (unsigned char *) buf;
2955 unsigned char *end = p + len;
2956 const unsigned char *params_end;
2957 /*
2958 * SubjectPublicKeyInfo ::= SEQUENCE {
2959 * algorithm AlgorithmIdentifier,
2960 * subjectPublicKey BIT STRING
2961 * }
2962 * -- algorithm parameters are ECParameters
2963 * -- subjectPublicKey is an ECPoint
2964 */
2965 if( ( ret = asn1_get_tag( &p, end, &len,
2966 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2967 {
2968 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
2969 }
2970
2971 if( ( ret = x509_get_alg( &p, end, &alg_oid, &params_end ) ) != 0 )
2972 return( ret );
2973
2974 if( oid_get_pk_alg( &alg_oid, &alg ) != 0 )
2975 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2976
2977 if( alg != POLARSSL_PK_ECKEY && alg != POLARSSL_PK_ECKEY_DH )
2978 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2979
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002980 if( alg == POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002981 key->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2982
2983 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2984 return( ret );
2985
2986 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
2987 return( ret );
2988
2989 if( ( ret = x509_get_subpubkey_ec( &p, end, &key->grp, &key->Q ) ) != 0 )
2990 {
2991 return( ret );
2992 }
2993
2994 return( 0 );
2995}
2996
2997/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002998 * Parse a public EC key
2999 */
3000int x509parse_public_key_ec( ecp_keypair *eckey,
3001 const unsigned char *key, size_t keylen )
3002{
3003 int ret;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003004#if defined(POLARSSL_PEM_C)
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003005 size_t len;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003006 pem_context pem;
3007
3008 pem_init( &pem );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003009 ret = pem_read_buffer( &pem,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003010 "-----BEGIN PUBLIC KEY-----",
3011 "-----END PUBLIC KEY-----",
3012 key, NULL, 0, &len );
3013
3014 if( ret == 0 )
3015 {
3016 /*
3017 * Was PEM encoded
3018 */
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003019 key = pem.buf;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003020 keylen = pem.buflen;
3021 }
3022 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3023 {
3024 pem_free( &pem );
3025 return( ret );
3026 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003027#endif
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003028
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003029 if( ( ret = x509parse_public_key_ec_der ( eckey, key, keylen ) ) != 0 ||
3030 ( ret = ecp_check_pubkey( &eckey->grp, &eckey->Q ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003031 {
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003032 ecp_keypair_free( eckey );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003033 }
3034
3035#if defined(POLARSSL_PEM_C)
3036 pem_free( &pem );
3037#endif
3038
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003039 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003040}
3041#endif /* defined(POLARSSL_ECP_C) */
3042
Paul Bakkereaa89f82011-04-04 21:36:15 +00003043#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003044/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003045 * Parse DHM parameters
3046 */
Paul Bakker23986e52011-04-24 08:57:21 +00003047int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003048{
Paul Bakker23986e52011-04-24 08:57:21 +00003049 int ret;
3050 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003051 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003052#if defined(POLARSSL_PEM_C)
3053 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003054
Paul Bakker96743fc2011-02-12 14:30:57 +00003055 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003056
Paul Bakker96743fc2011-02-12 14:30:57 +00003057 ret = pem_read_buffer( &pem,
3058 "-----BEGIN DH PARAMETERS-----",
3059 "-----END DH PARAMETERS-----",
3060 dhmin, NULL, 0, &dhminlen );
3061
3062 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003063 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003064 /*
3065 * Was PEM encoded
3066 */
3067 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003068 }
Paul Bakker00b28602013-06-24 13:02:41 +02003069 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003070 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003071 pem_free( &pem );
3072 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003073 }
3074
Paul Bakker96743fc2011-02-12 14:30:57 +00003075 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3076#else
3077 p = (unsigned char *) dhmin;
3078#endif
3079 end = p + dhminlen;
3080
Paul Bakker1b57b062011-01-06 15:48:19 +00003081 memset( dhm, 0, sizeof( dhm_context ) );
3082
Paul Bakker1b57b062011-01-06 15:48:19 +00003083 /*
3084 * DHParams ::= SEQUENCE {
3085 * prime INTEGER, -- P
3086 * generator INTEGER, -- g
3087 * }
3088 */
3089 if( ( ret = asn1_get_tag( &p, end, &len,
3090 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3091 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003092#if defined(POLARSSL_PEM_C)
3093 pem_free( &pem );
3094#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003095 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003096 }
3097
3098 end = p + len;
3099
3100 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3101 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3102 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003103#if defined(POLARSSL_PEM_C)
3104 pem_free( &pem );
3105#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003106 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003107 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003108 }
3109
3110 if( p != end )
3111 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003112#if defined(POLARSSL_PEM_C)
3113 pem_free( &pem );
3114#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003115 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003116 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003117 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3118 }
3119
Paul Bakker96743fc2011-02-12 14:30:57 +00003120#if defined(POLARSSL_PEM_C)
3121 pem_free( &pem );
3122#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003123
3124 return( 0 );
3125}
3126
Paul Bakker335db3f2011-04-25 15:28:35 +00003127#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003128/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003129 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003130 */
3131int x509parse_dhmfile( dhm_context *dhm, const char *path )
3132{
3133 int ret;
3134 size_t n;
3135 unsigned char *buf;
3136
Paul Bakker69e095c2011-12-10 21:55:01 +00003137 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3138 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003139
Paul Bakker27fdf462011-06-09 13:55:13 +00003140 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003141
3142 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003143 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003144
3145 return( ret );
3146}
Paul Bakker335db3f2011-04-25 15:28:35 +00003147#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003148#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003149
Paul Bakker5121ce52009-01-03 21:22:43 +00003150#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003151#include <stdarg.h>
3152
3153#if !defined vsnprintf
3154#define vsnprintf _vsnprintf
3155#endif // vsnprintf
3156
3157/*
3158 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3159 * Result value is not size of buffer needed, but -1 if no fit is possible.
3160 *
3161 * This fuction tries to 'fix' this by at least suggesting enlarging the
3162 * size by 20.
3163 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003164static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003165{
3166 va_list ap;
3167 int res = -1;
3168
3169 va_start( ap, format );
3170
3171 res = vsnprintf( str, size, format, ap );
3172
3173 va_end( ap );
3174
3175 // No quick fix possible
3176 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003177 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003178
3179 return res;
3180}
3181
3182#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003183#endif
3184
Paul Bakkerd98030e2009-05-02 15:13:40 +00003185#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3186
3187#define SAFE_SNPRINTF() \
3188{ \
3189 if( ret == -1 ) \
3190 return( -1 ); \
3191 \
Paul Bakker23986e52011-04-24 08:57:21 +00003192 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003193 p[n - 1] = '\0'; \
3194 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3195 } \
3196 \
Paul Bakker23986e52011-04-24 08:57:21 +00003197 n -= (unsigned int) ret; \
3198 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003199}
3200
Paul Bakker5121ce52009-01-03 21:22:43 +00003201/*
3202 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003203 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003205int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003206{
Paul Bakker23986e52011-04-24 08:57:21 +00003207 int ret;
3208 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003210 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003211 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 char s[128], *p;
3213
3214 memset( s, 0, sizeof( s ) );
3215
3216 name = dn;
3217 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003218 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003219
3220 while( name != NULL )
3221 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003222 if( !name->oid.p )
3223 {
3224 name = name->next;
3225 continue;
3226 }
3227
Paul Bakker74111d32011-01-15 16:57:55 +00003228 if( name != dn )
3229 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003230 ret = snprintf( p, n, ", " );
3231 SAFE_SNPRINTF();
3232 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Paul Bakkerc70b9822013-04-07 22:00:46 +02003234 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Paul Bakkerc70b9822013-04-07 22:00:46 +02003236 if( ret == 0 )
3237 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003239 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003240 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003241
3242 for( i = 0; i < name->val.len; i++ )
3243 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003244 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 break;
3246
3247 c = name->val.p[i];
3248 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3249 s[i] = '?';
3250 else s[i] = c;
3251 }
3252 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003253 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003254 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003255 name = name->next;
3256 }
3257
Paul Bakker23986e52011-04-24 08:57:21 +00003258 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003259}
3260
3261/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003262 * Store the serial in printable form into buf; no more
3263 * than size characters will be written
3264 */
3265int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3266{
Paul Bakker23986e52011-04-24 08:57:21 +00003267 int ret;
3268 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003269 char *p;
3270
3271 p = buf;
3272 n = size;
3273
3274 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003275 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003276
3277 for( i = 0; i < nr; i++ )
3278 {
Paul Bakker93048802011-12-05 14:38:06 +00003279 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003280 continue;
3281
Paul Bakkerdd476992011-01-16 21:34:59 +00003282 ret = snprintf( p, n, "%02X%s",
3283 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3284 SAFE_SNPRINTF();
3285 }
3286
Paul Bakker03c7c252011-11-25 12:37:37 +00003287 if( nr != serial->len )
3288 {
3289 ret = snprintf( p, n, "...." );
3290 SAFE_SNPRINTF();
3291 }
3292
Paul Bakker23986e52011-04-24 08:57:21 +00003293 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003294}
3295
3296/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003297 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003299int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3300 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003301{
Paul Bakker23986e52011-04-24 08:57:21 +00003302 int ret;
3303 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003304 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003305 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
3307 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003308 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003309
Paul Bakkerd98030e2009-05-02 15:13:40 +00003310 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003311 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003312 SAFE_SNPRINTF();
3313 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003315 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
Paul Bakkerdd476992011-01-16 21:34:59 +00003317 ret = x509parse_serial_gets( p, n, &crt->serial);
3318 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003319
Paul Bakkerd98030e2009-05-02 15:13:40 +00003320 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3321 SAFE_SNPRINTF();
3322 ret = x509parse_dn_gets( p, n, &crt->issuer );
3323 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003324
Paul Bakkerd98030e2009-05-02 15:13:40 +00003325 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3326 SAFE_SNPRINTF();
3327 ret = x509parse_dn_gets( p, n, &crt->subject );
3328 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003329
Paul Bakkerd98030e2009-05-02 15:13:40 +00003330 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3332 crt->valid_from.year, crt->valid_from.mon,
3333 crt->valid_from.day, crt->valid_from.hour,
3334 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003335 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003336
Paul Bakkerd98030e2009-05-02 15:13:40 +00003337 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003338 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3339 crt->valid_to.year, crt->valid_to.mon,
3340 crt->valid_to.day, crt->valid_to.hour,
3341 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003342 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003343
Paul Bakkerc70b9822013-04-07 22:00:46 +02003344 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003345 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003346
Paul Bakkerc70b9822013-04-07 22:00:46 +02003347 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3348 if( ret != 0 )
3349 ret = snprintf( p, n, "???" );
3350 else
3351 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003352 SAFE_SNPRINTF();
3353
3354 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003355 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003356 SAFE_SNPRINTF();
3357
Paul Bakker23986e52011-04-24 08:57:21 +00003358 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003359}
3360
Paul Bakker74111d32011-01-15 16:57:55 +00003361/*
3362 * Return an informational string describing the given OID
3363 */
3364const char *x509_oid_get_description( x509_buf *oid )
3365{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003366 const char *desc = NULL;
3367 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003368
Paul Bakkerc70b9822013-04-07 22:00:46 +02003369 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003370
Paul Bakkerc70b9822013-04-07 22:00:46 +02003371 if( ret != 0 )
3372 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003373
Paul Bakkerc70b9822013-04-07 22:00:46 +02003374 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003375}
3376
3377/* Return the x.y.z.... style numeric string for the given OID */
3378int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3379{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003380 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003381}
3382
Paul Bakkerd98030e2009-05-02 15:13:40 +00003383/*
3384 * Return an informational string about the CRL.
3385 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003386int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3387 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003388{
Paul Bakker23986e52011-04-24 08:57:21 +00003389 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003390 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003391 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003392 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003393 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003394
3395 p = buf;
3396 n = size;
3397
3398 ret = snprintf( p, n, "%sCRL version : %d",
3399 prefix, crl->version );
3400 SAFE_SNPRINTF();
3401
3402 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3403 SAFE_SNPRINTF();
3404 ret = x509parse_dn_gets( p, n, &crl->issuer );
3405 SAFE_SNPRINTF();
3406
3407 ret = snprintf( p, n, "\n%sthis update : " \
3408 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3409 crl->this_update.year, crl->this_update.mon,
3410 crl->this_update.day, crl->this_update.hour,
3411 crl->this_update.min, crl->this_update.sec );
3412 SAFE_SNPRINTF();
3413
3414 ret = snprintf( p, n, "\n%snext update : " \
3415 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3416 crl->next_update.year, crl->next_update.mon,
3417 crl->next_update.day, crl->next_update.hour,
3418 crl->next_update.min, crl->next_update.sec );
3419 SAFE_SNPRINTF();
3420
3421 entry = &crl->entry;
3422
3423 ret = snprintf( p, n, "\n%sRevoked certificates:",
3424 prefix );
3425 SAFE_SNPRINTF();
3426
Paul Bakker9be19372009-07-27 20:21:53 +00003427 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003428 {
3429 ret = snprintf( p, n, "\n%sserial number: ",
3430 prefix );
3431 SAFE_SNPRINTF();
3432
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003433 ret = x509parse_serial_gets( p, n, &entry->serial);
3434 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003435
Paul Bakkerd98030e2009-05-02 15:13:40 +00003436 ret = snprintf( p, n, " revocation date: " \
3437 "%04d-%02d-%02d %02d:%02d:%02d",
3438 entry->revocation_date.year, entry->revocation_date.mon,
3439 entry->revocation_date.day, entry->revocation_date.hour,
3440 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003441 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003442
3443 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 }
3445
Paul Bakkerc70b9822013-04-07 22:00:46 +02003446 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003447 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003448
Paul Bakkerc70b9822013-04-07 22:00:46 +02003449 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3450 if( ret != 0 )
3451 ret = snprintf( p, n, "???" );
3452 else
3453 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003454 SAFE_SNPRINTF();
3455
Paul Bakker1e27bb22009-07-19 20:25:25 +00003456 ret = snprintf( p, n, "\n" );
3457 SAFE_SNPRINTF();
3458
Paul Bakker23986e52011-04-24 08:57:21 +00003459 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003460}
3461
3462/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003463 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003464 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003465#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003466int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003467{
Paul Bakkercce9d772011-11-18 14:26:47 +00003468 int year, mon, day;
3469 int hour, min, sec;
3470
3471#if defined(_WIN32)
3472 SYSTEMTIME st;
3473
3474 GetLocalTime(&st);
3475
3476 year = st.wYear;
3477 mon = st.wMonth;
3478 day = st.wDay;
3479 hour = st.wHour;
3480 min = st.wMinute;
3481 sec = st.wSecond;
3482#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 struct tm *lt;
3484 time_t tt;
3485
3486 tt = time( NULL );
3487 lt = localtime( &tt );
3488
Paul Bakkercce9d772011-11-18 14:26:47 +00003489 year = lt->tm_year + 1900;
3490 mon = lt->tm_mon + 1;
3491 day = lt->tm_mday;
3492 hour = lt->tm_hour;
3493 min = lt->tm_min;
3494 sec = lt->tm_sec;
3495#endif
3496
3497 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003498 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003499
Paul Bakkercce9d772011-11-18 14:26:47 +00003500 if( year == to->year &&
3501 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003502 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003503
Paul Bakkercce9d772011-11-18 14:26:47 +00003504 if( year == to->year &&
3505 mon == to->mon &&
3506 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +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 )
Paul Bakkerb6194992011-01-16 21:40:22 +00003513 return( 1 );
3514
Paul Bakkercce9d772011-11-18 14:26:47 +00003515 if( year == to->year &&
3516 mon == to->mon &&
3517 day == to->day &&
3518 hour == to->hour &&
3519 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003520 return( 1 );
3521
Paul Bakkercce9d772011-11-18 14:26:47 +00003522 if( year == to->year &&
3523 mon == to->mon &&
3524 day == to->day &&
3525 hour == to->hour &&
3526 min == to->min &&
3527 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003528 return( 1 );
3529
Paul Bakker40ea7de2009-05-03 10:18:48 +00003530 return( 0 );
3531}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003532#else /* POLARSSL_HAVE_TIME */
3533int x509parse_time_expired( const x509_time *to )
3534{
3535 ((void) to);
3536 return( 0 );
3537}
3538#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003539
3540/*
3541 * Return 1 if the certificate is revoked, or 0 otherwise.
3542 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003543int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003544{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003545 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003546
3547 while( cur != NULL && cur->serial.len != 0 )
3548 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003549 if( crt->serial.len == cur->serial.len &&
3550 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003551 {
3552 if( x509parse_time_expired( &cur->revocation_date ) )
3553 return( 1 );
3554 }
3555
3556 cur = cur->next;
3557 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003558
3559 return( 0 );
3560}
3561
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003562/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003563 * Check that the given certificate is valid accoring to the CRL.
3564 */
3565static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3566 x509_crl *crl_list)
3567{
3568 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003569 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3570 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003571
Paul Bakker915275b2012-09-28 07:10:55 +00003572 if( ca == NULL )
3573 return( flags );
3574
Paul Bakker76fd75a2011-01-16 21:12:10 +00003575 /*
3576 * TODO: What happens if no CRL is present?
3577 * Suggestion: Revocation state should be unknown if no CRL is present.
3578 * For backwards compatibility this is not yet implemented.
3579 */
3580
Paul Bakker915275b2012-09-28 07:10:55 +00003581 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003582 {
Paul Bakker915275b2012-09-28 07:10:55 +00003583 if( crl_list->version == 0 ||
3584 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003585 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3586 crl_list->issuer_raw.len ) != 0 )
3587 {
3588 crl_list = crl_list->next;
3589 continue;
3590 }
3591
3592 /*
3593 * Check if CRL is correctly signed by the trusted CA
3594 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003595 md_info = md_info_from_type( crl_list->sig_md );
3596 if( md_info == NULL )
3597 {
3598 /*
3599 * Cannot check 'unknown' hash
3600 */
3601 flags |= BADCRL_NOT_TRUSTED;
3602 break;
3603 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003604
Paul Bakkerc70b9822013-04-07 22:00:46 +02003605 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003606
Paul Bakkerc70b9822013-04-07 22:00:46 +02003607 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003608 0, hash, crl_list->sig.p ) == 0 )
3609 {
3610 /*
3611 * CRL is not trusted
3612 */
3613 flags |= BADCRL_NOT_TRUSTED;
3614 break;
3615 }
3616
3617 /*
3618 * Check for validity of CRL (Do not drop out)
3619 */
3620 if( x509parse_time_expired( &crl_list->next_update ) )
3621 flags |= BADCRL_EXPIRED;
3622
3623 /*
3624 * Check if certificate is revoked
3625 */
3626 if( x509parse_revoked(crt, crl_list) )
3627 {
3628 flags |= BADCERT_REVOKED;
3629 break;
3630 }
3631
3632 crl_list = crl_list->next;
3633 }
3634 return flags;
3635}
3636
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003637static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003638{
3639 size_t i;
3640 size_t cn_idx = 0;
3641
Paul Bakker57b12982012-02-11 17:38:38 +00003642 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003643 return( 0 );
3644
3645 for( i = 0; i < strlen( cn ); ++i )
3646 {
3647 if( cn[i] == '.' )
3648 {
3649 cn_idx = i;
3650 break;
3651 }
3652 }
3653
3654 if( cn_idx == 0 )
3655 return( 0 );
3656
Paul Bakker535e97d2012-08-23 10:49:55 +00003657 if( strlen( cn ) - cn_idx == name->len - 1 &&
3658 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003659 {
3660 return( 1 );
3661 }
3662
3663 return( 0 );
3664}
3665
Paul Bakker915275b2012-09-28 07:10:55 +00003666static int x509parse_verify_top(
3667 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003668 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003669 int (*f_vrfy)(void *, x509_cert *, int, int *),
3670 void *p_vrfy )
3671{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003672 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003673 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003674 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3675 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003676
3677 if( x509parse_time_expired( &child->valid_to ) )
3678 *flags |= BADCERT_EXPIRED;
3679
3680 /*
3681 * Child is the top of the chain. Check against the trust_ca list.
3682 */
3683 *flags |= BADCERT_NOT_TRUSTED;
3684
3685 while( trust_ca != NULL )
3686 {
3687 if( trust_ca->version == 0 ||
3688 child->issuer_raw.len != trust_ca->subject_raw.len ||
3689 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3690 child->issuer_raw.len ) != 0 )
3691 {
3692 trust_ca = trust_ca->next;
3693 continue;
3694 }
3695
Paul Bakker9a736322012-11-14 12:39:52 +00003696 /*
3697 * Reduce path_len to check against if top of the chain is
3698 * the same as the trusted CA
3699 */
3700 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3701 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3702 child->issuer_raw.len ) == 0 )
3703 {
3704 check_path_cnt--;
3705 }
3706
Paul Bakker915275b2012-09-28 07:10:55 +00003707 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003708 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003709 {
3710 trust_ca = trust_ca->next;
3711 continue;
3712 }
3713
Paul Bakkerc70b9822013-04-07 22:00:46 +02003714 md_info = md_info_from_type( child->sig_md );
3715 if( md_info == NULL )
3716 {
3717 /*
3718 * Cannot check 'unknown' hash
3719 */
3720 continue;
3721 }
Paul Bakker915275b2012-09-28 07:10:55 +00003722
Paul Bakkerc70b9822013-04-07 22:00:46 +02003723 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003724
Paul Bakkerc70b9822013-04-07 22:00:46 +02003725 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003726 0, hash, child->sig.p ) != 0 )
3727 {
3728 trust_ca = trust_ca->next;
3729 continue;
3730 }
3731
3732 /*
3733 * Top of chain is signed by a trusted CA
3734 */
3735 *flags &= ~BADCERT_NOT_TRUSTED;
3736 break;
3737 }
3738
Paul Bakker9a736322012-11-14 12:39:52 +00003739 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003740 * If top of chain is not the same as the trusted CA send a verify request
3741 * to the callback for any issues with validity and CRL presence for the
3742 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003743 */
3744 if( trust_ca != NULL &&
3745 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3746 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3747 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003748 {
3749 /* Check trusted CA's CRL for then chain's top crt */
3750 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3751
3752 if( x509parse_time_expired( &trust_ca->valid_to ) )
3753 ca_flags |= BADCERT_EXPIRED;
3754
Paul Bakker915275b2012-09-28 07:10:55 +00003755 if( NULL != f_vrfy )
3756 {
Paul Bakker9a736322012-11-14 12:39:52 +00003757 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003758 return( ret );
3759 }
3760 }
3761
3762 /* Call callback on top cert */
3763 if( NULL != f_vrfy )
3764 {
Paul Bakker9a736322012-11-14 12:39:52 +00003765 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003766 return( ret );
3767 }
3768
Paul Bakker915275b2012-09-28 07:10:55 +00003769 *flags |= ca_flags;
3770
3771 return( 0 );
3772}
3773
3774static int x509parse_verify_child(
3775 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003776 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003777 int (*f_vrfy)(void *, x509_cert *, int, int *),
3778 void *p_vrfy )
3779{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003780 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003781 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003782 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003783 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003784 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003785
3786 if( x509parse_time_expired( &child->valid_to ) )
3787 *flags |= BADCERT_EXPIRED;
3788
Paul Bakkerc70b9822013-04-07 22:00:46 +02003789 md_info = md_info_from_type( child->sig_md );
3790 if( md_info == NULL )
3791 {
3792 /*
3793 * Cannot check 'unknown' hash
3794 */
Paul Bakker915275b2012-09-28 07:10:55 +00003795 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003796 }
3797 else
3798 {
3799 md( md_info, child->tbs.p, child->tbs.len, hash );
3800
3801 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3802 child->sig.p ) != 0 )
3803 *flags |= BADCERT_NOT_TRUSTED;
3804 }
3805
Paul Bakker915275b2012-09-28 07:10:55 +00003806 /* Check trusted CA's CRL for the given crt */
3807 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3808
3809 grandparent = parent->next;
3810
3811 while( grandparent != NULL )
3812 {
3813 if( grandparent->version == 0 ||
3814 grandparent->ca_istrue == 0 ||
3815 parent->issuer_raw.len != grandparent->subject_raw.len ||
3816 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3817 parent->issuer_raw.len ) != 0 )
3818 {
3819 grandparent = grandparent->next;
3820 continue;
3821 }
3822 break;
3823 }
3824
Paul Bakker915275b2012-09-28 07:10:55 +00003825 if( grandparent != NULL )
3826 {
3827 /*
3828 * Part of the chain
3829 */
Paul Bakker9a736322012-11-14 12:39:52 +00003830 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 +00003831 if( ret != 0 )
3832 return( ret );
3833 }
3834 else
3835 {
Paul Bakker9a736322012-11-14 12:39:52 +00003836 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 +00003837 if( ret != 0 )
3838 return( ret );
3839 }
3840
3841 /* child is verified to be a child of the parent, call verify callback */
3842 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003843 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003844 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003845
3846 *flags |= parent_flags;
3847
3848 return( 0 );
3849}
3850
Paul Bakker76fd75a2011-01-16 21:12:10 +00003851/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 * Verify the certificate validity
3853 */
3854int x509parse_verify( x509_cert *crt,
3855 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003856 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003857 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003858 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003859 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003860{
Paul Bakker23986e52011-04-24 08:57:21 +00003861 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003862 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003863 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003864 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003866 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003867
Paul Bakker40ea7de2009-05-03 10:18:48 +00003868 *flags = 0;
3869
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 if( cn != NULL )
3871 {
3872 name = &crt->subject;
3873 cn_len = strlen( cn );
3874
Paul Bakker4d2c1242012-05-10 14:12:46 +00003875 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003877 cur = &crt->subject_alt_names;
3878
3879 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003880 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003881 if( cur->buf.len == cn_len &&
3882 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003883 break;
3884
Paul Bakker535e97d2012-08-23 10:49:55 +00003885 if( cur->buf.len > 2 &&
3886 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003887 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003888 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003889
Paul Bakker4d2c1242012-05-10 14:12:46 +00003890 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003891 }
3892
3893 if( cur == NULL )
3894 *flags |= BADCERT_CN_MISMATCH;
3895 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003896 else
3897 {
3898 while( name != NULL )
3899 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003900 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003901 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003902 if( name->val.len == cn_len &&
3903 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003904 break;
3905
Paul Bakker535e97d2012-08-23 10:49:55 +00003906 if( name->val.len > 2 &&
3907 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003908 x509_wildcard_verify( cn, &name->val ) )
3909 break;
3910 }
3911
3912 name = name->next;
3913 }
3914
3915 if( name == NULL )
3916 *flags |= BADCERT_CN_MISMATCH;
3917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003918 }
3919
Paul Bakker5121ce52009-01-03 21:22:43 +00003920 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003921 * Iterate upwards in the given cert chain, to find our crt parent.
3922 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003923 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003924 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003925
Paul Bakker76fd75a2011-01-16 21:12:10 +00003926 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003928 if( parent->ca_istrue == 0 ||
3929 crt->issuer_raw.len != parent->subject_raw.len ||
3930 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003931 crt->issuer_raw.len ) != 0 )
3932 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003933 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003934 continue;
3935 }
Paul Bakker915275b2012-09-28 07:10:55 +00003936 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003937 }
3938
Paul Bakker915275b2012-09-28 07:10:55 +00003939 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003940 {
Paul Bakker915275b2012-09-28 07:10:55 +00003941 /*
3942 * Part of the chain
3943 */
Paul Bakker9a736322012-11-14 12:39:52 +00003944 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003945 if( ret != 0 )
3946 return( ret );
3947 }
3948 else
Paul Bakker74111d32011-01-15 16:57:55 +00003949 {
Paul Bakker9a736322012-11-14 12:39:52 +00003950 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003951 if( ret != 0 )
3952 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003953 }
Paul Bakker915275b2012-09-28 07:10:55 +00003954
3955 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003956 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003957
Paul Bakker5121ce52009-01-03 21:22:43 +00003958 return( 0 );
3959}
3960
3961/*
3962 * Unallocate all certificate data
3963 */
3964void x509_free( x509_cert *crt )
3965{
3966 x509_cert *cert_cur = crt;
3967 x509_cert *cert_prv;
3968 x509_name *name_cur;
3969 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003970 x509_sequence *seq_cur;
3971 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003972
3973 if( crt == NULL )
3974 return;
3975
3976 do
3977 {
3978 rsa_free( &cert_cur->rsa );
3979
3980 name_cur = cert_cur->issuer.next;
3981 while( name_cur != NULL )
3982 {
3983 name_prv = name_cur;
3984 name_cur = name_cur->next;
3985 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003986 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 }
3988
3989 name_cur = cert_cur->subject.next;
3990 while( name_cur != NULL )
3991 {
3992 name_prv = name_cur;
3993 name_cur = name_cur->next;
3994 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003995 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003996 }
3997
Paul Bakker74111d32011-01-15 16:57:55 +00003998 seq_cur = cert_cur->ext_key_usage.next;
3999 while( seq_cur != NULL )
4000 {
4001 seq_prv = seq_cur;
4002 seq_cur = seq_cur->next;
4003 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004004 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004005 }
4006
Paul Bakker8afa70d2012-02-11 18:42:45 +00004007 seq_cur = cert_cur->subject_alt_names.next;
4008 while( seq_cur != NULL )
4009 {
4010 seq_prv = seq_cur;
4011 seq_cur = seq_cur->next;
4012 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004013 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004014 }
4015
Paul Bakker5121ce52009-01-03 21:22:43 +00004016 if( cert_cur->raw.p != NULL )
4017 {
4018 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004019 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004020 }
4021
4022 cert_cur = cert_cur->next;
4023 }
4024 while( cert_cur != NULL );
4025
4026 cert_cur = crt;
4027 do
4028 {
4029 cert_prv = cert_cur;
4030 cert_cur = cert_cur->next;
4031
4032 memset( cert_prv, 0, sizeof( x509_cert ) );
4033 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004034 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004035 }
4036 while( cert_cur != NULL );
4037}
4038
Paul Bakkerd98030e2009-05-02 15:13:40 +00004039/*
4040 * Unallocate all CRL data
4041 */
4042void x509_crl_free( x509_crl *crl )
4043{
4044 x509_crl *crl_cur = crl;
4045 x509_crl *crl_prv;
4046 x509_name *name_cur;
4047 x509_name *name_prv;
4048 x509_crl_entry *entry_cur;
4049 x509_crl_entry *entry_prv;
4050
4051 if( crl == NULL )
4052 return;
4053
4054 do
4055 {
4056 name_cur = crl_cur->issuer.next;
4057 while( name_cur != NULL )
4058 {
4059 name_prv = name_cur;
4060 name_cur = name_cur->next;
4061 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004062 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004063 }
4064
4065 entry_cur = crl_cur->entry.next;
4066 while( entry_cur != NULL )
4067 {
4068 entry_prv = entry_cur;
4069 entry_cur = entry_cur->next;
4070 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004071 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004072 }
4073
4074 if( crl_cur->raw.p != NULL )
4075 {
4076 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004077 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004078 }
4079
4080 crl_cur = crl_cur->next;
4081 }
4082 while( crl_cur != NULL );
4083
4084 crl_cur = crl;
4085 do
4086 {
4087 crl_prv = crl_cur;
4088 crl_cur = crl_cur->next;
4089
4090 memset( crl_prv, 0, sizeof( x509_crl ) );
4091 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004092 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004093 }
4094 while( crl_cur != NULL );
4095}
4096
Paul Bakker40e46942009-01-03 21:51:57 +00004097#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004098
Paul Bakker40e46942009-01-03 21:51:57 +00004099#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004100
4101/*
4102 * Checkup routine
4103 */
4104int x509_self_test( int verbose )
4105{
Paul Bakker5690efc2011-05-26 13:16:06 +00004106#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004107 int ret;
4108 int flags;
4109 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004110 x509_cert cacert;
4111 x509_cert clicert;
4112 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004113#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004114 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004115#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004116
4117 if( verbose != 0 )
4118 printf( " X.509 certificate load: " );
4119
4120 memset( &clicert, 0, sizeof( x509_cert ) );
4121
Paul Bakker3c2122f2013-06-24 19:03:14 +02004122 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004123 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 if( ret != 0 )
4125 {
4126 if( verbose != 0 )
4127 printf( "failed\n" );
4128
4129 return( ret );
4130 }
4131
4132 memset( &cacert, 0, sizeof( x509_cert ) );
4133
Paul Bakker3c2122f2013-06-24 19:03:14 +02004134 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004135 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004136 if( ret != 0 )
4137 {
4138 if( verbose != 0 )
4139 printf( "failed\n" );
4140
4141 return( ret );
4142 }
4143
4144 if( verbose != 0 )
4145 printf( "passed\n X.509 private key load: " );
4146
4147 i = strlen( test_ca_key );
4148 j = strlen( test_ca_pwd );
4149
Paul Bakker66b78b22011-03-25 14:22:50 +00004150 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4151
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004152 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004153 (const unsigned char *) test_ca_key, i,
4154 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004155 {
4156 if( verbose != 0 )
4157 printf( "failed\n" );
4158
4159 return( ret );
4160 }
4161
4162 if( verbose != 0 )
4163 printf( "passed\n X.509 signature verify: ");
4164
Paul Bakker23986e52011-04-24 08:57:21 +00004165 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004166 if( ret != 0 )
4167 {
Paul Bakker23986e52011-04-24 08:57:21 +00004168 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004169 if( verbose != 0 )
4170 printf( "failed\n" );
4171
4172 return( ret );
4173 }
4174
Paul Bakker5690efc2011-05-26 13:16:06 +00004175#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004176 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004177 printf( "passed\n X.509 DHM parameter load: " );
4178
4179 i = strlen( test_dhm_params );
4180 j = strlen( test_ca_pwd );
4181
Paul Bakker3c2122f2013-06-24 19:03:14 +02004182 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004183 {
4184 if( verbose != 0 )
4185 printf( "failed\n" );
4186
4187 return( ret );
4188 }
4189
4190 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004193
4194 x509_free( &cacert );
4195 x509_free( &clicert );
4196 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004197#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004198 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004199#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004200
4201 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004202#else
4203 ((void) verbose);
4204 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4205#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004206}
4207
4208#endif
4209
4210#endif