blob: e04d56b0e40b10ebc4db0332510c6b014deeec83 [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
2471 // At this point we only know it's not a PEM formatted key. Could be any
2472 // of the known DER encoded private key formats
2473 //
2474 // We try the different DER format parsers to see if one passes without
2475 // error
2476 //
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002477 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2478 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002479 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002480 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002483 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002484
2485 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2486 {
2487 return( ret );
2488 }
2489
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002490 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2491 return( 0 );
2492
2493 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002494
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002495 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2496 return( 0 );
2497
2498 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002499
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002500 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002501}
2502
2503/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002504 * Parse a public RSA key
2505 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002506int x509parse_public_key_rsa( rsa_context *rsa,
2507 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002508{
Paul Bakker23986e52011-04-24 08:57:21 +00002509 int ret;
2510 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002511 unsigned char *p, *end;
2512 x509_buf alg_oid;
2513#if defined(POLARSSL_PEM_C)
2514 pem_context pem;
2515
2516 pem_init( &pem );
2517 ret = pem_read_buffer( &pem,
2518 "-----BEGIN PUBLIC KEY-----",
2519 "-----END PUBLIC KEY-----",
2520 key, NULL, 0, &len );
2521
2522 if( ret == 0 )
2523 {
2524 /*
2525 * Was PEM encoded
2526 */
2527 keylen = pem.buflen;
2528 }
Paul Bakker00b28602013-06-24 13:02:41 +02002529 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002530 {
2531 pem_free( &pem );
2532 return( ret );
2533 }
2534
2535 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2536#else
2537 p = (unsigned char *) key;
2538#endif
2539 end = p + keylen;
2540
2541 /*
2542 * PublicKeyInfo ::= SEQUENCE {
2543 * algorithm AlgorithmIdentifier,
2544 * PublicKey BIT STRING
2545 * }
2546 *
2547 * AlgorithmIdentifier ::= SEQUENCE {
2548 * algorithm OBJECT IDENTIFIER,
2549 * parameters ANY DEFINED BY algorithm OPTIONAL
2550 * }
2551 *
2552 * RSAPublicKey ::= SEQUENCE {
2553 * modulus INTEGER, -- n
2554 * publicExponent INTEGER -- e
2555 * }
2556 */
2557
2558 if( ( ret = asn1_get_tag( &p, end, &len,
2559 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2560 {
2561#if defined(POLARSSL_PEM_C)
2562 pem_free( &pem );
2563#endif
2564 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002565 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002566 }
2567
2568 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2569 {
2570#if defined(POLARSSL_PEM_C)
2571 pem_free( &pem );
2572#endif
2573 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002574 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002575 }
2576
2577 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2578 {
2579#if defined(POLARSSL_PEM_C)
2580 pem_free( &pem );
2581#endif
2582 rsa_free( rsa );
2583 return( ret );
2584 }
2585
2586 rsa->len = mpi_size( &rsa->N );
2587
2588#if defined(POLARSSL_PEM_C)
2589 pem_free( &pem );
2590#endif
2591
2592 return( 0 );
2593}
2594
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002595#if defined(POLARSSL_ECP_C)
2596/*
2597 * Parse a private EC key
2598 */
2599int x509parse_key_ec( ecp_keypair *eckey,
2600 const unsigned char *key, size_t keylen,
2601 const unsigned char *pwd, size_t pwdlen )
2602{
2603 int ret;
2604
2605#if defined(POLARSSL_PEM_C)
2606 size_t len;
2607 pem_context pem;
2608
2609 pem_init( &pem );
2610 /* TODO: get list of correct PEM headers */
2611 ret = pem_read_buffer( &pem,
2612 "-----BEGIN EC PRIVATE KEY-----",
2613 "-----END EC PRIVATE KEY-----",
2614 key, pwd, pwdlen, &len );
2615 if( ret == 0 )
2616 {
2617 /* TODO: write der decoding function
2618 if( ( ret = x509parse_key_pkcs8_encrypted_der( eckey,
2619 pem.buf, pem.buflen,
2620 pwd, pwdlen ) ) != 0 )
2621 {
2622 ecp_keypair_free( eckey );
2623 }
2624 */
2625
2626 pem_free( &pem );
2627 return( ret );
2628 }
2629 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2630 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2631 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2632 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2633 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2634 return( ret );
2635
2636 /* TODO: now repeat with other valid PEM headers */
2637
2638#else
2639 ((void) pwd);
2640 ((void) pwdlen);
2641#endif /* POLARSSL_PEM_C */
2642
2643 ((void) keylen);
2644 /* TODO: write der decoding functions (encrypted, unencnrypted)
2645 if( ( ret = x509parse_key_pkcs8_encrypted_der( eckey, key, keylen,
2646 pwd, pwdlen ) ) == 0 )
2647 {
2648 return( 0 );
2649 }
2650
2651 ecp_keypair_free( eckey );
2652
2653 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2654 {
2655 return( ret );
2656 }
2657
2658 if( ( ret = x509parse_key_pkcs8_unencrypted_der( eckey, key, keylen ) )
2659 == 0 )
2660 {
2661 return( 0 );
2662 }
2663 */
2664
2665 ecp_keypair_free( eckey );
2666
2667 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2668}
2669
2670/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002671 * Parse a public EC key in RFC 5480 format, der-encoded
2672 */
2673static int x509parse_public_key_ec_der( ecp_keypair *key,
2674 const unsigned char *buf, size_t len )
2675{
2676 int ret;
2677 ecp_group_id grp_id;
2678 x509_buf alg_oid;
2679 pk_type_t alg = POLARSSL_PK_NONE;
2680 unsigned char *p = (unsigned char *) buf;
2681 unsigned char *end = p + len;
2682 const unsigned char *params_end;
2683 /*
2684 * SubjectPublicKeyInfo ::= SEQUENCE {
2685 * algorithm AlgorithmIdentifier,
2686 * subjectPublicKey BIT STRING
2687 * }
2688 * -- algorithm parameters are ECParameters
2689 * -- subjectPublicKey is an ECPoint
2690 */
2691 if( ( ret = asn1_get_tag( &p, end, &len,
2692 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2693 {
2694 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
2695 }
2696
2697 if( ( ret = x509_get_alg( &p, end, &alg_oid, &params_end ) ) != 0 )
2698 return( ret );
2699
2700 if( oid_get_pk_alg( &alg_oid, &alg ) != 0 )
2701 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2702
2703 if( alg != POLARSSL_PK_ECKEY && alg != POLARSSL_PK_ECKEY_DH )
2704 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2705
2706 if ( alg == POLARSSL_PK_ECKEY_DH )
2707 key->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2708
2709 if( ( ret = x509_get_ecparams( &p, params_end, &grp_id ) ) != 0 )
2710 return( ret );
2711
2712 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
2713 return( ret );
2714
2715 if( ( ret = x509_get_subpubkey_ec( &p, end, &key->grp, &key->Q ) ) != 0 )
2716 {
2717 return( ret );
2718 }
2719
2720 return( 0 );
2721}
2722
2723/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002724 * Parse a public EC key
2725 */
2726int x509parse_public_key_ec( ecp_keypair *eckey,
2727 const unsigned char *key, size_t keylen )
2728{
2729 int ret;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002730#if defined(POLARSSL_PEM_C)
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002731 size_t len;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002732 pem_context pem;
2733
2734 pem_init( &pem );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002735 ret = pem_read_buffer( &pem,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002736 "-----BEGIN PUBLIC KEY-----",
2737 "-----END PUBLIC KEY-----",
2738 key, NULL, 0, &len );
2739
2740 if( ret == 0 )
2741 {
2742 /*
2743 * Was PEM encoded
2744 */
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002745 key = pem.buf;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002746 keylen = pem.buflen;
2747 }
2748 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2749 {
2750 pem_free( &pem );
2751 return( ret );
2752 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002753#endif
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002754
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002755 if( ( ret = x509parse_public_key_ec_der ( eckey, key, keylen ) ) != 0 ||
2756 ( ret = ecp_check_pubkey( &eckey->grp, &eckey->Q ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002757 {
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002758 ecp_keypair_free( eckey );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002759 }
2760
2761#if defined(POLARSSL_PEM_C)
2762 pem_free( &pem );
2763#endif
2764
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002765 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002766}
2767#endif /* defined(POLARSSL_ECP_C) */
2768
Paul Bakkereaa89f82011-04-04 21:36:15 +00002769#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002770/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002771 * Parse DHM parameters
2772 */
Paul Bakker23986e52011-04-24 08:57:21 +00002773int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002774{
Paul Bakker23986e52011-04-24 08:57:21 +00002775 int ret;
2776 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002777 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002778#if defined(POLARSSL_PEM_C)
2779 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002780
Paul Bakker96743fc2011-02-12 14:30:57 +00002781 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002782
Paul Bakker96743fc2011-02-12 14:30:57 +00002783 ret = pem_read_buffer( &pem,
2784 "-----BEGIN DH PARAMETERS-----",
2785 "-----END DH PARAMETERS-----",
2786 dhmin, NULL, 0, &dhminlen );
2787
2788 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002789 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002790 /*
2791 * Was PEM encoded
2792 */
2793 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002794 }
Paul Bakker00b28602013-06-24 13:02:41 +02002795 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002796 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002797 pem_free( &pem );
2798 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002799 }
2800
Paul Bakker96743fc2011-02-12 14:30:57 +00002801 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2802#else
2803 p = (unsigned char *) dhmin;
2804#endif
2805 end = p + dhminlen;
2806
Paul Bakker1b57b062011-01-06 15:48:19 +00002807 memset( dhm, 0, sizeof( dhm_context ) );
2808
Paul Bakker1b57b062011-01-06 15:48:19 +00002809 /*
2810 * DHParams ::= SEQUENCE {
2811 * prime INTEGER, -- P
2812 * generator INTEGER, -- g
2813 * }
2814 */
2815 if( ( ret = asn1_get_tag( &p, end, &len,
2816 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2817 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002818#if defined(POLARSSL_PEM_C)
2819 pem_free( &pem );
2820#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002821 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002822 }
2823
2824 end = p + len;
2825
2826 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2827 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2828 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002829#if defined(POLARSSL_PEM_C)
2830 pem_free( &pem );
2831#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002832 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002833 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002834 }
2835
2836 if( p != end )
2837 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002838#if defined(POLARSSL_PEM_C)
2839 pem_free( &pem );
2840#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002841 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002842 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002843 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2844 }
2845
Paul Bakker96743fc2011-02-12 14:30:57 +00002846#if defined(POLARSSL_PEM_C)
2847 pem_free( &pem );
2848#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002849
2850 return( 0 );
2851}
2852
Paul Bakker335db3f2011-04-25 15:28:35 +00002853#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002854/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002855 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002856 */
2857int x509parse_dhmfile( dhm_context *dhm, const char *path )
2858{
2859 int ret;
2860 size_t n;
2861 unsigned char *buf;
2862
Paul Bakker69e095c2011-12-10 21:55:01 +00002863 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2864 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002865
Paul Bakker27fdf462011-06-09 13:55:13 +00002866 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002867
2868 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002869 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002870
2871 return( ret );
2872}
Paul Bakker335db3f2011-04-25 15:28:35 +00002873#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002874#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002875
Paul Bakker5121ce52009-01-03 21:22:43 +00002876#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002877#include <stdarg.h>
2878
2879#if !defined vsnprintf
2880#define vsnprintf _vsnprintf
2881#endif // vsnprintf
2882
2883/*
2884 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2885 * Result value is not size of buffer needed, but -1 if no fit is possible.
2886 *
2887 * This fuction tries to 'fix' this by at least suggesting enlarging the
2888 * size by 20.
2889 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002890static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002891{
2892 va_list ap;
2893 int res = -1;
2894
2895 va_start( ap, format );
2896
2897 res = vsnprintf( str, size, format, ap );
2898
2899 va_end( ap );
2900
2901 // No quick fix possible
2902 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002903 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002904
2905 return res;
2906}
2907
2908#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002909#endif
2910
Paul Bakkerd98030e2009-05-02 15:13:40 +00002911#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2912
2913#define SAFE_SNPRINTF() \
2914{ \
2915 if( ret == -1 ) \
2916 return( -1 ); \
2917 \
Paul Bakker23986e52011-04-24 08:57:21 +00002918 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002919 p[n - 1] = '\0'; \
2920 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2921 } \
2922 \
Paul Bakker23986e52011-04-24 08:57:21 +00002923 n -= (unsigned int) ret; \
2924 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002925}
2926
Paul Bakker5121ce52009-01-03 21:22:43 +00002927/*
2928 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002929 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002931int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002932{
Paul Bakker23986e52011-04-24 08:57:21 +00002933 int ret;
2934 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002936 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002937 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002938 char s[128], *p;
2939
2940 memset( s, 0, sizeof( s ) );
2941
2942 name = dn;
2943 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002944 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
2946 while( name != NULL )
2947 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002948 if( !name->oid.p )
2949 {
2950 name = name->next;
2951 continue;
2952 }
2953
Paul Bakker74111d32011-01-15 16:57:55 +00002954 if( name != dn )
2955 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002956 ret = snprintf( p, n, ", " );
2957 SAFE_SNPRINTF();
2958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Paul Bakkerc70b9822013-04-07 22:00:46 +02002960 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Paul Bakkerc70b9822013-04-07 22:00:46 +02002962 if( ret == 0 )
2963 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002964 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002965 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002966 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
2968 for( i = 0; i < name->val.len; i++ )
2969 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002970 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002971 break;
2972
2973 c = name->val.p[i];
2974 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2975 s[i] = '?';
2976 else s[i] = c;
2977 }
2978 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002979 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002980 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 name = name->next;
2982 }
2983
Paul Bakker23986e52011-04-24 08:57:21 +00002984 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002985}
2986
2987/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002988 * Store the serial in printable form into buf; no more
2989 * than size characters will be written
2990 */
2991int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2992{
Paul Bakker23986e52011-04-24 08:57:21 +00002993 int ret;
2994 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002995 char *p;
2996
2997 p = buf;
2998 n = size;
2999
3000 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003001 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003002
3003 for( i = 0; i < nr; i++ )
3004 {
Paul Bakker93048802011-12-05 14:38:06 +00003005 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003006 continue;
3007
Paul Bakkerdd476992011-01-16 21:34:59 +00003008 ret = snprintf( p, n, "%02X%s",
3009 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3010 SAFE_SNPRINTF();
3011 }
3012
Paul Bakker03c7c252011-11-25 12:37:37 +00003013 if( nr != serial->len )
3014 {
3015 ret = snprintf( p, n, "...." );
3016 SAFE_SNPRINTF();
3017 }
3018
Paul Bakker23986e52011-04-24 08:57:21 +00003019 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003020}
3021
3022/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003023 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003025int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3026 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027{
Paul Bakker23986e52011-04-24 08:57:21 +00003028 int ret;
3029 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003030 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003031 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
3033 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003034 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Paul Bakkerd98030e2009-05-02 15:13:40 +00003036 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003038 SAFE_SNPRINTF();
3039 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003041 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
Paul Bakkerdd476992011-01-16 21:34:59 +00003043 ret = x509parse_serial_gets( p, n, &crt->serial);
3044 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Paul Bakkerd98030e2009-05-02 15:13:40 +00003046 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3047 SAFE_SNPRINTF();
3048 ret = x509parse_dn_gets( p, n, &crt->issuer );
3049 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakkerd98030e2009-05-02 15:13:40 +00003051 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3052 SAFE_SNPRINTF();
3053 ret = x509parse_dn_gets( p, n, &crt->subject );
3054 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakkerd98030e2009-05-02 15:13:40 +00003056 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003057 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3058 crt->valid_from.year, crt->valid_from.mon,
3059 crt->valid_from.day, crt->valid_from.hour,
3060 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003061 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Paul Bakkerd98030e2009-05-02 15:13:40 +00003063 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003064 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3065 crt->valid_to.year, crt->valid_to.mon,
3066 crt->valid_to.day, crt->valid_to.hour,
3067 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003068 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003069
Paul Bakkerc70b9822013-04-07 22:00:46 +02003070 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003071 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
Paul Bakkerc70b9822013-04-07 22:00:46 +02003073 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3074 if( ret != 0 )
3075 ret = snprintf( p, n, "???" );
3076 else
3077 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003078 SAFE_SNPRINTF();
3079
3080 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003081 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003082 SAFE_SNPRINTF();
3083
Paul Bakker23986e52011-04-24 08:57:21 +00003084 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003085}
3086
Paul Bakker74111d32011-01-15 16:57:55 +00003087/*
3088 * Return an informational string describing the given OID
3089 */
3090const char *x509_oid_get_description( x509_buf *oid )
3091{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003092 const char *desc = NULL;
3093 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003094
Paul Bakkerc70b9822013-04-07 22:00:46 +02003095 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003096
Paul Bakkerc70b9822013-04-07 22:00:46 +02003097 if( ret != 0 )
3098 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003099
Paul Bakkerc70b9822013-04-07 22:00:46 +02003100 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003101}
3102
3103/* Return the x.y.z.... style numeric string for the given OID */
3104int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3105{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003106 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003107}
3108
Paul Bakkerd98030e2009-05-02 15:13:40 +00003109/*
3110 * Return an informational string about the CRL.
3111 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003112int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3113 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003114{
Paul Bakker23986e52011-04-24 08:57:21 +00003115 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003116 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003117 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003118 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003119 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003120
3121 p = buf;
3122 n = size;
3123
3124 ret = snprintf( p, n, "%sCRL version : %d",
3125 prefix, crl->version );
3126 SAFE_SNPRINTF();
3127
3128 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3129 SAFE_SNPRINTF();
3130 ret = x509parse_dn_gets( p, n, &crl->issuer );
3131 SAFE_SNPRINTF();
3132
3133 ret = snprintf( p, n, "\n%sthis update : " \
3134 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3135 crl->this_update.year, crl->this_update.mon,
3136 crl->this_update.day, crl->this_update.hour,
3137 crl->this_update.min, crl->this_update.sec );
3138 SAFE_SNPRINTF();
3139
3140 ret = snprintf( p, n, "\n%snext update : " \
3141 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3142 crl->next_update.year, crl->next_update.mon,
3143 crl->next_update.day, crl->next_update.hour,
3144 crl->next_update.min, crl->next_update.sec );
3145 SAFE_SNPRINTF();
3146
3147 entry = &crl->entry;
3148
3149 ret = snprintf( p, n, "\n%sRevoked certificates:",
3150 prefix );
3151 SAFE_SNPRINTF();
3152
Paul Bakker9be19372009-07-27 20:21:53 +00003153 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003154 {
3155 ret = snprintf( p, n, "\n%sserial number: ",
3156 prefix );
3157 SAFE_SNPRINTF();
3158
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003159 ret = x509parse_serial_gets( p, n, &entry->serial);
3160 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003161
Paul Bakkerd98030e2009-05-02 15:13:40 +00003162 ret = snprintf( p, n, " revocation date: " \
3163 "%04d-%02d-%02d %02d:%02d:%02d",
3164 entry->revocation_date.year, entry->revocation_date.mon,
3165 entry->revocation_date.day, entry->revocation_date.hour,
3166 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003167 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003168
3169 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003170 }
3171
Paul Bakkerc70b9822013-04-07 22:00:46 +02003172 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003173 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
Paul Bakkerc70b9822013-04-07 22:00:46 +02003175 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3176 if( ret != 0 )
3177 ret = snprintf( p, n, "???" );
3178 else
3179 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003180 SAFE_SNPRINTF();
3181
Paul Bakker1e27bb22009-07-19 20:25:25 +00003182 ret = snprintf( p, n, "\n" );
3183 SAFE_SNPRINTF();
3184
Paul Bakker23986e52011-04-24 08:57:21 +00003185 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003186}
3187
3188/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003189 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003190 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003191#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003192int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003193{
Paul Bakkercce9d772011-11-18 14:26:47 +00003194 int year, mon, day;
3195 int hour, min, sec;
3196
3197#if defined(_WIN32)
3198 SYSTEMTIME st;
3199
3200 GetLocalTime(&st);
3201
3202 year = st.wYear;
3203 mon = st.wMonth;
3204 day = st.wDay;
3205 hour = st.wHour;
3206 min = st.wMinute;
3207 sec = st.wSecond;
3208#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 struct tm *lt;
3210 time_t tt;
3211
3212 tt = time( NULL );
3213 lt = localtime( &tt );
3214
Paul Bakkercce9d772011-11-18 14:26:47 +00003215 year = lt->tm_year + 1900;
3216 mon = lt->tm_mon + 1;
3217 day = lt->tm_mday;
3218 hour = lt->tm_hour;
3219 min = lt->tm_min;
3220 sec = lt->tm_sec;
3221#endif
3222
3223 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003224 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Paul Bakkercce9d772011-11-18 14:26:47 +00003226 if( year == to->year &&
3227 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003228 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003229
Paul Bakkercce9d772011-11-18 14:26:47 +00003230 if( year == to->year &&
3231 mon == to->mon &&
3232 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003233 return( 1 );
3234
Paul Bakkercce9d772011-11-18 14:26:47 +00003235 if( year == to->year &&
3236 mon == to->mon &&
3237 day == to->day &&
3238 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003239 return( 1 );
3240
Paul Bakkercce9d772011-11-18 14:26:47 +00003241 if( year == to->year &&
3242 mon == to->mon &&
3243 day == to->day &&
3244 hour == to->hour &&
3245 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003246 return( 1 );
3247
Paul Bakkercce9d772011-11-18 14:26:47 +00003248 if( year == to->year &&
3249 mon == to->mon &&
3250 day == to->day &&
3251 hour == to->hour &&
3252 min == to->min &&
3253 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003254 return( 1 );
3255
Paul Bakker40ea7de2009-05-03 10:18:48 +00003256 return( 0 );
3257}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003258#else /* POLARSSL_HAVE_TIME */
3259int x509parse_time_expired( const x509_time *to )
3260{
3261 ((void) to);
3262 return( 0 );
3263}
3264#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003265
3266/*
3267 * Return 1 if the certificate is revoked, or 0 otherwise.
3268 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003269int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003270{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003271 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003272
3273 while( cur != NULL && cur->serial.len != 0 )
3274 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003275 if( crt->serial.len == cur->serial.len &&
3276 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003277 {
3278 if( x509parse_time_expired( &cur->revocation_date ) )
3279 return( 1 );
3280 }
3281
3282 cur = cur->next;
3283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003284
3285 return( 0 );
3286}
3287
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003288/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003289 * Check that the given certificate is valid accoring to the CRL.
3290 */
3291static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3292 x509_crl *crl_list)
3293{
3294 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003295 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3296 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003297
Paul Bakker915275b2012-09-28 07:10:55 +00003298 if( ca == NULL )
3299 return( flags );
3300
Paul Bakker76fd75a2011-01-16 21:12:10 +00003301 /*
3302 * TODO: What happens if no CRL is present?
3303 * Suggestion: Revocation state should be unknown if no CRL is present.
3304 * For backwards compatibility this is not yet implemented.
3305 */
3306
Paul Bakker915275b2012-09-28 07:10:55 +00003307 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003308 {
Paul Bakker915275b2012-09-28 07:10:55 +00003309 if( crl_list->version == 0 ||
3310 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003311 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3312 crl_list->issuer_raw.len ) != 0 )
3313 {
3314 crl_list = crl_list->next;
3315 continue;
3316 }
3317
3318 /*
3319 * Check if CRL is correctly signed by the trusted CA
3320 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003321 md_info = md_info_from_type( crl_list->sig_md );
3322 if( md_info == NULL )
3323 {
3324 /*
3325 * Cannot check 'unknown' hash
3326 */
3327 flags |= BADCRL_NOT_TRUSTED;
3328 break;
3329 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003330
Paul Bakkerc70b9822013-04-07 22:00:46 +02003331 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003332
Paul Bakkerc70b9822013-04-07 22:00:46 +02003333 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003334 0, hash, crl_list->sig.p ) == 0 )
3335 {
3336 /*
3337 * CRL is not trusted
3338 */
3339 flags |= BADCRL_NOT_TRUSTED;
3340 break;
3341 }
3342
3343 /*
3344 * Check for validity of CRL (Do not drop out)
3345 */
3346 if( x509parse_time_expired( &crl_list->next_update ) )
3347 flags |= BADCRL_EXPIRED;
3348
3349 /*
3350 * Check if certificate is revoked
3351 */
3352 if( x509parse_revoked(crt, crl_list) )
3353 {
3354 flags |= BADCERT_REVOKED;
3355 break;
3356 }
3357
3358 crl_list = crl_list->next;
3359 }
3360 return flags;
3361}
3362
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003363static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003364{
3365 size_t i;
3366 size_t cn_idx = 0;
3367
Paul Bakker57b12982012-02-11 17:38:38 +00003368 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003369 return( 0 );
3370
3371 for( i = 0; i < strlen( cn ); ++i )
3372 {
3373 if( cn[i] == '.' )
3374 {
3375 cn_idx = i;
3376 break;
3377 }
3378 }
3379
3380 if( cn_idx == 0 )
3381 return( 0 );
3382
Paul Bakker535e97d2012-08-23 10:49:55 +00003383 if( strlen( cn ) - cn_idx == name->len - 1 &&
3384 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003385 {
3386 return( 1 );
3387 }
3388
3389 return( 0 );
3390}
3391
Paul Bakker915275b2012-09-28 07:10:55 +00003392static int x509parse_verify_top(
3393 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003394 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003395 int (*f_vrfy)(void *, x509_cert *, int, int *),
3396 void *p_vrfy )
3397{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003398 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003399 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003400 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3401 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003402
3403 if( x509parse_time_expired( &child->valid_to ) )
3404 *flags |= BADCERT_EXPIRED;
3405
3406 /*
3407 * Child is the top of the chain. Check against the trust_ca list.
3408 */
3409 *flags |= BADCERT_NOT_TRUSTED;
3410
3411 while( trust_ca != NULL )
3412 {
3413 if( trust_ca->version == 0 ||
3414 child->issuer_raw.len != trust_ca->subject_raw.len ||
3415 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3416 child->issuer_raw.len ) != 0 )
3417 {
3418 trust_ca = trust_ca->next;
3419 continue;
3420 }
3421
Paul Bakker9a736322012-11-14 12:39:52 +00003422 /*
3423 * Reduce path_len to check against if top of the chain is
3424 * the same as the trusted CA
3425 */
3426 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3427 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3428 child->issuer_raw.len ) == 0 )
3429 {
3430 check_path_cnt--;
3431 }
3432
Paul Bakker915275b2012-09-28 07:10:55 +00003433 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003434 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003435 {
3436 trust_ca = trust_ca->next;
3437 continue;
3438 }
3439
Paul Bakkerc70b9822013-04-07 22:00:46 +02003440 md_info = md_info_from_type( child->sig_md );
3441 if( md_info == NULL )
3442 {
3443 /*
3444 * Cannot check 'unknown' hash
3445 */
3446 continue;
3447 }
Paul Bakker915275b2012-09-28 07:10:55 +00003448
Paul Bakkerc70b9822013-04-07 22:00:46 +02003449 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003450
Paul Bakkerc70b9822013-04-07 22:00:46 +02003451 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003452 0, hash, child->sig.p ) != 0 )
3453 {
3454 trust_ca = trust_ca->next;
3455 continue;
3456 }
3457
3458 /*
3459 * Top of chain is signed by a trusted CA
3460 */
3461 *flags &= ~BADCERT_NOT_TRUSTED;
3462 break;
3463 }
3464
Paul Bakker9a736322012-11-14 12:39:52 +00003465 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003466 * If top of chain is not the same as the trusted CA send a verify request
3467 * to the callback for any issues with validity and CRL presence for the
3468 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003469 */
3470 if( trust_ca != NULL &&
3471 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3472 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3473 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003474 {
3475 /* Check trusted CA's CRL for then chain's top crt */
3476 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3477
3478 if( x509parse_time_expired( &trust_ca->valid_to ) )
3479 ca_flags |= BADCERT_EXPIRED;
3480
Paul Bakker915275b2012-09-28 07:10:55 +00003481 if( NULL != f_vrfy )
3482 {
Paul Bakker9a736322012-11-14 12:39:52 +00003483 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003484 return( ret );
3485 }
3486 }
3487
3488 /* Call callback on top cert */
3489 if( NULL != f_vrfy )
3490 {
Paul Bakker9a736322012-11-14 12:39:52 +00003491 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003492 return( ret );
3493 }
3494
Paul Bakker915275b2012-09-28 07:10:55 +00003495 *flags |= ca_flags;
3496
3497 return( 0 );
3498}
3499
3500static int x509parse_verify_child(
3501 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003502 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003503 int (*f_vrfy)(void *, x509_cert *, int, int *),
3504 void *p_vrfy )
3505{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003506 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003507 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003508 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003509 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003510 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003511
3512 if( x509parse_time_expired( &child->valid_to ) )
3513 *flags |= BADCERT_EXPIRED;
3514
Paul Bakkerc70b9822013-04-07 22:00:46 +02003515 md_info = md_info_from_type( child->sig_md );
3516 if( md_info == NULL )
3517 {
3518 /*
3519 * Cannot check 'unknown' hash
3520 */
Paul Bakker915275b2012-09-28 07:10:55 +00003521 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003522 }
3523 else
3524 {
3525 md( md_info, child->tbs.p, child->tbs.len, hash );
3526
3527 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3528 child->sig.p ) != 0 )
3529 *flags |= BADCERT_NOT_TRUSTED;
3530 }
3531
Paul Bakker915275b2012-09-28 07:10:55 +00003532 /* Check trusted CA's CRL for the given crt */
3533 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3534
3535 grandparent = parent->next;
3536
3537 while( grandparent != NULL )
3538 {
3539 if( grandparent->version == 0 ||
3540 grandparent->ca_istrue == 0 ||
3541 parent->issuer_raw.len != grandparent->subject_raw.len ||
3542 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3543 parent->issuer_raw.len ) != 0 )
3544 {
3545 grandparent = grandparent->next;
3546 continue;
3547 }
3548 break;
3549 }
3550
Paul Bakker915275b2012-09-28 07:10:55 +00003551 if( grandparent != NULL )
3552 {
3553 /*
3554 * Part of the chain
3555 */
Paul Bakker9a736322012-11-14 12:39:52 +00003556 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 +00003557 if( ret != 0 )
3558 return( ret );
3559 }
3560 else
3561 {
Paul Bakker9a736322012-11-14 12:39:52 +00003562 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 +00003563 if( ret != 0 )
3564 return( ret );
3565 }
3566
3567 /* child is verified to be a child of the parent, call verify callback */
3568 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003569 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003570 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003571
3572 *flags |= parent_flags;
3573
3574 return( 0 );
3575}
3576
Paul Bakker76fd75a2011-01-16 21:12:10 +00003577/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 * Verify the certificate validity
3579 */
3580int x509parse_verify( x509_cert *crt,
3581 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003582 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003583 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003584 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003585 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003586{
Paul Bakker23986e52011-04-24 08:57:21 +00003587 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003588 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003589 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003590 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003592 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003593
Paul Bakker40ea7de2009-05-03 10:18:48 +00003594 *flags = 0;
3595
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 if( cn != NULL )
3597 {
3598 name = &crt->subject;
3599 cn_len = strlen( cn );
3600
Paul Bakker4d2c1242012-05-10 14:12:46 +00003601 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003603 cur = &crt->subject_alt_names;
3604
3605 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003606 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003607 if( cur->buf.len == cn_len &&
3608 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003609 break;
3610
Paul Bakker535e97d2012-08-23 10:49:55 +00003611 if( cur->buf.len > 2 &&
3612 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003613 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003614 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003615
Paul Bakker4d2c1242012-05-10 14:12:46 +00003616 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003617 }
3618
3619 if( cur == NULL )
3620 *flags |= BADCERT_CN_MISMATCH;
3621 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003622 else
3623 {
3624 while( name != NULL )
3625 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003626 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003627 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003628 if( name->val.len == cn_len &&
3629 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003630 break;
3631
Paul Bakker535e97d2012-08-23 10:49:55 +00003632 if( name->val.len > 2 &&
3633 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003634 x509_wildcard_verify( cn, &name->val ) )
3635 break;
3636 }
3637
3638 name = name->next;
3639 }
3640
3641 if( name == NULL )
3642 *flags |= BADCERT_CN_MISMATCH;
3643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 }
3645
Paul Bakker5121ce52009-01-03 21:22:43 +00003646 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003647 * Iterate upwards in the given cert chain, to find our crt parent.
3648 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003650 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003651
Paul Bakker76fd75a2011-01-16 21:12:10 +00003652 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003654 if( parent->ca_istrue == 0 ||
3655 crt->issuer_raw.len != parent->subject_raw.len ||
3656 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 crt->issuer_raw.len ) != 0 )
3658 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003659 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 continue;
3661 }
Paul Bakker915275b2012-09-28 07:10:55 +00003662 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003663 }
3664
Paul Bakker915275b2012-09-28 07:10:55 +00003665 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 {
Paul Bakker915275b2012-09-28 07:10:55 +00003667 /*
3668 * Part of the chain
3669 */
Paul Bakker9a736322012-11-14 12:39:52 +00003670 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003671 if( ret != 0 )
3672 return( ret );
3673 }
3674 else
Paul Bakker74111d32011-01-15 16:57:55 +00003675 {
Paul Bakker9a736322012-11-14 12:39:52 +00003676 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003677 if( ret != 0 )
3678 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003679 }
Paul Bakker915275b2012-09-28 07:10:55 +00003680
3681 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003682 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003683
Paul Bakker5121ce52009-01-03 21:22:43 +00003684 return( 0 );
3685}
3686
3687/*
3688 * Unallocate all certificate data
3689 */
3690void x509_free( x509_cert *crt )
3691{
3692 x509_cert *cert_cur = crt;
3693 x509_cert *cert_prv;
3694 x509_name *name_cur;
3695 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003696 x509_sequence *seq_cur;
3697 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003698
3699 if( crt == NULL )
3700 return;
3701
3702 do
3703 {
3704 rsa_free( &cert_cur->rsa );
3705
3706 name_cur = cert_cur->issuer.next;
3707 while( name_cur != NULL )
3708 {
3709 name_prv = name_cur;
3710 name_cur = name_cur->next;
3711 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003712 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003713 }
3714
3715 name_cur = cert_cur->subject.next;
3716 while( name_cur != NULL )
3717 {
3718 name_prv = name_cur;
3719 name_cur = name_cur->next;
3720 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003721 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722 }
3723
Paul Bakker74111d32011-01-15 16:57:55 +00003724 seq_cur = cert_cur->ext_key_usage.next;
3725 while( seq_cur != NULL )
3726 {
3727 seq_prv = seq_cur;
3728 seq_cur = seq_cur->next;
3729 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003730 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003731 }
3732
Paul Bakker8afa70d2012-02-11 18:42:45 +00003733 seq_cur = cert_cur->subject_alt_names.next;
3734 while( seq_cur != NULL )
3735 {
3736 seq_prv = seq_cur;
3737 seq_cur = seq_cur->next;
3738 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003739 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003740 }
3741
Paul Bakker5121ce52009-01-03 21:22:43 +00003742 if( cert_cur->raw.p != NULL )
3743 {
3744 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003745 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 }
3747
3748 cert_cur = cert_cur->next;
3749 }
3750 while( cert_cur != NULL );
3751
3752 cert_cur = crt;
3753 do
3754 {
3755 cert_prv = cert_cur;
3756 cert_cur = cert_cur->next;
3757
3758 memset( cert_prv, 0, sizeof( x509_cert ) );
3759 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003760 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003761 }
3762 while( cert_cur != NULL );
3763}
3764
Paul Bakkerd98030e2009-05-02 15:13:40 +00003765/*
3766 * Unallocate all CRL data
3767 */
3768void x509_crl_free( x509_crl *crl )
3769{
3770 x509_crl *crl_cur = crl;
3771 x509_crl *crl_prv;
3772 x509_name *name_cur;
3773 x509_name *name_prv;
3774 x509_crl_entry *entry_cur;
3775 x509_crl_entry *entry_prv;
3776
3777 if( crl == NULL )
3778 return;
3779
3780 do
3781 {
3782 name_cur = crl_cur->issuer.next;
3783 while( name_cur != NULL )
3784 {
3785 name_prv = name_cur;
3786 name_cur = name_cur->next;
3787 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003788 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003789 }
3790
3791 entry_cur = crl_cur->entry.next;
3792 while( entry_cur != NULL )
3793 {
3794 entry_prv = entry_cur;
3795 entry_cur = entry_cur->next;
3796 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003797 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003798 }
3799
3800 if( crl_cur->raw.p != NULL )
3801 {
3802 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003803 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003804 }
3805
3806 crl_cur = crl_cur->next;
3807 }
3808 while( crl_cur != NULL );
3809
3810 crl_cur = crl;
3811 do
3812 {
3813 crl_prv = crl_cur;
3814 crl_cur = crl_cur->next;
3815
3816 memset( crl_prv, 0, sizeof( x509_crl ) );
3817 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003818 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003819 }
3820 while( crl_cur != NULL );
3821}
3822
Paul Bakker40e46942009-01-03 21:51:57 +00003823#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003824
Paul Bakker40e46942009-01-03 21:51:57 +00003825#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003826
3827/*
3828 * Checkup routine
3829 */
3830int x509_self_test( int verbose )
3831{
Paul Bakker5690efc2011-05-26 13:16:06 +00003832#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003833 int ret;
3834 int flags;
3835 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003836 x509_cert cacert;
3837 x509_cert clicert;
3838 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003839#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003840 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
3843 if( verbose != 0 )
3844 printf( " X.509 certificate load: " );
3845
3846 memset( &clicert, 0, sizeof( x509_cert ) );
3847
Paul Bakker3c2122f2013-06-24 19:03:14 +02003848 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003849 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 if( ret != 0 )
3851 {
3852 if( verbose != 0 )
3853 printf( "failed\n" );
3854
3855 return( ret );
3856 }
3857
3858 memset( &cacert, 0, sizeof( x509_cert ) );
3859
Paul Bakker3c2122f2013-06-24 19:03:14 +02003860 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003861 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862 if( ret != 0 )
3863 {
3864 if( verbose != 0 )
3865 printf( "failed\n" );
3866
3867 return( ret );
3868 }
3869
3870 if( verbose != 0 )
3871 printf( "passed\n X.509 private key load: " );
3872
3873 i = strlen( test_ca_key );
3874 j = strlen( test_ca_pwd );
3875
Paul Bakker66b78b22011-03-25 14:22:50 +00003876 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3877
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003878 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003879 (const unsigned char *) test_ca_key, i,
3880 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003881 {
3882 if( verbose != 0 )
3883 printf( "failed\n" );
3884
3885 return( ret );
3886 }
3887
3888 if( verbose != 0 )
3889 printf( "passed\n X.509 signature verify: ");
3890
Paul Bakker23986e52011-04-24 08:57:21 +00003891 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003892 if( ret != 0 )
3893 {
Paul Bakker23986e52011-04-24 08:57:21 +00003894 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003895 if( verbose != 0 )
3896 printf( "failed\n" );
3897
3898 return( ret );
3899 }
3900
Paul Bakker5690efc2011-05-26 13:16:06 +00003901#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003903 printf( "passed\n X.509 DHM parameter load: " );
3904
3905 i = strlen( test_dhm_params );
3906 j = strlen( test_ca_pwd );
3907
Paul Bakker3c2122f2013-06-24 19:03:14 +02003908 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003909 {
3910 if( verbose != 0 )
3911 printf( "failed\n" );
3912
3913 return( ret );
3914 }
3915
3916 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003917 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003918#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003919
3920 x509_free( &cacert );
3921 x509_free( &clicert );
3922 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003923#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003924 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003925#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003926
3927 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003928#else
3929 ((void) verbose);
3930 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3931#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003932}
3933
3934#endif
3935
3936#endif