blob: decac2697ced551a8d50ec717f235c5f4c54909d [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 {
Paul Bakker9d781402011-05-09 16:17:09 +00002172 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
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/*
2646 * Parse a PKCS#1 encoded private EC key
2647 */
2648static int x509parse_key_sec1_der( ecp_keypair *eck,
2649 const unsigned char *key,
2650 size_t keylen )
2651{
2652 int ret;
2653
2654 (void) key;
2655 (void) keylen;
2656
2657 if( ( ret = ecp_check_prvkey( &eck->grp, &eck->d ) ) == 0 )
2658 return 0;
2659
2660cleanup:
2661 ecp_keypair_free( eck );
2662
2663 return( ret );
2664}
2665
2666/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002667 * Parse a private EC key
2668 */
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002669int x509parse_key_ec( ecp_keypair *eck,
2670 const unsigned char *key, size_t keylen,
2671 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002672{
2673 int ret;
2674
2675#if defined(POLARSSL_PEM_C)
2676 size_t len;
2677 pem_context pem;
2678
2679 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002680 ret = pem_read_buffer( &pem,
2681 "-----BEGIN EC PRIVATE KEY-----",
2682 "-----END EC PRIVATE KEY-----",
2683 key, pwd, pwdlen, &len );
2684 if( ret == 0 )
2685 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002686 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002687 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002688 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002689 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002690
2691 pem_free( &pem );
2692 return( ret );
2693 }
2694 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2695 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2696 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2697 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2698 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2699 return( ret );
2700
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002701 ret = pem_read_buffer( &pem,
2702 "-----BEGIN PRIVATE KEY-----",
2703 "-----END PRIVATE KEY-----",
2704 key, NULL, 0, &len );
2705 if( ret == 0 )
2706 {
2707 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2708 pem.buf, pem.buflen ) ) != 0 )
2709 {
2710 ecp_keypair_free( eck );
2711 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002712
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002713 pem_free( &pem );
2714 return( ret );
2715 }
2716 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2717 return( ret );
2718
2719 ret = pem_read_buffer( &pem,
2720 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2721 "-----END ENCRYPTED PRIVATE KEY-----",
2722 key, NULL, 0, &len );
2723 if( ret == 0 )
2724 {
2725 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2726 pem.buf, pem.buflen,
2727 pwd, pwdlen ) ) != 0 )
2728 {
2729 ecp_keypair_free( eck );
2730 }
2731
2732 pem_free( &pem );
2733 return( ret );
2734 }
2735 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2736 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002737#else
2738 ((void) pwd);
2739 ((void) pwdlen);
2740#endif /* POLARSSL_PEM_C */
2741
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002742 /*
2743 * At this point we only know it's not a PEM formatted key. Could be any
2744 * of the known DER encoded private key formats
2745 *
2746 * We try the different DER format parsers to see if one passes without
2747 * error
2748 */
2749 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2750 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002751 {
2752 return( 0 );
2753 }
2754
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002755 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002756
2757 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2758 {
2759 return( ret );
2760 }
2761
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002762 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2763 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002764 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002765
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002766 ecp_keypair_free( eck );
2767
2768 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2769 return( 0 );
2770
2771 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002772
2773 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2774}
2775
2776/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002777 * Parse a public EC key in RFC 5480 format, der-encoded
2778 */
2779static int x509parse_public_key_ec_der( ecp_keypair *key,
2780 const unsigned char *buf, size_t len )
2781{
2782 int ret;
2783 ecp_group_id grp_id;
2784 x509_buf alg_oid;
2785 pk_type_t alg = POLARSSL_PK_NONE;
2786 unsigned char *p = (unsigned char *) buf;
2787 unsigned char *end = p + len;
2788 const unsigned char *params_end;
2789 /*
2790 * SubjectPublicKeyInfo ::= SEQUENCE {
2791 * algorithm AlgorithmIdentifier,
2792 * subjectPublicKey BIT STRING
2793 * }
2794 * -- algorithm parameters are ECParameters
2795 * -- subjectPublicKey is an ECPoint
2796 */
2797 if( ( ret = asn1_get_tag( &p, end, &len,
2798 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2799 {
2800 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
2801 }
2802
2803 if( ( ret = x509_get_alg( &p, end, &alg_oid, &params_end ) ) != 0 )
2804 return( ret );
2805
2806 if( oid_get_pk_alg( &alg_oid, &alg ) != 0 )
2807 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2808
2809 if( alg != POLARSSL_PK_ECKEY && alg != POLARSSL_PK_ECKEY_DH )
2810 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2811
2812 if ( alg == POLARSSL_PK_ECKEY_DH )
2813 key->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2814
2815 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2816 return( ret );
2817
2818 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
2819 return( ret );
2820
2821 if( ( ret = x509_get_subpubkey_ec( &p, end, &key->grp, &key->Q ) ) != 0 )
2822 {
2823 return( ret );
2824 }
2825
2826 return( 0 );
2827}
2828
2829/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002830 * Parse a public EC key
2831 */
2832int x509parse_public_key_ec( ecp_keypair *eckey,
2833 const unsigned char *key, size_t keylen )
2834{
2835 int ret;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002836#if defined(POLARSSL_PEM_C)
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002837 size_t len;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002838 pem_context pem;
2839
2840 pem_init( &pem );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002841 ret = pem_read_buffer( &pem,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002842 "-----BEGIN PUBLIC KEY-----",
2843 "-----END PUBLIC KEY-----",
2844 key, NULL, 0, &len );
2845
2846 if( ret == 0 )
2847 {
2848 /*
2849 * Was PEM encoded
2850 */
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002851 key = pem.buf;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002852 keylen = pem.buflen;
2853 }
2854 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2855 {
2856 pem_free( &pem );
2857 return( ret );
2858 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002859#endif
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002860
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002861 if( ( ret = x509parse_public_key_ec_der ( eckey, key, keylen ) ) != 0 ||
2862 ( ret = ecp_check_pubkey( &eckey->grp, &eckey->Q ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002863 {
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002864 ecp_keypair_free( eckey );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002865 }
2866
2867#if defined(POLARSSL_PEM_C)
2868 pem_free( &pem );
2869#endif
2870
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002871 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002872}
2873#endif /* defined(POLARSSL_ECP_C) */
2874
Paul Bakkereaa89f82011-04-04 21:36:15 +00002875#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002876/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002877 * Parse DHM parameters
2878 */
Paul Bakker23986e52011-04-24 08:57:21 +00002879int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002880{
Paul Bakker23986e52011-04-24 08:57:21 +00002881 int ret;
2882 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002883 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002884#if defined(POLARSSL_PEM_C)
2885 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002886
Paul Bakker96743fc2011-02-12 14:30:57 +00002887 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002888
Paul Bakker96743fc2011-02-12 14:30:57 +00002889 ret = pem_read_buffer( &pem,
2890 "-----BEGIN DH PARAMETERS-----",
2891 "-----END DH PARAMETERS-----",
2892 dhmin, NULL, 0, &dhminlen );
2893
2894 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002895 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002896 /*
2897 * Was PEM encoded
2898 */
2899 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002900 }
Paul Bakker00b28602013-06-24 13:02:41 +02002901 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002902 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002903 pem_free( &pem );
2904 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002905 }
2906
Paul Bakker96743fc2011-02-12 14:30:57 +00002907 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2908#else
2909 p = (unsigned char *) dhmin;
2910#endif
2911 end = p + dhminlen;
2912
Paul Bakker1b57b062011-01-06 15:48:19 +00002913 memset( dhm, 0, sizeof( dhm_context ) );
2914
Paul Bakker1b57b062011-01-06 15:48:19 +00002915 /*
2916 * DHParams ::= SEQUENCE {
2917 * prime INTEGER, -- P
2918 * generator INTEGER, -- g
2919 * }
2920 */
2921 if( ( ret = asn1_get_tag( &p, end, &len,
2922 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2923 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002924#if defined(POLARSSL_PEM_C)
2925 pem_free( &pem );
2926#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002927 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002928 }
2929
2930 end = p + len;
2931
2932 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2933 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2934 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002935#if defined(POLARSSL_PEM_C)
2936 pem_free( &pem );
2937#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002938 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002939 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002940 }
2941
2942 if( p != end )
2943 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002944#if defined(POLARSSL_PEM_C)
2945 pem_free( &pem );
2946#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002947 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002948 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002949 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2950 }
2951
Paul Bakker96743fc2011-02-12 14:30:57 +00002952#if defined(POLARSSL_PEM_C)
2953 pem_free( &pem );
2954#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002955
2956 return( 0 );
2957}
2958
Paul Bakker335db3f2011-04-25 15:28:35 +00002959#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002960/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002961 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002962 */
2963int x509parse_dhmfile( dhm_context *dhm, const char *path )
2964{
2965 int ret;
2966 size_t n;
2967 unsigned char *buf;
2968
Paul Bakker69e095c2011-12-10 21:55:01 +00002969 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2970 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002971
Paul Bakker27fdf462011-06-09 13:55:13 +00002972 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002973
2974 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002975 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002976
2977 return( ret );
2978}
Paul Bakker335db3f2011-04-25 15:28:35 +00002979#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002980#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002981
Paul Bakker5121ce52009-01-03 21:22:43 +00002982#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002983#include <stdarg.h>
2984
2985#if !defined vsnprintf
2986#define vsnprintf _vsnprintf
2987#endif // vsnprintf
2988
2989/*
2990 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2991 * Result value is not size of buffer needed, but -1 if no fit is possible.
2992 *
2993 * This fuction tries to 'fix' this by at least suggesting enlarging the
2994 * size by 20.
2995 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002996static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002997{
2998 va_list ap;
2999 int res = -1;
3000
3001 va_start( ap, format );
3002
3003 res = vsnprintf( str, size, format, ap );
3004
3005 va_end( ap );
3006
3007 // No quick fix possible
3008 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003009 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003010
3011 return res;
3012}
3013
3014#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003015#endif
3016
Paul Bakkerd98030e2009-05-02 15:13:40 +00003017#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3018
3019#define SAFE_SNPRINTF() \
3020{ \
3021 if( ret == -1 ) \
3022 return( -1 ); \
3023 \
Paul Bakker23986e52011-04-24 08:57:21 +00003024 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003025 p[n - 1] = '\0'; \
3026 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3027 } \
3028 \
Paul Bakker23986e52011-04-24 08:57:21 +00003029 n -= (unsigned int) ret; \
3030 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003031}
3032
Paul Bakker5121ce52009-01-03 21:22:43 +00003033/*
3034 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003035 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003037int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003038{
Paul Bakker23986e52011-04-24 08:57:21 +00003039 int ret;
3040 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003042 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003043 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 char s[128], *p;
3045
3046 memset( s, 0, sizeof( s ) );
3047
3048 name = dn;
3049 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003050 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
3052 while( name != NULL )
3053 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003054 if( !name->oid.p )
3055 {
3056 name = name->next;
3057 continue;
3058 }
3059
Paul Bakker74111d32011-01-15 16:57:55 +00003060 if( name != dn )
3061 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003062 ret = snprintf( p, n, ", " );
3063 SAFE_SNPRINTF();
3064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Paul Bakkerc70b9822013-04-07 22:00:46 +02003066 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
Paul Bakkerc70b9822013-04-07 22:00:46 +02003068 if( ret == 0 )
3069 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003070 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003071 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003072 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
3074 for( i = 0; i < name->val.len; i++ )
3075 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003076 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 break;
3078
3079 c = name->val.p[i];
3080 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3081 s[i] = '?';
3082 else s[i] = c;
3083 }
3084 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003085 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003086 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 name = name->next;
3088 }
3089
Paul Bakker23986e52011-04-24 08:57:21 +00003090 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091}
3092
3093/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003094 * Store the serial in printable form into buf; no more
3095 * than size characters will be written
3096 */
3097int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3098{
Paul Bakker23986e52011-04-24 08:57:21 +00003099 int ret;
3100 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003101 char *p;
3102
3103 p = buf;
3104 n = size;
3105
3106 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003107 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003108
3109 for( i = 0; i < nr; i++ )
3110 {
Paul Bakker93048802011-12-05 14:38:06 +00003111 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003112 continue;
3113
Paul Bakkerdd476992011-01-16 21:34:59 +00003114 ret = snprintf( p, n, "%02X%s",
3115 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3116 SAFE_SNPRINTF();
3117 }
3118
Paul Bakker03c7c252011-11-25 12:37:37 +00003119 if( nr != serial->len )
3120 {
3121 ret = snprintf( p, n, "...." );
3122 SAFE_SNPRINTF();
3123 }
3124
Paul Bakker23986e52011-04-24 08:57:21 +00003125 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003126}
3127
3128/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003129 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003131int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3132 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003133{
Paul Bakker23986e52011-04-24 08:57:21 +00003134 int ret;
3135 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003136 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003137 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
3139 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003140 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003141
Paul Bakkerd98030e2009-05-02 15:13:40 +00003142 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003143 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003144 SAFE_SNPRINTF();
3145 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003147 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003148
Paul Bakkerdd476992011-01-16 21:34:59 +00003149 ret = x509parse_serial_gets( p, n, &crt->serial);
3150 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
Paul Bakkerd98030e2009-05-02 15:13:40 +00003152 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3153 SAFE_SNPRINTF();
3154 ret = x509parse_dn_gets( p, n, &crt->issuer );
3155 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003156
Paul Bakkerd98030e2009-05-02 15:13:40 +00003157 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3158 SAFE_SNPRINTF();
3159 ret = x509parse_dn_gets( p, n, &crt->subject );
3160 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Paul Bakkerd98030e2009-05-02 15:13:40 +00003162 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003163 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3164 crt->valid_from.year, crt->valid_from.mon,
3165 crt->valid_from.day, crt->valid_from.hour,
3166 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003167 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
Paul Bakkerd98030e2009-05-02 15:13:40 +00003169 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003170 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3171 crt->valid_to.year, crt->valid_to.mon,
3172 crt->valid_to.day, crt->valid_to.hour,
3173 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003174 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003175
Paul Bakkerc70b9822013-04-07 22:00:46 +02003176 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003177 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003178
Paul Bakkerc70b9822013-04-07 22:00:46 +02003179 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3180 if( ret != 0 )
3181 ret = snprintf( p, n, "???" );
3182 else
3183 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003184 SAFE_SNPRINTF();
3185
3186 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003187 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003188 SAFE_SNPRINTF();
3189
Paul Bakker23986e52011-04-24 08:57:21 +00003190 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003191}
3192
Paul Bakker74111d32011-01-15 16:57:55 +00003193/*
3194 * Return an informational string describing the given OID
3195 */
3196const char *x509_oid_get_description( x509_buf *oid )
3197{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003198 const char *desc = NULL;
3199 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003200
Paul Bakkerc70b9822013-04-07 22:00:46 +02003201 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003202
Paul Bakkerc70b9822013-04-07 22:00:46 +02003203 if( ret != 0 )
3204 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003205
Paul Bakkerc70b9822013-04-07 22:00:46 +02003206 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003207}
3208
3209/* Return the x.y.z.... style numeric string for the given OID */
3210int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3211{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003212 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003213}
3214
Paul Bakkerd98030e2009-05-02 15:13:40 +00003215/*
3216 * Return an informational string about the CRL.
3217 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003218int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3219 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003220{
Paul Bakker23986e52011-04-24 08:57:21 +00003221 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003222 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003223 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003224 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003225 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003226
3227 p = buf;
3228 n = size;
3229
3230 ret = snprintf( p, n, "%sCRL version : %d",
3231 prefix, crl->version );
3232 SAFE_SNPRINTF();
3233
3234 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3235 SAFE_SNPRINTF();
3236 ret = x509parse_dn_gets( p, n, &crl->issuer );
3237 SAFE_SNPRINTF();
3238
3239 ret = snprintf( p, n, "\n%sthis update : " \
3240 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3241 crl->this_update.year, crl->this_update.mon,
3242 crl->this_update.day, crl->this_update.hour,
3243 crl->this_update.min, crl->this_update.sec );
3244 SAFE_SNPRINTF();
3245
3246 ret = snprintf( p, n, "\n%snext update : " \
3247 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3248 crl->next_update.year, crl->next_update.mon,
3249 crl->next_update.day, crl->next_update.hour,
3250 crl->next_update.min, crl->next_update.sec );
3251 SAFE_SNPRINTF();
3252
3253 entry = &crl->entry;
3254
3255 ret = snprintf( p, n, "\n%sRevoked certificates:",
3256 prefix );
3257 SAFE_SNPRINTF();
3258
Paul Bakker9be19372009-07-27 20:21:53 +00003259 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003260 {
3261 ret = snprintf( p, n, "\n%sserial number: ",
3262 prefix );
3263 SAFE_SNPRINTF();
3264
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003265 ret = x509parse_serial_gets( p, n, &entry->serial);
3266 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003267
Paul Bakkerd98030e2009-05-02 15:13:40 +00003268 ret = snprintf( p, n, " revocation date: " \
3269 "%04d-%02d-%02d %02d:%02d:%02d",
3270 entry->revocation_date.year, entry->revocation_date.mon,
3271 entry->revocation_date.day, entry->revocation_date.hour,
3272 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003273 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003274
3275 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277
Paul Bakkerc70b9822013-04-07 22:00:46 +02003278 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003279 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
Paul Bakkerc70b9822013-04-07 22:00:46 +02003281 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3282 if( ret != 0 )
3283 ret = snprintf( p, n, "???" );
3284 else
3285 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003286 SAFE_SNPRINTF();
3287
Paul Bakker1e27bb22009-07-19 20:25:25 +00003288 ret = snprintf( p, n, "\n" );
3289 SAFE_SNPRINTF();
3290
Paul Bakker23986e52011-04-24 08:57:21 +00003291 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003292}
3293
3294/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003295 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003297#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003298int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003299{
Paul Bakkercce9d772011-11-18 14:26:47 +00003300 int year, mon, day;
3301 int hour, min, sec;
3302
3303#if defined(_WIN32)
3304 SYSTEMTIME st;
3305
3306 GetLocalTime(&st);
3307
3308 year = st.wYear;
3309 mon = st.wMonth;
3310 day = st.wDay;
3311 hour = st.wHour;
3312 min = st.wMinute;
3313 sec = st.wSecond;
3314#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003315 struct tm *lt;
3316 time_t tt;
3317
3318 tt = time( NULL );
3319 lt = localtime( &tt );
3320
Paul Bakkercce9d772011-11-18 14:26:47 +00003321 year = lt->tm_year + 1900;
3322 mon = lt->tm_mon + 1;
3323 day = lt->tm_mday;
3324 hour = lt->tm_hour;
3325 min = lt->tm_min;
3326 sec = lt->tm_sec;
3327#endif
3328
3329 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003330 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003331
Paul Bakkercce9d772011-11-18 14:26:47 +00003332 if( year == to->year &&
3333 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003334 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Paul Bakkercce9d772011-11-18 14:26:47 +00003336 if( year == to->year &&
3337 mon == to->mon &&
3338 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003339 return( 1 );
3340
Paul Bakkercce9d772011-11-18 14:26:47 +00003341 if( year == to->year &&
3342 mon == to->mon &&
3343 day == to->day &&
3344 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003345 return( 1 );
3346
Paul Bakkercce9d772011-11-18 14:26:47 +00003347 if( year == to->year &&
3348 mon == to->mon &&
3349 day == to->day &&
3350 hour == to->hour &&
3351 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003352 return( 1 );
3353
Paul Bakkercce9d772011-11-18 14:26:47 +00003354 if( year == to->year &&
3355 mon == to->mon &&
3356 day == to->day &&
3357 hour == to->hour &&
3358 min == to->min &&
3359 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003360 return( 1 );
3361
Paul Bakker40ea7de2009-05-03 10:18:48 +00003362 return( 0 );
3363}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003364#else /* POLARSSL_HAVE_TIME */
3365int x509parse_time_expired( const x509_time *to )
3366{
3367 ((void) to);
3368 return( 0 );
3369}
3370#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003371
3372/*
3373 * Return 1 if the certificate is revoked, or 0 otherwise.
3374 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003375int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003376{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003377 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003378
3379 while( cur != NULL && cur->serial.len != 0 )
3380 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003381 if( crt->serial.len == cur->serial.len &&
3382 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003383 {
3384 if( x509parse_time_expired( &cur->revocation_date ) )
3385 return( 1 );
3386 }
3387
3388 cur = cur->next;
3389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003390
3391 return( 0 );
3392}
3393
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003394/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003395 * Check that the given certificate is valid accoring to the CRL.
3396 */
3397static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3398 x509_crl *crl_list)
3399{
3400 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003401 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3402 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003403
Paul Bakker915275b2012-09-28 07:10:55 +00003404 if( ca == NULL )
3405 return( flags );
3406
Paul Bakker76fd75a2011-01-16 21:12:10 +00003407 /*
3408 * TODO: What happens if no CRL is present?
3409 * Suggestion: Revocation state should be unknown if no CRL is present.
3410 * For backwards compatibility this is not yet implemented.
3411 */
3412
Paul Bakker915275b2012-09-28 07:10:55 +00003413 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003414 {
Paul Bakker915275b2012-09-28 07:10:55 +00003415 if( crl_list->version == 0 ||
3416 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003417 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3418 crl_list->issuer_raw.len ) != 0 )
3419 {
3420 crl_list = crl_list->next;
3421 continue;
3422 }
3423
3424 /*
3425 * Check if CRL is correctly signed by the trusted CA
3426 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003427 md_info = md_info_from_type( crl_list->sig_md );
3428 if( md_info == NULL )
3429 {
3430 /*
3431 * Cannot check 'unknown' hash
3432 */
3433 flags |= BADCRL_NOT_TRUSTED;
3434 break;
3435 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003436
Paul Bakkerc70b9822013-04-07 22:00:46 +02003437 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003438
Paul Bakkerc70b9822013-04-07 22:00:46 +02003439 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003440 0, hash, crl_list->sig.p ) == 0 )
3441 {
3442 /*
3443 * CRL is not trusted
3444 */
3445 flags |= BADCRL_NOT_TRUSTED;
3446 break;
3447 }
3448
3449 /*
3450 * Check for validity of CRL (Do not drop out)
3451 */
3452 if( x509parse_time_expired( &crl_list->next_update ) )
3453 flags |= BADCRL_EXPIRED;
3454
3455 /*
3456 * Check if certificate is revoked
3457 */
3458 if( x509parse_revoked(crt, crl_list) )
3459 {
3460 flags |= BADCERT_REVOKED;
3461 break;
3462 }
3463
3464 crl_list = crl_list->next;
3465 }
3466 return flags;
3467}
3468
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003469static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003470{
3471 size_t i;
3472 size_t cn_idx = 0;
3473
Paul Bakker57b12982012-02-11 17:38:38 +00003474 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003475 return( 0 );
3476
3477 for( i = 0; i < strlen( cn ); ++i )
3478 {
3479 if( cn[i] == '.' )
3480 {
3481 cn_idx = i;
3482 break;
3483 }
3484 }
3485
3486 if( cn_idx == 0 )
3487 return( 0 );
3488
Paul Bakker535e97d2012-08-23 10:49:55 +00003489 if( strlen( cn ) - cn_idx == name->len - 1 &&
3490 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003491 {
3492 return( 1 );
3493 }
3494
3495 return( 0 );
3496}
3497
Paul Bakker915275b2012-09-28 07:10:55 +00003498static int x509parse_verify_top(
3499 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003500 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003501 int (*f_vrfy)(void *, x509_cert *, int, int *),
3502 void *p_vrfy )
3503{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003504 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003505 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003506 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3507 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003508
3509 if( x509parse_time_expired( &child->valid_to ) )
3510 *flags |= BADCERT_EXPIRED;
3511
3512 /*
3513 * Child is the top of the chain. Check against the trust_ca list.
3514 */
3515 *flags |= BADCERT_NOT_TRUSTED;
3516
3517 while( trust_ca != NULL )
3518 {
3519 if( trust_ca->version == 0 ||
3520 child->issuer_raw.len != trust_ca->subject_raw.len ||
3521 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3522 child->issuer_raw.len ) != 0 )
3523 {
3524 trust_ca = trust_ca->next;
3525 continue;
3526 }
3527
Paul Bakker9a736322012-11-14 12:39:52 +00003528 /*
3529 * Reduce path_len to check against if top of the chain is
3530 * the same as the trusted CA
3531 */
3532 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3533 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3534 child->issuer_raw.len ) == 0 )
3535 {
3536 check_path_cnt--;
3537 }
3538
Paul Bakker915275b2012-09-28 07:10:55 +00003539 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003540 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003541 {
3542 trust_ca = trust_ca->next;
3543 continue;
3544 }
3545
Paul Bakkerc70b9822013-04-07 22:00:46 +02003546 md_info = md_info_from_type( child->sig_md );
3547 if( md_info == NULL )
3548 {
3549 /*
3550 * Cannot check 'unknown' hash
3551 */
3552 continue;
3553 }
Paul Bakker915275b2012-09-28 07:10:55 +00003554
Paul Bakkerc70b9822013-04-07 22:00:46 +02003555 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003556
Paul Bakkerc70b9822013-04-07 22:00:46 +02003557 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003558 0, hash, child->sig.p ) != 0 )
3559 {
3560 trust_ca = trust_ca->next;
3561 continue;
3562 }
3563
3564 /*
3565 * Top of chain is signed by a trusted CA
3566 */
3567 *flags &= ~BADCERT_NOT_TRUSTED;
3568 break;
3569 }
3570
Paul Bakker9a736322012-11-14 12:39:52 +00003571 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003572 * If top of chain is not the same as the trusted CA send a verify request
3573 * to the callback for any issues with validity and CRL presence for the
3574 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003575 */
3576 if( trust_ca != NULL &&
3577 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3578 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3579 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003580 {
3581 /* Check trusted CA's CRL for then chain's top crt */
3582 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3583
3584 if( x509parse_time_expired( &trust_ca->valid_to ) )
3585 ca_flags |= BADCERT_EXPIRED;
3586
Paul Bakker915275b2012-09-28 07:10:55 +00003587 if( NULL != f_vrfy )
3588 {
Paul Bakker9a736322012-11-14 12:39:52 +00003589 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003590 return( ret );
3591 }
3592 }
3593
3594 /* Call callback on top cert */
3595 if( NULL != f_vrfy )
3596 {
Paul Bakker9a736322012-11-14 12:39:52 +00003597 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003598 return( ret );
3599 }
3600
Paul Bakker915275b2012-09-28 07:10:55 +00003601 *flags |= ca_flags;
3602
3603 return( 0 );
3604}
3605
3606static int x509parse_verify_child(
3607 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003608 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003609 int (*f_vrfy)(void *, x509_cert *, int, int *),
3610 void *p_vrfy )
3611{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003612 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003613 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003614 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003615 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003616 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003617
3618 if( x509parse_time_expired( &child->valid_to ) )
3619 *flags |= BADCERT_EXPIRED;
3620
Paul Bakkerc70b9822013-04-07 22:00:46 +02003621 md_info = md_info_from_type( child->sig_md );
3622 if( md_info == NULL )
3623 {
3624 /*
3625 * Cannot check 'unknown' hash
3626 */
Paul Bakker915275b2012-09-28 07:10:55 +00003627 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003628 }
3629 else
3630 {
3631 md( md_info, child->tbs.p, child->tbs.len, hash );
3632
3633 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3634 child->sig.p ) != 0 )
3635 *flags |= BADCERT_NOT_TRUSTED;
3636 }
3637
Paul Bakker915275b2012-09-28 07:10:55 +00003638 /* Check trusted CA's CRL for the given crt */
3639 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3640
3641 grandparent = parent->next;
3642
3643 while( grandparent != NULL )
3644 {
3645 if( grandparent->version == 0 ||
3646 grandparent->ca_istrue == 0 ||
3647 parent->issuer_raw.len != grandparent->subject_raw.len ||
3648 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3649 parent->issuer_raw.len ) != 0 )
3650 {
3651 grandparent = grandparent->next;
3652 continue;
3653 }
3654 break;
3655 }
3656
Paul Bakker915275b2012-09-28 07:10:55 +00003657 if( grandparent != NULL )
3658 {
3659 /*
3660 * Part of the chain
3661 */
Paul Bakker9a736322012-11-14 12:39:52 +00003662 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 +00003663 if( ret != 0 )
3664 return( ret );
3665 }
3666 else
3667 {
Paul Bakker9a736322012-11-14 12:39:52 +00003668 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 +00003669 if( ret != 0 )
3670 return( ret );
3671 }
3672
3673 /* child is verified to be a child of the parent, call verify callback */
3674 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003675 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003676 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003677
3678 *flags |= parent_flags;
3679
3680 return( 0 );
3681}
3682
Paul Bakker76fd75a2011-01-16 21:12:10 +00003683/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003684 * Verify the certificate validity
3685 */
3686int x509parse_verify( x509_cert *crt,
3687 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003688 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003689 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003690 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003691 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003692{
Paul Bakker23986e52011-04-24 08:57:21 +00003693 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003694 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003695 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003696 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003698 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003699
Paul Bakker40ea7de2009-05-03 10:18:48 +00003700 *flags = 0;
3701
Paul Bakker5121ce52009-01-03 21:22:43 +00003702 if( cn != NULL )
3703 {
3704 name = &crt->subject;
3705 cn_len = strlen( cn );
3706
Paul Bakker4d2c1242012-05-10 14:12:46 +00003707 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003709 cur = &crt->subject_alt_names;
3710
3711 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003712 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003713 if( cur->buf.len == cn_len &&
3714 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003715 break;
3716
Paul Bakker535e97d2012-08-23 10:49:55 +00003717 if( cur->buf.len > 2 &&
3718 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003719 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003720 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003721
Paul Bakker4d2c1242012-05-10 14:12:46 +00003722 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003723 }
3724
3725 if( cur == NULL )
3726 *flags |= BADCERT_CN_MISMATCH;
3727 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003728 else
3729 {
3730 while( name != NULL )
3731 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003732 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003733 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003734 if( name->val.len == cn_len &&
3735 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003736 break;
3737
Paul Bakker535e97d2012-08-23 10:49:55 +00003738 if( name->val.len > 2 &&
3739 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003740 x509_wildcard_verify( cn, &name->val ) )
3741 break;
3742 }
3743
3744 name = name->next;
3745 }
3746
3747 if( name == NULL )
3748 *flags |= BADCERT_CN_MISMATCH;
3749 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003750 }
3751
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003753 * Iterate upwards in the given cert chain, to find our crt parent.
3754 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003755 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003756 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003757
Paul Bakker76fd75a2011-01-16 21:12:10 +00003758 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003759 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003760 if( parent->ca_istrue == 0 ||
3761 crt->issuer_raw.len != parent->subject_raw.len ||
3762 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003763 crt->issuer_raw.len ) != 0 )
3764 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003765 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 continue;
3767 }
Paul Bakker915275b2012-09-28 07:10:55 +00003768 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 }
3770
Paul Bakker915275b2012-09-28 07:10:55 +00003771 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003772 {
Paul Bakker915275b2012-09-28 07:10:55 +00003773 /*
3774 * Part of the chain
3775 */
Paul Bakker9a736322012-11-14 12:39:52 +00003776 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003777 if( ret != 0 )
3778 return( ret );
3779 }
3780 else
Paul Bakker74111d32011-01-15 16:57:55 +00003781 {
Paul Bakker9a736322012-11-14 12:39:52 +00003782 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003783 if( ret != 0 )
3784 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003785 }
Paul Bakker915275b2012-09-28 07:10:55 +00003786
3787 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003788 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003789
Paul Bakker5121ce52009-01-03 21:22:43 +00003790 return( 0 );
3791}
3792
3793/*
3794 * Unallocate all certificate data
3795 */
3796void x509_free( x509_cert *crt )
3797{
3798 x509_cert *cert_cur = crt;
3799 x509_cert *cert_prv;
3800 x509_name *name_cur;
3801 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003802 x509_sequence *seq_cur;
3803 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003804
3805 if( crt == NULL )
3806 return;
3807
3808 do
3809 {
3810 rsa_free( &cert_cur->rsa );
3811
3812 name_cur = cert_cur->issuer.next;
3813 while( name_cur != NULL )
3814 {
3815 name_prv = name_cur;
3816 name_cur = name_cur->next;
3817 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003818 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003819 }
3820
3821 name_cur = cert_cur->subject.next;
3822 while( name_cur != NULL )
3823 {
3824 name_prv = name_cur;
3825 name_cur = name_cur->next;
3826 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003827 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003828 }
3829
Paul Bakker74111d32011-01-15 16:57:55 +00003830 seq_cur = cert_cur->ext_key_usage.next;
3831 while( seq_cur != NULL )
3832 {
3833 seq_prv = seq_cur;
3834 seq_cur = seq_cur->next;
3835 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003836 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003837 }
3838
Paul Bakker8afa70d2012-02-11 18:42:45 +00003839 seq_cur = cert_cur->subject_alt_names.next;
3840 while( seq_cur != NULL )
3841 {
3842 seq_prv = seq_cur;
3843 seq_cur = seq_cur->next;
3844 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003845 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003846 }
3847
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 if( cert_cur->raw.p != NULL )
3849 {
3850 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003851 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 }
3853
3854 cert_cur = cert_cur->next;
3855 }
3856 while( cert_cur != NULL );
3857
3858 cert_cur = crt;
3859 do
3860 {
3861 cert_prv = cert_cur;
3862 cert_cur = cert_cur->next;
3863
3864 memset( cert_prv, 0, sizeof( x509_cert ) );
3865 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003866 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003867 }
3868 while( cert_cur != NULL );
3869}
3870
Paul Bakkerd98030e2009-05-02 15:13:40 +00003871/*
3872 * Unallocate all CRL data
3873 */
3874void x509_crl_free( x509_crl *crl )
3875{
3876 x509_crl *crl_cur = crl;
3877 x509_crl *crl_prv;
3878 x509_name *name_cur;
3879 x509_name *name_prv;
3880 x509_crl_entry *entry_cur;
3881 x509_crl_entry *entry_prv;
3882
3883 if( crl == NULL )
3884 return;
3885
3886 do
3887 {
3888 name_cur = crl_cur->issuer.next;
3889 while( name_cur != NULL )
3890 {
3891 name_prv = name_cur;
3892 name_cur = name_cur->next;
3893 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003894 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003895 }
3896
3897 entry_cur = crl_cur->entry.next;
3898 while( entry_cur != NULL )
3899 {
3900 entry_prv = entry_cur;
3901 entry_cur = entry_cur->next;
3902 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003903 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003904 }
3905
3906 if( crl_cur->raw.p != NULL )
3907 {
3908 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003909 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003910 }
3911
3912 crl_cur = crl_cur->next;
3913 }
3914 while( crl_cur != NULL );
3915
3916 crl_cur = crl;
3917 do
3918 {
3919 crl_prv = crl_cur;
3920 crl_cur = crl_cur->next;
3921
3922 memset( crl_prv, 0, sizeof( x509_crl ) );
3923 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003924 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003925 }
3926 while( crl_cur != NULL );
3927}
3928
Paul Bakker40e46942009-01-03 21:51:57 +00003929#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003930
Paul Bakker40e46942009-01-03 21:51:57 +00003931#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003932
3933/*
3934 * Checkup routine
3935 */
3936int x509_self_test( int verbose )
3937{
Paul Bakker5690efc2011-05-26 13:16:06 +00003938#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003939 int ret;
3940 int flags;
3941 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 x509_cert cacert;
3943 x509_cert clicert;
3944 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003945#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003946 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003947#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003948
3949 if( verbose != 0 )
3950 printf( " X.509 certificate load: " );
3951
3952 memset( &clicert, 0, sizeof( x509_cert ) );
3953
Paul Bakker3c2122f2013-06-24 19:03:14 +02003954 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003955 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003956 if( ret != 0 )
3957 {
3958 if( verbose != 0 )
3959 printf( "failed\n" );
3960
3961 return( ret );
3962 }
3963
3964 memset( &cacert, 0, sizeof( x509_cert ) );
3965
Paul Bakker3c2122f2013-06-24 19:03:14 +02003966 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003967 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 if( ret != 0 )
3969 {
3970 if( verbose != 0 )
3971 printf( "failed\n" );
3972
3973 return( ret );
3974 }
3975
3976 if( verbose != 0 )
3977 printf( "passed\n X.509 private key load: " );
3978
3979 i = strlen( test_ca_key );
3980 j = strlen( test_ca_pwd );
3981
Paul Bakker66b78b22011-03-25 14:22:50 +00003982 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3983
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003984 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003985 (const unsigned char *) test_ca_key, i,
3986 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 {
3988 if( verbose != 0 )
3989 printf( "failed\n" );
3990
3991 return( ret );
3992 }
3993
3994 if( verbose != 0 )
3995 printf( "passed\n X.509 signature verify: ");
3996
Paul Bakker23986e52011-04-24 08:57:21 +00003997 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998 if( ret != 0 )
3999 {
Paul Bakker23986e52011-04-24 08:57:21 +00004000 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004001 if( verbose != 0 )
4002 printf( "failed\n" );
4003
4004 return( ret );
4005 }
4006
Paul Bakker5690efc2011-05-26 13:16:06 +00004007#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004008 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004009 printf( "passed\n X.509 DHM parameter load: " );
4010
4011 i = strlen( test_dhm_params );
4012 j = strlen( test_ca_pwd );
4013
Paul Bakker3c2122f2013-06-24 19:03:14 +02004014 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004015 {
4016 if( verbose != 0 )
4017 printf( "failed\n" );
4018
4019 return( ret );
4020 }
4021
4022 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004023 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004025
4026 x509_free( &cacert );
4027 x509_free( &clicert );
4028 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004029#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004030 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
4033 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004034#else
4035 ((void) verbose);
4036 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4037#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004038}
4039
4040#endif
4041
4042#endif