blob: bda81117f707045d6b48c931ff635822701e7e19 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerefc30292011-11-10 14:43:23 +00004 * Copyright (C) 2006-2011, 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 Bakker40e46942009-01-03 21:51:57 +000045#include "polarssl/des.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010046#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000047#include "polarssl/md2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010048#endif
49#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000050#include "polarssl/md4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010051#endif
52#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000053#include "polarssl/md5.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010054#endif
55#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000056#include "polarssl/sha1.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010057#endif
58#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000059#include "polarssl/sha2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010060#endif
61#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000062#include "polarssl/sha4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010063#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000064#include "polarssl/dhm.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020065#include "polarssl/pkcs12.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67#include <string.h>
68#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000069#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000070#include <windows.h>
71#else
Paul Bakker5121ce52009-01-03 21:22:43 +000072#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Paul Bakker335db3f2011-04-25 15:28:35 +000075#if defined(POLARSSL_FS_IO)
76#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000077#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000078#include <sys/types.h>
79#include <dirent.h>
80#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000081#endif
82
Paul Bakker5121ce52009-01-03 21:22:43 +000083/*
Paul Bakker5121ce52009-01-03 21:22:43 +000084 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
85 */
86static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000087 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000088 int *ver )
89{
Paul Bakker23986e52011-04-24 08:57:21 +000090 int ret;
91 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93 if( ( ret = asn1_get_tag( p, end, &len,
94 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
95 {
Paul Bakker40e46942009-01-03 21:51:57 +000096 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000097 {
98 *ver = 0;
99 return( 0 );
100 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102 return( ret );
103 }
104
105 end = *p + len;
106
107 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000108 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000111 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000112 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114 return( 0 );
115}
116
117/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000118 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000119 */
120static int x509_crl_get_version( unsigned char **p,
121 const unsigned char *end,
122 int *ver )
123{
124 int ret;
125
126 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
127 {
128 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000129 {
130 *ver = 0;
131 return( 0 );
132 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000133
134 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
135 }
136
137 return( 0 );
138}
139
140/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 * CertificateSerialNumber ::= INTEGER
142 */
143static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000144 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 x509_buf *serial )
146{
147 int ret;
148
149 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000150 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000151 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
154 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000156 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158 serial->tag = *(*p)++;
159
160 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000161 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 serial->p = *p;
164 *p += serial->len;
165
166 return( 0 );
167}
168
169/*
170 * AlgorithmIdentifier ::= SEQUENCE {
171 * algorithm OBJECT IDENTIFIER,
172 * parameters ANY DEFINED BY algorithm OPTIONAL }
173 */
174static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000175 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 x509_buf *alg )
177{
Paul Bakker23986e52011-04-24 08:57:21 +0000178 int ret;
179 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181 if( ( ret = asn1_get_tag( p, end, &len,
182 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000183 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 end = *p + len;
186 alg->tag = **p;
187
188 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000189 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 alg->p = *p;
192 *p += alg->len;
193
194 if( *p == end )
195 return( 0 );
196
197 /*
198 * assume the algorithm parameters must be NULL
199 */
200 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000201 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000204 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000205 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207 return( 0 );
208}
209
210/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 * AttributeTypeAndValue ::= SEQUENCE {
212 * type AttributeType,
213 * value AttributeValue }
214 *
215 * AttributeType ::= OBJECT IDENTIFIER
216 *
217 * AttributeValue ::= ANY DEFINED BY AttributeType
218 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000219static int x509_get_attr_type_value( unsigned char **p,
220 const unsigned char *end,
221 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000222{
Paul Bakker23986e52011-04-24 08:57:21 +0000223 int ret;
224 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 x509_buf *oid;
226 x509_buf *val;
227
228 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000230 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 oid = &cur->oid;
233 oid->tag = **p;
234
235 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000236 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
238 oid->p = *p;
239 *p += oid->len;
240
241 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000242 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000243 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
246 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
247 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000248 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000249 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251 val = &cur->val;
252 val->tag = *(*p)++;
253
254 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000255 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257 val->p = *p;
258 *p += val->len;
259
260 cur->next = NULL;
261
Paul Bakker400ff6f2011-02-20 10:40:16 +0000262 return( 0 );
263}
264
265/*
266 * RelativeDistinguishedName ::=
267 * SET OF AttributeTypeAndValue
268 *
269 * AttributeTypeAndValue ::= SEQUENCE {
270 * type AttributeType,
271 * value AttributeValue }
272 *
273 * AttributeType ::= OBJECT IDENTIFIER
274 *
275 * AttributeValue ::= ANY DEFINED BY AttributeType
276 */
277static int x509_get_name( unsigned char **p,
278 const unsigned char *end,
279 x509_name *cur )
280{
Paul Bakker23986e52011-04-24 08:57:21 +0000281 int ret;
282 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000283 const unsigned char *end2;
284 x509_name *use;
285
286 if( ( ret = asn1_get_tag( p, end, &len,
287 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000288 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000289
290 end2 = end;
291 end = *p + len;
292 use = cur;
293
294 do
295 {
296 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
297 return( ret );
298
299 if( *p != end )
300 {
301 use->next = (x509_name *) malloc(
302 sizeof( x509_name ) );
303
304 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000305 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000306
307 memset( use->next, 0, sizeof( x509_name ) );
308
309 use = use->next;
310 }
311 }
312 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
314 /*
315 * recurse until end of SEQUENCE is reached
316 */
317 if( *p == end2 )
318 return( 0 );
319
320 cur->next = (x509_name *) malloc(
321 sizeof( x509_name ) );
322
323 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000324 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Paul Bakker430ffbe2012-05-01 08:14:20 +0000326 memset( cur->next, 0, sizeof( x509_name ) );
327
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 return( x509_get_name( p, end2, cur->next ) );
329}
330
331/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 * Time ::= CHOICE {
333 * utcTime UTCTime,
334 * generalTime GeneralizedTime }
335 */
Paul Bakker91200182010-02-18 21:26:15 +0000336static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000337 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338 x509_time *time )
339{
Paul Bakker23986e52011-04-24 08:57:21 +0000340 int ret;
341 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000342 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000343 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344
Paul Bakker91200182010-02-18 21:26:15 +0000345 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000346 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
347 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000348
Paul Bakker91200182010-02-18 21:26:15 +0000349 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000350
Paul Bakker91200182010-02-18 21:26:15 +0000351 if ( tag == ASN1_UTC_TIME )
352 {
353 (*p)++;
354 ret = asn1_get_len( p, end, &len );
355
356 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000357 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000358
Paul Bakker91200182010-02-18 21:26:15 +0000359 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000360 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
361 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000362
Paul Bakker91200182010-02-18 21:26:15 +0000363 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
364 &time->year, &time->mon, &time->day,
365 &time->hour, &time->min, &time->sec ) < 5 )
366 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000367
Paul Bakker400ff6f2011-02-20 10:40:16 +0000368 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000369 time->year += 1900;
370
371 *p += len;
372
373 return( 0 );
374 }
375 else if ( tag == ASN1_GENERALIZED_TIME )
376 {
377 (*p)++;
378 ret = asn1_get_len( p, end, &len );
379
380 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000381 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000382
383 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000384 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
385 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000386
387 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
388 &time->year, &time->mon, &time->day,
389 &time->hour, &time->min, &time->sec ) < 5 )
390 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
391
392 *p += len;
393
394 return( 0 );
395 }
396 else
Paul Bakker9d781402011-05-09 16:17:09 +0000397 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000398}
399
400
401/*
402 * Validity ::= SEQUENCE {
403 * notBefore Time,
404 * notAfter Time }
405 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000406static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000407 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 x509_time *from,
409 x509_time *to )
410{
Paul Bakker23986e52011-04-24 08:57:21 +0000411 int ret;
412 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414 if( ( ret = asn1_get_tag( p, end, &len,
415 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000416 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418 end = *p + len;
419
Paul Bakker91200182010-02-18 21:26:15 +0000420 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000421 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
Paul Bakker91200182010-02-18 21:26:15 +0000423 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000424 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000427 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000428 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
430 return( 0 );
431}
432
433/*
434 * SubjectPublicKeyInfo ::= SEQUENCE {
435 * algorithm AlgorithmIdentifier,
436 * subjectPublicKey BIT STRING }
437 */
438static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000439 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 x509_buf *pk_alg_oid,
441 mpi *N, mpi *E )
442{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200443 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000444 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 unsigned char *end2;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200446 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
448 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
449 return( ret );
450
451 /*
452 * only RSA public keys handled at this time
453 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200454 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000455 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000458 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
460 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000461 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000462 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464 end2 = *p + len;
465
466 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000467 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469 /*
470 * RSAPublicKey ::= SEQUENCE {
471 * modulus INTEGER, -- n
472 * publicExponent INTEGER -- e
473 * }
474 */
475 if( ( ret = asn1_get_tag( p, end2, &len,
476 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000477 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000480 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000481 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
484 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000485 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000488 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000489 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
491 return( 0 );
492}
493
494static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000495 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 x509_buf *sig )
497{
Paul Bakker23986e52011-04-24 08:57:21 +0000498 int ret;
499 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
Paul Bakker8afa70d2012-02-11 18:42:45 +0000501 if( ( end - *p ) < 1 )
502 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
503 POLARSSL_ERR_ASN1_OUT_OF_DATA );
504
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 sig->tag = **p;
506
507 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000508 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
Paul Bakker74111d32011-01-15 16:57:55 +0000510
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000512 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514 sig->len = len;
515 sig->p = *p;
516
517 *p += len;
518
519 return( 0 );
520}
521
522/*
523 * X.509 v2/v3 unique identifier (not parsed)
524 */
525static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000526 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 x509_buf *uid, int n )
528{
529 int ret;
530
531 if( *p == end )
532 return( 0 );
533
534 uid->tag = **p;
535
536 if( ( ret = asn1_get_tag( p, end, &uid->len,
537 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
538 {
Paul Bakker40e46942009-01-03 21:51:57 +0000539 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 return( 0 );
541
542 return( ret );
543 }
544
545 uid->p = *p;
546 *p += uid->len;
547
548 return( 0 );
549}
550
551/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000552 * X.509 Extensions (No parsing of extensions, pointer should
553 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 */
555static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000556 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000557 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558{
Paul Bakker23986e52011-04-24 08:57:21 +0000559 int ret;
560 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
562 if( *p == end )
563 return( 0 );
564
565 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000566
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000568 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571 ext->p = *p;
572 end = *p + ext->len;
573
574 /*
575 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
576 *
577 * Extension ::= SEQUENCE {
578 * extnID OBJECT IDENTIFIER,
579 * critical BOOLEAN DEFAULT FALSE,
580 * extnValue OCTET STRING }
581 */
582 if( ( ret = asn1_get_tag( p, end, &len,
583 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000584 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000587 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000588 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Paul Bakkerd98030e2009-05-02 15:13:40 +0000590 return( 0 );
591}
592
593/*
594 * X.509 CRL v2 extensions (no extensions parsed yet.)
595 */
596static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000597 const unsigned char *end,
598 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000599{
Paul Bakker23986e52011-04-24 08:57:21 +0000600 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000601 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000602
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000603 /* Get explicit tag */
604 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000605 {
606 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
607 return( 0 );
608
609 return( ret );
610 }
611
612 while( *p < end )
613 {
614 if( ( ret = asn1_get_tag( p, end, &len,
615 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000616 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000617
618 *p += len;
619 }
620
621 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000622 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000623 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
624
625 return( 0 );
626}
627
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000628/*
629 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
630 */
631static int x509_get_crl_entry_ext( unsigned char **p,
632 const unsigned char *end,
633 x509_buf *ext )
634{
635 int ret;
636 size_t len = 0;
637
638 /* OPTIONAL */
639 if (end <= *p)
640 return( 0 );
641
642 ext->tag = **p;
643 ext->p = *p;
644
645 /*
646 * Get CRL-entry extension sequence header
647 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
648 */
649 if( ( ret = asn1_get_tag( p, end, &ext->len,
650 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
651 {
652 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
653 {
654 ext->p = NULL;
655 return( 0 );
656 }
657 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
658 }
659
660 end = *p + ext->len;
661
662 if( end != *p + ext->len )
663 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
664 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
665
666 while( *p < end )
667 {
668 if( ( ret = asn1_get_tag( p, end, &len,
669 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
670 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
671
672 *p += len;
673 }
674
675 if( *p != end )
676 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
677 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
678
679 return( 0 );
680}
681
Paul Bakker74111d32011-01-15 16:57:55 +0000682static int x509_get_basic_constraints( unsigned char **p,
683 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000684 int *ca_istrue,
685 int *max_pathlen )
686{
Paul Bakker23986e52011-04-24 08:57:21 +0000687 int ret;
688 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000689
690 /*
691 * BasicConstraints ::= SEQUENCE {
692 * cA BOOLEAN DEFAULT FALSE,
693 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
694 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000695 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000696 *max_pathlen = 0; /* endless */
697
698 if( ( ret = asn1_get_tag( p, end, &len,
699 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000700 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000701
702 if( *p == end )
703 return 0;
704
Paul Bakker3cccddb2011-01-16 21:46:31 +0000705 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000706 {
707 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000708 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000709
710 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000711 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000712
Paul Bakker3cccddb2011-01-16 21:46:31 +0000713 if( *ca_istrue != 0 )
714 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000715 }
716
717 if( *p == end )
718 return 0;
719
720 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000721 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000722
723 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000724 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000725 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
726
727 (*max_pathlen)++;
728
Paul Bakker74111d32011-01-15 16:57:55 +0000729 return 0;
730}
731
732static int x509_get_ns_cert_type( unsigned char **p,
733 const unsigned char *end,
734 unsigned char *ns_cert_type)
735{
736 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000737 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000738
739 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000740 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000741
742 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000743 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000744 POLARSSL_ERR_ASN1_INVALID_LENGTH );
745
746 /* Get actual bitstring */
747 *ns_cert_type = *bs.p;
748 return 0;
749}
750
751static int x509_get_key_usage( unsigned char **p,
752 const unsigned char *end,
753 unsigned char *key_usage)
754{
755 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000756 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000757
758 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000759 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000760
Paul Bakker94a67962012-08-23 13:03:52 +0000761 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000762 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000763 POLARSSL_ERR_ASN1_INVALID_LENGTH );
764
765 /* Get actual bitstring */
766 *key_usage = *bs.p;
767 return 0;
768}
769
Paul Bakkerd98030e2009-05-02 15:13:40 +0000770/*
Paul Bakker74111d32011-01-15 16:57:55 +0000771 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
772 *
773 * KeyPurposeId ::= OBJECT IDENTIFIER
774 */
775static int x509_get_ext_key_usage( unsigned char **p,
776 const unsigned char *end,
777 x509_sequence *ext_key_usage)
778{
779 int ret;
780
781 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000782 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000783
784 /* Sequence length must be >= 1 */
785 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000786 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000787 POLARSSL_ERR_ASN1_INVALID_LENGTH );
788
789 return 0;
790}
791
792/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000793 * SubjectAltName ::= GeneralNames
794 *
795 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
796 *
797 * GeneralName ::= CHOICE {
798 * otherName [0] OtherName,
799 * rfc822Name [1] IA5String,
800 * dNSName [2] IA5String,
801 * x400Address [3] ORAddress,
802 * directoryName [4] Name,
803 * ediPartyName [5] EDIPartyName,
804 * uniformResourceIdentifier [6] IA5String,
805 * iPAddress [7] OCTET STRING,
806 * registeredID [8] OBJECT IDENTIFIER }
807 *
808 * OtherName ::= SEQUENCE {
809 * type-id OBJECT IDENTIFIER,
810 * value [0] EXPLICIT ANY DEFINED BY type-id }
811 *
812 * EDIPartyName ::= SEQUENCE {
813 * nameAssigner [0] DirectoryString OPTIONAL,
814 * partyName [1] DirectoryString }
815 *
816 * NOTE: PolarSSL only parses and uses dNSName at this point.
817 */
818static int x509_get_subject_alt_name( unsigned char **p,
819 const unsigned char *end,
820 x509_sequence *subject_alt_name )
821{
822 int ret;
823 size_t len, tag_len;
824 asn1_buf *buf;
825 unsigned char tag;
826 asn1_sequence *cur = subject_alt_name;
827
828 /* Get main sequence tag */
829 if( ( ret = asn1_get_tag( p, end, &len,
830 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
831 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
832
833 if( *p + len != end )
834 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
835 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
836
837 while( *p < end )
838 {
839 if( ( end - *p ) < 1 )
840 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
841 POLARSSL_ERR_ASN1_OUT_OF_DATA );
842
843 tag = **p;
844 (*p)++;
845 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
847
848 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
849 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
850 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
851
852 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
853 {
854 *p += tag_len;
855 continue;
856 }
857
858 buf = &(cur->buf);
859 buf->tag = tag;
860 buf->p = *p;
861 buf->len = tag_len;
862 *p += buf->len;
863
864 /* Allocate and assign next pointer */
865 if (*p < end)
866 {
867 cur->next = (asn1_sequence *) malloc(
868 sizeof( asn1_sequence ) );
869
870 if( cur->next == NULL )
871 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
872 POLARSSL_ERR_ASN1_MALLOC_FAILED );
873
Paul Bakker535e97d2012-08-23 10:49:55 +0000874 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000875 cur = cur->next;
876 }
877 }
878
879 /* Set final sequence entry's next pointer to NULL */
880 cur->next = NULL;
881
882 if( *p != end )
883 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
884 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
885
886 return( 0 );
887}
888
889/*
Paul Bakker74111d32011-01-15 16:57:55 +0000890 * X.509 v3 extensions
891 *
892 * TODO: Perform all of the basic constraints tests required by the RFC
893 * TODO: Set values for undetected extensions to a sane default?
894 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000895 */
896static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000897 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000898 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000899{
Paul Bakker23986e52011-04-24 08:57:21 +0000900 int ret;
901 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000902 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000903
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000904 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000905 {
906 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
907 return( 0 );
908
909 return( ret );
910 }
911
Paul Bakker5121ce52009-01-03 21:22:43 +0000912 while( *p < end )
913 {
Paul Bakker74111d32011-01-15 16:57:55 +0000914 /*
915 * Extension ::= SEQUENCE {
916 * extnID OBJECT IDENTIFIER,
917 * critical BOOLEAN DEFAULT FALSE,
918 * extnValue OCTET STRING }
919 */
920 x509_buf extn_oid = {0, 0, NULL};
921 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200922 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000923
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 if( ( ret = asn1_get_tag( p, end, &len,
925 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000926 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000928 end_ext_data = *p + len;
929
Paul Bakker74111d32011-01-15 16:57:55 +0000930 /* Get extension ID */
931 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Paul Bakker74111d32011-01-15 16:57:55 +0000933 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000934 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Paul Bakker74111d32011-01-15 16:57:55 +0000936 extn_oid.p = *p;
937 *p += extn_oid.len;
938
939 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000940 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000941 POLARSSL_ERR_ASN1_OUT_OF_DATA );
942
943 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000944 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000945 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000946 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947
Paul Bakker74111d32011-01-15 16:57:55 +0000948 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000949 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000951 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000953 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000954
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000955 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000956 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000957 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Paul Bakker74111d32011-01-15 16:57:55 +0000959 /*
960 * Detect supported extensions
961 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200962 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
963
964 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000965 {
966 /* No parser found, skip extension */
967 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Paul Bakker5c721f92011-07-27 16:51:09 +0000969#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000970 if( is_critical )
971 {
972 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000973 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000974 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
975 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000976#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +0200977 continue;
978 }
979
980 crt->ext_types |= ext_type;
981
982 switch( ext_type )
983 {
984 case EXT_BASIC_CONSTRAINTS:
985 /* Parse basic constraints */
986 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
987 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
988 return ( ret );
989 break;
990
991 case EXT_KEY_USAGE:
992 /* Parse key usage */
993 if( ( ret = x509_get_key_usage( p, end_ext_octet,
994 &crt->key_usage ) ) != 0 )
995 return ( ret );
996 break;
997
998 case EXT_EXTENDED_KEY_USAGE:
999 /* Parse extended key usage */
1000 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1001 &crt->ext_key_usage ) ) != 0 )
1002 return ( ret );
1003 break;
1004
1005 case EXT_SUBJECT_ALT_NAME:
1006 /* Parse subject alt name */
1007 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1008 &crt->subject_alt_names ) ) != 0 )
1009 return ( ret );
1010 break;
1011
1012 case EXT_NS_CERT_TYPE:
1013 /* Parse netscape certificate type */
1014 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1015 &crt->ns_cert_type ) ) != 0 )
1016 return ( ret );
1017 break;
1018
1019 default:
1020 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001021 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 }
1023
1024 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001025 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001026 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 return( 0 );
1029}
1030
1031/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001032 * X.509 CRL Entries
1033 */
1034static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001035 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001036 x509_crl_entry *entry )
1037{
Paul Bakker23986e52011-04-24 08:57:21 +00001038 int ret;
1039 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001040 x509_crl_entry *cur_entry = entry;
1041
1042 if( *p == end )
1043 return( 0 );
1044
Paul Bakker9be19372009-07-27 20:21:53 +00001045 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001046 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1047 {
1048 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1049 return( 0 );
1050
1051 return( ret );
1052 }
1053
Paul Bakker9be19372009-07-27 20:21:53 +00001054 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001055
1056 while( *p < end )
1057 {
Paul Bakker23986e52011-04-24 08:57:21 +00001058 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001059 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001060
1061 if( ( ret = asn1_get_tag( p, end, &len2,
1062 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1063 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001064 return( ret );
1065 }
1066
Paul Bakker9be19372009-07-27 20:21:53 +00001067 cur_entry->raw.tag = **p;
1068 cur_entry->raw.p = *p;
1069 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001070 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001071
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001072 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001073 return( ret );
1074
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001075 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001076 return( ret );
1077
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001078 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001079 return( ret );
1080
Paul Bakker74111d32011-01-15 16:57:55 +00001081 if ( *p < end )
1082 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001083 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001084
1085 if( cur_entry->next == NULL )
1086 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1087
Paul Bakkerd98030e2009-05-02 15:13:40 +00001088 cur_entry = cur_entry->next;
1089 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1090 }
1091 }
1092
1093 return( 0 );
1094}
1095
Paul Bakkerc70b9822013-04-07 22:00:46 +02001096static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1097 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001098{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001099 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001100
Paul Bakkerc70b9822013-04-07 22:00:46 +02001101 if( ret != 0 )
1102 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001103
Paul Bakkerc70b9822013-04-07 22:00:46 +02001104 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001105}
1106
Paul Bakkerd98030e2009-05-02 15:13:40 +00001107/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001108 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001110int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001111{
Paul Bakker23986e52011-04-24 08:57:21 +00001112 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001113 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001114 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Paul Bakker320a4b52009-03-28 18:52:39 +00001116 /*
1117 * Check for valid input
1118 */
1119 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001120 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001121
Paul Bakker96743fc2011-02-12 14:30:57 +00001122 p = (unsigned char *) malloc( len = buflen );
1123
1124 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001125 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001126
1127 memcpy( p, buf, buflen );
1128
1129 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
1131 crt->raw.p = p;
1132 crt->raw.len = len;
1133 end = p + len;
1134
1135 /*
1136 * Certificate ::= SEQUENCE {
1137 * tbsCertificate TBSCertificate,
1138 * signatureAlgorithm AlgorithmIdentifier,
1139 * signatureValue BIT STRING }
1140 */
1141 if( ( ret = asn1_get_tag( &p, end, &len,
1142 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1143 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001144 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001145 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 }
1147
Paul Bakkerb00ca422012-09-25 12:10:00 +00001148 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001150 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001151 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001152 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001154 crt_end = p + len;
1155
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 /*
1157 * TBSCertificate ::= SEQUENCE {
1158 */
1159 crt->tbs.p = p;
1160
1161 if( ( ret = asn1_get_tag( &p, end, &len,
1162 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1163 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001164 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001165 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 }
1167
1168 end = p + len;
1169 crt->tbs.len = end - crt->tbs.p;
1170
1171 /*
1172 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1173 *
1174 * CertificateSerialNumber ::= INTEGER
1175 *
1176 * signature AlgorithmIdentifier
1177 */
1178 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1179 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1180 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1181 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001182 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 return( ret );
1184 }
1185
1186 crt->version++;
1187
1188 if( crt->version > 3 )
1189 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001190 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001191 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 }
1193
Paul Bakkerc70b9822013-04-07 22:00:46 +02001194 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1195 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001197 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001198 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
1200
1201 /*
1202 * issuer Name
1203 */
1204 crt->issuer_raw.p = p;
1205
1206 if( ( ret = asn1_get_tag( &p, end, &len,
1207 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1208 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001209 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001210 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 }
1212
1213 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1214 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001215 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 return( ret );
1217 }
1218
1219 crt->issuer_raw.len = p - crt->issuer_raw.p;
1220
1221 /*
1222 * Validity ::= SEQUENCE {
1223 * notBefore Time,
1224 * notAfter Time }
1225 *
1226 */
1227 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1228 &crt->valid_to ) ) != 0 )
1229 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001230 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 return( ret );
1232 }
1233
1234 /*
1235 * subject Name
1236 */
1237 crt->subject_raw.p = p;
1238
1239 if( ( ret = asn1_get_tag( &p, end, &len,
1240 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1241 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001242 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001243 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
1245
Paul Bakkercefb3962012-06-27 11:51:09 +00001246 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001248 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 return( ret );
1250 }
1251
1252 crt->subject_raw.len = p - crt->subject_raw.p;
1253
1254 /*
1255 * SubjectPublicKeyInfo ::= SEQUENCE
1256 * algorithm AlgorithmIdentifier,
1257 * subjectPublicKey BIT STRING }
1258 */
1259 if( ( ret = asn1_get_tag( &p, end, &len,
1260 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1261 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001262 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001263 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 }
1265
1266 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1267 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1268 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001269 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 return( ret );
1271 }
1272
1273 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1274 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001275 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 return( ret );
1277 }
1278
1279 crt->rsa.len = mpi_size( &crt->rsa.N );
1280
1281 /*
1282 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1283 * -- If present, version shall be v2 or v3
1284 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1285 * -- If present, version shall be v2 or v3
1286 * extensions [3] EXPLICIT Extensions OPTIONAL
1287 * -- If present, version shall be v3
1288 */
1289 if( crt->version == 2 || crt->version == 3 )
1290 {
1291 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1292 if( ret != 0 )
1293 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001294 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 return( ret );
1296 }
1297 }
1298
1299 if( crt->version == 2 || crt->version == 3 )
1300 {
1301 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1302 if( ret != 0 )
1303 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001304 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 return( ret );
1306 }
1307 }
1308
1309 if( crt->version == 3 )
1310 {
Paul Bakker74111d32011-01-15 16:57:55 +00001311 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 if( ret != 0 )
1313 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001314 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 return( ret );
1316 }
1317 }
1318
1319 if( p != end )
1320 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001321 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001322 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001323 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 }
1325
Paul Bakkerb00ca422012-09-25 12:10:00 +00001326 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
1328 /*
1329 * signatureAlgorithm AlgorithmIdentifier,
1330 * signatureValue BIT STRING
1331 */
1332 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1333 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001334 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 return( ret );
1336 }
1337
Paul Bakker535e97d2012-08-23 10:49:55 +00001338 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1339 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001341 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001342 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 }
1344
1345 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1346 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001347 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 return( ret );
1349 }
1350
1351 if( p != end )
1352 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001353 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001354 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001355 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 }
1357
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001358 return( 0 );
1359}
1360
1361/*
1362 * Parse one or more PEM certificates from a buffer and add them to the chained list
1363 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001364int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001365{
Paul Bakker69e095c2011-12-10 21:55:01 +00001366 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001367 x509_cert *crt, *prev = NULL;
1368 int buf_format = X509_FORMAT_DER;
1369
1370 crt = chain;
1371
1372 /*
1373 * Check for valid input
1374 */
1375 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001376 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001377
1378 while( crt->version != 0 && crt->next != NULL )
1379 {
1380 prev = crt;
1381 crt = crt->next;
1382 }
1383
1384 /*
1385 * Add new certificate on the end of the chain if needed.
1386 */
1387 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001388 {
1389 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1390
Paul Bakker7d06ad22009-05-02 15:53:56 +00001391 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001392 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001393
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001394 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001395 crt = crt->next;
1396 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001399 /*
1400 * Determine buffer content. Buffer contains either one DER certificate or
1401 * one or more PEM certificates.
1402 */
1403#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001404 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001405 buf_format = X509_FORMAT_PEM;
1406#endif
1407
1408 if( buf_format == X509_FORMAT_DER )
1409 return x509parse_crt_der( crt, buf, buflen );
1410
1411#if defined(POLARSSL_PEM_C)
1412 if( buf_format == X509_FORMAT_PEM )
1413 {
1414 pem_context pem;
1415
1416 while( buflen > 0 )
1417 {
1418 size_t use_len;
1419 pem_init( &pem );
1420
1421 ret = pem_read_buffer( &pem,
1422 "-----BEGIN CERTIFICATE-----",
1423 "-----END CERTIFICATE-----",
1424 buf, NULL, 0, &use_len );
1425
1426 if( ret == 0 )
1427 {
1428 /*
1429 * Was PEM encoded
1430 */
1431 buflen -= use_len;
1432 buf += use_len;
1433 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001434 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1435 {
1436 return( ret );
1437 }
Paul Bakker00b28602013-06-24 13:02:41 +02001438 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001439 {
1440 pem_free( &pem );
1441
Paul Bakker5ed3b342013-06-24 19:05:46 +02001442 /*
1443 * PEM header and footer were found
1444 */
1445 buflen -= use_len;
1446 buf += use_len;
1447
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001448 if( first_error == 0 )
1449 first_error = ret;
1450
1451 continue;
1452 }
1453 else
1454 break;
1455
1456 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1457
1458 pem_free( &pem );
1459
1460 if( ret != 0 )
1461 {
1462 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001463 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001464 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001465 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001466 {
1467 if( prev )
1468 prev->next = NULL;
1469
1470 if( crt != chain )
1471 free( crt );
1472
1473 return( ret );
1474 }
1475
1476 if( first_error == 0 )
1477 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001478
1479 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001480
1481 memset( crt, 0, sizeof( x509_cert ) );
1482 continue;
1483 }
1484
1485 success = 1;
1486
1487 /*
1488 * Add new certificate to the list
1489 */
1490 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1491
1492 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001493 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001494
1495 prev = crt;
1496 crt = crt->next;
1497 memset( crt, 0, sizeof( x509_cert ) );
1498 }
1499 }
1500#endif
1501
1502 if( crt->version == 0 )
1503 {
1504 if( prev )
1505 prev->next = NULL;
1506
1507 if( crt != chain )
1508 free( crt );
1509 }
1510
1511 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001512 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001513 else if( first_error )
1514 return( first_error );
1515 else
1516 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001517}
1518
1519/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001520 * Parse one or more CRLs and add them to the chained list
1521 */
Paul Bakker23986e52011-04-24 08:57:21 +00001522int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001523{
Paul Bakker23986e52011-04-24 08:57:21 +00001524 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001525 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001526 unsigned char *p, *end;
1527 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001528#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001529 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001530 pem_context pem;
1531#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001532
1533 crl = chain;
1534
1535 /*
1536 * Check for valid input
1537 */
1538 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001539 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001540
1541 while( crl->version != 0 && crl->next != NULL )
1542 crl = crl->next;
1543
1544 /*
1545 * Add new CRL on the end of the chain if needed.
1546 */
1547 if ( crl->version != 0 && crl->next == NULL)
1548 {
1549 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1550
Paul Bakker7d06ad22009-05-02 15:53:56 +00001551 if( crl->next == NULL )
1552 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001553 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001554 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001555 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001556
Paul Bakker7d06ad22009-05-02 15:53:56 +00001557 crl = crl->next;
1558 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001559 }
1560
Paul Bakker96743fc2011-02-12 14:30:57 +00001561#if defined(POLARSSL_PEM_C)
1562 pem_init( &pem );
1563 ret = pem_read_buffer( &pem,
1564 "-----BEGIN X509 CRL-----",
1565 "-----END X509 CRL-----",
1566 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001567
Paul Bakker96743fc2011-02-12 14:30:57 +00001568 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001569 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001570 /*
1571 * Was PEM encoded
1572 */
1573 buflen -= use_len;
1574 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001575
1576 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001577 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001578 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001579 p = pem.buf;
1580 pem.buf = NULL;
1581 len = pem.buflen;
1582 pem_free( &pem );
1583 }
Paul Bakker00b28602013-06-24 13:02:41 +02001584 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001585 {
1586 pem_free( &pem );
1587 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001588 }
1589 else
1590 {
1591 /*
1592 * nope, copy the raw DER data
1593 */
1594 p = (unsigned char *) malloc( len = buflen );
1595
1596 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001597 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001598
1599 memcpy( p, buf, buflen );
1600
1601 buflen = 0;
1602 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001603#else
1604 p = (unsigned char *) malloc( len = buflen );
1605
1606 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001607 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001608
1609 memcpy( p, buf, buflen );
1610
1611 buflen = 0;
1612#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001613
1614 crl->raw.p = p;
1615 crl->raw.len = len;
1616 end = p + len;
1617
1618 /*
1619 * CertificateList ::= SEQUENCE {
1620 * tbsCertList TBSCertList,
1621 * signatureAlgorithm AlgorithmIdentifier,
1622 * signatureValue BIT STRING }
1623 */
1624 if( ( ret = asn1_get_tag( &p, end, &len,
1625 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1626 {
1627 x509_crl_free( crl );
1628 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1629 }
1630
Paul Bakker23986e52011-04-24 08:57:21 +00001631 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001632 {
1633 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001634 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001635 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1636 }
1637
1638 /*
1639 * TBSCertList ::= SEQUENCE {
1640 */
1641 crl->tbs.p = p;
1642
1643 if( ( ret = asn1_get_tag( &p, end, &len,
1644 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1645 {
1646 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001647 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001648 }
1649
1650 end = p + len;
1651 crl->tbs.len = end - crl->tbs.p;
1652
1653 /*
1654 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1655 * -- if present, MUST be v2
1656 *
1657 * signature AlgorithmIdentifier
1658 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001659 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001660 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1661 {
1662 x509_crl_free( crl );
1663 return( ret );
1664 }
1665
1666 crl->version++;
1667
1668 if( crl->version > 2 )
1669 {
1670 x509_crl_free( crl );
1671 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1672 }
1673
Paul Bakkerc70b9822013-04-07 22:00:46 +02001674 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1675 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001676 {
1677 x509_crl_free( crl );
1678 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1679 }
1680
1681 /*
1682 * issuer Name
1683 */
1684 crl->issuer_raw.p = p;
1685
1686 if( ( ret = asn1_get_tag( &p, end, &len,
1687 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1688 {
1689 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001690 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 }
1692
1693 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1694 {
1695 x509_crl_free( crl );
1696 return( ret );
1697 }
1698
1699 crl->issuer_raw.len = p - crl->issuer_raw.p;
1700
1701 /*
1702 * thisUpdate Time
1703 * nextUpdate Time OPTIONAL
1704 */
Paul Bakker91200182010-02-18 21:26:15 +00001705 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001706 {
1707 x509_crl_free( crl );
1708 return( ret );
1709 }
1710
Paul Bakker91200182010-02-18 21:26:15 +00001711 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001712 {
Paul Bakker9d781402011-05-09 16:17:09 +00001713 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001714 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001715 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001716 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001717 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001718 x509_crl_free( crl );
1719 return( ret );
1720 }
1721 }
1722
1723 /*
1724 * revokedCertificates SEQUENCE OF SEQUENCE {
1725 * userCertificate CertificateSerialNumber,
1726 * revocationDate Time,
1727 * crlEntryExtensions Extensions OPTIONAL
1728 * -- if present, MUST be v2
1729 * } OPTIONAL
1730 */
1731 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1732 {
1733 x509_crl_free( crl );
1734 return( ret );
1735 }
1736
1737 /*
1738 * crlExtensions EXPLICIT Extensions OPTIONAL
1739 * -- if present, MUST be v2
1740 */
1741 if( crl->version == 2 )
1742 {
1743 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1744
1745 if( ret != 0 )
1746 {
1747 x509_crl_free( crl );
1748 return( ret );
1749 }
1750 }
1751
1752 if( p != end )
1753 {
1754 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001755 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001756 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1757 }
1758
1759 end = crl->raw.p + crl->raw.len;
1760
1761 /*
1762 * signatureAlgorithm AlgorithmIdentifier,
1763 * signatureValue BIT STRING
1764 */
1765 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1766 {
1767 x509_crl_free( crl );
1768 return( ret );
1769 }
1770
Paul Bakker535e97d2012-08-23 10:49:55 +00001771 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1772 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001773 {
1774 x509_crl_free( crl );
1775 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1776 }
1777
1778 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1779 {
1780 x509_crl_free( crl );
1781 return( ret );
1782 }
1783
1784 if( p != end )
1785 {
1786 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001787 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001788 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1789 }
1790
1791 if( buflen > 0 )
1792 {
1793 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1794
Paul Bakker7d06ad22009-05-02 15:53:56 +00001795 if( crl->next == NULL )
1796 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001797 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001798 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001799 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001800
Paul Bakker7d06ad22009-05-02 15:53:56 +00001801 crl = crl->next;
1802 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001803
1804 return( x509parse_crl( crl, buf, buflen ) );
1805 }
1806
1807 return( 0 );
1808}
1809
Paul Bakker335db3f2011-04-25 15:28:35 +00001810#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001811/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001812 * Load all data from a file into a given buffer.
1813 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001814int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001815{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001816 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001817
Paul Bakkerd98030e2009-05-02 15:13:40 +00001818 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001819 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001820
Paul Bakkerd98030e2009-05-02 15:13:40 +00001821 fseek( f, 0, SEEK_END );
1822 *n = (size_t) ftell( f );
1823 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001824
Paul Bakkerd98030e2009-05-02 15:13:40 +00001825 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001826 {
1827 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001828 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001829 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001830
Paul Bakkerd98030e2009-05-02 15:13:40 +00001831 if( fread( *buf, 1, *n, f ) != *n )
1832 {
1833 fclose( f );
1834 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001835 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001836 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001837
Paul Bakkerd98030e2009-05-02 15:13:40 +00001838 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001839
Paul Bakkerd98030e2009-05-02 15:13:40 +00001840 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001841
Paul Bakkerd98030e2009-05-02 15:13:40 +00001842 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001843}
1844
1845/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 * Load one or more certificates and add them to the chained list
1847 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001848int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001849{
1850 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001851 size_t n;
1852 unsigned char *buf;
1853
Paul Bakker69e095c2011-12-10 21:55:01 +00001854 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1855 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001856
Paul Bakker69e095c2011-12-10 21:55:01 +00001857 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
1859 memset( buf, 0, n + 1 );
1860 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
1862 return( ret );
1863}
1864
Paul Bakker8d914582012-06-04 12:46:42 +00001865int x509parse_crtpath( x509_cert *chain, const char *path )
1866{
1867 int ret = 0;
1868#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001869 int w_ret;
1870 WCHAR szDir[MAX_PATH];
1871 char filename[MAX_PATH];
1872 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001873 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001874
Paul Bakker97872ac2012-11-02 12:53:26 +00001875 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001876 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001877
1878 if( len > MAX_PATH - 3 )
1879 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001880
Paul Bakker3338b792012-10-01 21:13:10 +00001881 memset( szDir, 0, sizeof(szDir) );
1882 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001883 memcpy( filename, path, len );
1884 filename[len++] = '\\';
1885 p = filename + len;
1886 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001887
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001888 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001889
Paul Bakker97872ac2012-11-02 12:53:26 +00001890 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001891 if (hFind == INVALID_HANDLE_VALUE)
1892 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1893
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001894 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001895 do
1896 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001897 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001898
Paul Bakkere4791f32012-06-04 21:29:15 +00001899 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001900 continue;
1901
Paul Bakker3338b792012-10-01 21:13:10 +00001902 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1903 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001904 p, len - 1,
1905 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001906
Paul Bakker3338b792012-10-01 21:13:10 +00001907 w_ret = x509parse_crtfile( chain, filename );
1908 if( w_ret < 0 )
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001909 {
1910 ret = w_ret;
1911 goto cleanup;
1912 }
Paul Bakker3338b792012-10-01 21:13:10 +00001913
1914 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001915 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001916 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001917
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001918 if (GetLastError() != ERROR_NO_MORE_FILES)
1919 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001920
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001921cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001922 FindClose( hFind );
1923#else
1924 int t_ret;
1925 struct dirent *entry;
1926 char entry_name[255];
1927 DIR *dir = opendir( path );
1928
1929 if( dir == NULL)
1930 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1931
1932 while( ( entry = readdir( dir ) ) != NULL )
1933 {
1934 if( entry->d_type != DT_REG )
1935 continue;
1936
1937 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1938 t_ret = x509parse_crtfile( chain, entry_name );
1939 if( t_ret < 0 )
Paul Bakker97872ac2012-11-02 12:53:26 +00001940 {
1941 ret = t_ret;
1942 break;
1943 }
Paul Bakker8d914582012-06-04 12:46:42 +00001944
1945 ret += t_ret;
1946 }
1947 closedir( dir );
1948#endif
1949
1950 return( ret );
1951}
1952
Paul Bakkerd98030e2009-05-02 15:13:40 +00001953/*
1954 * Load one or more CRLs and add them to the chained list
1955 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001956int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001957{
1958 int ret;
1959 size_t n;
1960 unsigned char *buf;
1961
Paul Bakker69e095c2011-12-10 21:55:01 +00001962 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1963 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001964
Paul Bakker27fdf462011-06-09 13:55:13 +00001965 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001966
1967 memset( buf, 0, n + 1 );
1968 free( buf );
1969
1970 return( ret );
1971}
1972
Paul Bakker5121ce52009-01-03 21:22:43 +00001973/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001974 * Load and parse a private RSA key
1975 */
1976int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1977{
1978 int ret;
1979 size_t n;
1980 unsigned char *buf;
1981
Paul Bakker69e095c2011-12-10 21:55:01 +00001982 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1983 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001984
1985 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001986 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001987 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001988 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001989 (unsigned char *) pwd, strlen( pwd ) );
1990
1991 memset( buf, 0, n + 1 );
1992 free( buf );
1993
1994 return( ret );
1995}
1996
1997/*
1998 * Load and parse a public RSA key
1999 */
2000int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2001{
2002 int ret;
2003 size_t n;
2004 unsigned char *buf;
2005
Paul Bakker69e095c2011-12-10 21:55:01 +00002006 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2007 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002008
Paul Bakker27fdf462011-06-09 13:55:13 +00002009 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002010
2011 memset( buf, 0, n + 1 );
2012 free( buf );
2013
2014 return( ret );
2015}
2016#endif /* POLARSSL_FS_IO */
2017
2018/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002019 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002021static int x509parse_key_pkcs1_der( rsa_context *rsa,
2022 const unsigned char *key,
2023 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002024{
Paul Bakker23986e52011-04-24 08:57:21 +00002025 int ret;
2026 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002028
Paul Bakker96743fc2011-02-12 14:30:57 +00002029 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002030 end = p + keylen;
2031
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002033 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002034 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 * RSAPrivateKey ::= SEQUENCE {
2036 * version Version,
2037 * modulus INTEGER, -- n
2038 * publicExponent INTEGER, -- e
2039 * privateExponent INTEGER, -- d
2040 * prime1 INTEGER, -- p
2041 * prime2 INTEGER, -- q
2042 * exponent1 INTEGER, -- d mod (p-1)
2043 * exponent2 INTEGER, -- d mod (q-1)
2044 * coefficient INTEGER, -- (inverse of q) mod p
2045 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2046 * }
2047 */
2048 if( ( ret = asn1_get_tag( &p, end, &len,
2049 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2050 {
Paul Bakker9d781402011-05-09 16:17:09 +00002051 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 }
2053
2054 end = p + len;
2055
2056 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2057 {
Paul Bakker9d781402011-05-09 16:17:09 +00002058 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 }
2060
2061 if( rsa->ver != 0 )
2062 {
Paul Bakker9d781402011-05-09 16:17:09 +00002063 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 }
2065
2066 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2067 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2068 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2069 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2070 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2071 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2072 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2073 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2074 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002076 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 }
2078
2079 rsa->len = mpi_size( &rsa->N );
2080
2081 if( p != end )
2082 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002084 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002085 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 }
2087
2088 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2089 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 rsa_free( rsa );
2091 return( ret );
2092 }
2093
Paul Bakkere2f50402013-06-24 19:00:59 +02002094 return( 0 );
2095}
2096
2097/*
2098 * Parse an unencrypted PKCS#8 encoded private RSA key
2099 */
2100static int x509parse_key_pkcs8_unencrypted_der(
2101 rsa_context *rsa,
2102 const unsigned char *key,
2103 size_t keylen )
2104{
2105 int ret;
2106 size_t len;
2107 unsigned char *p, *end;
2108 x509_buf pk_alg_oid;
2109 pk_type_t pk_alg = POLARSSL_PK_NONE;
2110
2111 p = (unsigned char *) key;
2112 end = p + keylen;
2113
2114 /*
2115 * This function parses the PrivatKeyInfo object (PKCS#8)
2116 *
2117 * PrivateKeyInfo ::= SEQUENCE {
2118 * version Version,
2119 * algorithm AlgorithmIdentifier,
2120 * PrivateKey BIT STRING
2121 * }
2122 *
2123 * AlgorithmIdentifier ::= SEQUENCE {
2124 * algorithm OBJECT IDENTIFIER,
2125 * parameters ANY DEFINED BY algorithm OPTIONAL
2126 * }
2127 *
2128 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2129 */
2130 if( ( ret = asn1_get_tag( &p, end, &len,
2131 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2132 {
2133 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2134 }
2135
2136 end = p + len;
2137
2138 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2139 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2140
2141 if( rsa->ver != 0 )
2142 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2143
2144 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2145 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2146
2147 /*
2148 * only RSA keys handled at this time
2149 */
2150 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2151 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2152
2153 /*
2154 * Get the OCTET STRING and parse the PKCS#1 format inside
2155 */
2156 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2157 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2158
2159 if( ( end - p ) < 1 )
2160 {
2161 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2162 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2163 }
2164
2165 end = p + len;
2166
2167 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2168 return( ret );
2169
2170 return( 0 );
2171}
2172
2173/*
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002174 * Parse an unencrypted PKCS#8 encoded private RSA key
2175 */
2176static int x509parse_key_pkcs8_encrypted_der(
2177 rsa_context *rsa,
2178 const unsigned char *key,
2179 size_t keylen,
2180 const unsigned char *pwd,
2181 size_t pwdlen )
2182{
2183 int ret;
2184 size_t len;
2185 unsigned char *p, *end, *end2;
2186 x509_buf pbe_alg_oid, pbe_params;
2187 unsigned char buf[2048];
2188
2189 memset(buf, 0, 2048);
2190
2191 p = (unsigned char *) key;
2192 end = p + keylen;
2193
2194 /*
2195 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2196 *
2197 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2198 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2199 * encryptedData EncryptedData
2200 * }
2201 *
2202 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2203 *
2204 * EncryptedData ::= OCTET STRING
2205 *
2206 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2207 */
2208 if( ( ret = asn1_get_tag( &p, end, &len,
2209 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2210 {
2211 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2212 }
2213
2214 end = p + len;
2215
2216 if( ( ret = asn1_get_tag( &p, end, &len,
2217 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2218 {
2219 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2220 }
2221
2222 end2 = p + len;
2223
2224 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2225 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2226
2227 pbe_alg_oid.p = p;
2228 p += pbe_alg_oid.len;
2229
2230 /*
2231 * Store the algorithm parameters
2232 */
2233 pbe_params.p = p;
2234 pbe_params.len = end2 - p;
2235 p += pbe_params.len;
2236
2237 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2238 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2239
2240 // buf has been sized to 2048 bytes
2241 if( len > 2048 )
2242 return( POLARSSL_ERR_X509_INVALID_INPUT );
2243
2244 /*
2245 * Decrypt EncryptedData with appropriate PDE
2246 */
2247 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2248 {
2249 if( ( ret = pkcs12_pbe_sha1_des3_ede_cbc( &pbe_params,
2250 PKCS12_PBE_DECRYPT,
2251 pwd, pwdlen,
2252 p, len, buf ) ) != 0 )
2253 {
2254 return( ret );
2255 }
2256 }
2257 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2258 {
2259 if( ( ret = pkcs12_pbe_sha1_des2_ede_cbc( &pbe_params,
2260 PKCS12_PBE_DECRYPT,
2261 pwd, pwdlen,
2262 p, len, buf ) ) != 0 )
2263 {
2264 return( ret );
2265 }
2266 }
2267 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2268 {
2269 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2270 PKCS12_PBE_DECRYPT,
2271 pwd, pwdlen,
2272 p, len, buf ) ) != 0 )
2273 {
2274 return( ret );
2275 }
2276 }
2277 else
2278 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2279
2280 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2281}
2282
2283/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002284 * Parse a private RSA key
2285 */
2286int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2287 const unsigned char *pwd, size_t pwdlen )
2288{
2289 int ret;
2290
Paul Bakker96743fc2011-02-12 14:30:57 +00002291#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002292 size_t len;
2293 pem_context pem;
2294
2295 pem_init( &pem );
2296 ret = pem_read_buffer( &pem,
2297 "-----BEGIN RSA PRIVATE KEY-----",
2298 "-----END RSA PRIVATE KEY-----",
2299 key, pwd, pwdlen, &len );
2300 if( ret == 0 )
2301 {
2302 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2303 {
2304 rsa_free( rsa );
2305 }
2306
2307 pem_free( &pem );
2308 return( ret );
2309 }
2310 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2311 {
2312 pem_free( &pem );
2313 return( ret );
2314 }
2315
2316 ret = pem_read_buffer( &pem,
2317 "-----BEGIN PRIVATE KEY-----",
2318 "-----END PRIVATE KEY-----",
2319 key, NULL, 0, &len );
2320 if( ret == 0 )
2321 {
2322 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2323 pem.buf, pem.buflen ) ) != 0 )
2324 {
2325 rsa_free( rsa );
2326 }
2327
2328 pem_free( &pem );
2329 return( ret );
2330 }
2331 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2332 {
2333 pem_free( &pem );
2334 return( ret );
2335 }
2336
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002337 ret = pem_read_buffer( &pem,
2338 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2339 "-----END ENCRYPTED PRIVATE KEY-----",
2340 key, NULL, 0, &len );
2341 if( ret == 0 )
2342 {
2343 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2344 pem.buf, pem.buflen,
2345 pwd, pwdlen ) ) != 0 )
2346 {
2347 rsa_free( rsa );
2348 }
2349
2350 pem_free( &pem );
2351 return( ret );
2352 }
2353 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2354 {
2355 pem_free( &pem );
2356 return( ret );
2357 }
Paul Bakkere2f50402013-06-24 19:00:59 +02002358#else
2359 ((void) pwd);
2360 ((void) pwdlen);
2361#endif /* POLARSSL_PEM_C */
2362
2363 // At this point we only know it's not a PEM formatted key. Could be any
2364 // of the known DER encoded private key formats
2365 //
2366 // We try the different DER format parsers to see if one passes without
2367 // error
2368 //
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002369 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2370 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002371 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002372 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002373 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002375 rsa_free( rsa );
2376 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2377 return( 0 );
2378
2379 rsa_free( rsa );
2380 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2381 return( 0 );
2382
2383 rsa_free( rsa );
2384 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385}
2386
2387/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002388 * Parse a public RSA key
2389 */
Paul Bakker23986e52011-04-24 08:57:21 +00002390int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002391{
Paul Bakker23986e52011-04-24 08:57:21 +00002392 int ret;
2393 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002394 unsigned char *p, *end;
2395 x509_buf alg_oid;
2396#if defined(POLARSSL_PEM_C)
2397 pem_context pem;
2398
2399 pem_init( &pem );
2400 ret = pem_read_buffer( &pem,
2401 "-----BEGIN PUBLIC KEY-----",
2402 "-----END PUBLIC KEY-----",
2403 key, NULL, 0, &len );
2404
2405 if( ret == 0 )
2406 {
2407 /*
2408 * Was PEM encoded
2409 */
2410 keylen = pem.buflen;
2411 }
Paul Bakker00b28602013-06-24 13:02:41 +02002412 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002413 {
2414 pem_free( &pem );
2415 return( ret );
2416 }
2417
2418 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2419#else
2420 p = (unsigned char *) key;
2421#endif
2422 end = p + keylen;
2423
2424 /*
2425 * PublicKeyInfo ::= SEQUENCE {
2426 * algorithm AlgorithmIdentifier,
2427 * PublicKey BIT STRING
2428 * }
2429 *
2430 * AlgorithmIdentifier ::= SEQUENCE {
2431 * algorithm OBJECT IDENTIFIER,
2432 * parameters ANY DEFINED BY algorithm OPTIONAL
2433 * }
2434 *
2435 * RSAPublicKey ::= SEQUENCE {
2436 * modulus INTEGER, -- n
2437 * publicExponent INTEGER -- e
2438 * }
2439 */
2440
2441 if( ( ret = asn1_get_tag( &p, end, &len,
2442 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2443 {
2444#if defined(POLARSSL_PEM_C)
2445 pem_free( &pem );
2446#endif
2447 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002448 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002449 }
2450
2451 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2452 {
2453#if defined(POLARSSL_PEM_C)
2454 pem_free( &pem );
2455#endif
2456 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002457 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002458 }
2459
2460 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2461 {
2462#if defined(POLARSSL_PEM_C)
2463 pem_free( &pem );
2464#endif
2465 rsa_free( rsa );
2466 return( ret );
2467 }
2468
2469 rsa->len = mpi_size( &rsa->N );
2470
2471#if defined(POLARSSL_PEM_C)
2472 pem_free( &pem );
2473#endif
2474
2475 return( 0 );
2476}
2477
Paul Bakkereaa89f82011-04-04 21:36:15 +00002478#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002479/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002480 * Parse DHM parameters
2481 */
Paul Bakker23986e52011-04-24 08:57:21 +00002482int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002483{
Paul Bakker23986e52011-04-24 08:57:21 +00002484 int ret;
2485 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002486 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002487#if defined(POLARSSL_PEM_C)
2488 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002489
Paul Bakker96743fc2011-02-12 14:30:57 +00002490 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002491
Paul Bakker96743fc2011-02-12 14:30:57 +00002492 ret = pem_read_buffer( &pem,
2493 "-----BEGIN DH PARAMETERS-----",
2494 "-----END DH PARAMETERS-----",
2495 dhmin, NULL, 0, &dhminlen );
2496
2497 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002498 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002499 /*
2500 * Was PEM encoded
2501 */
2502 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002503 }
Paul Bakker00b28602013-06-24 13:02:41 +02002504 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002505 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002506 pem_free( &pem );
2507 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002508 }
2509
Paul Bakker96743fc2011-02-12 14:30:57 +00002510 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2511#else
2512 p = (unsigned char *) dhmin;
2513#endif
2514 end = p + dhminlen;
2515
Paul Bakker1b57b062011-01-06 15:48:19 +00002516 memset( dhm, 0, sizeof( dhm_context ) );
2517
Paul Bakker1b57b062011-01-06 15:48:19 +00002518 /*
2519 * DHParams ::= SEQUENCE {
2520 * prime INTEGER, -- P
2521 * generator INTEGER, -- g
2522 * }
2523 */
2524 if( ( ret = asn1_get_tag( &p, end, &len,
2525 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2526 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002527#if defined(POLARSSL_PEM_C)
2528 pem_free( &pem );
2529#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002530 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002531 }
2532
2533 end = p + len;
2534
2535 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2536 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2537 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002538#if defined(POLARSSL_PEM_C)
2539 pem_free( &pem );
2540#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002541 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002542 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002543 }
2544
2545 if( p != end )
2546 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002547#if defined(POLARSSL_PEM_C)
2548 pem_free( &pem );
2549#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002550 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002551 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002552 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2553 }
2554
Paul Bakker96743fc2011-02-12 14:30:57 +00002555#if defined(POLARSSL_PEM_C)
2556 pem_free( &pem );
2557#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002558
2559 return( 0 );
2560}
2561
Paul Bakker335db3f2011-04-25 15:28:35 +00002562#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002563/*
2564 * Load and parse a private RSA key
2565 */
2566int x509parse_dhmfile( dhm_context *dhm, const char *path )
2567{
2568 int ret;
2569 size_t n;
2570 unsigned char *buf;
2571
Paul Bakker69e095c2011-12-10 21:55:01 +00002572 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2573 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002574
Paul Bakker27fdf462011-06-09 13:55:13 +00002575 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002576
2577 memset( buf, 0, n + 1 );
2578 free( buf );
2579
2580 return( ret );
2581}
Paul Bakker335db3f2011-04-25 15:28:35 +00002582#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002583#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002584
Paul Bakker5121ce52009-01-03 21:22:43 +00002585#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002586#include <stdarg.h>
2587
2588#if !defined vsnprintf
2589#define vsnprintf _vsnprintf
2590#endif // vsnprintf
2591
2592/*
2593 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2594 * Result value is not size of buffer needed, but -1 if no fit is possible.
2595 *
2596 * This fuction tries to 'fix' this by at least suggesting enlarging the
2597 * size by 20.
2598 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002599static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002600{
2601 va_list ap;
2602 int res = -1;
2603
2604 va_start( ap, format );
2605
2606 res = vsnprintf( str, size, format, ap );
2607
2608 va_end( ap );
2609
2610 // No quick fix possible
2611 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002612 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002613
2614 return res;
2615}
2616
2617#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002618#endif
2619
Paul Bakkerd98030e2009-05-02 15:13:40 +00002620#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2621
2622#define SAFE_SNPRINTF() \
2623{ \
2624 if( ret == -1 ) \
2625 return( -1 ); \
2626 \
Paul Bakker23986e52011-04-24 08:57:21 +00002627 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002628 p[n - 1] = '\0'; \
2629 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2630 } \
2631 \
Paul Bakker23986e52011-04-24 08:57:21 +00002632 n -= (unsigned int) ret; \
2633 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002634}
2635
Paul Bakker5121ce52009-01-03 21:22:43 +00002636/*
2637 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002638 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002640int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002641{
Paul Bakker23986e52011-04-24 08:57:21 +00002642 int ret;
2643 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002645 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002646 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 char s[128], *p;
2648
2649 memset( s, 0, sizeof( s ) );
2650
2651 name = dn;
2652 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002653 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655 while( name != NULL )
2656 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002657 if( !name->oid.p )
2658 {
2659 name = name->next;
2660 continue;
2661 }
2662
Paul Bakker74111d32011-01-15 16:57:55 +00002663 if( name != dn )
2664 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002665 ret = snprintf( p, n, ", " );
2666 SAFE_SNPRINTF();
2667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Paul Bakkerc70b9822013-04-07 22:00:46 +02002669 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Paul Bakkerc70b9822013-04-07 22:00:46 +02002671 if( ret == 0 )
2672 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002674 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002675 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
2677 for( i = 0; i < name->val.len; i++ )
2678 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002679 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 break;
2681
2682 c = name->val.p[i];
2683 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2684 s[i] = '?';
2685 else s[i] = c;
2686 }
2687 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002688 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002689 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 name = name->next;
2691 }
2692
Paul Bakker23986e52011-04-24 08:57:21 +00002693 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694}
2695
2696/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002697 * Store the serial in printable form into buf; no more
2698 * than size characters will be written
2699 */
2700int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2701{
Paul Bakker23986e52011-04-24 08:57:21 +00002702 int ret;
2703 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002704 char *p;
2705
2706 p = buf;
2707 n = size;
2708
2709 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002710 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002711
2712 for( i = 0; i < nr; i++ )
2713 {
Paul Bakker93048802011-12-05 14:38:06 +00002714 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002715 continue;
2716
Paul Bakkerdd476992011-01-16 21:34:59 +00002717 ret = snprintf( p, n, "%02X%s",
2718 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2719 SAFE_SNPRINTF();
2720 }
2721
Paul Bakker03c7c252011-11-25 12:37:37 +00002722 if( nr != serial->len )
2723 {
2724 ret = snprintf( p, n, "...." );
2725 SAFE_SNPRINTF();
2726 }
2727
Paul Bakker23986e52011-04-24 08:57:21 +00002728 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002729}
2730
2731/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002732 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002734int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2735 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002736{
Paul Bakker23986e52011-04-24 08:57:21 +00002737 int ret;
2738 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002739 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002740 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
2742 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002743 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Paul Bakkerd98030e2009-05-02 15:13:40 +00002745 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002747 SAFE_SNPRINTF();
2748 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002750 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Paul Bakkerdd476992011-01-16 21:34:59 +00002752 ret = x509parse_serial_gets( p, n, &crt->serial);
2753 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
Paul Bakkerd98030e2009-05-02 15:13:40 +00002755 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2756 SAFE_SNPRINTF();
2757 ret = x509parse_dn_gets( p, n, &crt->issuer );
2758 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
Paul Bakkerd98030e2009-05-02 15:13:40 +00002760 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2761 SAFE_SNPRINTF();
2762 ret = x509parse_dn_gets( p, n, &crt->subject );
2763 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002764
Paul Bakkerd98030e2009-05-02 15:13:40 +00002765 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2767 crt->valid_from.year, crt->valid_from.mon,
2768 crt->valid_from.day, crt->valid_from.hour,
2769 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002770 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Paul Bakkerd98030e2009-05-02 15:13:40 +00002772 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2774 crt->valid_to.year, crt->valid_to.mon,
2775 crt->valid_to.day, crt->valid_to.hour,
2776 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002777 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Paul Bakkerc70b9822013-04-07 22:00:46 +02002779 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002780 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Paul Bakkerc70b9822013-04-07 22:00:46 +02002782 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2783 if( ret != 0 )
2784 ret = snprintf( p, n, "???" );
2785 else
2786 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002787 SAFE_SNPRINTF();
2788
2789 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002790 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002791 SAFE_SNPRINTF();
2792
Paul Bakker23986e52011-04-24 08:57:21 +00002793 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002794}
2795
Paul Bakker74111d32011-01-15 16:57:55 +00002796/*
2797 * Return an informational string describing the given OID
2798 */
2799const char *x509_oid_get_description( x509_buf *oid )
2800{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002801 const char *desc = NULL;
2802 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002803
Paul Bakkerc70b9822013-04-07 22:00:46 +02002804 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002805
Paul Bakkerc70b9822013-04-07 22:00:46 +02002806 if( ret != 0 )
2807 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002808
Paul Bakkerc70b9822013-04-07 22:00:46 +02002809 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002810}
2811
2812/* Return the x.y.z.... style numeric string for the given OID */
2813int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2814{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002815 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002816}
2817
Paul Bakkerd98030e2009-05-02 15:13:40 +00002818/*
2819 * Return an informational string about the CRL.
2820 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002821int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2822 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002823{
Paul Bakker23986e52011-04-24 08:57:21 +00002824 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002825 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002826 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002827 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002828 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002829
2830 p = buf;
2831 n = size;
2832
2833 ret = snprintf( p, n, "%sCRL version : %d",
2834 prefix, crl->version );
2835 SAFE_SNPRINTF();
2836
2837 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2838 SAFE_SNPRINTF();
2839 ret = x509parse_dn_gets( p, n, &crl->issuer );
2840 SAFE_SNPRINTF();
2841
2842 ret = snprintf( p, n, "\n%sthis update : " \
2843 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2844 crl->this_update.year, crl->this_update.mon,
2845 crl->this_update.day, crl->this_update.hour,
2846 crl->this_update.min, crl->this_update.sec );
2847 SAFE_SNPRINTF();
2848
2849 ret = snprintf( p, n, "\n%snext update : " \
2850 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2851 crl->next_update.year, crl->next_update.mon,
2852 crl->next_update.day, crl->next_update.hour,
2853 crl->next_update.min, crl->next_update.sec );
2854 SAFE_SNPRINTF();
2855
2856 entry = &crl->entry;
2857
2858 ret = snprintf( p, n, "\n%sRevoked certificates:",
2859 prefix );
2860 SAFE_SNPRINTF();
2861
Paul Bakker9be19372009-07-27 20:21:53 +00002862 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002863 {
2864 ret = snprintf( p, n, "\n%sserial number: ",
2865 prefix );
2866 SAFE_SNPRINTF();
2867
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002868 ret = x509parse_serial_gets( p, n, &entry->serial);
2869 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002870
Paul Bakkerd98030e2009-05-02 15:13:40 +00002871 ret = snprintf( p, n, " revocation date: " \
2872 "%04d-%02d-%02d %02d:%02d:%02d",
2873 entry->revocation_date.year, entry->revocation_date.mon,
2874 entry->revocation_date.day, entry->revocation_date.hour,
2875 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002876 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002877
2878 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 }
2880
Paul Bakkerc70b9822013-04-07 22:00:46 +02002881 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002882 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakkerc70b9822013-04-07 22:00:46 +02002884 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2885 if( ret != 0 )
2886 ret = snprintf( p, n, "???" );
2887 else
2888 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002889 SAFE_SNPRINTF();
2890
Paul Bakker1e27bb22009-07-19 20:25:25 +00002891 ret = snprintf( p, n, "\n" );
2892 SAFE_SNPRINTF();
2893
Paul Bakker23986e52011-04-24 08:57:21 +00002894 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002895}
2896
2897/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002898 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002900int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002901{
Paul Bakkercce9d772011-11-18 14:26:47 +00002902 int year, mon, day;
2903 int hour, min, sec;
2904
2905#if defined(_WIN32)
2906 SYSTEMTIME st;
2907
2908 GetLocalTime(&st);
2909
2910 year = st.wYear;
2911 mon = st.wMonth;
2912 day = st.wDay;
2913 hour = st.wHour;
2914 min = st.wMinute;
2915 sec = st.wSecond;
2916#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 struct tm *lt;
2918 time_t tt;
2919
2920 tt = time( NULL );
2921 lt = localtime( &tt );
2922
Paul Bakkercce9d772011-11-18 14:26:47 +00002923 year = lt->tm_year + 1900;
2924 mon = lt->tm_mon + 1;
2925 day = lt->tm_mday;
2926 hour = lt->tm_hour;
2927 min = lt->tm_min;
2928 sec = lt->tm_sec;
2929#endif
2930
2931 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002932 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Paul Bakkercce9d772011-11-18 14:26:47 +00002934 if( year == to->year &&
2935 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002936 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
Paul Bakkercce9d772011-11-18 14:26:47 +00002938 if( year == to->year &&
2939 mon == to->mon &&
2940 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002941 return( 1 );
2942
Paul Bakkercce9d772011-11-18 14:26:47 +00002943 if( year == to->year &&
2944 mon == to->mon &&
2945 day == to->day &&
2946 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002947 return( 1 );
2948
Paul Bakkercce9d772011-11-18 14:26:47 +00002949 if( year == to->year &&
2950 mon == to->mon &&
2951 day == to->day &&
2952 hour == to->hour &&
2953 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002954 return( 1 );
2955
Paul Bakkercce9d772011-11-18 14:26:47 +00002956 if( year == to->year &&
2957 mon == to->mon &&
2958 day == to->day &&
2959 hour == to->hour &&
2960 min == to->min &&
2961 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002962 return( 1 );
2963
Paul Bakker40ea7de2009-05-03 10:18:48 +00002964 return( 0 );
2965}
2966
2967/*
2968 * Return 1 if the certificate is revoked, or 0 otherwise.
2969 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002970int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002971{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002972 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002973
2974 while( cur != NULL && cur->serial.len != 0 )
2975 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002976 if( crt->serial.len == cur->serial.len &&
2977 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002978 {
2979 if( x509parse_time_expired( &cur->revocation_date ) )
2980 return( 1 );
2981 }
2982
2983 cur = cur->next;
2984 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
2986 return( 0 );
2987}
2988
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002989/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002990 * Check that the given certificate is valid accoring to the CRL.
2991 */
2992static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2993 x509_crl *crl_list)
2994{
2995 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002996 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2997 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002998
Paul Bakker915275b2012-09-28 07:10:55 +00002999 if( ca == NULL )
3000 return( flags );
3001
Paul Bakker76fd75a2011-01-16 21:12:10 +00003002 /*
3003 * TODO: What happens if no CRL is present?
3004 * Suggestion: Revocation state should be unknown if no CRL is present.
3005 * For backwards compatibility this is not yet implemented.
3006 */
3007
Paul Bakker915275b2012-09-28 07:10:55 +00003008 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003009 {
Paul Bakker915275b2012-09-28 07:10:55 +00003010 if( crl_list->version == 0 ||
3011 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003012 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3013 crl_list->issuer_raw.len ) != 0 )
3014 {
3015 crl_list = crl_list->next;
3016 continue;
3017 }
3018
3019 /*
3020 * Check if CRL is correctly signed by the trusted CA
3021 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003022 md_info = md_info_from_type( crl_list->sig_md );
3023 if( md_info == NULL )
3024 {
3025 /*
3026 * Cannot check 'unknown' hash
3027 */
3028 flags |= BADCRL_NOT_TRUSTED;
3029 break;
3030 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003031
Paul Bakkerc70b9822013-04-07 22:00:46 +02003032 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003033
Paul Bakkerc70b9822013-04-07 22:00:46 +02003034 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003035 0, hash, crl_list->sig.p ) == 0 )
3036 {
3037 /*
3038 * CRL is not trusted
3039 */
3040 flags |= BADCRL_NOT_TRUSTED;
3041 break;
3042 }
3043
3044 /*
3045 * Check for validity of CRL (Do not drop out)
3046 */
3047 if( x509parse_time_expired( &crl_list->next_update ) )
3048 flags |= BADCRL_EXPIRED;
3049
3050 /*
3051 * Check if certificate is revoked
3052 */
3053 if( x509parse_revoked(crt, crl_list) )
3054 {
3055 flags |= BADCERT_REVOKED;
3056 break;
3057 }
3058
3059 crl_list = crl_list->next;
3060 }
3061 return flags;
3062}
3063
Paul Bakker57b12982012-02-11 17:38:38 +00003064int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003065{
3066 size_t i;
3067 size_t cn_idx = 0;
3068
Paul Bakker57b12982012-02-11 17:38:38 +00003069 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003070 return( 0 );
3071
3072 for( i = 0; i < strlen( cn ); ++i )
3073 {
3074 if( cn[i] == '.' )
3075 {
3076 cn_idx = i;
3077 break;
3078 }
3079 }
3080
3081 if( cn_idx == 0 )
3082 return( 0 );
3083
Paul Bakker535e97d2012-08-23 10:49:55 +00003084 if( strlen( cn ) - cn_idx == name->len - 1 &&
3085 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003086 {
3087 return( 1 );
3088 }
3089
3090 return( 0 );
3091}
3092
Paul Bakker915275b2012-09-28 07:10:55 +00003093static int x509parse_verify_top(
3094 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003095 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003096 int (*f_vrfy)(void *, x509_cert *, int, int *),
3097 void *p_vrfy )
3098{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003099 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003100 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003101 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3102 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003103
3104 if( x509parse_time_expired( &child->valid_to ) )
3105 *flags |= BADCERT_EXPIRED;
3106
3107 /*
3108 * Child is the top of the chain. Check against the trust_ca list.
3109 */
3110 *flags |= BADCERT_NOT_TRUSTED;
3111
3112 while( trust_ca != NULL )
3113 {
3114 if( trust_ca->version == 0 ||
3115 child->issuer_raw.len != trust_ca->subject_raw.len ||
3116 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3117 child->issuer_raw.len ) != 0 )
3118 {
3119 trust_ca = trust_ca->next;
3120 continue;
3121 }
3122
Paul Bakker9a736322012-11-14 12:39:52 +00003123 /*
3124 * Reduce path_len to check against if top of the chain is
3125 * the same as the trusted CA
3126 */
3127 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3128 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3129 child->issuer_raw.len ) == 0 )
3130 {
3131 check_path_cnt--;
3132 }
3133
Paul Bakker915275b2012-09-28 07:10:55 +00003134 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003135 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003136 {
3137 trust_ca = trust_ca->next;
3138 continue;
3139 }
3140
Paul Bakkerc70b9822013-04-07 22:00:46 +02003141 md_info = md_info_from_type( child->sig_md );
3142 if( md_info == NULL )
3143 {
3144 /*
3145 * Cannot check 'unknown' hash
3146 */
3147 continue;
3148 }
Paul Bakker915275b2012-09-28 07:10:55 +00003149
Paul Bakkerc70b9822013-04-07 22:00:46 +02003150 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003151
Paul Bakkerc70b9822013-04-07 22:00:46 +02003152 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003153 0, hash, child->sig.p ) != 0 )
3154 {
3155 trust_ca = trust_ca->next;
3156 continue;
3157 }
3158
3159 /*
3160 * Top of chain is signed by a trusted CA
3161 */
3162 *flags &= ~BADCERT_NOT_TRUSTED;
3163 break;
3164 }
3165
Paul Bakker9a736322012-11-14 12:39:52 +00003166 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003167 * If top of chain is not the same as the trusted CA send a verify request
3168 * to the callback for any issues with validity and CRL presence for the
3169 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003170 */
3171 if( trust_ca != NULL &&
3172 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3173 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3174 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003175 {
3176 /* Check trusted CA's CRL for then chain's top crt */
3177 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3178
3179 if( x509parse_time_expired( &trust_ca->valid_to ) )
3180 ca_flags |= BADCERT_EXPIRED;
3181
Paul Bakker915275b2012-09-28 07:10:55 +00003182 if( NULL != f_vrfy )
3183 {
Paul Bakker9a736322012-11-14 12:39:52 +00003184 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003185 return( ret );
3186 }
3187 }
3188
3189 /* Call callback on top cert */
3190 if( NULL != f_vrfy )
3191 {
Paul Bakker9a736322012-11-14 12:39:52 +00003192 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003193 return( ret );
3194 }
3195
Paul Bakker915275b2012-09-28 07:10:55 +00003196 *flags |= ca_flags;
3197
3198 return( 0 );
3199}
3200
3201static int x509parse_verify_child(
3202 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003203 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003204 int (*f_vrfy)(void *, x509_cert *, int, int *),
3205 void *p_vrfy )
3206{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003207 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003208 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003209 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003210 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003211 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003212
3213 if( x509parse_time_expired( &child->valid_to ) )
3214 *flags |= BADCERT_EXPIRED;
3215
Paul Bakkerc70b9822013-04-07 22:00:46 +02003216 md_info = md_info_from_type( child->sig_md );
3217 if( md_info == NULL )
3218 {
3219 /*
3220 * Cannot check 'unknown' hash
3221 */
Paul Bakker915275b2012-09-28 07:10:55 +00003222 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003223 }
3224 else
3225 {
3226 md( md_info, child->tbs.p, child->tbs.len, hash );
3227
3228 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3229 child->sig.p ) != 0 )
3230 *flags |= BADCERT_NOT_TRUSTED;
3231 }
3232
Paul Bakker915275b2012-09-28 07:10:55 +00003233 /* Check trusted CA's CRL for the given crt */
3234 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3235
3236 grandparent = parent->next;
3237
3238 while( grandparent != NULL )
3239 {
3240 if( grandparent->version == 0 ||
3241 grandparent->ca_istrue == 0 ||
3242 parent->issuer_raw.len != grandparent->subject_raw.len ||
3243 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3244 parent->issuer_raw.len ) != 0 )
3245 {
3246 grandparent = grandparent->next;
3247 continue;
3248 }
3249 break;
3250 }
3251
Paul Bakker915275b2012-09-28 07:10:55 +00003252 if( grandparent != NULL )
3253 {
3254 /*
3255 * Part of the chain
3256 */
Paul Bakker9a736322012-11-14 12:39:52 +00003257 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 +00003258 if( ret != 0 )
3259 return( ret );
3260 }
3261 else
3262 {
Paul Bakker9a736322012-11-14 12:39:52 +00003263 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 +00003264 if( ret != 0 )
3265 return( ret );
3266 }
3267
3268 /* child is verified to be a child of the parent, call verify callback */
3269 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003270 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003271 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003272
3273 *flags |= parent_flags;
3274
3275 return( 0 );
3276}
3277
Paul Bakker76fd75a2011-01-16 21:12:10 +00003278/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 * Verify the certificate validity
3280 */
3281int x509parse_verify( x509_cert *crt,
3282 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003283 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003284 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003285 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003286 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003287{
Paul Bakker23986e52011-04-24 08:57:21 +00003288 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003289 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003290 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003291 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003293 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Paul Bakker40ea7de2009-05-03 10:18:48 +00003295 *flags = 0;
3296
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 if( cn != NULL )
3298 {
3299 name = &crt->subject;
3300 cn_len = strlen( cn );
3301
Paul Bakker4d2c1242012-05-10 14:12:46 +00003302 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003304 cur = &crt->subject_alt_names;
3305
3306 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003307 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003308 if( cur->buf.len == cn_len &&
3309 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003310 break;
3311
Paul Bakker535e97d2012-08-23 10:49:55 +00003312 if( cur->buf.len > 2 &&
3313 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003314 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003315 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
Paul Bakker4d2c1242012-05-10 14:12:46 +00003317 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003318 }
3319
3320 if( cur == NULL )
3321 *flags |= BADCERT_CN_MISMATCH;
3322 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003323 else
3324 {
3325 while( name != NULL )
3326 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003327 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003328 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003329 if( name->val.len == cn_len &&
3330 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003331 break;
3332
Paul Bakker535e97d2012-08-23 10:49:55 +00003333 if( name->val.len > 2 &&
3334 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003335 x509_wildcard_verify( cn, &name->val ) )
3336 break;
3337 }
3338
3339 name = name->next;
3340 }
3341
3342 if( name == NULL )
3343 *flags |= BADCERT_CN_MISMATCH;
3344 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 }
3346
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003348 * Iterate upwards in the given cert chain, to find our crt parent.
3349 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003351 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003352
Paul Bakker76fd75a2011-01-16 21:12:10 +00003353 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003355 if( parent->ca_istrue == 0 ||
3356 crt->issuer_raw.len != parent->subject_raw.len ||
3357 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 crt->issuer_raw.len ) != 0 )
3359 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003360 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 continue;
3362 }
Paul Bakker915275b2012-09-28 07:10:55 +00003363 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 }
3365
Paul Bakker915275b2012-09-28 07:10:55 +00003366 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003367 {
Paul Bakker915275b2012-09-28 07:10:55 +00003368 /*
3369 * Part of the chain
3370 */
Paul Bakker9a736322012-11-14 12:39:52 +00003371 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003372 if( ret != 0 )
3373 return( ret );
3374 }
3375 else
Paul Bakker74111d32011-01-15 16:57:55 +00003376 {
Paul Bakker9a736322012-11-14 12:39:52 +00003377 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003378 if( ret != 0 )
3379 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003380 }
Paul Bakker915275b2012-09-28 07:10:55 +00003381
3382 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003383 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003384
Paul Bakker5121ce52009-01-03 21:22:43 +00003385 return( 0 );
3386}
3387
3388/*
3389 * Unallocate all certificate data
3390 */
3391void x509_free( x509_cert *crt )
3392{
3393 x509_cert *cert_cur = crt;
3394 x509_cert *cert_prv;
3395 x509_name *name_cur;
3396 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003397 x509_sequence *seq_cur;
3398 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003399
3400 if( crt == NULL )
3401 return;
3402
3403 do
3404 {
3405 rsa_free( &cert_cur->rsa );
3406
3407 name_cur = cert_cur->issuer.next;
3408 while( name_cur != NULL )
3409 {
3410 name_prv = name_cur;
3411 name_cur = name_cur->next;
3412 memset( name_prv, 0, sizeof( x509_name ) );
3413 free( name_prv );
3414 }
3415
3416 name_cur = cert_cur->subject.next;
3417 while( name_cur != NULL )
3418 {
3419 name_prv = name_cur;
3420 name_cur = name_cur->next;
3421 memset( name_prv, 0, sizeof( x509_name ) );
3422 free( name_prv );
3423 }
3424
Paul Bakker74111d32011-01-15 16:57:55 +00003425 seq_cur = cert_cur->ext_key_usage.next;
3426 while( seq_cur != NULL )
3427 {
3428 seq_prv = seq_cur;
3429 seq_cur = seq_cur->next;
3430 memset( seq_prv, 0, sizeof( x509_sequence ) );
3431 free( seq_prv );
3432 }
3433
Paul Bakker8afa70d2012-02-11 18:42:45 +00003434 seq_cur = cert_cur->subject_alt_names.next;
3435 while( seq_cur != NULL )
3436 {
3437 seq_prv = seq_cur;
3438 seq_cur = seq_cur->next;
3439 memset( seq_prv, 0, sizeof( x509_sequence ) );
3440 free( seq_prv );
3441 }
3442
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 if( cert_cur->raw.p != NULL )
3444 {
3445 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3446 free( cert_cur->raw.p );
3447 }
3448
3449 cert_cur = cert_cur->next;
3450 }
3451 while( cert_cur != NULL );
3452
3453 cert_cur = crt;
3454 do
3455 {
3456 cert_prv = cert_cur;
3457 cert_cur = cert_cur->next;
3458
3459 memset( cert_prv, 0, sizeof( x509_cert ) );
3460 if( cert_prv != crt )
3461 free( cert_prv );
3462 }
3463 while( cert_cur != NULL );
3464}
3465
Paul Bakkerd98030e2009-05-02 15:13:40 +00003466/*
3467 * Unallocate all CRL data
3468 */
3469void x509_crl_free( x509_crl *crl )
3470{
3471 x509_crl *crl_cur = crl;
3472 x509_crl *crl_prv;
3473 x509_name *name_cur;
3474 x509_name *name_prv;
3475 x509_crl_entry *entry_cur;
3476 x509_crl_entry *entry_prv;
3477
3478 if( crl == NULL )
3479 return;
3480
3481 do
3482 {
3483 name_cur = crl_cur->issuer.next;
3484 while( name_cur != NULL )
3485 {
3486 name_prv = name_cur;
3487 name_cur = name_cur->next;
3488 memset( name_prv, 0, sizeof( x509_name ) );
3489 free( name_prv );
3490 }
3491
3492 entry_cur = crl_cur->entry.next;
3493 while( entry_cur != NULL )
3494 {
3495 entry_prv = entry_cur;
3496 entry_cur = entry_cur->next;
3497 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3498 free( entry_prv );
3499 }
3500
3501 if( crl_cur->raw.p != NULL )
3502 {
3503 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3504 free( crl_cur->raw.p );
3505 }
3506
3507 crl_cur = crl_cur->next;
3508 }
3509 while( crl_cur != NULL );
3510
3511 crl_cur = crl;
3512 do
3513 {
3514 crl_prv = crl_cur;
3515 crl_cur = crl_cur->next;
3516
3517 memset( crl_prv, 0, sizeof( x509_crl ) );
3518 if( crl_prv != crl )
3519 free( crl_prv );
3520 }
3521 while( crl_cur != NULL );
3522}
3523
Paul Bakker40e46942009-01-03 21:51:57 +00003524#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003525
Paul Bakker40e46942009-01-03 21:51:57 +00003526#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
3528/*
3529 * Checkup routine
3530 */
3531int x509_self_test( int verbose )
3532{
Paul Bakker5690efc2011-05-26 13:16:06 +00003533#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003534 int ret;
3535 int flags;
3536 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003537 x509_cert cacert;
3538 x509_cert clicert;
3539 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003540#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003541 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003542#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
3544 if( verbose != 0 )
3545 printf( " X.509 certificate load: " );
3546
3547 memset( &clicert, 0, sizeof( x509_cert ) );
3548
Paul Bakker3c2122f2013-06-24 19:03:14 +02003549 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003550 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 if( ret != 0 )
3552 {
3553 if( verbose != 0 )
3554 printf( "failed\n" );
3555
3556 return( ret );
3557 }
3558
3559 memset( &cacert, 0, sizeof( x509_cert ) );
3560
Paul Bakker3c2122f2013-06-24 19:03:14 +02003561 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003562 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003563 if( ret != 0 )
3564 {
3565 if( verbose != 0 )
3566 printf( "failed\n" );
3567
3568 return( ret );
3569 }
3570
3571 if( verbose != 0 )
3572 printf( "passed\n X.509 private key load: " );
3573
3574 i = strlen( test_ca_key );
3575 j = strlen( test_ca_pwd );
3576
Paul Bakker66b78b22011-03-25 14:22:50 +00003577 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3578
Paul Bakker5121ce52009-01-03 21:22:43 +00003579 if( ( ret = x509parse_key( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003580 (const unsigned char *) test_ca_key, i,
3581 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003582 {
3583 if( verbose != 0 )
3584 printf( "failed\n" );
3585
3586 return( ret );
3587 }
3588
3589 if( verbose != 0 )
3590 printf( "passed\n X.509 signature verify: ");
3591
Paul Bakker23986e52011-04-24 08:57:21 +00003592 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 if( ret != 0 )
3594 {
Paul Bakker23986e52011-04-24 08:57:21 +00003595 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 if( verbose != 0 )
3597 printf( "failed\n" );
3598
3599 return( ret );
3600 }
3601
Paul Bakker5690efc2011-05-26 13:16:06 +00003602#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003604 printf( "passed\n X.509 DHM parameter load: " );
3605
3606 i = strlen( test_dhm_params );
3607 j = strlen( test_ca_pwd );
3608
Paul Bakker3c2122f2013-06-24 19:03:14 +02003609 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003610 {
3611 if( verbose != 0 )
3612 printf( "failed\n" );
3613
3614 return( ret );
3615 }
3616
3617 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003618 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003619#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003620
3621 x509_free( &cacert );
3622 x509_free( &clicert );
3623 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003624#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003625 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003626#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
3628 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003629#else
3630 ((void) verbose);
3631 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3632#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003633}
3634
3635#endif
3636
3637#endif