blob: f2e2e24967765fb7768937af2f7fab9306df2e98 [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 Bakker42c65812013-06-24 19:21:59 +02001110int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1111 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001112{
Paul Bakker23986e52011-04-24 08:57:21 +00001113 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001114 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001115 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001116
Paul Bakker320a4b52009-03-28 18:52:39 +00001117 /*
1118 * Check for valid input
1119 */
1120 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001121 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001122
Paul Bakker96743fc2011-02-12 14:30:57 +00001123 p = (unsigned char *) malloc( len = buflen );
1124
1125 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001126 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001127
1128 memcpy( p, buf, buflen );
1129
1130 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001131
1132 crt->raw.p = p;
1133 crt->raw.len = len;
1134 end = p + len;
1135
1136 /*
1137 * Certificate ::= SEQUENCE {
1138 * tbsCertificate TBSCertificate,
1139 * signatureAlgorithm AlgorithmIdentifier,
1140 * signatureValue BIT STRING }
1141 */
1142 if( ( ret = asn1_get_tag( &p, end, &len,
1143 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1144 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001145 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001146 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 }
1148
Paul Bakkerb00ca422012-09-25 12:10:00 +00001149 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001151 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001152 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001153 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001155 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001156
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 /*
1158 * TBSCertificate ::= SEQUENCE {
1159 */
1160 crt->tbs.p = p;
1161
1162 if( ( ret = asn1_get_tag( &p, end, &len,
1163 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1164 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001165 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001166 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 }
1168
1169 end = p + len;
1170 crt->tbs.len = end - crt->tbs.p;
1171
1172 /*
1173 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1174 *
1175 * CertificateSerialNumber ::= INTEGER
1176 *
1177 * signature AlgorithmIdentifier
1178 */
1179 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1180 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1181 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1182 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001183 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 return( ret );
1185 }
1186
1187 crt->version++;
1188
1189 if( crt->version > 3 )
1190 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001191 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001192 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 }
1194
Paul Bakkerc70b9822013-04-07 22:00:46 +02001195 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1196 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001198 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001199 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 }
1201
1202 /*
1203 * issuer Name
1204 */
1205 crt->issuer_raw.p = p;
1206
1207 if( ( ret = asn1_get_tag( &p, end, &len,
1208 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1209 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001210 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001211 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 }
1213
1214 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1215 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001216 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 return( ret );
1218 }
1219
1220 crt->issuer_raw.len = p - crt->issuer_raw.p;
1221
1222 /*
1223 * Validity ::= SEQUENCE {
1224 * notBefore Time,
1225 * notAfter Time }
1226 *
1227 */
1228 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1229 &crt->valid_to ) ) != 0 )
1230 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001231 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 return( ret );
1233 }
1234
1235 /*
1236 * subject Name
1237 */
1238 crt->subject_raw.p = p;
1239
1240 if( ( ret = asn1_get_tag( &p, end, &len,
1241 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1242 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001243 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001244 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 }
1246
Paul Bakkercefb3962012-06-27 11:51:09 +00001247 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001249 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 return( ret );
1251 }
1252
1253 crt->subject_raw.len = p - crt->subject_raw.p;
1254
1255 /*
1256 * SubjectPublicKeyInfo ::= SEQUENCE
1257 * algorithm AlgorithmIdentifier,
1258 * subjectPublicKey BIT STRING }
1259 */
1260 if( ( ret = asn1_get_tag( &p, end, &len,
1261 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1262 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001263 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001264 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 }
1266
1267 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1268 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1269 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001270 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 return( ret );
1272 }
1273
1274 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1275 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001276 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 return( ret );
1278 }
1279
1280 crt->rsa.len = mpi_size( &crt->rsa.N );
1281
1282 /*
1283 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1284 * -- If present, version shall be v2 or v3
1285 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1286 * -- If present, version shall be v2 or v3
1287 * extensions [3] EXPLICIT Extensions OPTIONAL
1288 * -- If present, version shall be v3
1289 */
1290 if( crt->version == 2 || crt->version == 3 )
1291 {
1292 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1293 if( ret != 0 )
1294 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001295 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 return( ret );
1297 }
1298 }
1299
1300 if( crt->version == 2 || crt->version == 3 )
1301 {
1302 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1303 if( ret != 0 )
1304 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001305 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 return( ret );
1307 }
1308 }
1309
1310 if( crt->version == 3 )
1311 {
Paul Bakker74111d32011-01-15 16:57:55 +00001312 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 if( ret != 0 )
1314 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001315 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 return( ret );
1317 }
1318 }
1319
1320 if( p != end )
1321 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001322 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001323 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001324 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
Paul Bakkerb00ca422012-09-25 12:10:00 +00001327 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
1329 /*
1330 * signatureAlgorithm AlgorithmIdentifier,
1331 * signatureValue BIT STRING
1332 */
1333 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1334 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001335 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 return( ret );
1337 }
1338
Paul Bakker535e97d2012-08-23 10:49:55 +00001339 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1340 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001342 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001343 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 }
1345
1346 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1347 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001348 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 return( ret );
1350 }
1351
1352 if( p != end )
1353 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001354 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001355 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001356 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
1358
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001359 return( 0 );
1360}
1361
1362/*
Paul Bakker42c65812013-06-24 19:21:59 +02001363 * Parse one X.509 certificate in DER format from a buffer and add them to a
1364 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001365 */
Paul Bakker42c65812013-06-24 19:21:59 +02001366int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001367{
Paul Bakker42c65812013-06-24 19:21:59 +02001368 int ret;
1369 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001370
1371 /*
1372 * Check for valid input
1373 */
1374 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001375 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001376
1377 while( crt->version != 0 && crt->next != NULL )
1378 {
1379 prev = crt;
1380 crt = crt->next;
1381 }
1382
1383 /*
1384 * Add new certificate on the end of the chain if needed.
1385 */
1386 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001387 {
1388 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1389
Paul Bakker7d06ad22009-05-02 15:53:56 +00001390 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001391 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001392
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001393 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001394 crt = crt->next;
1395 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001396 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Paul Bakker42c65812013-06-24 19:21:59 +02001398 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1399 {
1400 if( prev )
1401 prev->next = NULL;
1402
1403 if( crt != chain )
1404 free( crt );
1405
1406 return( ret );
1407 }
1408
1409 return( 0 );
1410}
1411
1412/*
1413 * Parse one or more PEM certificates from a buffer and add them to the chained list
1414 */
1415int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1416{
1417 int ret, success = 0, first_error = 0, total_failed = 0;
1418 int buf_format = X509_FORMAT_DER;
1419
1420 /*
1421 * Check for valid input
1422 */
1423 if( chain == NULL || buf == NULL )
1424 return( POLARSSL_ERR_X509_INVALID_INPUT );
1425
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001426 /*
1427 * Determine buffer content. Buffer contains either one DER certificate or
1428 * one or more PEM certificates.
1429 */
1430#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001431 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001432 buf_format = X509_FORMAT_PEM;
1433#endif
1434
1435 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001436 return x509parse_crt_der( chain, buf, buflen );
1437
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001438#if defined(POLARSSL_PEM_C)
1439 if( buf_format == X509_FORMAT_PEM )
1440 {
1441 pem_context pem;
1442
1443 while( buflen > 0 )
1444 {
1445 size_t use_len;
1446 pem_init( &pem );
1447
1448 ret = pem_read_buffer( &pem,
1449 "-----BEGIN CERTIFICATE-----",
1450 "-----END CERTIFICATE-----",
1451 buf, NULL, 0, &use_len );
1452
1453 if( ret == 0 )
1454 {
1455 /*
1456 * Was PEM encoded
1457 */
1458 buflen -= use_len;
1459 buf += use_len;
1460 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001461 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1462 {
1463 return( ret );
1464 }
Paul Bakker00b28602013-06-24 13:02:41 +02001465 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001466 {
1467 pem_free( &pem );
1468
Paul Bakker5ed3b342013-06-24 19:05:46 +02001469 /*
1470 * PEM header and footer were found
1471 */
1472 buflen -= use_len;
1473 buf += use_len;
1474
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001475 if( first_error == 0 )
1476 first_error = ret;
1477
1478 continue;
1479 }
1480 else
1481 break;
1482
Paul Bakker42c65812013-06-24 19:21:59 +02001483 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001484
1485 pem_free( &pem );
1486
1487 if( ret != 0 )
1488 {
1489 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001490 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001491 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001492 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001493 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001494
1495 if( first_error == 0 )
1496 first_error = ret;
1497
Paul Bakker42c65812013-06-24 19:21:59 +02001498 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001499 continue;
1500 }
1501
1502 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001503 }
1504 }
1505#endif
1506
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001507 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001508 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001509 else if( first_error )
1510 return( first_error );
1511 else
1512 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001513}
1514
1515/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001516 * Parse one or more CRLs and add them to the chained list
1517 */
Paul Bakker23986e52011-04-24 08:57:21 +00001518int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001519{
Paul Bakker23986e52011-04-24 08:57:21 +00001520 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001521 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001522 unsigned char *p, *end;
1523 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001524#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001525 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001526 pem_context pem;
1527#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001528
1529 crl = chain;
1530
1531 /*
1532 * Check for valid input
1533 */
1534 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001535 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001536
1537 while( crl->version != 0 && crl->next != NULL )
1538 crl = crl->next;
1539
1540 /*
1541 * Add new CRL on the end of the chain if needed.
1542 */
1543 if ( crl->version != 0 && crl->next == NULL)
1544 {
1545 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1546
Paul Bakker7d06ad22009-05-02 15:53:56 +00001547 if( crl->next == NULL )
1548 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001549 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001550 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001551 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001552
Paul Bakker7d06ad22009-05-02 15:53:56 +00001553 crl = crl->next;
1554 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001555 }
1556
Paul Bakker96743fc2011-02-12 14:30:57 +00001557#if defined(POLARSSL_PEM_C)
1558 pem_init( &pem );
1559 ret = pem_read_buffer( &pem,
1560 "-----BEGIN X509 CRL-----",
1561 "-----END X509 CRL-----",
1562 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001563
Paul Bakker96743fc2011-02-12 14:30:57 +00001564 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001565 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001566 /*
1567 * Was PEM encoded
1568 */
1569 buflen -= use_len;
1570 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001571
1572 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001573 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001574 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001575 p = pem.buf;
1576 pem.buf = NULL;
1577 len = pem.buflen;
1578 pem_free( &pem );
1579 }
Paul Bakker00b28602013-06-24 13:02:41 +02001580 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001581 {
1582 pem_free( &pem );
1583 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001584 }
1585 else
1586 {
1587 /*
1588 * nope, copy the raw DER data
1589 */
1590 p = (unsigned char *) malloc( len = buflen );
1591
1592 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001593 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001594
1595 memcpy( p, buf, buflen );
1596
1597 buflen = 0;
1598 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001599#else
1600 p = (unsigned char *) malloc( len = buflen );
1601
1602 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001603 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001604
1605 memcpy( p, buf, buflen );
1606
1607 buflen = 0;
1608#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609
1610 crl->raw.p = p;
1611 crl->raw.len = len;
1612 end = p + len;
1613
1614 /*
1615 * CertificateList ::= SEQUENCE {
1616 * tbsCertList TBSCertList,
1617 * signatureAlgorithm AlgorithmIdentifier,
1618 * signatureValue BIT STRING }
1619 */
1620 if( ( ret = asn1_get_tag( &p, end, &len,
1621 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1622 {
1623 x509_crl_free( crl );
1624 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1625 }
1626
Paul Bakker23986e52011-04-24 08:57:21 +00001627 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001628 {
1629 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001630 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1632 }
1633
1634 /*
1635 * TBSCertList ::= SEQUENCE {
1636 */
1637 crl->tbs.p = p;
1638
1639 if( ( ret = asn1_get_tag( &p, end, &len,
1640 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1641 {
1642 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001643 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001644 }
1645
1646 end = p + len;
1647 crl->tbs.len = end - crl->tbs.p;
1648
1649 /*
1650 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1651 * -- if present, MUST be v2
1652 *
1653 * signature AlgorithmIdentifier
1654 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001655 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001656 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1657 {
1658 x509_crl_free( crl );
1659 return( ret );
1660 }
1661
1662 crl->version++;
1663
1664 if( crl->version > 2 )
1665 {
1666 x509_crl_free( crl );
1667 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1668 }
1669
Paul Bakkerc70b9822013-04-07 22:00:46 +02001670 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1671 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 {
1673 x509_crl_free( crl );
1674 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1675 }
1676
1677 /*
1678 * issuer Name
1679 */
1680 crl->issuer_raw.p = p;
1681
1682 if( ( ret = asn1_get_tag( &p, end, &len,
1683 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1684 {
1685 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001686 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687 }
1688
1689 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1690 {
1691 x509_crl_free( crl );
1692 return( ret );
1693 }
1694
1695 crl->issuer_raw.len = p - crl->issuer_raw.p;
1696
1697 /*
1698 * thisUpdate Time
1699 * nextUpdate Time OPTIONAL
1700 */
Paul Bakker91200182010-02-18 21:26:15 +00001701 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001702 {
1703 x509_crl_free( crl );
1704 return( ret );
1705 }
1706
Paul Bakker91200182010-02-18 21:26:15 +00001707 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001708 {
Paul Bakker9d781402011-05-09 16:17:09 +00001709 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001710 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001711 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001712 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001713 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001714 x509_crl_free( crl );
1715 return( ret );
1716 }
1717 }
1718
1719 /*
1720 * revokedCertificates SEQUENCE OF SEQUENCE {
1721 * userCertificate CertificateSerialNumber,
1722 * revocationDate Time,
1723 * crlEntryExtensions Extensions OPTIONAL
1724 * -- if present, MUST be v2
1725 * } OPTIONAL
1726 */
1727 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1728 {
1729 x509_crl_free( crl );
1730 return( ret );
1731 }
1732
1733 /*
1734 * crlExtensions EXPLICIT Extensions OPTIONAL
1735 * -- if present, MUST be v2
1736 */
1737 if( crl->version == 2 )
1738 {
1739 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1740
1741 if( ret != 0 )
1742 {
1743 x509_crl_free( crl );
1744 return( ret );
1745 }
1746 }
1747
1748 if( p != end )
1749 {
1750 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001751 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1753 }
1754
1755 end = crl->raw.p + crl->raw.len;
1756
1757 /*
1758 * signatureAlgorithm AlgorithmIdentifier,
1759 * signatureValue BIT STRING
1760 */
1761 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1762 {
1763 x509_crl_free( crl );
1764 return( ret );
1765 }
1766
Paul Bakker535e97d2012-08-23 10:49:55 +00001767 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1768 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001769 {
1770 x509_crl_free( crl );
1771 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1772 }
1773
1774 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1775 {
1776 x509_crl_free( crl );
1777 return( ret );
1778 }
1779
1780 if( p != end )
1781 {
1782 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001783 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001784 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1785 }
1786
1787 if( buflen > 0 )
1788 {
1789 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1790
Paul Bakker7d06ad22009-05-02 15:53:56 +00001791 if( crl->next == NULL )
1792 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001793 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001794 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001795 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001796
Paul Bakker7d06ad22009-05-02 15:53:56 +00001797 crl = crl->next;
1798 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799
1800 return( x509parse_crl( crl, buf, buflen ) );
1801 }
1802
1803 return( 0 );
1804}
1805
Paul Bakker335db3f2011-04-25 15:28:35 +00001806#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001807/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001808 * Load all data from a file into a given buffer.
1809 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001810int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001811{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001812 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001813
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001815 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001816
Paul Bakkerd98030e2009-05-02 15:13:40 +00001817 fseek( f, 0, SEEK_END );
1818 *n = (size_t) ftell( f );
1819 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001820
Paul Bakkerd98030e2009-05-02 15:13:40 +00001821 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001822 {
1823 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001824 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001825 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001826
Paul Bakkerd98030e2009-05-02 15:13:40 +00001827 if( fread( *buf, 1, *n, f ) != *n )
1828 {
1829 fclose( f );
1830 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001831 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001832 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001833
Paul Bakkerd98030e2009-05-02 15:13:40 +00001834 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001835
Paul Bakkerd98030e2009-05-02 15:13:40 +00001836 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001837
Paul Bakkerd98030e2009-05-02 15:13:40 +00001838 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001839}
1840
1841/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 * Load one or more certificates and add them to the chained list
1843 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001844int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001845{
1846 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 size_t n;
1848 unsigned char *buf;
1849
Paul Bakker69e095c2011-12-10 21:55:01 +00001850 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1851 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Paul Bakker69e095c2011-12-10 21:55:01 +00001853 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
1855 memset( buf, 0, n + 1 );
1856 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
1858 return( ret );
1859}
1860
Paul Bakker8d914582012-06-04 12:46:42 +00001861int x509parse_crtpath( x509_cert *chain, const char *path )
1862{
1863 int ret = 0;
1864#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001865 int w_ret;
1866 WCHAR szDir[MAX_PATH];
1867 char filename[MAX_PATH];
1868 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001869 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001870
Paul Bakker97872ac2012-11-02 12:53:26 +00001871 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001872 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001873
1874 if( len > MAX_PATH - 3 )
1875 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001876
Paul Bakker3338b792012-10-01 21:13:10 +00001877 memset( szDir, 0, sizeof(szDir) );
1878 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001879 memcpy( filename, path, len );
1880 filename[len++] = '\\';
1881 p = filename + len;
1882 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001883
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001884 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001885
Paul Bakker97872ac2012-11-02 12:53:26 +00001886 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001887 if (hFind == INVALID_HANDLE_VALUE)
1888 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1889
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001890 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001891 do
1892 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001893 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001894
Paul Bakkere4791f32012-06-04 21:29:15 +00001895 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001896 continue;
1897
Paul Bakker3338b792012-10-01 21:13:10 +00001898 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1899 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001900 p, len - 1,
1901 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001902
Paul Bakker3338b792012-10-01 21:13:10 +00001903 w_ret = x509parse_crtfile( chain, filename );
1904 if( w_ret < 0 )
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001905 {
1906 ret = w_ret;
1907 goto cleanup;
1908 }
Paul Bakker3338b792012-10-01 21:13:10 +00001909
1910 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001911 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001912 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001913
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001914 if (GetLastError() != ERROR_NO_MORE_FILES)
1915 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001916
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001917cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001918 FindClose( hFind );
1919#else
1920 int t_ret;
1921 struct dirent *entry;
1922 char entry_name[255];
1923 DIR *dir = opendir( path );
1924
1925 if( dir == NULL)
1926 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1927
1928 while( ( entry = readdir( dir ) ) != NULL )
1929 {
1930 if( entry->d_type != DT_REG )
1931 continue;
1932
1933 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1934 t_ret = x509parse_crtfile( chain, entry_name );
1935 if( t_ret < 0 )
Paul Bakker97872ac2012-11-02 12:53:26 +00001936 {
1937 ret = t_ret;
1938 break;
1939 }
Paul Bakker8d914582012-06-04 12:46:42 +00001940
1941 ret += t_ret;
1942 }
1943 closedir( dir );
1944#endif
1945
1946 return( ret );
1947}
1948
Paul Bakkerd98030e2009-05-02 15:13:40 +00001949/*
1950 * Load one or more CRLs and add them to the chained list
1951 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001952int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001953{
1954 int ret;
1955 size_t n;
1956 unsigned char *buf;
1957
Paul Bakker69e095c2011-12-10 21:55:01 +00001958 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1959 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001960
Paul Bakker27fdf462011-06-09 13:55:13 +00001961 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001962
1963 memset( buf, 0, n + 1 );
1964 free( buf );
1965
1966 return( ret );
1967}
1968
Paul Bakker5121ce52009-01-03 21:22:43 +00001969/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001970 * Load and parse a private RSA key
1971 */
1972int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1973{
1974 int ret;
1975 size_t n;
1976 unsigned char *buf;
1977
Paul Bakker69e095c2011-12-10 21:55:01 +00001978 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1979 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001980
1981 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001982 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001983 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001984 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001985 (unsigned char *) pwd, strlen( pwd ) );
1986
1987 memset( buf, 0, n + 1 );
1988 free( buf );
1989
1990 return( ret );
1991}
1992
1993/*
1994 * Load and parse a public RSA key
1995 */
1996int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1997{
1998 int ret;
1999 size_t n;
2000 unsigned char *buf;
2001
Paul Bakker69e095c2011-12-10 21:55:01 +00002002 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2003 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002004
Paul Bakker27fdf462011-06-09 13:55:13 +00002005 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002006
2007 memset( buf, 0, n + 1 );
2008 free( buf );
2009
2010 return( ret );
2011}
2012#endif /* POLARSSL_FS_IO */
2013
2014/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002015 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002017static int x509parse_key_pkcs1_der( rsa_context *rsa,
2018 const unsigned char *key,
2019 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002020{
Paul Bakker23986e52011-04-24 08:57:21 +00002021 int ret;
2022 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002024
Paul Bakker96743fc2011-02-12 14:30:57 +00002025 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002026 end = p + keylen;
2027
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002029 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002030 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 * RSAPrivateKey ::= SEQUENCE {
2032 * version Version,
2033 * modulus INTEGER, -- n
2034 * publicExponent INTEGER, -- e
2035 * privateExponent INTEGER, -- d
2036 * prime1 INTEGER, -- p
2037 * prime2 INTEGER, -- q
2038 * exponent1 INTEGER, -- d mod (p-1)
2039 * exponent2 INTEGER, -- d mod (q-1)
2040 * coefficient INTEGER, -- (inverse of q) mod p
2041 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2042 * }
2043 */
2044 if( ( ret = asn1_get_tag( &p, end, &len,
2045 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2046 {
Paul Bakker9d781402011-05-09 16:17:09 +00002047 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 }
2049
2050 end = p + len;
2051
2052 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2053 {
Paul Bakker9d781402011-05-09 16:17:09 +00002054 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 }
2056
2057 if( rsa->ver != 0 )
2058 {
Paul Bakker9d781402011-05-09 16:17:09 +00002059 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 }
2061
2062 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2063 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2064 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2065 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2066 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2067 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2068 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2069 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2070 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002072 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 }
2074
2075 rsa->len = mpi_size( &rsa->N );
2076
2077 if( p != end )
2078 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002080 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002081 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 }
2083
2084 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2085 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 rsa_free( rsa );
2087 return( ret );
2088 }
2089
Paul Bakkere2f50402013-06-24 19:00:59 +02002090 return( 0 );
2091}
2092
2093/*
2094 * Parse an unencrypted PKCS#8 encoded private RSA key
2095 */
2096static int x509parse_key_pkcs8_unencrypted_der(
2097 rsa_context *rsa,
2098 const unsigned char *key,
2099 size_t keylen )
2100{
2101 int ret;
2102 size_t len;
2103 unsigned char *p, *end;
2104 x509_buf pk_alg_oid;
2105 pk_type_t pk_alg = POLARSSL_PK_NONE;
2106
2107 p = (unsigned char *) key;
2108 end = p + keylen;
2109
2110 /*
2111 * This function parses the PrivatKeyInfo object (PKCS#8)
2112 *
2113 * PrivateKeyInfo ::= SEQUENCE {
2114 * version Version,
2115 * algorithm AlgorithmIdentifier,
2116 * PrivateKey BIT STRING
2117 * }
2118 *
2119 * AlgorithmIdentifier ::= SEQUENCE {
2120 * algorithm OBJECT IDENTIFIER,
2121 * parameters ANY DEFINED BY algorithm OPTIONAL
2122 * }
2123 *
2124 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2125 */
2126 if( ( ret = asn1_get_tag( &p, end, &len,
2127 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2128 {
2129 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2130 }
2131
2132 end = p + len;
2133
2134 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2135 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2136
2137 if( rsa->ver != 0 )
2138 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2139
2140 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2141 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2142
2143 /*
2144 * only RSA keys handled at this time
2145 */
2146 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2147 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2148
2149 /*
2150 * Get the OCTET STRING and parse the PKCS#1 format inside
2151 */
2152 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2153 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2154
2155 if( ( end - p ) < 1 )
2156 {
2157 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2158 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2159 }
2160
2161 end = p + len;
2162
2163 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2164 return( ret );
2165
2166 return( 0 );
2167}
2168
2169/*
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002170 * Parse an unencrypted PKCS#8 encoded private RSA key
2171 */
2172static int x509parse_key_pkcs8_encrypted_der(
2173 rsa_context *rsa,
2174 const unsigned char *key,
2175 size_t keylen,
2176 const unsigned char *pwd,
2177 size_t pwdlen )
2178{
2179 int ret;
2180 size_t len;
2181 unsigned char *p, *end, *end2;
2182 x509_buf pbe_alg_oid, pbe_params;
2183 unsigned char buf[2048];
2184
2185 memset(buf, 0, 2048);
2186
2187 p = (unsigned char *) key;
2188 end = p + keylen;
2189
2190 /*
2191 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2192 *
2193 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2194 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2195 * encryptedData EncryptedData
2196 * }
2197 *
2198 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2199 *
2200 * EncryptedData ::= OCTET STRING
2201 *
2202 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2203 */
2204 if( ( ret = asn1_get_tag( &p, end, &len,
2205 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2206 {
2207 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2208 }
2209
2210 end = p + len;
2211
2212 if( ( ret = asn1_get_tag( &p, end, &len,
2213 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2214 {
2215 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2216 }
2217
2218 end2 = p + len;
2219
2220 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2221 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2222
2223 pbe_alg_oid.p = p;
2224 p += pbe_alg_oid.len;
2225
2226 /*
2227 * Store the algorithm parameters
2228 */
2229 pbe_params.p = p;
2230 pbe_params.len = end2 - p;
2231 p += pbe_params.len;
2232
2233 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2234 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2235
2236 // buf has been sized to 2048 bytes
2237 if( len > 2048 )
2238 return( POLARSSL_ERR_X509_INVALID_INPUT );
2239
2240 /*
2241 * Decrypt EncryptedData with appropriate PDE
2242 */
2243 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2244 {
2245 if( ( ret = pkcs12_pbe_sha1_des3_ede_cbc( &pbe_params,
2246 PKCS12_PBE_DECRYPT,
2247 pwd, pwdlen,
2248 p, len, buf ) ) != 0 )
2249 {
2250 return( ret );
2251 }
2252 }
2253 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2254 {
2255 if( ( ret = pkcs12_pbe_sha1_des2_ede_cbc( &pbe_params,
2256 PKCS12_PBE_DECRYPT,
2257 pwd, pwdlen,
2258 p, len, buf ) ) != 0 )
2259 {
2260 return( ret );
2261 }
2262 }
2263 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2264 {
2265 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2266 PKCS12_PBE_DECRYPT,
2267 pwd, pwdlen,
2268 p, len, buf ) ) != 0 )
2269 {
2270 return( ret );
2271 }
2272 }
2273 else
2274 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2275
2276 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2277}
2278
2279/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002280 * Parse a private RSA key
2281 */
2282int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2283 const unsigned char *pwd, size_t pwdlen )
2284{
2285 int ret;
2286
Paul Bakker96743fc2011-02-12 14:30:57 +00002287#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002288 size_t len;
2289 pem_context pem;
2290
2291 pem_init( &pem );
2292 ret = pem_read_buffer( &pem,
2293 "-----BEGIN RSA PRIVATE KEY-----",
2294 "-----END RSA PRIVATE KEY-----",
2295 key, pwd, pwdlen, &len );
2296 if( ret == 0 )
2297 {
2298 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2299 {
2300 rsa_free( rsa );
2301 }
2302
2303 pem_free( &pem );
2304 return( ret );
2305 }
2306 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2307 {
2308 pem_free( &pem );
2309 return( ret );
2310 }
2311
2312 ret = pem_read_buffer( &pem,
2313 "-----BEGIN PRIVATE KEY-----",
2314 "-----END PRIVATE KEY-----",
2315 key, NULL, 0, &len );
2316 if( ret == 0 )
2317 {
2318 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2319 pem.buf, pem.buflen ) ) != 0 )
2320 {
2321 rsa_free( rsa );
2322 }
2323
2324 pem_free( &pem );
2325 return( ret );
2326 }
2327 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2328 {
2329 pem_free( &pem );
2330 return( ret );
2331 }
2332
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002333 ret = pem_read_buffer( &pem,
2334 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2335 "-----END ENCRYPTED PRIVATE KEY-----",
2336 key, NULL, 0, &len );
2337 if( ret == 0 )
2338 {
2339 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2340 pem.buf, pem.buflen,
2341 pwd, pwdlen ) ) != 0 )
2342 {
2343 rsa_free( rsa );
2344 }
2345
2346 pem_free( &pem );
2347 return( ret );
2348 }
2349 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2350 {
2351 pem_free( &pem );
2352 return( ret );
2353 }
Paul Bakkere2f50402013-06-24 19:00:59 +02002354#else
2355 ((void) pwd);
2356 ((void) pwdlen);
2357#endif /* POLARSSL_PEM_C */
2358
2359 // At this point we only know it's not a PEM formatted key. Could be any
2360 // of the known DER encoded private key formats
2361 //
2362 // We try the different DER format parsers to see if one passes without
2363 // error
2364 //
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002365 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2366 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002367 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002368 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002371 rsa_free( rsa );
2372 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2373 return( 0 );
2374
2375 rsa_free( rsa );
2376 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2377 return( 0 );
2378
2379 rsa_free( rsa );
2380 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002381}
2382
2383/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002384 * Parse a public RSA key
2385 */
Paul Bakker23986e52011-04-24 08:57:21 +00002386int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002387{
Paul Bakker23986e52011-04-24 08:57:21 +00002388 int ret;
2389 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002390 unsigned char *p, *end;
2391 x509_buf alg_oid;
2392#if defined(POLARSSL_PEM_C)
2393 pem_context pem;
2394
2395 pem_init( &pem );
2396 ret = pem_read_buffer( &pem,
2397 "-----BEGIN PUBLIC KEY-----",
2398 "-----END PUBLIC KEY-----",
2399 key, NULL, 0, &len );
2400
2401 if( ret == 0 )
2402 {
2403 /*
2404 * Was PEM encoded
2405 */
2406 keylen = pem.buflen;
2407 }
Paul Bakker00b28602013-06-24 13:02:41 +02002408 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002409 {
2410 pem_free( &pem );
2411 return( ret );
2412 }
2413
2414 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2415#else
2416 p = (unsigned char *) key;
2417#endif
2418 end = p + keylen;
2419
2420 /*
2421 * PublicKeyInfo ::= SEQUENCE {
2422 * algorithm AlgorithmIdentifier,
2423 * PublicKey BIT STRING
2424 * }
2425 *
2426 * AlgorithmIdentifier ::= SEQUENCE {
2427 * algorithm OBJECT IDENTIFIER,
2428 * parameters ANY DEFINED BY algorithm OPTIONAL
2429 * }
2430 *
2431 * RSAPublicKey ::= SEQUENCE {
2432 * modulus INTEGER, -- n
2433 * publicExponent INTEGER -- e
2434 * }
2435 */
2436
2437 if( ( ret = asn1_get_tag( &p, end, &len,
2438 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2439 {
2440#if defined(POLARSSL_PEM_C)
2441 pem_free( &pem );
2442#endif
2443 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002444 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002445 }
2446
2447 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2448 {
2449#if defined(POLARSSL_PEM_C)
2450 pem_free( &pem );
2451#endif
2452 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002453 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002454 }
2455
2456 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2457 {
2458#if defined(POLARSSL_PEM_C)
2459 pem_free( &pem );
2460#endif
2461 rsa_free( rsa );
2462 return( ret );
2463 }
2464
2465 rsa->len = mpi_size( &rsa->N );
2466
2467#if defined(POLARSSL_PEM_C)
2468 pem_free( &pem );
2469#endif
2470
2471 return( 0 );
2472}
2473
Paul Bakkereaa89f82011-04-04 21:36:15 +00002474#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002475/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002476 * Parse DHM parameters
2477 */
Paul Bakker23986e52011-04-24 08:57:21 +00002478int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002479{
Paul Bakker23986e52011-04-24 08:57:21 +00002480 int ret;
2481 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002482 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002483#if defined(POLARSSL_PEM_C)
2484 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002485
Paul Bakker96743fc2011-02-12 14:30:57 +00002486 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002487
Paul Bakker96743fc2011-02-12 14:30:57 +00002488 ret = pem_read_buffer( &pem,
2489 "-----BEGIN DH PARAMETERS-----",
2490 "-----END DH PARAMETERS-----",
2491 dhmin, NULL, 0, &dhminlen );
2492
2493 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002494 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002495 /*
2496 * Was PEM encoded
2497 */
2498 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002499 }
Paul Bakker00b28602013-06-24 13:02:41 +02002500 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002501 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002502 pem_free( &pem );
2503 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002504 }
2505
Paul Bakker96743fc2011-02-12 14:30:57 +00002506 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2507#else
2508 p = (unsigned char *) dhmin;
2509#endif
2510 end = p + dhminlen;
2511
Paul Bakker1b57b062011-01-06 15:48:19 +00002512 memset( dhm, 0, sizeof( dhm_context ) );
2513
Paul Bakker1b57b062011-01-06 15:48:19 +00002514 /*
2515 * DHParams ::= SEQUENCE {
2516 * prime INTEGER, -- P
2517 * generator INTEGER, -- g
2518 * }
2519 */
2520 if( ( ret = asn1_get_tag( &p, end, &len,
2521 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2522 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002523#if defined(POLARSSL_PEM_C)
2524 pem_free( &pem );
2525#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002526 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002527 }
2528
2529 end = p + len;
2530
2531 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2532 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2533 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002534#if defined(POLARSSL_PEM_C)
2535 pem_free( &pem );
2536#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002537 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002538 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002539 }
2540
2541 if( p != end )
2542 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002543#if defined(POLARSSL_PEM_C)
2544 pem_free( &pem );
2545#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002546 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002547 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002548 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2549 }
2550
Paul Bakker96743fc2011-02-12 14:30:57 +00002551#if defined(POLARSSL_PEM_C)
2552 pem_free( &pem );
2553#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002554
2555 return( 0 );
2556}
2557
Paul Bakker335db3f2011-04-25 15:28:35 +00002558#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002559/*
2560 * Load and parse a private RSA key
2561 */
2562int x509parse_dhmfile( dhm_context *dhm, const char *path )
2563{
2564 int ret;
2565 size_t n;
2566 unsigned char *buf;
2567
Paul Bakker69e095c2011-12-10 21:55:01 +00002568 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2569 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002570
Paul Bakker27fdf462011-06-09 13:55:13 +00002571 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002572
2573 memset( buf, 0, n + 1 );
2574 free( buf );
2575
2576 return( ret );
2577}
Paul Bakker335db3f2011-04-25 15:28:35 +00002578#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002579#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002580
Paul Bakker5121ce52009-01-03 21:22:43 +00002581#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002582#include <stdarg.h>
2583
2584#if !defined vsnprintf
2585#define vsnprintf _vsnprintf
2586#endif // vsnprintf
2587
2588/*
2589 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2590 * Result value is not size of buffer needed, but -1 if no fit is possible.
2591 *
2592 * This fuction tries to 'fix' this by at least suggesting enlarging the
2593 * size by 20.
2594 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002595static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002596{
2597 va_list ap;
2598 int res = -1;
2599
2600 va_start( ap, format );
2601
2602 res = vsnprintf( str, size, format, ap );
2603
2604 va_end( ap );
2605
2606 // No quick fix possible
2607 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002608 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002609
2610 return res;
2611}
2612
2613#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002614#endif
2615
Paul Bakkerd98030e2009-05-02 15:13:40 +00002616#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2617
2618#define SAFE_SNPRINTF() \
2619{ \
2620 if( ret == -1 ) \
2621 return( -1 ); \
2622 \
Paul Bakker23986e52011-04-24 08:57:21 +00002623 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002624 p[n - 1] = '\0'; \
2625 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2626 } \
2627 \
Paul Bakker23986e52011-04-24 08:57:21 +00002628 n -= (unsigned int) ret; \
2629 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002630}
2631
Paul Bakker5121ce52009-01-03 21:22:43 +00002632/*
2633 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002634 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002636int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002637{
Paul Bakker23986e52011-04-24 08:57:21 +00002638 int ret;
2639 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002641 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002642 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 char s[128], *p;
2644
2645 memset( s, 0, sizeof( s ) );
2646
2647 name = dn;
2648 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002649 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
2651 while( name != NULL )
2652 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002653 if( !name->oid.p )
2654 {
2655 name = name->next;
2656 continue;
2657 }
2658
Paul Bakker74111d32011-01-15 16:57:55 +00002659 if( name != dn )
2660 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002661 ret = snprintf( p, n, ", " );
2662 SAFE_SNPRINTF();
2663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Paul Bakkerc70b9822013-04-07 22:00:46 +02002665 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
Paul Bakkerc70b9822013-04-07 22:00:46 +02002667 if( ret == 0 )
2668 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002670 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002671 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
2673 for( i = 0; i < name->val.len; i++ )
2674 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002675 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 break;
2677
2678 c = name->val.p[i];
2679 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2680 s[i] = '?';
2681 else s[i] = c;
2682 }
2683 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002684 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002685 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 name = name->next;
2687 }
2688
Paul Bakker23986e52011-04-24 08:57:21 +00002689 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002690}
2691
2692/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002693 * Store the serial in printable form into buf; no more
2694 * than size characters will be written
2695 */
2696int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2697{
Paul Bakker23986e52011-04-24 08:57:21 +00002698 int ret;
2699 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002700 char *p;
2701
2702 p = buf;
2703 n = size;
2704
2705 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002706 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002707
2708 for( i = 0; i < nr; i++ )
2709 {
Paul Bakker93048802011-12-05 14:38:06 +00002710 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002711 continue;
2712
Paul Bakkerdd476992011-01-16 21:34:59 +00002713 ret = snprintf( p, n, "%02X%s",
2714 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2715 SAFE_SNPRINTF();
2716 }
2717
Paul Bakker03c7c252011-11-25 12:37:37 +00002718 if( nr != serial->len )
2719 {
2720 ret = snprintf( p, n, "...." );
2721 SAFE_SNPRINTF();
2722 }
2723
Paul Bakker23986e52011-04-24 08:57:21 +00002724 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002725}
2726
2727/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002728 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002730int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2731 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002732{
Paul Bakker23986e52011-04-24 08:57:21 +00002733 int ret;
2734 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002735 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002736 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
2738 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002739 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Paul Bakkerd98030e2009-05-02 15:13:40 +00002741 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002743 SAFE_SNPRINTF();
2744 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002746 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
Paul Bakkerdd476992011-01-16 21:34:59 +00002748 ret = x509parse_serial_gets( p, n, &crt->serial);
2749 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Paul Bakkerd98030e2009-05-02 15:13:40 +00002751 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2752 SAFE_SNPRINTF();
2753 ret = x509parse_dn_gets( p, n, &crt->issuer );
2754 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Paul Bakkerd98030e2009-05-02 15:13:40 +00002756 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2757 SAFE_SNPRINTF();
2758 ret = x509parse_dn_gets( p, n, &crt->subject );
2759 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
Paul Bakkerd98030e2009-05-02 15:13:40 +00002761 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2763 crt->valid_from.year, crt->valid_from.mon,
2764 crt->valid_from.day, crt->valid_from.hour,
2765 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002766 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
Paul Bakkerd98030e2009-05-02 15:13:40 +00002768 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2770 crt->valid_to.year, crt->valid_to.mon,
2771 crt->valid_to.day, crt->valid_to.hour,
2772 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002773 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Paul Bakkerc70b9822013-04-07 22:00:46 +02002775 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002776 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Paul Bakkerc70b9822013-04-07 22:00:46 +02002778 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2779 if( ret != 0 )
2780 ret = snprintf( p, n, "???" );
2781 else
2782 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002783 SAFE_SNPRINTF();
2784
2785 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002786 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002787 SAFE_SNPRINTF();
2788
Paul Bakker23986e52011-04-24 08:57:21 +00002789 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002790}
2791
Paul Bakker74111d32011-01-15 16:57:55 +00002792/*
2793 * Return an informational string describing the given OID
2794 */
2795const char *x509_oid_get_description( x509_buf *oid )
2796{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002797 const char *desc = NULL;
2798 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002799
Paul Bakkerc70b9822013-04-07 22:00:46 +02002800 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002801
Paul Bakkerc70b9822013-04-07 22:00:46 +02002802 if( ret != 0 )
2803 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002804
Paul Bakkerc70b9822013-04-07 22:00:46 +02002805 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002806}
2807
2808/* Return the x.y.z.... style numeric string for the given OID */
2809int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2810{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002811 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002812}
2813
Paul Bakkerd98030e2009-05-02 15:13:40 +00002814/*
2815 * Return an informational string about the CRL.
2816 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002817int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2818 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002819{
Paul Bakker23986e52011-04-24 08:57:21 +00002820 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002821 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002822 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002823 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002824 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002825
2826 p = buf;
2827 n = size;
2828
2829 ret = snprintf( p, n, "%sCRL version : %d",
2830 prefix, crl->version );
2831 SAFE_SNPRINTF();
2832
2833 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2834 SAFE_SNPRINTF();
2835 ret = x509parse_dn_gets( p, n, &crl->issuer );
2836 SAFE_SNPRINTF();
2837
2838 ret = snprintf( p, n, "\n%sthis update : " \
2839 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2840 crl->this_update.year, crl->this_update.mon,
2841 crl->this_update.day, crl->this_update.hour,
2842 crl->this_update.min, crl->this_update.sec );
2843 SAFE_SNPRINTF();
2844
2845 ret = snprintf( p, n, "\n%snext update : " \
2846 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2847 crl->next_update.year, crl->next_update.mon,
2848 crl->next_update.day, crl->next_update.hour,
2849 crl->next_update.min, crl->next_update.sec );
2850 SAFE_SNPRINTF();
2851
2852 entry = &crl->entry;
2853
2854 ret = snprintf( p, n, "\n%sRevoked certificates:",
2855 prefix );
2856 SAFE_SNPRINTF();
2857
Paul Bakker9be19372009-07-27 20:21:53 +00002858 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002859 {
2860 ret = snprintf( p, n, "\n%sserial number: ",
2861 prefix );
2862 SAFE_SNPRINTF();
2863
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002864 ret = x509parse_serial_gets( p, n, &entry->serial);
2865 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002866
Paul Bakkerd98030e2009-05-02 15:13:40 +00002867 ret = snprintf( p, n, " revocation date: " \
2868 "%04d-%02d-%02d %02d:%02d:%02d",
2869 entry->revocation_date.year, entry->revocation_date.mon,
2870 entry->revocation_date.day, entry->revocation_date.hour,
2871 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002872 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002873
2874 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002875 }
2876
Paul Bakkerc70b9822013-04-07 22:00:46 +02002877 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002878 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Paul Bakkerc70b9822013-04-07 22:00:46 +02002880 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2881 if( ret != 0 )
2882 ret = snprintf( p, n, "???" );
2883 else
2884 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002885 SAFE_SNPRINTF();
2886
Paul Bakker1e27bb22009-07-19 20:25:25 +00002887 ret = snprintf( p, n, "\n" );
2888 SAFE_SNPRINTF();
2889
Paul Bakker23986e52011-04-24 08:57:21 +00002890 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891}
2892
2893/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002894 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002896int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002897{
Paul Bakkercce9d772011-11-18 14:26:47 +00002898 int year, mon, day;
2899 int hour, min, sec;
2900
2901#if defined(_WIN32)
2902 SYSTEMTIME st;
2903
2904 GetLocalTime(&st);
2905
2906 year = st.wYear;
2907 mon = st.wMonth;
2908 day = st.wDay;
2909 hour = st.wHour;
2910 min = st.wMinute;
2911 sec = st.wSecond;
2912#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002913 struct tm *lt;
2914 time_t tt;
2915
2916 tt = time( NULL );
2917 lt = localtime( &tt );
2918
Paul Bakkercce9d772011-11-18 14:26:47 +00002919 year = lt->tm_year + 1900;
2920 mon = lt->tm_mon + 1;
2921 day = lt->tm_mday;
2922 hour = lt->tm_hour;
2923 min = lt->tm_min;
2924 sec = lt->tm_sec;
2925#endif
2926
2927 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002928 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Paul Bakkercce9d772011-11-18 14:26:47 +00002930 if( year == to->year &&
2931 mon > to->mon )
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 &&
2936 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002937 return( 1 );
2938
Paul Bakkercce9d772011-11-18 14:26:47 +00002939 if( year == to->year &&
2940 mon == to->mon &&
2941 day == to->day &&
2942 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002943 return( 1 );
2944
Paul Bakkercce9d772011-11-18 14:26:47 +00002945 if( year == to->year &&
2946 mon == to->mon &&
2947 day == to->day &&
2948 hour == to->hour &&
2949 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002950 return( 1 );
2951
Paul Bakkercce9d772011-11-18 14:26:47 +00002952 if( year == to->year &&
2953 mon == to->mon &&
2954 day == to->day &&
2955 hour == to->hour &&
2956 min == to->min &&
2957 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002958 return( 1 );
2959
Paul Bakker40ea7de2009-05-03 10:18:48 +00002960 return( 0 );
2961}
2962
2963/*
2964 * Return 1 if the certificate is revoked, or 0 otherwise.
2965 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002966int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002967{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002968 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002969
2970 while( cur != NULL && cur->serial.len != 0 )
2971 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002972 if( crt->serial.len == cur->serial.len &&
2973 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002974 {
2975 if( x509parse_time_expired( &cur->revocation_date ) )
2976 return( 1 );
2977 }
2978
2979 cur = cur->next;
2980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002981
2982 return( 0 );
2983}
2984
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002985/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002986 * Check that the given certificate is valid accoring to the CRL.
2987 */
2988static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2989 x509_crl *crl_list)
2990{
2991 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002992 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2993 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002994
Paul Bakker915275b2012-09-28 07:10:55 +00002995 if( ca == NULL )
2996 return( flags );
2997
Paul Bakker76fd75a2011-01-16 21:12:10 +00002998 /*
2999 * TODO: What happens if no CRL is present?
3000 * Suggestion: Revocation state should be unknown if no CRL is present.
3001 * For backwards compatibility this is not yet implemented.
3002 */
3003
Paul Bakker915275b2012-09-28 07:10:55 +00003004 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003005 {
Paul Bakker915275b2012-09-28 07:10:55 +00003006 if( crl_list->version == 0 ||
3007 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003008 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3009 crl_list->issuer_raw.len ) != 0 )
3010 {
3011 crl_list = crl_list->next;
3012 continue;
3013 }
3014
3015 /*
3016 * Check if CRL is correctly signed by the trusted CA
3017 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003018 md_info = md_info_from_type( crl_list->sig_md );
3019 if( md_info == NULL )
3020 {
3021 /*
3022 * Cannot check 'unknown' hash
3023 */
3024 flags |= BADCRL_NOT_TRUSTED;
3025 break;
3026 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003027
Paul Bakkerc70b9822013-04-07 22:00:46 +02003028 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003029
Paul Bakkerc70b9822013-04-07 22:00:46 +02003030 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003031 0, hash, crl_list->sig.p ) == 0 )
3032 {
3033 /*
3034 * CRL is not trusted
3035 */
3036 flags |= BADCRL_NOT_TRUSTED;
3037 break;
3038 }
3039
3040 /*
3041 * Check for validity of CRL (Do not drop out)
3042 */
3043 if( x509parse_time_expired( &crl_list->next_update ) )
3044 flags |= BADCRL_EXPIRED;
3045
3046 /*
3047 * Check if certificate is revoked
3048 */
3049 if( x509parse_revoked(crt, crl_list) )
3050 {
3051 flags |= BADCERT_REVOKED;
3052 break;
3053 }
3054
3055 crl_list = crl_list->next;
3056 }
3057 return flags;
3058}
3059
Paul Bakker57b12982012-02-11 17:38:38 +00003060int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003061{
3062 size_t i;
3063 size_t cn_idx = 0;
3064
Paul Bakker57b12982012-02-11 17:38:38 +00003065 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003066 return( 0 );
3067
3068 for( i = 0; i < strlen( cn ); ++i )
3069 {
3070 if( cn[i] == '.' )
3071 {
3072 cn_idx = i;
3073 break;
3074 }
3075 }
3076
3077 if( cn_idx == 0 )
3078 return( 0 );
3079
Paul Bakker535e97d2012-08-23 10:49:55 +00003080 if( strlen( cn ) - cn_idx == name->len - 1 &&
3081 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003082 {
3083 return( 1 );
3084 }
3085
3086 return( 0 );
3087}
3088
Paul Bakker915275b2012-09-28 07:10:55 +00003089static int x509parse_verify_top(
3090 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003091 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003092 int (*f_vrfy)(void *, x509_cert *, int, int *),
3093 void *p_vrfy )
3094{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003095 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003096 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003097 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3098 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003099
3100 if( x509parse_time_expired( &child->valid_to ) )
3101 *flags |= BADCERT_EXPIRED;
3102
3103 /*
3104 * Child is the top of the chain. Check against the trust_ca list.
3105 */
3106 *flags |= BADCERT_NOT_TRUSTED;
3107
3108 while( trust_ca != NULL )
3109 {
3110 if( trust_ca->version == 0 ||
3111 child->issuer_raw.len != trust_ca->subject_raw.len ||
3112 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3113 child->issuer_raw.len ) != 0 )
3114 {
3115 trust_ca = trust_ca->next;
3116 continue;
3117 }
3118
Paul Bakker9a736322012-11-14 12:39:52 +00003119 /*
3120 * Reduce path_len to check against if top of the chain is
3121 * the same as the trusted CA
3122 */
3123 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3124 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3125 child->issuer_raw.len ) == 0 )
3126 {
3127 check_path_cnt--;
3128 }
3129
Paul Bakker915275b2012-09-28 07:10:55 +00003130 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003131 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003132 {
3133 trust_ca = trust_ca->next;
3134 continue;
3135 }
3136
Paul Bakkerc70b9822013-04-07 22:00:46 +02003137 md_info = md_info_from_type( child->sig_md );
3138 if( md_info == NULL )
3139 {
3140 /*
3141 * Cannot check 'unknown' hash
3142 */
3143 continue;
3144 }
Paul Bakker915275b2012-09-28 07:10:55 +00003145
Paul Bakkerc70b9822013-04-07 22:00:46 +02003146 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003147
Paul Bakkerc70b9822013-04-07 22:00:46 +02003148 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003149 0, hash, child->sig.p ) != 0 )
3150 {
3151 trust_ca = trust_ca->next;
3152 continue;
3153 }
3154
3155 /*
3156 * Top of chain is signed by a trusted CA
3157 */
3158 *flags &= ~BADCERT_NOT_TRUSTED;
3159 break;
3160 }
3161
Paul Bakker9a736322012-11-14 12:39:52 +00003162 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003163 * If top of chain is not the same as the trusted CA send a verify request
3164 * to the callback for any issues with validity and CRL presence for the
3165 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003166 */
3167 if( trust_ca != NULL &&
3168 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3169 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3170 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003171 {
3172 /* Check trusted CA's CRL for then chain's top crt */
3173 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3174
3175 if( x509parse_time_expired( &trust_ca->valid_to ) )
3176 ca_flags |= BADCERT_EXPIRED;
3177
Paul Bakker915275b2012-09-28 07:10:55 +00003178 if( NULL != f_vrfy )
3179 {
Paul Bakker9a736322012-11-14 12:39:52 +00003180 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003181 return( ret );
3182 }
3183 }
3184
3185 /* Call callback on top cert */
3186 if( NULL != f_vrfy )
3187 {
Paul Bakker9a736322012-11-14 12:39:52 +00003188 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003189 return( ret );
3190 }
3191
Paul Bakker915275b2012-09-28 07:10:55 +00003192 *flags |= ca_flags;
3193
3194 return( 0 );
3195}
3196
3197static int x509parse_verify_child(
3198 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003199 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003200 int (*f_vrfy)(void *, x509_cert *, int, int *),
3201 void *p_vrfy )
3202{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003203 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003204 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003205 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003206 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003207 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003208
3209 if( x509parse_time_expired( &child->valid_to ) )
3210 *flags |= BADCERT_EXPIRED;
3211
Paul Bakkerc70b9822013-04-07 22:00:46 +02003212 md_info = md_info_from_type( child->sig_md );
3213 if( md_info == NULL )
3214 {
3215 /*
3216 * Cannot check 'unknown' hash
3217 */
Paul Bakker915275b2012-09-28 07:10:55 +00003218 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003219 }
3220 else
3221 {
3222 md( md_info, child->tbs.p, child->tbs.len, hash );
3223
3224 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3225 child->sig.p ) != 0 )
3226 *flags |= BADCERT_NOT_TRUSTED;
3227 }
3228
Paul Bakker915275b2012-09-28 07:10:55 +00003229 /* Check trusted CA's CRL for the given crt */
3230 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3231
3232 grandparent = parent->next;
3233
3234 while( grandparent != NULL )
3235 {
3236 if( grandparent->version == 0 ||
3237 grandparent->ca_istrue == 0 ||
3238 parent->issuer_raw.len != grandparent->subject_raw.len ||
3239 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3240 parent->issuer_raw.len ) != 0 )
3241 {
3242 grandparent = grandparent->next;
3243 continue;
3244 }
3245 break;
3246 }
3247
Paul Bakker915275b2012-09-28 07:10:55 +00003248 if( grandparent != NULL )
3249 {
3250 /*
3251 * Part of the chain
3252 */
Paul Bakker9a736322012-11-14 12:39:52 +00003253 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 +00003254 if( ret != 0 )
3255 return( ret );
3256 }
3257 else
3258 {
Paul Bakker9a736322012-11-14 12:39:52 +00003259 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 +00003260 if( ret != 0 )
3261 return( ret );
3262 }
3263
3264 /* child is verified to be a child of the parent, call verify callback */
3265 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003266 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003267 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003268
3269 *flags |= parent_flags;
3270
3271 return( 0 );
3272}
3273
Paul Bakker76fd75a2011-01-16 21:12:10 +00003274/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 * Verify the certificate validity
3276 */
3277int x509parse_verify( x509_cert *crt,
3278 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003279 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003280 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003281 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003282 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003283{
Paul Bakker23986e52011-04-24 08:57:21 +00003284 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003285 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003286 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003287 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003289 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Paul Bakker40ea7de2009-05-03 10:18:48 +00003291 *flags = 0;
3292
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 if( cn != NULL )
3294 {
3295 name = &crt->subject;
3296 cn_len = strlen( cn );
3297
Paul Bakker4d2c1242012-05-10 14:12:46 +00003298 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003300 cur = &crt->subject_alt_names;
3301
3302 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003303 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003304 if( cur->buf.len == cn_len &&
3305 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003306 break;
3307
Paul Bakker535e97d2012-08-23 10:49:55 +00003308 if( cur->buf.len > 2 &&
3309 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003310 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003311 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
Paul Bakker4d2c1242012-05-10 14:12:46 +00003313 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003314 }
3315
3316 if( cur == NULL )
3317 *flags |= BADCERT_CN_MISMATCH;
3318 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003319 else
3320 {
3321 while( name != NULL )
3322 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003323 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003324 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003325 if( name->val.len == cn_len &&
3326 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003327 break;
3328
Paul Bakker535e97d2012-08-23 10:49:55 +00003329 if( name->val.len > 2 &&
3330 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003331 x509_wildcard_verify( cn, &name->val ) )
3332 break;
3333 }
3334
3335 name = name->next;
3336 }
3337
3338 if( name == NULL )
3339 *flags |= BADCERT_CN_MISMATCH;
3340 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003341 }
3342
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003344 * Iterate upwards in the given cert chain, to find our crt parent.
3345 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003347 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003348
Paul Bakker76fd75a2011-01-16 21:12:10 +00003349 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003351 if( parent->ca_istrue == 0 ||
3352 crt->issuer_raw.len != parent->subject_raw.len ||
3353 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 crt->issuer_raw.len ) != 0 )
3355 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003356 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 continue;
3358 }
Paul Bakker915275b2012-09-28 07:10:55 +00003359 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 }
3361
Paul Bakker915275b2012-09-28 07:10:55 +00003362 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 {
Paul Bakker915275b2012-09-28 07:10:55 +00003364 /*
3365 * Part of the chain
3366 */
Paul Bakker9a736322012-11-14 12:39:52 +00003367 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003368 if( ret != 0 )
3369 return( ret );
3370 }
3371 else
Paul Bakker74111d32011-01-15 16:57:55 +00003372 {
Paul Bakker9a736322012-11-14 12:39:52 +00003373 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003374 if( ret != 0 )
3375 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003376 }
Paul Bakker915275b2012-09-28 07:10:55 +00003377
3378 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003379 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003380
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 return( 0 );
3382}
3383
3384/*
3385 * Unallocate all certificate data
3386 */
3387void x509_free( x509_cert *crt )
3388{
3389 x509_cert *cert_cur = crt;
3390 x509_cert *cert_prv;
3391 x509_name *name_cur;
3392 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003393 x509_sequence *seq_cur;
3394 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003395
3396 if( crt == NULL )
3397 return;
3398
3399 do
3400 {
3401 rsa_free( &cert_cur->rsa );
3402
3403 name_cur = cert_cur->issuer.next;
3404 while( name_cur != NULL )
3405 {
3406 name_prv = name_cur;
3407 name_cur = name_cur->next;
3408 memset( name_prv, 0, sizeof( x509_name ) );
3409 free( name_prv );
3410 }
3411
3412 name_cur = cert_cur->subject.next;
3413 while( name_cur != NULL )
3414 {
3415 name_prv = name_cur;
3416 name_cur = name_cur->next;
3417 memset( name_prv, 0, sizeof( x509_name ) );
3418 free( name_prv );
3419 }
3420
Paul Bakker74111d32011-01-15 16:57:55 +00003421 seq_cur = cert_cur->ext_key_usage.next;
3422 while( seq_cur != NULL )
3423 {
3424 seq_prv = seq_cur;
3425 seq_cur = seq_cur->next;
3426 memset( seq_prv, 0, sizeof( x509_sequence ) );
3427 free( seq_prv );
3428 }
3429
Paul Bakker8afa70d2012-02-11 18:42:45 +00003430 seq_cur = cert_cur->subject_alt_names.next;
3431 while( seq_cur != NULL )
3432 {
3433 seq_prv = seq_cur;
3434 seq_cur = seq_cur->next;
3435 memset( seq_prv, 0, sizeof( x509_sequence ) );
3436 free( seq_prv );
3437 }
3438
Paul Bakker5121ce52009-01-03 21:22:43 +00003439 if( cert_cur->raw.p != NULL )
3440 {
3441 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3442 free( cert_cur->raw.p );
3443 }
3444
3445 cert_cur = cert_cur->next;
3446 }
3447 while( cert_cur != NULL );
3448
3449 cert_cur = crt;
3450 do
3451 {
3452 cert_prv = cert_cur;
3453 cert_cur = cert_cur->next;
3454
3455 memset( cert_prv, 0, sizeof( x509_cert ) );
3456 if( cert_prv != crt )
3457 free( cert_prv );
3458 }
3459 while( cert_cur != NULL );
3460}
3461
Paul Bakkerd98030e2009-05-02 15:13:40 +00003462/*
3463 * Unallocate all CRL data
3464 */
3465void x509_crl_free( x509_crl *crl )
3466{
3467 x509_crl *crl_cur = crl;
3468 x509_crl *crl_prv;
3469 x509_name *name_cur;
3470 x509_name *name_prv;
3471 x509_crl_entry *entry_cur;
3472 x509_crl_entry *entry_prv;
3473
3474 if( crl == NULL )
3475 return;
3476
3477 do
3478 {
3479 name_cur = crl_cur->issuer.next;
3480 while( name_cur != NULL )
3481 {
3482 name_prv = name_cur;
3483 name_cur = name_cur->next;
3484 memset( name_prv, 0, sizeof( x509_name ) );
3485 free( name_prv );
3486 }
3487
3488 entry_cur = crl_cur->entry.next;
3489 while( entry_cur != NULL )
3490 {
3491 entry_prv = entry_cur;
3492 entry_cur = entry_cur->next;
3493 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3494 free( entry_prv );
3495 }
3496
3497 if( crl_cur->raw.p != NULL )
3498 {
3499 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3500 free( crl_cur->raw.p );
3501 }
3502
3503 crl_cur = crl_cur->next;
3504 }
3505 while( crl_cur != NULL );
3506
3507 crl_cur = crl;
3508 do
3509 {
3510 crl_prv = crl_cur;
3511 crl_cur = crl_cur->next;
3512
3513 memset( crl_prv, 0, sizeof( x509_crl ) );
3514 if( crl_prv != crl )
3515 free( crl_prv );
3516 }
3517 while( crl_cur != NULL );
3518}
3519
Paul Bakker40e46942009-01-03 21:51:57 +00003520#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003521
Paul Bakker40e46942009-01-03 21:51:57 +00003522#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
3524/*
3525 * Checkup routine
3526 */
3527int x509_self_test( int verbose )
3528{
Paul Bakker5690efc2011-05-26 13:16:06 +00003529#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003530 int ret;
3531 int flags;
3532 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003533 x509_cert cacert;
3534 x509_cert clicert;
3535 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003536#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003537 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003538#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
3540 if( verbose != 0 )
3541 printf( " X.509 certificate load: " );
3542
3543 memset( &clicert, 0, sizeof( x509_cert ) );
3544
Paul Bakker3c2122f2013-06-24 19:03:14 +02003545 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003546 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 if( ret != 0 )
3548 {
3549 if( verbose != 0 )
3550 printf( "failed\n" );
3551
3552 return( ret );
3553 }
3554
3555 memset( &cacert, 0, sizeof( x509_cert ) );
3556
Paul Bakker3c2122f2013-06-24 19:03:14 +02003557 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003558 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 if( ret != 0 )
3560 {
3561 if( verbose != 0 )
3562 printf( "failed\n" );
3563
3564 return( ret );
3565 }
3566
3567 if( verbose != 0 )
3568 printf( "passed\n X.509 private key load: " );
3569
3570 i = strlen( test_ca_key );
3571 j = strlen( test_ca_pwd );
3572
Paul Bakker66b78b22011-03-25 14:22:50 +00003573 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3574
Paul Bakker5121ce52009-01-03 21:22:43 +00003575 if( ( ret = x509parse_key( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003576 (const unsigned char *) test_ca_key, i,
3577 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 {
3579 if( verbose != 0 )
3580 printf( "failed\n" );
3581
3582 return( ret );
3583 }
3584
3585 if( verbose != 0 )
3586 printf( "passed\n X.509 signature verify: ");
3587
Paul Bakker23986e52011-04-24 08:57:21 +00003588 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003589 if( ret != 0 )
3590 {
Paul Bakker23986e52011-04-24 08:57:21 +00003591 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003592 if( verbose != 0 )
3593 printf( "failed\n" );
3594
3595 return( ret );
3596 }
3597
Paul Bakker5690efc2011-05-26 13:16:06 +00003598#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003600 printf( "passed\n X.509 DHM parameter load: " );
3601
3602 i = strlen( test_dhm_params );
3603 j = strlen( test_ca_pwd );
3604
Paul Bakker3c2122f2013-06-24 19:03:14 +02003605 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003606 {
3607 if( verbose != 0 )
3608 printf( "failed\n" );
3609
3610 return( ret );
3611 }
3612
3613 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003615#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003616
3617 x509_free( &cacert );
3618 x509_free( &clicert );
3619 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003620#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003621 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003622#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003623
3624 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003625#else
3626 ((void) verbose);
3627 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3628#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003629}
3630
3631#endif
3632
3633#endif