blob: 20c86071bc40343dea49ae732881602f608c8f2e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000045#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020046#if defined(POLARSSL_PKCS5_C)
47#include "polarssl/pkcs5.h"
48#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020049#if defined(POLARSSL_PKCS12_C)
50#include "polarssl/pkcs12.h"
51#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker6e339b52013-07-03 13:37:05 +020053#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <string.h>
61#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000062#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000063#include <windows.h>
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker335db3f2011-04-25 15:28:35 +000068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000070#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000071#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020072#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000073#include <dirent.h>
74#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000075#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
163/*
164 * AlgorithmIdentifier ::= SEQUENCE {
165 * algorithm OBJECT IDENTIFIER,
166 * parameters ANY DEFINED BY algorithm OPTIONAL }
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200167 *
168 * If params_end is NULL, then parameters must be absent or ANS.1 NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
170static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000171 const unsigned char *end,
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200172 x509_buf *alg, const unsigned char **params_end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000173{
Paul Bakker23986e52011-04-24 08:57:21 +0000174 int ret;
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200175 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200177 if( params_end == NULL ) {
178 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
179 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
180
181 return( 0 );
182 }
183
184 /* TODO: use asn1_get_alg */
185 if( ( ret = asn1_get_tag( p, end, &len,
186 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
187 {
188 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
189 }
190
191 end = *p + len;
192 alg->tag = **p;
193
194 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000195 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200197 alg->p = *p;
198 *p += alg->len;
199
200 *params_end = end;
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 return( 0 );
202}
203
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200204/* Get an EC group id from an ECParameters buffer
205 *
206 * ECParameters ::= CHOICE {
207 * namedCurve OBJECT IDENTIFIER
208 * -- implicitCurve NULL
209 * -- specifiedCurve SpecifiedECDomain
210 * }
211 */
212static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
213 ecp_group_id *grp_id )
214{
215 int ret;
216 x509_buf curve;
217
218 curve.tag = **p;
219
220 if( ( ret = asn1_get_tag( p, end, &curve.len, ASN1_OID ) ) != 0 )
221 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
222
223 curve.p = *p;
224 *p += curve.len;
225
226 if( *p != end )
227 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
228 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
229
230 if( ( ret = oid_get_ec_grp( &curve, grp_id ) ) != 0 )
231 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
232
233 return( 0 );
234}
235
Paul Bakker5121ce52009-01-03 21:22:43 +0000236/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200237 * subjectPublicKey BIT STRING
238 * -- which, in our case, contains
239 * ECPoint ::= octet string (not ASN.1)
240 */
241static int x509_get_subpubkey_ec( unsigned char **p, const unsigned char *end,
242 const ecp_group *grp, ecp_point *pt )
243{
244 int ret;
245 size_t len;
246
247 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
248 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
249
250 if( *p + len != end )
251 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
252 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
253
254 /*
255 * First byte in the content of BIT STRING is the nummber of padding bit.
256 * Here it is always 0 since ECPoint is an octet string, so skip it.
257 */
258 ++*p;
259 --len;
260
261 if( ( ret = ecp_point_read_binary( grp, pt,
262 (const unsigned char *) *p, len ) ) != 0 )
263 {
264 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
265 }
266
267 return( 0 );
268}
269
270/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 * AttributeTypeAndValue ::= SEQUENCE {
272 * type AttributeType,
273 * value AttributeValue }
274 *
275 * AttributeType ::= OBJECT IDENTIFIER
276 *
277 * AttributeValue ::= ANY DEFINED BY AttributeType
278 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000279static int x509_get_attr_type_value( unsigned char **p,
280 const unsigned char *end,
281 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000282{
Paul Bakker23986e52011-04-24 08:57:21 +0000283 int ret;
284 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 x509_buf *oid;
286 x509_buf *val;
287
288 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000290 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 oid = &cur->oid;
293 oid->tag = **p;
294
295 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000296 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298 oid->p = *p;
299 *p += oid->len;
300
301 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000302 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000303 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
306 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
307 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000308 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000309 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311 val = &cur->val;
312 val->tag = *(*p)++;
313
314 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000315 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317 val->p = *p;
318 *p += val->len;
319
320 cur->next = NULL;
321
Paul Bakker400ff6f2011-02-20 10:40:16 +0000322 return( 0 );
323}
324
325/*
326 * RelativeDistinguishedName ::=
327 * SET OF AttributeTypeAndValue
328 *
329 * AttributeTypeAndValue ::= SEQUENCE {
330 * type AttributeType,
331 * value AttributeValue }
332 *
333 * AttributeType ::= OBJECT IDENTIFIER
334 *
335 * AttributeValue ::= ANY DEFINED BY AttributeType
336 */
337static int x509_get_name( unsigned char **p,
338 const unsigned char *end,
339 x509_name *cur )
340{
Paul Bakker23986e52011-04-24 08:57:21 +0000341 int ret;
342 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000343 const unsigned char *end2;
344 x509_name *use;
345
346 if( ( ret = asn1_get_tag( p, end, &len,
347 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000348 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000349
350 end2 = end;
351 end = *p + len;
352 use = cur;
353
354 do
355 {
356 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
357 return( ret );
358
359 if( *p != end )
360 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200361 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000362 sizeof( x509_name ) );
363
364 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000365 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000366
367 memset( use->next, 0, sizeof( x509_name ) );
368
369 use = use->next;
370 }
371 }
372 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374 /*
375 * recurse until end of SEQUENCE is reached
376 */
377 if( *p == end2 )
378 return( 0 );
379
Paul Bakker6e339b52013-07-03 13:37:05 +0200380 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 sizeof( x509_name ) );
382
383 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000384 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
Paul Bakker430ffbe2012-05-01 08:14:20 +0000386 memset( cur->next, 0, sizeof( x509_name ) );
387
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 return( x509_get_name( p, end2, cur->next ) );
389}
390
391/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 * Time ::= CHOICE {
393 * utcTime UTCTime,
394 * generalTime GeneralizedTime }
395 */
Paul Bakker91200182010-02-18 21:26:15 +0000396static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000397 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000398 x509_time *time )
399{
Paul Bakker23986e52011-04-24 08:57:21 +0000400 int ret;
401 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000402 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000403 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404
Paul Bakker91200182010-02-18 21:26:15 +0000405 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000406 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
407 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000408
Paul Bakker91200182010-02-18 21:26:15 +0000409 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000410
Paul Bakker91200182010-02-18 21:26:15 +0000411 if ( tag == ASN1_UTC_TIME )
412 {
413 (*p)++;
414 ret = asn1_get_len( p, end, &len );
415
416 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000417 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000418
Paul Bakker91200182010-02-18 21:26:15 +0000419 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000420 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
421 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000422
Paul Bakker91200182010-02-18 21:26:15 +0000423 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
424 &time->year, &time->mon, &time->day,
425 &time->hour, &time->min, &time->sec ) < 5 )
426 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000427
Paul Bakker400ff6f2011-02-20 10:40:16 +0000428 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000429 time->year += 1900;
430
431 *p += len;
432
433 return( 0 );
434 }
435 else if ( tag == ASN1_GENERALIZED_TIME )
436 {
437 (*p)++;
438 ret = asn1_get_len( p, end, &len );
439
440 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000441 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000442
443 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000444 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
445 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000446
447 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
448 &time->year, &time->mon, &time->day,
449 &time->hour, &time->min, &time->sec ) < 5 )
450 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
451
452 *p += len;
453
454 return( 0 );
455 }
456 else
Paul Bakker9d781402011-05-09 16:17:09 +0000457 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000458}
459
460
461/*
462 * Validity ::= SEQUENCE {
463 * notBefore Time,
464 * notAfter Time }
465 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000466static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000467 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 x509_time *from,
469 x509_time *to )
470{
Paul Bakker23986e52011-04-24 08:57:21 +0000471 int ret;
472 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474 if( ( ret = asn1_get_tag( p, end, &len,
475 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000476 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 end = *p + len;
479
Paul Bakker91200182010-02-18 21:26:15 +0000480 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000481 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
Paul Bakker91200182010-02-18 21:26:15 +0000483 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000484 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 return( 0 );
491}
492
493/*
494 * SubjectPublicKeyInfo ::= SEQUENCE {
495 * algorithm AlgorithmIdentifier,
496 * subjectPublicKey BIT STRING }
497 */
498static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000499 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 x509_buf *pk_alg_oid,
501 mpi *N, mpi *E )
502{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200503 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000504 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 unsigned char *end2;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200506 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200508 if( ( ret = asn1_get_alg_null( p, end, pk_alg_oid ) ) != 0 )
509 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 /*
512 * only RSA public keys handled at this time
513 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200514 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000515 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000518 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000521 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000522 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 end2 = *p + len;
525
526 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000527 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 /*
530 * RSAPublicKey ::= SEQUENCE {
531 * modulus INTEGER, -- n
532 * publicExponent INTEGER -- e
533 * }
534 */
535 if( ( ret = asn1_get_tag( p, end2, &len,
536 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000537 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000540 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000541 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
544 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000545 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000548 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000549 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
551 return( 0 );
552}
553
554static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000555 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 x509_buf *sig )
557{
Paul Bakker23986e52011-04-24 08:57:21 +0000558 int ret;
559 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Paul Bakker8afa70d2012-02-11 18:42:45 +0000561 if( ( end - *p ) < 1 )
562 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
563 POLARSSL_ERR_ASN1_OUT_OF_DATA );
564
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 sig->tag = **p;
566
567 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000568 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Paul Bakker74111d32011-01-15 16:57:55 +0000570
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000572 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 sig->len = len;
575 sig->p = *p;
576
577 *p += len;
578
579 return( 0 );
580}
581
582/*
583 * X.509 v2/v3 unique identifier (not parsed)
584 */
585static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000586 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 x509_buf *uid, int n )
588{
589 int ret;
590
591 if( *p == end )
592 return( 0 );
593
594 uid->tag = **p;
595
596 if( ( ret = asn1_get_tag( p, end, &uid->len,
597 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
598 {
Paul Bakker40e46942009-01-03 21:51:57 +0000599 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 return( 0 );
601
602 return( ret );
603 }
604
605 uid->p = *p;
606 *p += uid->len;
607
608 return( 0 );
609}
610
611/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000612 * X.509 Extensions (No parsing of extensions, pointer should
613 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 */
615static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000616 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000617 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000618{
Paul Bakker23986e52011-04-24 08:57:21 +0000619 int ret;
620 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622 if( *p == end )
623 return( 0 );
624
625 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000626
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000628 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
631 ext->p = *p;
632 end = *p + ext->len;
633
634 /*
635 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
636 *
637 * Extension ::= SEQUENCE {
638 * extnID OBJECT IDENTIFIER,
639 * critical BOOLEAN DEFAULT FALSE,
640 * extnValue OCTET STRING }
641 */
642 if( ( ret = asn1_get_tag( p, end, &len,
643 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000644 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000647 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000648 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Paul Bakkerd98030e2009-05-02 15:13:40 +0000650 return( 0 );
651}
652
653/*
654 * X.509 CRL v2 extensions (no extensions parsed yet.)
655 */
656static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000657 const unsigned char *end,
658 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000659{
Paul Bakker23986e52011-04-24 08:57:21 +0000660 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000661 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000662
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000663 /* Get explicit tag */
664 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000665 {
666 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
667 return( 0 );
668
669 return( ret );
670 }
671
672 while( *p < end )
673 {
674 if( ( ret = asn1_get_tag( p, end, &len,
675 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000676 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000677
678 *p += len;
679 }
680
681 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000682 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000683 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
684
685 return( 0 );
686}
687
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000688/*
689 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
690 */
691static int x509_get_crl_entry_ext( unsigned char **p,
692 const unsigned char *end,
693 x509_buf *ext )
694{
695 int ret;
696 size_t len = 0;
697
698 /* OPTIONAL */
699 if (end <= *p)
700 return( 0 );
701
702 ext->tag = **p;
703 ext->p = *p;
704
705 /*
706 * Get CRL-entry extension sequence header
707 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
708 */
709 if( ( ret = asn1_get_tag( p, end, &ext->len,
710 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
711 {
712 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
713 {
714 ext->p = NULL;
715 return( 0 );
716 }
717 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
718 }
719
720 end = *p + ext->len;
721
722 if( end != *p + ext->len )
723 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
724 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
725
726 while( *p < end )
727 {
728 if( ( ret = asn1_get_tag( p, end, &len,
729 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
730 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
731
732 *p += len;
733 }
734
735 if( *p != end )
736 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
737 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
738
739 return( 0 );
740}
741
Paul Bakker74111d32011-01-15 16:57:55 +0000742static int x509_get_basic_constraints( unsigned char **p,
743 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000744 int *ca_istrue,
745 int *max_pathlen )
746{
Paul Bakker23986e52011-04-24 08:57:21 +0000747 int ret;
748 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000749
750 /*
751 * BasicConstraints ::= SEQUENCE {
752 * cA BOOLEAN DEFAULT FALSE,
753 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
754 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000755 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000756 *max_pathlen = 0; /* endless */
757
758 if( ( ret = asn1_get_tag( p, end, &len,
759 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000760 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000761
762 if( *p == end )
763 return 0;
764
Paul Bakker3cccddb2011-01-16 21:46:31 +0000765 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000766 {
767 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000768 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000769
770 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000771 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000772
Paul Bakker3cccddb2011-01-16 21:46:31 +0000773 if( *ca_istrue != 0 )
774 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000775 }
776
777 if( *p == end )
778 return 0;
779
780 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000782
783 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000784 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000785 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
786
787 (*max_pathlen)++;
788
Paul Bakker74111d32011-01-15 16:57:55 +0000789 return 0;
790}
791
792static int x509_get_ns_cert_type( unsigned char **p,
793 const unsigned char *end,
794 unsigned char *ns_cert_type)
795{
796 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000797 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000798
799 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000800 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000801
802 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000803 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000804 POLARSSL_ERR_ASN1_INVALID_LENGTH );
805
806 /* Get actual bitstring */
807 *ns_cert_type = *bs.p;
808 return 0;
809}
810
811static int x509_get_key_usage( unsigned char **p,
812 const unsigned char *end,
813 unsigned char *key_usage)
814{
815 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000816 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000817
818 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000819 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000820
Paul Bakker94a67962012-08-23 13:03:52 +0000821 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000823 POLARSSL_ERR_ASN1_INVALID_LENGTH );
824
825 /* Get actual bitstring */
826 *key_usage = *bs.p;
827 return 0;
828}
829
Paul Bakkerd98030e2009-05-02 15:13:40 +0000830/*
Paul Bakker74111d32011-01-15 16:57:55 +0000831 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
832 *
833 * KeyPurposeId ::= OBJECT IDENTIFIER
834 */
835static int x509_get_ext_key_usage( unsigned char **p,
836 const unsigned char *end,
837 x509_sequence *ext_key_usage)
838{
839 int ret;
840
841 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000842 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000843
844 /* Sequence length must be >= 1 */
845 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000847 POLARSSL_ERR_ASN1_INVALID_LENGTH );
848
849 return 0;
850}
851
852/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000853 * SubjectAltName ::= GeneralNames
854 *
855 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
856 *
857 * GeneralName ::= CHOICE {
858 * otherName [0] OtherName,
859 * rfc822Name [1] IA5String,
860 * dNSName [2] IA5String,
861 * x400Address [3] ORAddress,
862 * directoryName [4] Name,
863 * ediPartyName [5] EDIPartyName,
864 * uniformResourceIdentifier [6] IA5String,
865 * iPAddress [7] OCTET STRING,
866 * registeredID [8] OBJECT IDENTIFIER }
867 *
868 * OtherName ::= SEQUENCE {
869 * type-id OBJECT IDENTIFIER,
870 * value [0] EXPLICIT ANY DEFINED BY type-id }
871 *
872 * EDIPartyName ::= SEQUENCE {
873 * nameAssigner [0] DirectoryString OPTIONAL,
874 * partyName [1] DirectoryString }
875 *
876 * NOTE: PolarSSL only parses and uses dNSName at this point.
877 */
878static int x509_get_subject_alt_name( unsigned char **p,
879 const unsigned char *end,
880 x509_sequence *subject_alt_name )
881{
882 int ret;
883 size_t len, tag_len;
884 asn1_buf *buf;
885 unsigned char tag;
886 asn1_sequence *cur = subject_alt_name;
887
888 /* Get main sequence tag */
889 if( ( ret = asn1_get_tag( p, end, &len,
890 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
891 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
892
893 if( *p + len != end )
894 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
895 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
896
897 while( *p < end )
898 {
899 if( ( end - *p ) < 1 )
900 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
901 POLARSSL_ERR_ASN1_OUT_OF_DATA );
902
903 tag = **p;
904 (*p)++;
905 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
906 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
907
908 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
909 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
910 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
911
912 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
913 {
914 *p += tag_len;
915 continue;
916 }
917
918 buf = &(cur->buf);
919 buf->tag = tag;
920 buf->p = *p;
921 buf->len = tag_len;
922 *p += buf->len;
923
924 /* Allocate and assign next pointer */
925 if (*p < end)
926 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200927 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000928 sizeof( asn1_sequence ) );
929
930 if( cur->next == NULL )
931 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
932 POLARSSL_ERR_ASN1_MALLOC_FAILED );
933
Paul Bakker535e97d2012-08-23 10:49:55 +0000934 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000935 cur = cur->next;
936 }
937 }
938
939 /* Set final sequence entry's next pointer to NULL */
940 cur->next = NULL;
941
942 if( *p != end )
943 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
944 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
945
946 return( 0 );
947}
948
949/*
Paul Bakker74111d32011-01-15 16:57:55 +0000950 * X.509 v3 extensions
951 *
952 * TODO: Perform all of the basic constraints tests required by the RFC
953 * TODO: Set values for undetected extensions to a sane default?
954 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000955 */
956static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000957 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000958 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000959{
Paul Bakker23986e52011-04-24 08:57:21 +0000960 int ret;
961 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000962 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000963
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000964 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000965 {
966 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
967 return( 0 );
968
969 return( ret );
970 }
971
Paul Bakker5121ce52009-01-03 21:22:43 +0000972 while( *p < end )
973 {
Paul Bakker74111d32011-01-15 16:57:55 +0000974 /*
975 * Extension ::= SEQUENCE {
976 * extnID OBJECT IDENTIFIER,
977 * critical BOOLEAN DEFAULT FALSE,
978 * extnValue OCTET STRING }
979 */
980 x509_buf extn_oid = {0, 0, NULL};
981 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200982 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000983
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 if( ( ret = asn1_get_tag( p, end, &len,
985 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000986 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000988 end_ext_data = *p + len;
989
Paul Bakker74111d32011-01-15 16:57:55 +0000990 /* Get extension ID */
991 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
Paul Bakker74111d32011-01-15 16:57:55 +0000993 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000994 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
Paul Bakker74111d32011-01-15 16:57:55 +0000996 extn_oid.p = *p;
997 *p += extn_oid.len;
998
999 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001000 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001001 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1002
1003 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001004 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001005 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001006 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Paul Bakker74111d32011-01-15 16:57:55 +00001008 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001009 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001011 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001013 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001014
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001015 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001016 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001017 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Paul Bakker74111d32011-01-15 16:57:55 +00001019 /*
1020 * Detect supported extensions
1021 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001022 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1023
1024 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001025 {
1026 /* No parser found, skip extension */
1027 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker5c721f92011-07-27 16:51:09 +00001029#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001030 if( is_critical )
1031 {
1032 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001033 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001034 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1035 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001036#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001037 continue;
1038 }
1039
1040 crt->ext_types |= ext_type;
1041
1042 switch( ext_type )
1043 {
1044 case EXT_BASIC_CONSTRAINTS:
1045 /* Parse basic constraints */
1046 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1047 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1048 return ( ret );
1049 break;
1050
1051 case EXT_KEY_USAGE:
1052 /* Parse key usage */
1053 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1054 &crt->key_usage ) ) != 0 )
1055 return ( ret );
1056 break;
1057
1058 case EXT_EXTENDED_KEY_USAGE:
1059 /* Parse extended key usage */
1060 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1061 &crt->ext_key_usage ) ) != 0 )
1062 return ( ret );
1063 break;
1064
1065 case EXT_SUBJECT_ALT_NAME:
1066 /* Parse subject alt name */
1067 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1068 &crt->subject_alt_names ) ) != 0 )
1069 return ( ret );
1070 break;
1071
1072 case EXT_NS_CERT_TYPE:
1073 /* Parse netscape certificate type */
1074 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1075 &crt->ns_cert_type ) ) != 0 )
1076 return ( ret );
1077 break;
1078
1079 default:
1080 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001081 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 }
1083
1084 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001085 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001086 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 return( 0 );
1089}
1090
1091/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001092 * X.509 CRL Entries
1093 */
1094static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001095 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001096 x509_crl_entry *entry )
1097{
Paul Bakker23986e52011-04-24 08:57:21 +00001098 int ret;
1099 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001100 x509_crl_entry *cur_entry = entry;
1101
1102 if( *p == end )
1103 return( 0 );
1104
Paul Bakker9be19372009-07-27 20:21:53 +00001105 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001106 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1107 {
1108 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1109 return( 0 );
1110
1111 return( ret );
1112 }
1113
Paul Bakker9be19372009-07-27 20:21:53 +00001114 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001115
1116 while( *p < end )
1117 {
Paul Bakker23986e52011-04-24 08:57:21 +00001118 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001119 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001120
1121 if( ( ret = asn1_get_tag( p, end, &len2,
1122 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1123 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001124 return( ret );
1125 }
1126
Paul Bakker9be19372009-07-27 20:21:53 +00001127 cur_entry->raw.tag = **p;
1128 cur_entry->raw.p = *p;
1129 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001130 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001131
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001132 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001133 return( ret );
1134
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001135 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001136 return( ret );
1137
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001138 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001139 return( ret );
1140
Paul Bakker74111d32011-01-15 16:57:55 +00001141 if ( *p < end )
1142 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001143 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001144
1145 if( cur_entry->next == NULL )
1146 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1147
Paul Bakkerd98030e2009-05-02 15:13:40 +00001148 cur_entry = cur_entry->next;
1149 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1150 }
1151 }
1152
1153 return( 0 );
1154}
1155
Paul Bakkerc70b9822013-04-07 22:00:46 +02001156static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1157 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001158{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001159 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001160
Paul Bakkerc70b9822013-04-07 22:00:46 +02001161 if( ret != 0 )
1162 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001163
Paul Bakkerc70b9822013-04-07 22:00:46 +02001164 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001165}
1166
Paul Bakkerd98030e2009-05-02 15:13:40 +00001167/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001168 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001170static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1171 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001172{
Paul Bakker23986e52011-04-24 08:57:21 +00001173 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001174 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001175 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
Paul Bakker320a4b52009-03-28 18:52:39 +00001177 /*
1178 * Check for valid input
1179 */
1180 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001181 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001182
Paul Bakker6e339b52013-07-03 13:37:05 +02001183 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001184
1185 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001186 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001187
1188 memcpy( p, buf, buflen );
1189
1190 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
1192 crt->raw.p = p;
1193 crt->raw.len = len;
1194 end = p + len;
1195
1196 /*
1197 * Certificate ::= SEQUENCE {
1198 * tbsCertificate TBSCertificate,
1199 * signatureAlgorithm AlgorithmIdentifier,
1200 * signatureValue BIT STRING }
1201 */
1202 if( ( ret = asn1_get_tag( &p, end, &len,
1203 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1204 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001205 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001206 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
1208
Paul Bakkerb00ca422012-09-25 12:10:00 +00001209 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001211 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001212 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001213 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001215 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001216
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 /*
1218 * TBSCertificate ::= SEQUENCE {
1219 */
1220 crt->tbs.p = p;
1221
1222 if( ( ret = asn1_get_tag( &p, end, &len,
1223 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1224 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001225 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001226 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 }
1228
1229 end = p + len;
1230 crt->tbs.len = end - crt->tbs.p;
1231
1232 /*
1233 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1234 *
1235 * CertificateSerialNumber ::= INTEGER
1236 *
1237 * signature AlgorithmIdentifier
1238 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001239 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1240 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1241 ( ret = x509_get_alg( &p, end, &crt->sig_oid1, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001243 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 return( ret );
1245 }
1246
1247 crt->version++;
1248
1249 if( crt->version > 3 )
1250 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001251 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001252 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 }
1254
Paul Bakkerc70b9822013-04-07 22:00:46 +02001255 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1256 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001258 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001259 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 }
1261
1262 /*
1263 * issuer Name
1264 */
1265 crt->issuer_raw.p = p;
1266
1267 if( ( ret = asn1_get_tag( &p, end, &len,
1268 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1269 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001270 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001271 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 }
1273
1274 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1275 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001276 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 return( ret );
1278 }
1279
1280 crt->issuer_raw.len = p - crt->issuer_raw.p;
1281
1282 /*
1283 * Validity ::= SEQUENCE {
1284 * notBefore Time,
1285 * notAfter Time }
1286 *
1287 */
1288 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1289 &crt->valid_to ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 return( ret );
1293 }
1294
1295 /*
1296 * subject Name
1297 */
1298 crt->subject_raw.p = p;
1299
1300 if( ( ret = asn1_get_tag( &p, end, &len,
1301 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1302 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001303 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001304 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 }
1306
Paul Bakkercefb3962012-06-27 11:51:09 +00001307 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001309 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 return( ret );
1311 }
1312
1313 crt->subject_raw.len = p - crt->subject_raw.p;
1314
1315 /*
1316 * SubjectPublicKeyInfo ::= SEQUENCE
1317 * algorithm AlgorithmIdentifier,
1318 * subjectPublicKey BIT STRING }
1319 */
1320 if( ( ret = asn1_get_tag( &p, end, &len,
1321 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1322 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001323 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001324 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
1327 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1328 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1329 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 return( ret );
1332 }
1333
1334 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 return( ret );
1338 }
1339
1340 crt->rsa.len = mpi_size( &crt->rsa.N );
1341
1342 /*
1343 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1344 * -- If present, version shall be v2 or v3
1345 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1346 * -- If present, version shall be v2 or v3
1347 * extensions [3] EXPLICIT Extensions OPTIONAL
1348 * -- If present, version shall be v3
1349 */
1350 if( crt->version == 2 || crt->version == 3 )
1351 {
1352 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1353 if( ret != 0 )
1354 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001355 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 return( ret );
1357 }
1358 }
1359
1360 if( crt->version == 2 || crt->version == 3 )
1361 {
1362 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1363 if( ret != 0 )
1364 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001365 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 return( ret );
1367 }
1368 }
1369
1370 if( crt->version == 3 )
1371 {
Paul Bakker74111d32011-01-15 16:57:55 +00001372 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 if( ret != 0 )
1374 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001375 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 return( ret );
1377 }
1378 }
1379
1380 if( p != end )
1381 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001382 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001383 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001384 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 }
1386
Paul Bakkerb00ca422012-09-25 12:10:00 +00001387 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
1389 /*
1390 * signatureAlgorithm AlgorithmIdentifier,
1391 * signatureValue BIT STRING
1392 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001393 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001395 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 return( ret );
1397 }
1398
Paul Bakker535e97d2012-08-23 10:49:55 +00001399 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1400 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001402 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001403 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 }
1405
1406 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1407 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001408 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 return( ret );
1410 }
1411
1412 if( p != end )
1413 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001414 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001415 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001416 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 }
1418
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001419 return( 0 );
1420}
1421
1422/*
Paul Bakker42c65812013-06-24 19:21:59 +02001423 * Parse one X.509 certificate in DER format from a buffer and add them to a
1424 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001425 */
Paul Bakker42c65812013-06-24 19:21:59 +02001426int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001427{
Paul Bakker42c65812013-06-24 19:21:59 +02001428 int ret;
1429 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001430
1431 /*
1432 * Check for valid input
1433 */
1434 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001435 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001436
1437 while( crt->version != 0 && crt->next != NULL )
1438 {
1439 prev = crt;
1440 crt = crt->next;
1441 }
1442
1443 /*
1444 * Add new certificate on the end of the chain if needed.
1445 */
1446 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001447 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001448 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001449
Paul Bakker7d06ad22009-05-02 15:53:56 +00001450 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001451 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001452
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001453 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001454 crt = crt->next;
1455 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001456 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Paul Bakker42c65812013-06-24 19:21:59 +02001458 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1459 {
1460 if( prev )
1461 prev->next = NULL;
1462
1463 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001464 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001465
1466 return( ret );
1467 }
1468
1469 return( 0 );
1470}
1471
1472/*
1473 * Parse one or more PEM certificates from a buffer and add them to the chained list
1474 */
1475int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1476{
1477 int ret, success = 0, first_error = 0, total_failed = 0;
1478 int buf_format = X509_FORMAT_DER;
1479
1480 /*
1481 * Check for valid input
1482 */
1483 if( chain == NULL || buf == NULL )
1484 return( POLARSSL_ERR_X509_INVALID_INPUT );
1485
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001486 /*
1487 * Determine buffer content. Buffer contains either one DER certificate or
1488 * one or more PEM certificates.
1489 */
1490#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001491 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001492 buf_format = X509_FORMAT_PEM;
1493#endif
1494
1495 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001496 return x509parse_crt_der( chain, buf, buflen );
1497
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001498#if defined(POLARSSL_PEM_C)
1499 if( buf_format == X509_FORMAT_PEM )
1500 {
1501 pem_context pem;
1502
1503 while( buflen > 0 )
1504 {
1505 size_t use_len;
1506 pem_init( &pem );
1507
1508 ret = pem_read_buffer( &pem,
1509 "-----BEGIN CERTIFICATE-----",
1510 "-----END CERTIFICATE-----",
1511 buf, NULL, 0, &use_len );
1512
1513 if( ret == 0 )
1514 {
1515 /*
1516 * Was PEM encoded
1517 */
1518 buflen -= use_len;
1519 buf += use_len;
1520 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001521 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1522 {
1523 return( ret );
1524 }
Paul Bakker00b28602013-06-24 13:02:41 +02001525 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001526 {
1527 pem_free( &pem );
1528
Paul Bakker5ed3b342013-06-24 19:05:46 +02001529 /*
1530 * PEM header and footer were found
1531 */
1532 buflen -= use_len;
1533 buf += use_len;
1534
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001535 if( first_error == 0 )
1536 first_error = ret;
1537
1538 continue;
1539 }
1540 else
1541 break;
1542
Paul Bakker42c65812013-06-24 19:21:59 +02001543 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001544
1545 pem_free( &pem );
1546
1547 if( ret != 0 )
1548 {
1549 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001550 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001551 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001552 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001553 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001554
1555 if( first_error == 0 )
1556 first_error = ret;
1557
Paul Bakker42c65812013-06-24 19:21:59 +02001558 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001559 continue;
1560 }
1561
1562 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001563 }
1564 }
1565#endif
1566
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001567 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001568 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001569 else if( first_error )
1570 return( first_error );
1571 else
1572 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001573}
1574
1575/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001576 * Parse one or more CRLs and add them to the chained list
1577 */
Paul Bakker23986e52011-04-24 08:57:21 +00001578int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001579{
Paul Bakker23986e52011-04-24 08:57:21 +00001580 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001581 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001582 unsigned char *p, *end;
1583 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001584#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001585 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001586 pem_context pem;
1587#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001588
1589 crl = chain;
1590
1591 /*
1592 * Check for valid input
1593 */
1594 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001595 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001596
1597 while( crl->version != 0 && crl->next != NULL )
1598 crl = crl->next;
1599
1600 /*
1601 * Add new CRL on the end of the chain if needed.
1602 */
1603 if ( crl->version != 0 && crl->next == NULL)
1604 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001605 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001606
Paul Bakker7d06ad22009-05-02 15:53:56 +00001607 if( crl->next == NULL )
1608 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001610 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001611 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612
Paul Bakker7d06ad22009-05-02 15:53:56 +00001613 crl = crl->next;
1614 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001615 }
1616
Paul Bakker96743fc2011-02-12 14:30:57 +00001617#if defined(POLARSSL_PEM_C)
1618 pem_init( &pem );
1619 ret = pem_read_buffer( &pem,
1620 "-----BEGIN X509 CRL-----",
1621 "-----END X509 CRL-----",
1622 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001623
Paul Bakker96743fc2011-02-12 14:30:57 +00001624 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001625 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001626 /*
1627 * Was PEM encoded
1628 */
1629 buflen -= use_len;
1630 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631
1632 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001633 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001635 p = pem.buf;
1636 pem.buf = NULL;
1637 len = pem.buflen;
1638 pem_free( &pem );
1639 }
Paul Bakker00b28602013-06-24 13:02:41 +02001640 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001641 {
1642 pem_free( &pem );
1643 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001644 }
1645 else
1646 {
1647 /*
1648 * nope, copy the raw DER data
1649 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001650 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001651
1652 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001653 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001654
1655 memcpy( p, buf, buflen );
1656
1657 buflen = 0;
1658 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001659#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001660 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001661
1662 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001663 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001664
1665 memcpy( p, buf, buflen );
1666
1667 buflen = 0;
1668#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001669
1670 crl->raw.p = p;
1671 crl->raw.len = len;
1672 end = p + len;
1673
1674 /*
1675 * CertificateList ::= SEQUENCE {
1676 * tbsCertList TBSCertList,
1677 * signatureAlgorithm AlgorithmIdentifier,
1678 * signatureValue BIT STRING }
1679 */
1680 if( ( ret = asn1_get_tag( &p, end, &len,
1681 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1682 {
1683 x509_crl_free( crl );
1684 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1685 }
1686
Paul Bakker23986e52011-04-24 08:57:21 +00001687 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001688 {
1689 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001690 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1692 }
1693
1694 /*
1695 * TBSCertList ::= SEQUENCE {
1696 */
1697 crl->tbs.p = p;
1698
1699 if( ( ret = asn1_get_tag( &p, end, &len,
1700 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1701 {
1702 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001703 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001704 }
1705
1706 end = p + len;
1707 crl->tbs.len = end - crl->tbs.p;
1708
1709 /*
1710 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1711 * -- if present, MUST be v2
1712 *
1713 * signature AlgorithmIdentifier
1714 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001715 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001716 ( ret = x509_get_alg( &p, end, &crl->sig_oid1, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001717 {
1718 x509_crl_free( crl );
1719 return( ret );
1720 }
1721
1722 crl->version++;
1723
1724 if( crl->version > 2 )
1725 {
1726 x509_crl_free( crl );
1727 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1728 }
1729
Paul Bakkerc70b9822013-04-07 22:00:46 +02001730 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1731 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001732 {
1733 x509_crl_free( crl );
1734 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1735 }
1736
1737 /*
1738 * issuer Name
1739 */
1740 crl->issuer_raw.p = p;
1741
1742 if( ( ret = asn1_get_tag( &p, end, &len,
1743 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1744 {
1745 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001746 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001747 }
1748
1749 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1750 {
1751 x509_crl_free( crl );
1752 return( ret );
1753 }
1754
1755 crl->issuer_raw.len = p - crl->issuer_raw.p;
1756
1757 /*
1758 * thisUpdate Time
1759 * nextUpdate Time OPTIONAL
1760 */
Paul Bakker91200182010-02-18 21:26:15 +00001761 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001762 {
1763 x509_crl_free( crl );
1764 return( ret );
1765 }
1766
Paul Bakker91200182010-02-18 21:26:15 +00001767 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001768 {
Paul Bakker9d781402011-05-09 16:17:09 +00001769 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001770 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001771 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001772 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001773 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001774 x509_crl_free( crl );
1775 return( ret );
1776 }
1777 }
1778
1779 /*
1780 * revokedCertificates SEQUENCE OF SEQUENCE {
1781 * userCertificate CertificateSerialNumber,
1782 * revocationDate Time,
1783 * crlEntryExtensions Extensions OPTIONAL
1784 * -- if present, MUST be v2
1785 * } OPTIONAL
1786 */
1787 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1788 {
1789 x509_crl_free( crl );
1790 return( ret );
1791 }
1792
1793 /*
1794 * crlExtensions EXPLICIT Extensions OPTIONAL
1795 * -- if present, MUST be v2
1796 */
1797 if( crl->version == 2 )
1798 {
1799 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1800
1801 if( ret != 0 )
1802 {
1803 x509_crl_free( crl );
1804 return( ret );
1805 }
1806 }
1807
1808 if( p != end )
1809 {
1810 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001811 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001812 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1813 }
1814
1815 end = crl->raw.p + crl->raw.len;
1816
1817 /*
1818 * signatureAlgorithm AlgorithmIdentifier,
1819 * signatureValue BIT STRING
1820 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001821 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001822 {
1823 x509_crl_free( crl );
1824 return( ret );
1825 }
1826
Paul Bakker535e97d2012-08-23 10:49:55 +00001827 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1828 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001829 {
1830 x509_crl_free( crl );
1831 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1832 }
1833
1834 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1835 {
1836 x509_crl_free( crl );
1837 return( ret );
1838 }
1839
1840 if( p != end )
1841 {
1842 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001843 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001844 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1845 }
1846
1847 if( buflen > 0 )
1848 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001849 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850
Paul Bakker7d06ad22009-05-02 15:53:56 +00001851 if( crl->next == NULL )
1852 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001853 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001854 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001855 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001856
Paul Bakker7d06ad22009-05-02 15:53:56 +00001857 crl = crl->next;
1858 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001859
1860 return( x509parse_crl( crl, buf, buflen ) );
1861 }
1862
1863 return( 0 );
1864}
1865
Paul Bakker335db3f2011-04-25 15:28:35 +00001866#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001867/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001868 * Load all data from a file into a given buffer.
1869 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001870static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001871{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001872 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001873
Paul Bakkerd98030e2009-05-02 15:13:40 +00001874 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001875 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001876
Paul Bakkerd98030e2009-05-02 15:13:40 +00001877 fseek( f, 0, SEEK_END );
1878 *n = (size_t) ftell( f );
1879 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001880
Paul Bakker6e339b52013-07-03 13:37:05 +02001881 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001882 {
1883 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001884 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001885 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001886
Paul Bakkerd98030e2009-05-02 15:13:40 +00001887 if( fread( *buf, 1, *n, f ) != *n )
1888 {
1889 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001890 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001891 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001892 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001893
Paul Bakkerd98030e2009-05-02 15:13:40 +00001894 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001895
Paul Bakkerd98030e2009-05-02 15:13:40 +00001896 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001897
Paul Bakkerd98030e2009-05-02 15:13:40 +00001898 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001899}
1900
1901/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 * Load one or more certificates and add them to the chained list
1903 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001904int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001905{
1906 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 size_t n;
1908 unsigned char *buf;
1909
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001910 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001911 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
Paul Bakker69e095c2011-12-10 21:55:01 +00001913 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
1915 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001916 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
1918 return( ret );
1919}
1920
Paul Bakker8d914582012-06-04 12:46:42 +00001921int x509parse_crtpath( x509_cert *chain, const char *path )
1922{
1923 int ret = 0;
1924#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001925 int w_ret;
1926 WCHAR szDir[MAX_PATH];
1927 char filename[MAX_PATH];
1928 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001929 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001930
Paul Bakker97872ac2012-11-02 12:53:26 +00001931 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001932 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001933
1934 if( len > MAX_PATH - 3 )
1935 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001936
Paul Bakker3338b792012-10-01 21:13:10 +00001937 memset( szDir, 0, sizeof(szDir) );
1938 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001939 memcpy( filename, path, len );
1940 filename[len++] = '\\';
1941 p = filename + len;
1942 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001943
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001944 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001945
Paul Bakker97872ac2012-11-02 12:53:26 +00001946 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001947 if (hFind == INVALID_HANDLE_VALUE)
1948 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1949
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001950 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001951 do
1952 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001953 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001954
Paul Bakkere4791f32012-06-04 21:29:15 +00001955 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001956 continue;
1957
Paul Bakker3338b792012-10-01 21:13:10 +00001958 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1959 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001960 p, len - 1,
1961 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001962
Paul Bakker3338b792012-10-01 21:13:10 +00001963 w_ret = x509parse_crtfile( chain, filename );
1964 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001965 ret++;
1966 else
1967 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001968 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001969 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001970
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001971 if (GetLastError() != ERROR_NO_MORE_FILES)
1972 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001973
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001974cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001975 FindClose( hFind );
1976#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001977 int t_ret, i;
1978 struct stat sb;
1979 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001980 char entry_name[255];
1981 DIR *dir = opendir( path );
1982
1983 if( dir == NULL)
1984 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1985
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001986 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001987 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001988 if( result == NULL )
1989 break;
1990
1991 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1992
1993 i = stat( entry_name, &sb );
1994
1995 if( i == -1 )
1996 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1997
1998 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00001999 continue;
2000
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002001 // Ignore parse errors
2002 //
Paul Bakker8d914582012-06-04 12:46:42 +00002003 t_ret = x509parse_crtfile( chain, entry_name );
2004 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002005 ret++;
2006 else
2007 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002008 }
2009 closedir( dir );
2010#endif
2011
2012 return( ret );
2013}
2014
Paul Bakkerd98030e2009-05-02 15:13:40 +00002015/*
2016 * Load one or more CRLs and add them to the chained list
2017 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002018int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002019{
2020 int ret;
2021 size_t n;
2022 unsigned char *buf;
2023
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002024 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002025 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002026
Paul Bakker27fdf462011-06-09 13:55:13 +00002027 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002028
2029 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002030 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002031
2032 return( ret );
2033}
2034
Paul Bakker5121ce52009-01-03 21:22:43 +00002035/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002036 * Load and parse a private RSA key
2037 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002038int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002039{
2040 int ret;
2041 size_t n;
2042 unsigned char *buf;
2043
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002044 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002045 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002046
2047 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002048 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002049 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002050 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002051 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002052
2053 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002054 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002055
2056 return( ret );
2057}
2058
2059/*
2060 * Load and parse a public RSA key
2061 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002062int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002063{
2064 int ret;
2065 size_t n;
2066 unsigned char *buf;
2067
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002068 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002069 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002070
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002071 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002072
2073 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002074 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002075
2076 return( ret );
2077}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002078
2079#if defined(POLARSSL_ECP_C)
2080/*
2081 * Load and parse a private EC key
2082 */
2083int x509parse_keyfile_ec( ecp_keypair *eckey,
2084 const char *path, const char *pwd )
2085{
2086 int ret;
2087 size_t n;
2088 unsigned char *buf;
2089
2090 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2091 return( ret );
2092
2093 if( pwd == NULL )
2094 ret = x509parse_key_ec( eckey, buf, n, NULL, 0 );
2095 else
2096 ret = x509parse_key_ec( eckey, buf, n,
2097 (const unsigned char *) pwd, strlen( pwd ) );
2098
2099 memset( buf, 0, n + 1 );
2100 free( buf );
2101
2102 return( ret );
2103}
2104
2105/*
2106 * Load and parse a public EC key
2107 */
2108int x509parse_public_keyfile_ec( ecp_keypair *eckey, const char *path )
2109{
2110 int ret;
2111 size_t n;
2112 unsigned char *buf;
2113
2114 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2115 return( ret );
2116
2117 ret = x509parse_public_key_ec( eckey, buf, n );
2118
2119 memset( buf, 0, n + 1 );
2120 free( buf );
2121
2122 return( ret );
2123}
2124#endif /* defined(POLARSSL_ECP_C) */
Paul Bakker335db3f2011-04-25 15:28:35 +00002125#endif /* POLARSSL_FS_IO */
2126
2127/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002128 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002130static int x509parse_key_pkcs1_der( rsa_context *rsa,
2131 const unsigned char *key,
2132 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002133{
Paul Bakker23986e52011-04-24 08:57:21 +00002134 int ret;
2135 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002137
Paul Bakker96743fc2011-02-12 14:30:57 +00002138 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002139 end = p + keylen;
2140
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002142 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002143 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 * RSAPrivateKey ::= SEQUENCE {
2145 * version Version,
2146 * modulus INTEGER, -- n
2147 * publicExponent INTEGER, -- e
2148 * privateExponent INTEGER, -- d
2149 * prime1 INTEGER, -- p
2150 * prime2 INTEGER, -- q
2151 * exponent1 INTEGER, -- d mod (p-1)
2152 * exponent2 INTEGER, -- d mod (q-1)
2153 * coefficient INTEGER, -- (inverse of q) mod p
2154 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2155 * }
2156 */
2157 if( ( ret = asn1_get_tag( &p, end, &len,
2158 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2159 {
Paul Bakker9d781402011-05-09 16:17:09 +00002160 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 }
2162
2163 end = p + len;
2164
2165 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2166 {
Paul Bakker9d781402011-05-09 16:17:09 +00002167 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 }
2169
2170 if( rsa->ver != 0 )
2171 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002172 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 }
2174
2175 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2176 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2177 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2178 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2179 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2180 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2181 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2182 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2183 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002185 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 }
2187
2188 rsa->len = mpi_size( &rsa->N );
2189
2190 if( p != end )
2191 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002193 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002194 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 }
2196
2197 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2198 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 rsa_free( rsa );
2200 return( ret );
2201 }
2202
Paul Bakkere2f50402013-06-24 19:00:59 +02002203 return( 0 );
2204}
2205
2206/*
2207 * Parse an unencrypted PKCS#8 encoded private RSA key
2208 */
2209static int x509parse_key_pkcs8_unencrypted_der(
2210 rsa_context *rsa,
2211 const unsigned char *key,
2212 size_t keylen )
2213{
2214 int ret;
2215 size_t len;
2216 unsigned char *p, *end;
2217 x509_buf pk_alg_oid;
2218 pk_type_t pk_alg = POLARSSL_PK_NONE;
2219
2220 p = (unsigned char *) key;
2221 end = p + keylen;
2222
2223 /*
2224 * This function parses the PrivatKeyInfo object (PKCS#8)
2225 *
2226 * PrivateKeyInfo ::= SEQUENCE {
2227 * version Version,
2228 * algorithm AlgorithmIdentifier,
2229 * PrivateKey BIT STRING
2230 * }
2231 *
2232 * AlgorithmIdentifier ::= SEQUENCE {
2233 * algorithm OBJECT IDENTIFIER,
2234 * parameters ANY DEFINED BY algorithm OPTIONAL
2235 * }
2236 *
2237 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2238 */
2239 if( ( ret = asn1_get_tag( &p, end, &len,
2240 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2241 {
2242 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2243 }
2244
2245 end = p + len;
2246
2247 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2248 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2249
2250 if( rsa->ver != 0 )
2251 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2252
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002253 if( ( ret = asn1_get_alg_null( &p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002254 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2255
2256 /*
2257 * only RSA keys handled at this time
2258 */
2259 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2260 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2261
2262 /*
2263 * Get the OCTET STRING and parse the PKCS#1 format inside
2264 */
2265 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2266 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2267
2268 if( ( end - p ) < 1 )
2269 {
2270 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2271 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2272 }
2273
2274 end = p + len;
2275
2276 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2277 return( ret );
2278
2279 return( 0 );
2280}
2281
2282/*
Paul Bakkerbda7cb72013-06-24 19:34:25 +02002283 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002284 */
2285static int x509parse_key_pkcs8_encrypted_der(
2286 rsa_context *rsa,
2287 const unsigned char *key,
2288 size_t keylen,
2289 const unsigned char *pwd,
2290 size_t pwdlen )
2291{
2292 int ret;
2293 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002294 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002295 x509_buf pbe_alg_oid, pbe_params;
2296 unsigned char buf[2048];
Paul Bakker7749a222013-06-28 17:28:20 +02002297#if defined(POLARSSL_PKCS12_C)
2298 cipher_type_t cipher_alg;
2299 md_type_t md_alg;
2300#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002301
2302 memset(buf, 0, 2048);
2303
2304 p = (unsigned char *) key;
2305 end = p + keylen;
2306
Paul Bakker28144de2013-06-24 19:28:55 +02002307 if( pwdlen == 0 )
2308 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2309
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002310 /*
2311 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2312 *
2313 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2314 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2315 * encryptedData EncryptedData
2316 * }
2317 *
2318 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2319 *
2320 * EncryptedData ::= OCTET STRING
2321 *
2322 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2323 */
2324 if( ( ret = asn1_get_tag( &p, end, &len,
2325 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2326 {
2327 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2328 }
2329
2330 end = p + len;
2331
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002332 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002333 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002334
2335 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2336 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2337
2338 // buf has been sized to 2048 bytes
2339 if( len > 2048 )
2340 return( POLARSSL_ERR_X509_INVALID_INPUT );
2341
2342 /*
2343 * Decrypt EncryptedData with appropriate PDE
2344 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002345#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002346 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002347 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002348 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002349 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002350 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002351 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002352 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2353 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2354
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002355 return( ret );
2356 }
2357 }
2358 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2359 {
2360 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2361 PKCS12_PBE_DECRYPT,
2362 pwd, pwdlen,
2363 p, len, buf ) ) != 0 )
2364 {
2365 return( ret );
2366 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002367
2368 // Best guess for password mismatch when using RC4. If first tag is
2369 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2370 //
2371 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2372 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002373 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002374 else
2375#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002376#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002377 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002378 {
2379 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2380 p, len, buf ) ) != 0 )
2381 {
2382 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2383 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2384
2385 return( ret );
2386 }
2387 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002388 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002389#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002390 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2391
2392 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2393}
2394
2395/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002396 * Parse a private RSA key
2397 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002398int x509parse_key_rsa( rsa_context *rsa,
2399 const unsigned char *key, size_t keylen,
2400 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002401{
2402 int ret;
2403
Paul Bakker96743fc2011-02-12 14:30:57 +00002404#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002405 size_t len;
2406 pem_context pem;
2407
2408 pem_init( &pem );
2409 ret = pem_read_buffer( &pem,
2410 "-----BEGIN RSA PRIVATE KEY-----",
2411 "-----END RSA PRIVATE KEY-----",
2412 key, pwd, pwdlen, &len );
2413 if( ret == 0 )
2414 {
2415 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2416 {
2417 rsa_free( rsa );
2418 }
2419
2420 pem_free( &pem );
2421 return( ret );
2422 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002423 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2424 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2425 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2426 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002427 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002428 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002429
2430 ret = pem_read_buffer( &pem,
2431 "-----BEGIN PRIVATE KEY-----",
2432 "-----END PRIVATE KEY-----",
2433 key, NULL, 0, &len );
2434 if( ret == 0 )
2435 {
2436 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2437 pem.buf, pem.buflen ) ) != 0 )
2438 {
2439 rsa_free( rsa );
2440 }
2441
2442 pem_free( &pem );
2443 return( ret );
2444 }
2445 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002446 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002447
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002448 ret = pem_read_buffer( &pem,
2449 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2450 "-----END ENCRYPTED PRIVATE KEY-----",
2451 key, NULL, 0, &len );
2452 if( ret == 0 )
2453 {
2454 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2455 pem.buf, pem.buflen,
2456 pwd, pwdlen ) ) != 0 )
2457 {
2458 rsa_free( rsa );
2459 }
2460
2461 pem_free( &pem );
2462 return( ret );
2463 }
2464 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002465 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002466#else
2467 ((void) pwd);
2468 ((void) pwdlen);
2469#endif /* POLARSSL_PEM_C */
2470
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002471 /*
2472 * At this point we only know it's not a PEM formatted key. Could be any
2473 * of the known DER encoded private key formats
2474 *
2475 * We try the different DER format parsers to see if one passes without
2476 * error
2477 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002478 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2479 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002480 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002481 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002484 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002485
2486 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2487 {
2488 return( ret );
2489 }
2490
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002491 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2492 return( 0 );
2493
2494 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002495
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002496 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2497 return( 0 );
2498
2499 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002500
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002501 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502}
2503
2504/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002505 * Parse a public RSA key
2506 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002507int x509parse_public_key_rsa( rsa_context *rsa,
2508 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002509{
Paul Bakker23986e52011-04-24 08:57:21 +00002510 int ret;
2511 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002512 unsigned char *p, *end;
2513 x509_buf alg_oid;
2514#if defined(POLARSSL_PEM_C)
2515 pem_context pem;
2516
2517 pem_init( &pem );
2518 ret = pem_read_buffer( &pem,
2519 "-----BEGIN PUBLIC KEY-----",
2520 "-----END PUBLIC KEY-----",
2521 key, NULL, 0, &len );
2522
2523 if( ret == 0 )
2524 {
2525 /*
2526 * Was PEM encoded
2527 */
2528 keylen = pem.buflen;
2529 }
Paul Bakker00b28602013-06-24 13:02:41 +02002530 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002531 {
2532 pem_free( &pem );
2533 return( ret );
2534 }
2535
2536 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2537#else
2538 p = (unsigned char *) key;
2539#endif
2540 end = p + keylen;
2541
2542 /*
2543 * PublicKeyInfo ::= SEQUENCE {
2544 * algorithm AlgorithmIdentifier,
2545 * PublicKey BIT STRING
2546 * }
2547 *
2548 * AlgorithmIdentifier ::= SEQUENCE {
2549 * algorithm OBJECT IDENTIFIER,
2550 * parameters ANY DEFINED BY algorithm OPTIONAL
2551 * }
2552 *
2553 * RSAPublicKey ::= SEQUENCE {
2554 * modulus INTEGER, -- n
2555 * publicExponent INTEGER -- e
2556 * }
2557 */
2558
2559 if( ( ret = asn1_get_tag( &p, end, &len,
2560 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2561 {
2562#if defined(POLARSSL_PEM_C)
2563 pem_free( &pem );
2564#endif
2565 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002566 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002567 }
2568
2569 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2570 {
2571#if defined(POLARSSL_PEM_C)
2572 pem_free( &pem );
2573#endif
2574 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002575 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002576 }
2577
2578 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2579 {
2580#if defined(POLARSSL_PEM_C)
2581 pem_free( &pem );
2582#endif
2583 rsa_free( rsa );
2584 return( ret );
2585 }
2586
2587 rsa->len = mpi_size( &rsa->N );
2588
2589#if defined(POLARSSL_PEM_C)
2590 pem_free( &pem );
2591#endif
2592
2593 return( 0 );
2594}
2595
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002596#if defined(POLARSSL_ECP_C)
2597/*
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002598 * Parse an unencrypted PKCS#8 encoded private EC key
2599 */
2600static int x509parse_key_pkcs8_unencrypted_der_ec(
2601 ecp_keypair *eck,
2602 const unsigned char* key,
2603 size_t keylen )
2604{
2605 int ret;
2606
2607 (void) key;
2608 (void) keylen;
2609
2610 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) == 0 )
2611 return 0;
2612
2613cleanup:
2614 ecp_keypair_free( eck );
2615
2616 return( ret );
2617}
2618
2619/*
2620 * Parse an encrypted PKCS#8 encoded private EC key
2621 */
2622static int x509parse_key_pkcs8_encrypted_der_ec(
2623 ecp_keypair *eck,
2624 const unsigned char *key,
2625 size_t keylen,
2626 const unsigned char *pwd,
2627 size_t pwdlen )
2628{
2629 int ret;
2630
2631 (void) key;
2632 (void) keylen;
2633 (void) pwd;
2634 (void) pwdlen;
2635
2636 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) == 0 )
2637 return 0;
2638
2639cleanup:
2640 ecp_keypair_free( eck );
2641
2642 return( ret );
2643}
2644
2645/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002646 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002647 */
2648static int x509parse_key_sec1_der( ecp_keypair *eck,
2649 const unsigned char *key,
2650 size_t keylen )
2651{
2652 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002653 int version;
2654 size_t len;
2655 ecp_group_id grp_id;
2656 unsigned char *p = (unsigned char *) key;
2657 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002658
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002659 /*
2660 * RFC 5915, orf SEC1 Appendix C.4
2661 *
2662 * ECPrivateKey ::= SEQUENCE {
2663 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2664 * privateKey OCTET STRING,
2665 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2666 * publicKey [1] BIT STRING OPTIONAL
2667 * }
2668 */
2669 if( ( ret = asn1_get_tag( &p, end, &len,
2670 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2671 {
2672 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2673 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002674
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002675 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002676
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002677 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2678 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002679
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002680 if( version != 1 )
2681 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2682
2683 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2684 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2685
2686 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2687 {
2688 ecp_keypair_free( eck );
2689 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2690 }
2691
2692 p += len;
2693
2694 /*
2695 * Is 'parameters' present?
2696 */
2697 if( ( ret = asn1_get_tag( &p, end, &len,
2698 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2699 {
2700 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2701 return( ret );
2702
2703 /* TODO: grp may not be empty at this point,
2704 * if we're wrapped inside a PKCS#8 structure: check consistency */
2705 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2706 {
2707 ecp_keypair_free( eck );
2708 return( ret );
2709 }
2710 }
2711 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2712 {
2713 ecp_keypair_free( eck );
2714 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2715 }
2716
2717 /*
2718 * Is 'publickey' present?
2719 */
2720 if( ( ret = asn1_get_tag( &p, end, &len,
2721 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2722 {
2723 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2724 != 0 )
2725 {
2726 ecp_keypair_free( eck );
2727 return( ret );
2728 }
2729
2730 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2731 {
2732 ecp_keypair_free( eck );
2733 return( ret );
2734 }
2735 }
2736 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2737 {
2738 ecp_keypair_free( eck );
2739 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2740 }
2741
2742 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) != 0 )
2743 {
2744 ecp_keypair_free( eck );
2745 return( ret );
2746 }
2747
2748 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002749}
2750
2751/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002752 * Parse a private EC key
2753 */
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002754int x509parse_key_ec( ecp_keypair *eck,
2755 const unsigned char *key, size_t keylen,
2756 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002757{
2758 int ret;
2759
2760#if defined(POLARSSL_PEM_C)
2761 size_t len;
2762 pem_context pem;
2763
2764 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002765 ret = pem_read_buffer( &pem,
2766 "-----BEGIN EC PRIVATE KEY-----",
2767 "-----END EC PRIVATE KEY-----",
2768 key, pwd, pwdlen, &len );
2769 if( ret == 0 )
2770 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002771 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002772 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002773 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002774 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002775
2776 pem_free( &pem );
2777 return( ret );
2778 }
2779 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2780 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2781 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2782 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2783 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2784 return( ret );
2785
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002786 ret = pem_read_buffer( &pem,
2787 "-----BEGIN PRIVATE KEY-----",
2788 "-----END PRIVATE KEY-----",
2789 key, NULL, 0, &len );
2790 if( ret == 0 )
2791 {
2792 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2793 pem.buf, pem.buflen ) ) != 0 )
2794 {
2795 ecp_keypair_free( eck );
2796 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002797
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002798 pem_free( &pem );
2799 return( ret );
2800 }
2801 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2802 return( ret );
2803
2804 ret = pem_read_buffer( &pem,
2805 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2806 "-----END ENCRYPTED PRIVATE KEY-----",
2807 key, NULL, 0, &len );
2808 if( ret == 0 )
2809 {
2810 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2811 pem.buf, pem.buflen,
2812 pwd, pwdlen ) ) != 0 )
2813 {
2814 ecp_keypair_free( eck );
2815 }
2816
2817 pem_free( &pem );
2818 return( ret );
2819 }
2820 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2821 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002822#else
2823 ((void) pwd);
2824 ((void) pwdlen);
2825#endif /* POLARSSL_PEM_C */
2826
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002827 /*
2828 * At this point we only know it's not a PEM formatted key. Could be any
2829 * of the known DER encoded private key formats
2830 *
2831 * We try the different DER format parsers to see if one passes without
2832 * error
2833 */
2834 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2835 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002836 {
2837 return( 0 );
2838 }
2839
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002840 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002841
2842 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2843 {
2844 return( ret );
2845 }
2846
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002847 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2848 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002849 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002850
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002851 ecp_keypair_free( eck );
2852
2853 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2854 return( 0 );
2855
2856 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002857
2858 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2859}
2860
2861/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002862 * Parse a public EC key in RFC 5480 format, der-encoded
2863 */
2864static int x509parse_public_key_ec_der( ecp_keypair *key,
2865 const unsigned char *buf, size_t len )
2866{
2867 int ret;
2868 ecp_group_id grp_id;
2869 x509_buf alg_oid;
2870 pk_type_t alg = POLARSSL_PK_NONE;
2871 unsigned char *p = (unsigned char *) buf;
2872 unsigned char *end = p + len;
2873 const unsigned char *params_end;
2874 /*
2875 * SubjectPublicKeyInfo ::= SEQUENCE {
2876 * algorithm AlgorithmIdentifier,
2877 * subjectPublicKey BIT STRING
2878 * }
2879 * -- algorithm parameters are ECParameters
2880 * -- subjectPublicKey is an ECPoint
2881 */
2882 if( ( ret = asn1_get_tag( &p, end, &len,
2883 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2884 {
2885 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
2886 }
2887
2888 if( ( ret = x509_get_alg( &p, end, &alg_oid, &params_end ) ) != 0 )
2889 return( ret );
2890
2891 if( oid_get_pk_alg( &alg_oid, &alg ) != 0 )
2892 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2893
2894 if( alg != POLARSSL_PK_ECKEY && alg != POLARSSL_PK_ECKEY_DH )
2895 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2896
2897 if ( alg == POLARSSL_PK_ECKEY_DH )
2898 key->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2899
2900 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2901 return( ret );
2902
2903 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
2904 return( ret );
2905
2906 if( ( ret = x509_get_subpubkey_ec( &p, end, &key->grp, &key->Q ) ) != 0 )
2907 {
2908 return( ret );
2909 }
2910
2911 return( 0 );
2912}
2913
2914/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002915 * Parse a public EC key
2916 */
2917int x509parse_public_key_ec( ecp_keypair *eckey,
2918 const unsigned char *key, size_t keylen )
2919{
2920 int ret;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002921#if defined(POLARSSL_PEM_C)
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002922 size_t len;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002923 pem_context pem;
2924
2925 pem_init( &pem );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002926 ret = pem_read_buffer( &pem,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002927 "-----BEGIN PUBLIC KEY-----",
2928 "-----END PUBLIC KEY-----",
2929 key, NULL, 0, &len );
2930
2931 if( ret == 0 )
2932 {
2933 /*
2934 * Was PEM encoded
2935 */
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002936 key = pem.buf;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002937 keylen = pem.buflen;
2938 }
2939 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2940 {
2941 pem_free( &pem );
2942 return( ret );
2943 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002944#endif
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002945
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002946 if( ( ret = x509parse_public_key_ec_der ( eckey, key, keylen ) ) != 0 ||
2947 ( ret = ecp_check_pubkey( &eckey->grp, &eckey->Q ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002948 {
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002949 ecp_keypair_free( eckey );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002950 }
2951
2952#if defined(POLARSSL_PEM_C)
2953 pem_free( &pem );
2954#endif
2955
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002956 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002957}
2958#endif /* defined(POLARSSL_ECP_C) */
2959
Paul Bakkereaa89f82011-04-04 21:36:15 +00002960#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002961/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002962 * Parse DHM parameters
2963 */
Paul Bakker23986e52011-04-24 08:57:21 +00002964int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002965{
Paul Bakker23986e52011-04-24 08:57:21 +00002966 int ret;
2967 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002968 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002969#if defined(POLARSSL_PEM_C)
2970 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002971
Paul Bakker96743fc2011-02-12 14:30:57 +00002972 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002973
Paul Bakker96743fc2011-02-12 14:30:57 +00002974 ret = pem_read_buffer( &pem,
2975 "-----BEGIN DH PARAMETERS-----",
2976 "-----END DH PARAMETERS-----",
2977 dhmin, NULL, 0, &dhminlen );
2978
2979 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002980 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002981 /*
2982 * Was PEM encoded
2983 */
2984 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002985 }
Paul Bakker00b28602013-06-24 13:02:41 +02002986 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002987 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002988 pem_free( &pem );
2989 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002990 }
2991
Paul Bakker96743fc2011-02-12 14:30:57 +00002992 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2993#else
2994 p = (unsigned char *) dhmin;
2995#endif
2996 end = p + dhminlen;
2997
Paul Bakker1b57b062011-01-06 15:48:19 +00002998 memset( dhm, 0, sizeof( dhm_context ) );
2999
Paul Bakker1b57b062011-01-06 15:48:19 +00003000 /*
3001 * DHParams ::= SEQUENCE {
3002 * prime INTEGER, -- P
3003 * generator INTEGER, -- g
3004 * }
3005 */
3006 if( ( ret = asn1_get_tag( &p, end, &len,
3007 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3008 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003009#if defined(POLARSSL_PEM_C)
3010 pem_free( &pem );
3011#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003012 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003013 }
3014
3015 end = p + len;
3016
3017 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3018 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3019 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003020#if defined(POLARSSL_PEM_C)
3021 pem_free( &pem );
3022#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003023 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003024 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003025 }
3026
3027 if( p != end )
3028 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003029#if defined(POLARSSL_PEM_C)
3030 pem_free( &pem );
3031#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003032 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003033 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003034 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3035 }
3036
Paul Bakker96743fc2011-02-12 14:30:57 +00003037#if defined(POLARSSL_PEM_C)
3038 pem_free( &pem );
3039#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003040
3041 return( 0 );
3042}
3043
Paul Bakker335db3f2011-04-25 15:28:35 +00003044#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003045/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003046 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003047 */
3048int x509parse_dhmfile( dhm_context *dhm, const char *path )
3049{
3050 int ret;
3051 size_t n;
3052 unsigned char *buf;
3053
Paul Bakker69e095c2011-12-10 21:55:01 +00003054 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3055 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003056
Paul Bakker27fdf462011-06-09 13:55:13 +00003057 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003058
3059 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003060 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003061
3062 return( ret );
3063}
Paul Bakker335db3f2011-04-25 15:28:35 +00003064#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003065#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003066
Paul Bakker5121ce52009-01-03 21:22:43 +00003067#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003068#include <stdarg.h>
3069
3070#if !defined vsnprintf
3071#define vsnprintf _vsnprintf
3072#endif // vsnprintf
3073
3074/*
3075 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3076 * Result value is not size of buffer needed, but -1 if no fit is possible.
3077 *
3078 * This fuction tries to 'fix' this by at least suggesting enlarging the
3079 * size by 20.
3080 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003081static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003082{
3083 va_list ap;
3084 int res = -1;
3085
3086 va_start( ap, format );
3087
3088 res = vsnprintf( str, size, format, ap );
3089
3090 va_end( ap );
3091
3092 // No quick fix possible
3093 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003094 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003095
3096 return res;
3097}
3098
3099#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003100#endif
3101
Paul Bakkerd98030e2009-05-02 15:13:40 +00003102#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3103
3104#define SAFE_SNPRINTF() \
3105{ \
3106 if( ret == -1 ) \
3107 return( -1 ); \
3108 \
Paul Bakker23986e52011-04-24 08:57:21 +00003109 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003110 p[n - 1] = '\0'; \
3111 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3112 } \
3113 \
Paul Bakker23986e52011-04-24 08:57:21 +00003114 n -= (unsigned int) ret; \
3115 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003116}
3117
Paul Bakker5121ce52009-01-03 21:22:43 +00003118/*
3119 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003120 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003121 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003122int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003123{
Paul Bakker23986e52011-04-24 08:57:21 +00003124 int ret;
3125 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003126 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003127 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003128 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 char s[128], *p;
3130
3131 memset( s, 0, sizeof( s ) );
3132
3133 name = dn;
3134 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003135 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
3137 while( name != NULL )
3138 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003139 if( !name->oid.p )
3140 {
3141 name = name->next;
3142 continue;
3143 }
3144
Paul Bakker74111d32011-01-15 16:57:55 +00003145 if( name != dn )
3146 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003147 ret = snprintf( p, n, ", " );
3148 SAFE_SNPRINTF();
3149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Paul Bakkerc70b9822013-04-07 22:00:46 +02003151 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003152
Paul Bakkerc70b9822013-04-07 22:00:46 +02003153 if( ret == 0 )
3154 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003155 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003156 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003157 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
3159 for( i = 0; i < name->val.len; i++ )
3160 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003161 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 break;
3163
3164 c = name->val.p[i];
3165 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3166 s[i] = '?';
3167 else s[i] = c;
3168 }
3169 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003170 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003171 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003172 name = name->next;
3173 }
3174
Paul Bakker23986e52011-04-24 08:57:21 +00003175 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003176}
3177
3178/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003179 * Store the serial in printable form into buf; no more
3180 * than size characters will be written
3181 */
3182int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3183{
Paul Bakker23986e52011-04-24 08:57:21 +00003184 int ret;
3185 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003186 char *p;
3187
3188 p = buf;
3189 n = size;
3190
3191 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003192 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003193
3194 for( i = 0; i < nr; i++ )
3195 {
Paul Bakker93048802011-12-05 14:38:06 +00003196 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003197 continue;
3198
Paul Bakkerdd476992011-01-16 21:34:59 +00003199 ret = snprintf( p, n, "%02X%s",
3200 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3201 SAFE_SNPRINTF();
3202 }
3203
Paul Bakker03c7c252011-11-25 12:37:37 +00003204 if( nr != serial->len )
3205 {
3206 ret = snprintf( p, n, "...." );
3207 SAFE_SNPRINTF();
3208 }
3209
Paul Bakker23986e52011-04-24 08:57:21 +00003210 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003211}
3212
3213/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003214 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003216int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3217 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003218{
Paul Bakker23986e52011-04-24 08:57:21 +00003219 int ret;
3220 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003221 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003222 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
3224 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003225 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003226
Paul Bakkerd98030e2009-05-02 15:13:40 +00003227 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003229 SAFE_SNPRINTF();
3230 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003232 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Paul Bakkerdd476992011-01-16 21:34:59 +00003234 ret = x509parse_serial_gets( p, n, &crt->serial);
3235 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
Paul Bakkerd98030e2009-05-02 15:13:40 +00003237 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3238 SAFE_SNPRINTF();
3239 ret = x509parse_dn_gets( p, n, &crt->issuer );
3240 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003241
Paul Bakkerd98030e2009-05-02 15:13:40 +00003242 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3243 SAFE_SNPRINTF();
3244 ret = x509parse_dn_gets( p, n, &crt->subject );
3245 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
Paul Bakkerd98030e2009-05-02 15:13:40 +00003247 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3249 crt->valid_from.year, crt->valid_from.mon,
3250 crt->valid_from.day, crt->valid_from.hour,
3251 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003252 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Paul Bakkerd98030e2009-05-02 15:13:40 +00003254 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003255 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3256 crt->valid_to.year, crt->valid_to.mon,
3257 crt->valid_to.day, crt->valid_to.hour,
3258 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003259 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003260
Paul Bakkerc70b9822013-04-07 22:00:46 +02003261 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003262 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003263
Paul Bakkerc70b9822013-04-07 22:00:46 +02003264 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3265 if( ret != 0 )
3266 ret = snprintf( p, n, "???" );
3267 else
3268 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003269 SAFE_SNPRINTF();
3270
3271 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003272 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003273 SAFE_SNPRINTF();
3274
Paul Bakker23986e52011-04-24 08:57:21 +00003275 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003276}
3277
Paul Bakker74111d32011-01-15 16:57:55 +00003278/*
3279 * Return an informational string describing the given OID
3280 */
3281const char *x509_oid_get_description( x509_buf *oid )
3282{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003283 const char *desc = NULL;
3284 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003285
Paul Bakkerc70b9822013-04-07 22:00:46 +02003286 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003287
Paul Bakkerc70b9822013-04-07 22:00:46 +02003288 if( ret != 0 )
3289 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003290
Paul Bakkerc70b9822013-04-07 22:00:46 +02003291 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003292}
3293
3294/* Return the x.y.z.... style numeric string for the given OID */
3295int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3296{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003297 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003298}
3299
Paul Bakkerd98030e2009-05-02 15:13:40 +00003300/*
3301 * Return an informational string about the CRL.
3302 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003303int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3304 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003305{
Paul Bakker23986e52011-04-24 08:57:21 +00003306 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003307 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003308 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003309 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003310 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003311
3312 p = buf;
3313 n = size;
3314
3315 ret = snprintf( p, n, "%sCRL version : %d",
3316 prefix, crl->version );
3317 SAFE_SNPRINTF();
3318
3319 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3320 SAFE_SNPRINTF();
3321 ret = x509parse_dn_gets( p, n, &crl->issuer );
3322 SAFE_SNPRINTF();
3323
3324 ret = snprintf( p, n, "\n%sthis update : " \
3325 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3326 crl->this_update.year, crl->this_update.mon,
3327 crl->this_update.day, crl->this_update.hour,
3328 crl->this_update.min, crl->this_update.sec );
3329 SAFE_SNPRINTF();
3330
3331 ret = snprintf( p, n, "\n%snext update : " \
3332 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3333 crl->next_update.year, crl->next_update.mon,
3334 crl->next_update.day, crl->next_update.hour,
3335 crl->next_update.min, crl->next_update.sec );
3336 SAFE_SNPRINTF();
3337
3338 entry = &crl->entry;
3339
3340 ret = snprintf( p, n, "\n%sRevoked certificates:",
3341 prefix );
3342 SAFE_SNPRINTF();
3343
Paul Bakker9be19372009-07-27 20:21:53 +00003344 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003345 {
3346 ret = snprintf( p, n, "\n%sserial number: ",
3347 prefix );
3348 SAFE_SNPRINTF();
3349
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003350 ret = x509parse_serial_gets( p, n, &entry->serial);
3351 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003352
Paul Bakkerd98030e2009-05-02 15:13:40 +00003353 ret = snprintf( p, n, " revocation date: " \
3354 "%04d-%02d-%02d %02d:%02d:%02d",
3355 entry->revocation_date.year, entry->revocation_date.mon,
3356 entry->revocation_date.day, entry->revocation_date.hour,
3357 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003358 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003359
3360 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 }
3362
Paul Bakkerc70b9822013-04-07 22:00:46 +02003363 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003364 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003365
Paul Bakkerc70b9822013-04-07 22:00:46 +02003366 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3367 if( ret != 0 )
3368 ret = snprintf( p, n, "???" );
3369 else
3370 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003371 SAFE_SNPRINTF();
3372
Paul Bakker1e27bb22009-07-19 20:25:25 +00003373 ret = snprintf( p, n, "\n" );
3374 SAFE_SNPRINTF();
3375
Paul Bakker23986e52011-04-24 08:57:21 +00003376 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003377}
3378
3379/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003380 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003382#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003383int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003384{
Paul Bakkercce9d772011-11-18 14:26:47 +00003385 int year, mon, day;
3386 int hour, min, sec;
3387
3388#if defined(_WIN32)
3389 SYSTEMTIME st;
3390
3391 GetLocalTime(&st);
3392
3393 year = st.wYear;
3394 mon = st.wMonth;
3395 day = st.wDay;
3396 hour = st.wHour;
3397 min = st.wMinute;
3398 sec = st.wSecond;
3399#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 struct tm *lt;
3401 time_t tt;
3402
3403 tt = time( NULL );
3404 lt = localtime( &tt );
3405
Paul Bakkercce9d772011-11-18 14:26:47 +00003406 year = lt->tm_year + 1900;
3407 mon = lt->tm_mon + 1;
3408 day = lt->tm_mday;
3409 hour = lt->tm_hour;
3410 min = lt->tm_min;
3411 sec = lt->tm_sec;
3412#endif
3413
3414 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003415 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003416
Paul Bakkercce9d772011-11-18 14:26:47 +00003417 if( year == to->year &&
3418 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003419 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003420
Paul Bakkercce9d772011-11-18 14:26:47 +00003421 if( year == to->year &&
3422 mon == to->mon &&
3423 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003424 return( 1 );
3425
Paul Bakkercce9d772011-11-18 14:26:47 +00003426 if( year == to->year &&
3427 mon == to->mon &&
3428 day == to->day &&
3429 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003430 return( 1 );
3431
Paul Bakkercce9d772011-11-18 14:26:47 +00003432 if( year == to->year &&
3433 mon == to->mon &&
3434 day == to->day &&
3435 hour == to->hour &&
3436 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003437 return( 1 );
3438
Paul Bakkercce9d772011-11-18 14:26:47 +00003439 if( year == to->year &&
3440 mon == to->mon &&
3441 day == to->day &&
3442 hour == to->hour &&
3443 min == to->min &&
3444 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003445 return( 1 );
3446
Paul Bakker40ea7de2009-05-03 10:18:48 +00003447 return( 0 );
3448}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003449#else /* POLARSSL_HAVE_TIME */
3450int x509parse_time_expired( const x509_time *to )
3451{
3452 ((void) to);
3453 return( 0 );
3454}
3455#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003456
3457/*
3458 * Return 1 if the certificate is revoked, or 0 otherwise.
3459 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003460int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003461{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003462 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003463
3464 while( cur != NULL && cur->serial.len != 0 )
3465 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003466 if( crt->serial.len == cur->serial.len &&
3467 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003468 {
3469 if( x509parse_time_expired( &cur->revocation_date ) )
3470 return( 1 );
3471 }
3472
3473 cur = cur->next;
3474 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003475
3476 return( 0 );
3477}
3478
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003479/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003480 * Check that the given certificate is valid accoring to the CRL.
3481 */
3482static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3483 x509_crl *crl_list)
3484{
3485 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003486 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3487 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003488
Paul Bakker915275b2012-09-28 07:10:55 +00003489 if( ca == NULL )
3490 return( flags );
3491
Paul Bakker76fd75a2011-01-16 21:12:10 +00003492 /*
3493 * TODO: What happens if no CRL is present?
3494 * Suggestion: Revocation state should be unknown if no CRL is present.
3495 * For backwards compatibility this is not yet implemented.
3496 */
3497
Paul Bakker915275b2012-09-28 07:10:55 +00003498 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003499 {
Paul Bakker915275b2012-09-28 07:10:55 +00003500 if( crl_list->version == 0 ||
3501 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003502 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3503 crl_list->issuer_raw.len ) != 0 )
3504 {
3505 crl_list = crl_list->next;
3506 continue;
3507 }
3508
3509 /*
3510 * Check if CRL is correctly signed by the trusted CA
3511 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003512 md_info = md_info_from_type( crl_list->sig_md );
3513 if( md_info == NULL )
3514 {
3515 /*
3516 * Cannot check 'unknown' hash
3517 */
3518 flags |= BADCRL_NOT_TRUSTED;
3519 break;
3520 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003521
Paul Bakkerc70b9822013-04-07 22:00:46 +02003522 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003523
Paul Bakkerc70b9822013-04-07 22:00:46 +02003524 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003525 0, hash, crl_list->sig.p ) == 0 )
3526 {
3527 /*
3528 * CRL is not trusted
3529 */
3530 flags |= BADCRL_NOT_TRUSTED;
3531 break;
3532 }
3533
3534 /*
3535 * Check for validity of CRL (Do not drop out)
3536 */
3537 if( x509parse_time_expired( &crl_list->next_update ) )
3538 flags |= BADCRL_EXPIRED;
3539
3540 /*
3541 * Check if certificate is revoked
3542 */
3543 if( x509parse_revoked(crt, crl_list) )
3544 {
3545 flags |= BADCERT_REVOKED;
3546 break;
3547 }
3548
3549 crl_list = crl_list->next;
3550 }
3551 return flags;
3552}
3553
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003554static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003555{
3556 size_t i;
3557 size_t cn_idx = 0;
3558
Paul Bakker57b12982012-02-11 17:38:38 +00003559 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003560 return( 0 );
3561
3562 for( i = 0; i < strlen( cn ); ++i )
3563 {
3564 if( cn[i] == '.' )
3565 {
3566 cn_idx = i;
3567 break;
3568 }
3569 }
3570
3571 if( cn_idx == 0 )
3572 return( 0 );
3573
Paul Bakker535e97d2012-08-23 10:49:55 +00003574 if( strlen( cn ) - cn_idx == name->len - 1 &&
3575 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003576 {
3577 return( 1 );
3578 }
3579
3580 return( 0 );
3581}
3582
Paul Bakker915275b2012-09-28 07:10:55 +00003583static int x509parse_verify_top(
3584 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003585 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003586 int (*f_vrfy)(void *, x509_cert *, int, int *),
3587 void *p_vrfy )
3588{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003589 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003590 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003591 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3592 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003593
3594 if( x509parse_time_expired( &child->valid_to ) )
3595 *flags |= BADCERT_EXPIRED;
3596
3597 /*
3598 * Child is the top of the chain. Check against the trust_ca list.
3599 */
3600 *flags |= BADCERT_NOT_TRUSTED;
3601
3602 while( trust_ca != NULL )
3603 {
3604 if( trust_ca->version == 0 ||
3605 child->issuer_raw.len != trust_ca->subject_raw.len ||
3606 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3607 child->issuer_raw.len ) != 0 )
3608 {
3609 trust_ca = trust_ca->next;
3610 continue;
3611 }
3612
Paul Bakker9a736322012-11-14 12:39:52 +00003613 /*
3614 * Reduce path_len to check against if top of the chain is
3615 * the same as the trusted CA
3616 */
3617 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3618 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3619 child->issuer_raw.len ) == 0 )
3620 {
3621 check_path_cnt--;
3622 }
3623
Paul Bakker915275b2012-09-28 07:10:55 +00003624 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003625 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003626 {
3627 trust_ca = trust_ca->next;
3628 continue;
3629 }
3630
Paul Bakkerc70b9822013-04-07 22:00:46 +02003631 md_info = md_info_from_type( child->sig_md );
3632 if( md_info == NULL )
3633 {
3634 /*
3635 * Cannot check 'unknown' hash
3636 */
3637 continue;
3638 }
Paul Bakker915275b2012-09-28 07:10:55 +00003639
Paul Bakkerc70b9822013-04-07 22:00:46 +02003640 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003641
Paul Bakkerc70b9822013-04-07 22:00:46 +02003642 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003643 0, hash, child->sig.p ) != 0 )
3644 {
3645 trust_ca = trust_ca->next;
3646 continue;
3647 }
3648
3649 /*
3650 * Top of chain is signed by a trusted CA
3651 */
3652 *flags &= ~BADCERT_NOT_TRUSTED;
3653 break;
3654 }
3655
Paul Bakker9a736322012-11-14 12:39:52 +00003656 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003657 * If top of chain is not the same as the trusted CA send a verify request
3658 * to the callback for any issues with validity and CRL presence for the
3659 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003660 */
3661 if( trust_ca != NULL &&
3662 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3663 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3664 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003665 {
3666 /* Check trusted CA's CRL for then chain's top crt */
3667 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3668
3669 if( x509parse_time_expired( &trust_ca->valid_to ) )
3670 ca_flags |= BADCERT_EXPIRED;
3671
Paul Bakker915275b2012-09-28 07:10:55 +00003672 if( NULL != f_vrfy )
3673 {
Paul Bakker9a736322012-11-14 12:39:52 +00003674 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003675 return( ret );
3676 }
3677 }
3678
3679 /* Call callback on top cert */
3680 if( NULL != f_vrfy )
3681 {
Paul Bakker9a736322012-11-14 12:39:52 +00003682 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003683 return( ret );
3684 }
3685
Paul Bakker915275b2012-09-28 07:10:55 +00003686 *flags |= ca_flags;
3687
3688 return( 0 );
3689}
3690
3691static int x509parse_verify_child(
3692 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003693 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003694 int (*f_vrfy)(void *, x509_cert *, int, int *),
3695 void *p_vrfy )
3696{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003697 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003698 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003699 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003700 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003701 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003702
3703 if( x509parse_time_expired( &child->valid_to ) )
3704 *flags |= BADCERT_EXPIRED;
3705
Paul Bakkerc70b9822013-04-07 22:00:46 +02003706 md_info = md_info_from_type( child->sig_md );
3707 if( md_info == NULL )
3708 {
3709 /*
3710 * Cannot check 'unknown' hash
3711 */
Paul Bakker915275b2012-09-28 07:10:55 +00003712 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003713 }
3714 else
3715 {
3716 md( md_info, child->tbs.p, child->tbs.len, hash );
3717
3718 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3719 child->sig.p ) != 0 )
3720 *flags |= BADCERT_NOT_TRUSTED;
3721 }
3722
Paul Bakker915275b2012-09-28 07:10:55 +00003723 /* Check trusted CA's CRL for the given crt */
3724 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3725
3726 grandparent = parent->next;
3727
3728 while( grandparent != NULL )
3729 {
3730 if( grandparent->version == 0 ||
3731 grandparent->ca_istrue == 0 ||
3732 parent->issuer_raw.len != grandparent->subject_raw.len ||
3733 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3734 parent->issuer_raw.len ) != 0 )
3735 {
3736 grandparent = grandparent->next;
3737 continue;
3738 }
3739 break;
3740 }
3741
Paul Bakker915275b2012-09-28 07:10:55 +00003742 if( grandparent != NULL )
3743 {
3744 /*
3745 * Part of the chain
3746 */
Paul Bakker9a736322012-11-14 12:39:52 +00003747 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 +00003748 if( ret != 0 )
3749 return( ret );
3750 }
3751 else
3752 {
Paul Bakker9a736322012-11-14 12:39:52 +00003753 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 +00003754 if( ret != 0 )
3755 return( ret );
3756 }
3757
3758 /* child is verified to be a child of the parent, call verify callback */
3759 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003760 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003761 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003762
3763 *flags |= parent_flags;
3764
3765 return( 0 );
3766}
3767
Paul Bakker76fd75a2011-01-16 21:12:10 +00003768/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 * Verify the certificate validity
3770 */
3771int x509parse_verify( x509_cert *crt,
3772 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003773 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003774 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003775 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003776 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003777{
Paul Bakker23986e52011-04-24 08:57:21 +00003778 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003779 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003780 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003781 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003782 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003783 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003784
Paul Bakker40ea7de2009-05-03 10:18:48 +00003785 *flags = 0;
3786
Paul Bakker5121ce52009-01-03 21:22:43 +00003787 if( cn != NULL )
3788 {
3789 name = &crt->subject;
3790 cn_len = strlen( cn );
3791
Paul Bakker4d2c1242012-05-10 14:12:46 +00003792 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003793 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003794 cur = &crt->subject_alt_names;
3795
3796 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003797 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003798 if( cur->buf.len == cn_len &&
3799 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003800 break;
3801
Paul Bakker535e97d2012-08-23 10:49:55 +00003802 if( cur->buf.len > 2 &&
3803 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003804 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003805 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003806
Paul Bakker4d2c1242012-05-10 14:12:46 +00003807 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003808 }
3809
3810 if( cur == NULL )
3811 *flags |= BADCERT_CN_MISMATCH;
3812 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003813 else
3814 {
3815 while( name != NULL )
3816 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003817 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003818 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003819 if( name->val.len == cn_len &&
3820 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003821 break;
3822
Paul Bakker535e97d2012-08-23 10:49:55 +00003823 if( name->val.len > 2 &&
3824 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003825 x509_wildcard_verify( cn, &name->val ) )
3826 break;
3827 }
3828
3829 name = name->next;
3830 }
3831
3832 if( name == NULL )
3833 *flags |= BADCERT_CN_MISMATCH;
3834 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 }
3836
Paul Bakker5121ce52009-01-03 21:22:43 +00003837 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003838 * Iterate upwards in the given cert chain, to find our crt parent.
3839 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003841 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
Paul Bakker76fd75a2011-01-16 21:12:10 +00003843 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003845 if( parent->ca_istrue == 0 ||
3846 crt->issuer_raw.len != parent->subject_raw.len ||
3847 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 crt->issuer_raw.len ) != 0 )
3849 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003850 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003851 continue;
3852 }
Paul Bakker915275b2012-09-28 07:10:55 +00003853 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 }
3855
Paul Bakker915275b2012-09-28 07:10:55 +00003856 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 {
Paul Bakker915275b2012-09-28 07:10:55 +00003858 /*
3859 * Part of the chain
3860 */
Paul Bakker9a736322012-11-14 12:39:52 +00003861 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003862 if( ret != 0 )
3863 return( ret );
3864 }
3865 else
Paul Bakker74111d32011-01-15 16:57:55 +00003866 {
Paul Bakker9a736322012-11-14 12:39:52 +00003867 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003868 if( ret != 0 )
3869 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003870 }
Paul Bakker915275b2012-09-28 07:10:55 +00003871
3872 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003873 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003874
Paul Bakker5121ce52009-01-03 21:22:43 +00003875 return( 0 );
3876}
3877
3878/*
3879 * Unallocate all certificate data
3880 */
3881void x509_free( x509_cert *crt )
3882{
3883 x509_cert *cert_cur = crt;
3884 x509_cert *cert_prv;
3885 x509_name *name_cur;
3886 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003887 x509_sequence *seq_cur;
3888 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003889
3890 if( crt == NULL )
3891 return;
3892
3893 do
3894 {
3895 rsa_free( &cert_cur->rsa );
3896
3897 name_cur = cert_cur->issuer.next;
3898 while( name_cur != NULL )
3899 {
3900 name_prv = name_cur;
3901 name_cur = name_cur->next;
3902 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003903 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003904 }
3905
3906 name_cur = cert_cur->subject.next;
3907 while( name_cur != NULL )
3908 {
3909 name_prv = name_cur;
3910 name_cur = name_cur->next;
3911 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003912 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003913 }
3914
Paul Bakker74111d32011-01-15 16:57:55 +00003915 seq_cur = cert_cur->ext_key_usage.next;
3916 while( seq_cur != NULL )
3917 {
3918 seq_prv = seq_cur;
3919 seq_cur = seq_cur->next;
3920 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003921 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003922 }
3923
Paul Bakker8afa70d2012-02-11 18:42:45 +00003924 seq_cur = cert_cur->subject_alt_names.next;
3925 while( seq_cur != NULL )
3926 {
3927 seq_prv = seq_cur;
3928 seq_cur = seq_cur->next;
3929 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003930 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003931 }
3932
Paul Bakker5121ce52009-01-03 21:22:43 +00003933 if( cert_cur->raw.p != NULL )
3934 {
3935 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003936 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003937 }
3938
3939 cert_cur = cert_cur->next;
3940 }
3941 while( cert_cur != NULL );
3942
3943 cert_cur = crt;
3944 do
3945 {
3946 cert_prv = cert_cur;
3947 cert_cur = cert_cur->next;
3948
3949 memset( cert_prv, 0, sizeof( x509_cert ) );
3950 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003951 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003952 }
3953 while( cert_cur != NULL );
3954}
3955
Paul Bakkerd98030e2009-05-02 15:13:40 +00003956/*
3957 * Unallocate all CRL data
3958 */
3959void x509_crl_free( x509_crl *crl )
3960{
3961 x509_crl *crl_cur = crl;
3962 x509_crl *crl_prv;
3963 x509_name *name_cur;
3964 x509_name *name_prv;
3965 x509_crl_entry *entry_cur;
3966 x509_crl_entry *entry_prv;
3967
3968 if( crl == NULL )
3969 return;
3970
3971 do
3972 {
3973 name_cur = crl_cur->issuer.next;
3974 while( name_cur != NULL )
3975 {
3976 name_prv = name_cur;
3977 name_cur = name_cur->next;
3978 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003979 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003980 }
3981
3982 entry_cur = crl_cur->entry.next;
3983 while( entry_cur != NULL )
3984 {
3985 entry_prv = entry_cur;
3986 entry_cur = entry_cur->next;
3987 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003988 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003989 }
3990
3991 if( crl_cur->raw.p != NULL )
3992 {
3993 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003994 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003995 }
3996
3997 crl_cur = crl_cur->next;
3998 }
3999 while( crl_cur != NULL );
4000
4001 crl_cur = crl;
4002 do
4003 {
4004 crl_prv = crl_cur;
4005 crl_cur = crl_cur->next;
4006
4007 memset( crl_prv, 0, sizeof( x509_crl ) );
4008 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004009 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004010 }
4011 while( crl_cur != NULL );
4012}
4013
Paul Bakker40e46942009-01-03 21:51:57 +00004014#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Paul Bakker40e46942009-01-03 21:51:57 +00004016#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004017
4018/*
4019 * Checkup routine
4020 */
4021int x509_self_test( int verbose )
4022{
Paul Bakker5690efc2011-05-26 13:16:06 +00004023#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004024 int ret;
4025 int flags;
4026 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004027 x509_cert cacert;
4028 x509_cert clicert;
4029 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004030#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004031 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004033
4034 if( verbose != 0 )
4035 printf( " X.509 certificate load: " );
4036
4037 memset( &clicert, 0, sizeof( x509_cert ) );
4038
Paul Bakker3c2122f2013-06-24 19:03:14 +02004039 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004040 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004041 if( ret != 0 )
4042 {
4043 if( verbose != 0 )
4044 printf( "failed\n" );
4045
4046 return( ret );
4047 }
4048
4049 memset( &cacert, 0, sizeof( x509_cert ) );
4050
Paul Bakker3c2122f2013-06-24 19:03:14 +02004051 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004052 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 if( ret != 0 )
4054 {
4055 if( verbose != 0 )
4056 printf( "failed\n" );
4057
4058 return( ret );
4059 }
4060
4061 if( verbose != 0 )
4062 printf( "passed\n X.509 private key load: " );
4063
4064 i = strlen( test_ca_key );
4065 j = strlen( test_ca_pwd );
4066
Paul Bakker66b78b22011-03-25 14:22:50 +00004067 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4068
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004069 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004070 (const unsigned char *) test_ca_key, i,
4071 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004072 {
4073 if( verbose != 0 )
4074 printf( "failed\n" );
4075
4076 return( ret );
4077 }
4078
4079 if( verbose != 0 )
4080 printf( "passed\n X.509 signature verify: ");
4081
Paul Bakker23986e52011-04-24 08:57:21 +00004082 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004083 if( ret != 0 )
4084 {
Paul Bakker23986e52011-04-24 08:57:21 +00004085 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004086 if( verbose != 0 )
4087 printf( "failed\n" );
4088
4089 return( ret );
4090 }
4091
Paul Bakker5690efc2011-05-26 13:16:06 +00004092#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004093 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004094 printf( "passed\n X.509 DHM parameter load: " );
4095
4096 i = strlen( test_dhm_params );
4097 j = strlen( test_ca_pwd );
4098
Paul Bakker3c2122f2013-06-24 19:03:14 +02004099 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004100 {
4101 if( verbose != 0 )
4102 printf( "failed\n" );
4103
4104 return( ret );
4105 }
4106
4107 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004108 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004110
4111 x509_free( &cacert );
4112 x509_free( &clicert );
4113 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004114#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004115 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004117
4118 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004119#else
4120 ((void) verbose);
4121 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4122#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004123}
4124
4125#endif
4126
4127#endif