blob: 3968666c685895568de6a855449381a69e4c1736 [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 Bakker96743fc2011-02-12 14:30:57 +000043#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/des.h"
45#include "polarssl/md2.h"
46#include "polarssl/md4.h"
47#include "polarssl/md5.h"
48#include "polarssl/sha1.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000049#include "polarssl/sha2.h"
50#include "polarssl/sha4.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000051#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53#include <string.h>
54#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000055#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000056#include <windows.h>
57#else
Paul Bakker5121ce52009-01-03 21:22:43 +000058#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakker335db3f2011-04-25 15:28:35 +000061#if defined(POLARSSL_FS_IO)
62#include <stdio.h>
Paul Bakkere4791f32012-06-04 21:29:15 +000063#if defined(_WIN32)
64#include <strsafe.h>
65#else
Paul Bakker8d914582012-06-04 12:46:42 +000066#include <sys/types.h>
67#include <dirent.h>
68#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000069#endif
70
Paul Bakker5121ce52009-01-03 21:22:43 +000071/*
Paul Bakker5121ce52009-01-03 21:22:43 +000072 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
73 */
74static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000075 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000076 int *ver )
77{
Paul Bakker23986e52011-04-24 08:57:21 +000078 int ret;
79 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 if( ( ret = asn1_get_tag( p, end, &len,
82 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
83 {
Paul Bakker40e46942009-01-03 21:51:57 +000084 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000085 {
86 *ver = 0;
87 return( 0 );
88 }
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90 return( ret );
91 }
92
93 end = *p + len;
94
95 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000096 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +000099 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000100 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102 return( 0 );
103}
104
105/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000106 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000107 */
108static int x509_crl_get_version( unsigned char **p,
109 const unsigned char *end,
110 int *ver )
111{
112 int ret;
113
114 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
115 {
116 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000117 {
118 *ver = 0;
119 return( 0 );
120 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000121
122 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
123 }
124
125 return( 0 );
126}
127
128/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 * CertificateSerialNumber ::= INTEGER
130 */
131static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000132 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 x509_buf *serial )
134{
135 int ret;
136
137 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000138 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000139 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
142 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000143 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000144 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 serial->tag = *(*p)++;
147
148 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 serial->p = *p;
152 *p += serial->len;
153
154 return( 0 );
155}
156
157/*
158 * AlgorithmIdentifier ::= SEQUENCE {
159 * algorithm OBJECT IDENTIFIER,
160 * parameters ANY DEFINED BY algorithm OPTIONAL }
161 */
162static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000163 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 x509_buf *alg )
165{
Paul Bakker23986e52011-04-24 08:57:21 +0000166 int ret;
167 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 if( ( ret = asn1_get_tag( p, end, &len,
170 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000171 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 end = *p + len;
174 alg->tag = **p;
175
176 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000177 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 alg->p = *p;
180 *p += alg->len;
181
182 if( *p == end )
183 return( 0 );
184
185 /*
186 * assume the algorithm parameters must be NULL
187 */
188 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 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 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000192 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000193 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 return( 0 );
196}
197
198/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * AttributeTypeAndValue ::= SEQUENCE {
200 * type AttributeType,
201 * value AttributeValue }
202 *
203 * AttributeType ::= OBJECT IDENTIFIER
204 *
205 * AttributeValue ::= ANY DEFINED BY AttributeType
206 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000207static int x509_get_attr_type_value( unsigned char **p,
208 const unsigned char *end,
209 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210{
Paul Bakker23986e52011-04-24 08:57:21 +0000211 int ret;
212 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 x509_buf *oid;
214 x509_buf *val;
215
216 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000218 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 oid = &cur->oid;
221 oid->tag = **p;
222
223 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
226 oid->p = *p;
227 *p += oid->len;
228
229 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000230 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000231 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
234 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
235 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000236 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000237 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 val = &cur->val;
240 val->tag = *(*p)++;
241
242 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000243 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245 val->p = *p;
246 *p += val->len;
247
248 cur->next = NULL;
249
Paul Bakker400ff6f2011-02-20 10:40:16 +0000250 return( 0 );
251}
252
253/*
254 * RelativeDistinguishedName ::=
255 * SET OF AttributeTypeAndValue
256 *
257 * AttributeTypeAndValue ::= SEQUENCE {
258 * type AttributeType,
259 * value AttributeValue }
260 *
261 * AttributeType ::= OBJECT IDENTIFIER
262 *
263 * AttributeValue ::= ANY DEFINED BY AttributeType
264 */
265static int x509_get_name( unsigned char **p,
266 const unsigned char *end,
267 x509_name *cur )
268{
Paul Bakker23986e52011-04-24 08:57:21 +0000269 int ret;
270 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000271 const unsigned char *end2;
272 x509_name *use;
273
274 if( ( ret = asn1_get_tag( p, end, &len,
275 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000276 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277
278 end2 = end;
279 end = *p + len;
280 use = cur;
281
282 do
283 {
284 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
285 return( ret );
286
287 if( *p != end )
288 {
289 use->next = (x509_name *) malloc(
290 sizeof( x509_name ) );
291
292 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000293 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000294
295 memset( use->next, 0, sizeof( x509_name ) );
296
297 use = use->next;
298 }
299 }
300 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302 /*
303 * recurse until end of SEQUENCE is reached
304 */
305 if( *p == end2 )
306 return( 0 );
307
308 cur->next = (x509_name *) malloc(
309 sizeof( x509_name ) );
310
311 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000312 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
Paul Bakker430ffbe2012-05-01 08:14:20 +0000314 memset( cur->next, 0, sizeof( x509_name ) );
315
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 return( x509_get_name( p, end2, cur->next ) );
317}
318
319/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 * Time ::= CHOICE {
321 * utcTime UTCTime,
322 * generalTime GeneralizedTime }
323 */
Paul Bakker91200182010-02-18 21:26:15 +0000324static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000325 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000326 x509_time *time )
327{
Paul Bakker23986e52011-04-24 08:57:21 +0000328 int ret;
329 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000331 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332
Paul Bakker91200182010-02-18 21:26:15 +0000333 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000334 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
335 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336
Paul Bakker91200182010-02-18 21:26:15 +0000337 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 if ( tag == ASN1_UTC_TIME )
340 {
341 (*p)++;
342 ret = asn1_get_len( p, end, &len );
343
344 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000345 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000346
Paul Bakker91200182010-02-18 21:26:15 +0000347 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000348 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
349 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000350
Paul Bakker91200182010-02-18 21:26:15 +0000351 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
352 &time->year, &time->mon, &time->day,
353 &time->hour, &time->min, &time->sec ) < 5 )
354 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000355
Paul Bakker400ff6f2011-02-20 10:40:16 +0000356 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000357 time->year += 1900;
358
359 *p += len;
360
361 return( 0 );
362 }
363 else if ( tag == ASN1_GENERALIZED_TIME )
364 {
365 (*p)++;
366 ret = asn1_get_len( p, end, &len );
367
368 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000369 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000370
371 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000372 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
373 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000374
375 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
376 &time->year, &time->mon, &time->day,
377 &time->hour, &time->min, &time->sec ) < 5 )
378 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
379
380 *p += len;
381
382 return( 0 );
383 }
384 else
Paul Bakker9d781402011-05-09 16:17:09 +0000385 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000386}
387
388
389/*
390 * Validity ::= SEQUENCE {
391 * notBefore Time,
392 * notAfter Time }
393 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000394static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000395 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 x509_time *from,
397 x509_time *to )
398{
Paul Bakker23986e52011-04-24 08:57:21 +0000399 int ret;
400 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402 if( ( ret = asn1_get_tag( p, end, &len,
403 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000404 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 end = *p + len;
407
Paul Bakker91200182010-02-18 21:26:15 +0000408 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000409 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
Paul Bakker91200182010-02-18 21:26:15 +0000411 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000415 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000416 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418 return( 0 );
419}
420
421/*
422 * SubjectPublicKeyInfo ::= SEQUENCE {
423 * algorithm AlgorithmIdentifier,
424 * subjectPublicKey BIT STRING }
425 */
426static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000427 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 x509_buf *pk_alg_oid,
429 mpi *N, mpi *E )
430{
Paul Bakker23986e52011-04-24 08:57:21 +0000431 int ret, can_handle;
432 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 unsigned char *end2;
434
435 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
436 return( ret );
437
438 /*
439 * only RSA public keys handled at this time
440 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000441 can_handle = 0;
442
443 if( pk_alg_oid->len == 9 &&
444 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
445 can_handle = 1;
446
447 if( pk_alg_oid->len == 9 &&
448 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
449 {
450 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
451 can_handle = 1;
452
453 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
454 can_handle = 1;
455 }
456
457 if( pk_alg_oid->len == 5 &&
458 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
459 can_handle = 1;
460
461 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000462 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000465 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000468 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000469 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 end2 = *p + len;
472
473 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000474 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 /*
477 * RSAPublicKey ::= SEQUENCE {
478 * modulus INTEGER, -- n
479 * publicExponent INTEGER -- e
480 * }
481 */
482 if( ( ret = asn1_get_tag( p, end2, &len,
483 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
491 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000492 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000495 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000496 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 return( 0 );
499}
500
501static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000502 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 x509_buf *sig )
504{
Paul Bakker23986e52011-04-24 08:57:21 +0000505 int ret;
506 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakker8afa70d2012-02-11 18:42:45 +0000508 if( ( end - *p ) < 1 )
509 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
510 POLARSSL_ERR_ASN1_OUT_OF_DATA );
511
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 sig->tag = **p;
513
514 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000515 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker74111d32011-01-15 16:57:55 +0000517
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000519 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521 sig->len = len;
522 sig->p = *p;
523
524 *p += len;
525
526 return( 0 );
527}
528
529/*
530 * X.509 v2/v3 unique identifier (not parsed)
531 */
532static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000533 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 x509_buf *uid, int n )
535{
536 int ret;
537
538 if( *p == end )
539 return( 0 );
540
541 uid->tag = **p;
542
543 if( ( ret = asn1_get_tag( p, end, &uid->len,
544 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
545 {
Paul Bakker40e46942009-01-03 21:51:57 +0000546 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 return( 0 );
548
549 return( ret );
550 }
551
552 uid->p = *p;
553 *p += uid->len;
554
555 return( 0 );
556}
557
558/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000559 * X.509 Extensions (No parsing of extensions, pointer should
560 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
562static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000563 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000564 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565{
Paul Bakker23986e52011-04-24 08:57:21 +0000566 int ret;
567 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 if( *p == end )
570 return( 0 );
571
572 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000573
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000575 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 ext->p = *p;
579 end = *p + ext->len;
580
581 /*
582 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
583 *
584 * Extension ::= SEQUENCE {
585 * extnID OBJECT IDENTIFIER,
586 * critical BOOLEAN DEFAULT FALSE,
587 * extnValue OCTET STRING }
588 */
589 if( ( ret = asn1_get_tag( p, end, &len,
590 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000591 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000594 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000595 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Paul Bakkerd98030e2009-05-02 15:13:40 +0000597 return( 0 );
598}
599
600/*
601 * X.509 CRL v2 extensions (no extensions parsed yet.)
602 */
603static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000604 const unsigned char *end,
605 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000606{
Paul Bakker23986e52011-04-24 08:57:21 +0000607 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000608 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000609
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000610 /* Get explicit tag */
611 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000612 {
613 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
614 return( 0 );
615
616 return( ret );
617 }
618
619 while( *p < end )
620 {
621 if( ( ret = asn1_get_tag( p, end, &len,
622 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000623 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000624
625 *p += len;
626 }
627
628 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000629 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000630 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
631
632 return( 0 );
633}
634
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000635/*
636 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
637 */
638static int x509_get_crl_entry_ext( unsigned char **p,
639 const unsigned char *end,
640 x509_buf *ext )
641{
642 int ret;
643 size_t len = 0;
644
645 /* OPTIONAL */
646 if (end <= *p)
647 return( 0 );
648
649 ext->tag = **p;
650 ext->p = *p;
651
652 /*
653 * Get CRL-entry extension sequence header
654 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
655 */
656 if( ( ret = asn1_get_tag( p, end, &ext->len,
657 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
658 {
659 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
660 {
661 ext->p = NULL;
662 return( 0 );
663 }
664 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
665 }
666
667 end = *p + ext->len;
668
669 if( end != *p + ext->len )
670 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
671 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
672
673 while( *p < end )
674 {
675 if( ( ret = asn1_get_tag( p, end, &len,
676 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
677 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
678
679 *p += len;
680 }
681
682 if( *p != end )
683 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
684 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
685
686 return( 0 );
687}
688
Paul Bakker74111d32011-01-15 16:57:55 +0000689static int x509_get_basic_constraints( unsigned char **p,
690 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000691 int *ca_istrue,
692 int *max_pathlen )
693{
Paul Bakker23986e52011-04-24 08:57:21 +0000694 int ret;
695 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000696
697 /*
698 * BasicConstraints ::= SEQUENCE {
699 * cA BOOLEAN DEFAULT FALSE,
700 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
701 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000702 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000703 *max_pathlen = 0; /* endless */
704
705 if( ( ret = asn1_get_tag( p, end, &len,
706 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000707 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000708
709 if( *p == end )
710 return 0;
711
Paul Bakker3cccddb2011-01-16 21:46:31 +0000712 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000713 {
714 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000715 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000716
717 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000718 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000719
Paul Bakker3cccddb2011-01-16 21:46:31 +0000720 if( *ca_istrue != 0 )
721 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000722 }
723
724 if( *p == end )
725 return 0;
726
727 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000728 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000729
730 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000731 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000732 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
733
734 (*max_pathlen)++;
735
Paul Bakker74111d32011-01-15 16:57:55 +0000736 return 0;
737}
738
739static int x509_get_ns_cert_type( unsigned char **p,
740 const unsigned char *end,
741 unsigned char *ns_cert_type)
742{
743 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000744 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000745
746 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000747 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000748
749 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000750 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000751 POLARSSL_ERR_ASN1_INVALID_LENGTH );
752
753 /* Get actual bitstring */
754 *ns_cert_type = *bs.p;
755 return 0;
756}
757
758static int x509_get_key_usage( unsigned char **p,
759 const unsigned char *end,
760 unsigned char *key_usage)
761{
762 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000763 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000764
765 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000766 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000767
Paul Bakker94a67962012-08-23 13:03:52 +0000768 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000769 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000770 POLARSSL_ERR_ASN1_INVALID_LENGTH );
771
772 /* Get actual bitstring */
773 *key_usage = *bs.p;
774 return 0;
775}
776
Paul Bakkerd98030e2009-05-02 15:13:40 +0000777/*
Paul Bakker74111d32011-01-15 16:57:55 +0000778 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
779 *
780 * KeyPurposeId ::= OBJECT IDENTIFIER
781 */
782static int x509_get_ext_key_usage( unsigned char **p,
783 const unsigned char *end,
784 x509_sequence *ext_key_usage)
785{
786 int ret;
787
788 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000789 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000790
791 /* Sequence length must be >= 1 */
792 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000793 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000794 POLARSSL_ERR_ASN1_INVALID_LENGTH );
795
796 return 0;
797}
798
799/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000800 * SubjectAltName ::= GeneralNames
801 *
802 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
803 *
804 * GeneralName ::= CHOICE {
805 * otherName [0] OtherName,
806 * rfc822Name [1] IA5String,
807 * dNSName [2] IA5String,
808 * x400Address [3] ORAddress,
809 * directoryName [4] Name,
810 * ediPartyName [5] EDIPartyName,
811 * uniformResourceIdentifier [6] IA5String,
812 * iPAddress [7] OCTET STRING,
813 * registeredID [8] OBJECT IDENTIFIER }
814 *
815 * OtherName ::= SEQUENCE {
816 * type-id OBJECT IDENTIFIER,
817 * value [0] EXPLICIT ANY DEFINED BY type-id }
818 *
819 * EDIPartyName ::= SEQUENCE {
820 * nameAssigner [0] DirectoryString OPTIONAL,
821 * partyName [1] DirectoryString }
822 *
823 * NOTE: PolarSSL only parses and uses dNSName at this point.
824 */
825static int x509_get_subject_alt_name( unsigned char **p,
826 const unsigned char *end,
827 x509_sequence *subject_alt_name )
828{
829 int ret;
830 size_t len, tag_len;
831 asn1_buf *buf;
832 unsigned char tag;
833 asn1_sequence *cur = subject_alt_name;
834
835 /* Get main sequence tag */
836 if( ( ret = asn1_get_tag( p, end, &len,
837 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
838 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
839
840 if( *p + len != end )
841 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
842 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
843
844 while( *p < end )
845 {
846 if( ( end - *p ) < 1 )
847 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
848 POLARSSL_ERR_ASN1_OUT_OF_DATA );
849
850 tag = **p;
851 (*p)++;
852 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
853 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
854
855 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
856 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
857 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
858
859 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
860 {
861 *p += tag_len;
862 continue;
863 }
864
865 buf = &(cur->buf);
866 buf->tag = tag;
867 buf->p = *p;
868 buf->len = tag_len;
869 *p += buf->len;
870
871 /* Allocate and assign next pointer */
872 if (*p < end)
873 {
874 cur->next = (asn1_sequence *) malloc(
875 sizeof( asn1_sequence ) );
876
877 if( cur->next == NULL )
878 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
879 POLARSSL_ERR_ASN1_MALLOC_FAILED );
880
Paul Bakker535e97d2012-08-23 10:49:55 +0000881 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000882 cur = cur->next;
883 }
884 }
885
886 /* Set final sequence entry's next pointer to NULL */
887 cur->next = NULL;
888
889 if( *p != end )
890 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
891 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
892
893 return( 0 );
894}
895
896/*
Paul Bakker74111d32011-01-15 16:57:55 +0000897 * X.509 v3 extensions
898 *
899 * TODO: Perform all of the basic constraints tests required by the RFC
900 * TODO: Set values for undetected extensions to a sane default?
901 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000902 */
903static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000904 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000905 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000906{
Paul Bakker23986e52011-04-24 08:57:21 +0000907 int ret;
908 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000909 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000910
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000911 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000912 {
913 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
914 return( 0 );
915
916 return( ret );
917 }
918
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 while( *p < end )
920 {
Paul Bakker74111d32011-01-15 16:57:55 +0000921 /*
922 * Extension ::= SEQUENCE {
923 * extnID OBJECT IDENTIFIER,
924 * critical BOOLEAN DEFAULT FALSE,
925 * extnValue OCTET STRING }
926 */
927 x509_buf extn_oid = {0, 0, NULL};
928 int is_critical = 0; /* DEFAULT FALSE */
929
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 if( ( ret = asn1_get_tag( p, end, &len,
931 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000932 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000934 end_ext_data = *p + len;
935
Paul Bakker74111d32011-01-15 16:57:55 +0000936 /* Get extension ID */
937 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Paul Bakker74111d32011-01-15 16:57:55 +0000939 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000940 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Paul Bakker74111d32011-01-15 16:57:55 +0000942 extn_oid.p = *p;
943 *p += extn_oid.len;
944
945 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000946 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000947 POLARSSL_ERR_ASN1_OUT_OF_DATA );
948
949 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000950 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000951 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000952 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Paul Bakker74111d32011-01-15 16:57:55 +0000954 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000955 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000957 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000959 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000960
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000961 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000962 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000963 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Paul Bakker74111d32011-01-15 16:57:55 +0000965 /*
966 * Detect supported extensions
967 */
968 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
969 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 {
Paul Bakker74111d32011-01-15 16:57:55 +0000971 /* Parse basic constraints */
972 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000973 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000974 return ( ret );
975 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 }
Paul Bakker74111d32011-01-15 16:57:55 +0000977 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
978 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
979 {
980 /* Parse netscape certificate type */
981 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
982 &crt->ns_cert_type ) ) != 0 )
983 return ( ret );
984 crt->ext_types |= EXT_NS_CERT_TYPE;
985 }
986 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
987 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
988 {
989 /* Parse key usage */
990 if( ( ret = x509_get_key_usage( p, end_ext_octet,
991 &crt->key_usage ) ) != 0 )
992 return ( ret );
993 crt->ext_types |= EXT_KEY_USAGE;
994 }
995 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
996 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
997 {
998 /* Parse extended key usage */
999 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1000 &crt->ext_key_usage ) ) != 0 )
1001 return ( ret );
1002 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1003 }
Paul Bakkera8cd2392012-02-11 16:09:32 +00001004 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == extn_oid.len ) &&
1005 memcmp( extn_oid.p, OID_SUBJECT_ALT_NAME, extn_oid.len ) == 0 )
1006 {
1007 /* Parse extended key usage */
1008 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1009 &crt->subject_alt_names ) ) != 0 )
1010 return ( ret );
1011 crt->ext_types |= EXT_SUBJECT_ALT_NAME;
1012 }
Paul Bakker74111d32011-01-15 16:57:55 +00001013 else
1014 {
1015 /* No parser found, skip extension */
1016 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Paul Bakker5c721f92011-07-27 16:51:09 +00001018#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001019 if( is_critical )
1020 {
1021 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001022 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001023 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1024 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001025#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001026 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 }
1028
1029 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001030 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001031 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 return( 0 );
1034}
1035
1036/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001037 * X.509 CRL Entries
1038 */
1039static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001040 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001041 x509_crl_entry *entry )
1042{
Paul Bakker23986e52011-04-24 08:57:21 +00001043 int ret;
1044 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001045 x509_crl_entry *cur_entry = entry;
1046
1047 if( *p == end )
1048 return( 0 );
1049
Paul Bakker9be19372009-07-27 20:21:53 +00001050 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001051 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1052 {
1053 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1054 return( 0 );
1055
1056 return( ret );
1057 }
1058
Paul Bakker9be19372009-07-27 20:21:53 +00001059 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001060
1061 while( *p < end )
1062 {
Paul Bakker23986e52011-04-24 08:57:21 +00001063 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001064 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001065
1066 if( ( ret = asn1_get_tag( p, end, &len2,
1067 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1068 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001069 return( ret );
1070 }
1071
Paul Bakker9be19372009-07-27 20:21:53 +00001072 cur_entry->raw.tag = **p;
1073 cur_entry->raw.p = *p;
1074 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001075 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001076
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001077 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001078 return( ret );
1079
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001080 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001081 return( ret );
1082
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001083 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001084 return( ret );
1085
Paul Bakker74111d32011-01-15 16:57:55 +00001086 if ( *p < end )
1087 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001088 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001089
1090 if( cur_entry->next == NULL )
1091 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1092
Paul Bakkerd98030e2009-05-02 15:13:40 +00001093 cur_entry = cur_entry->next;
1094 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1095 }
1096 }
1097
1098 return( 0 );
1099}
1100
Paul Bakker27d66162010-03-17 06:56:01 +00001101static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1102{
1103 if( sig_oid->len == 9 &&
1104 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1105 {
1106 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1107 {
1108 *sig_alg = sig_oid->p[8];
1109 return( 0 );
1110 }
1111
1112 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1113 {
1114 *sig_alg = sig_oid->p[8];
1115 return( 0 );
1116 }
1117
1118 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1119 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001120 if( sig_oid->len == 5 &&
1121 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1122 {
1123 *sig_alg = SIG_RSA_SHA1;
1124 return( 0 );
1125 }
Paul Bakker27d66162010-03-17 06:56:01 +00001126
1127 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1128}
1129
Paul Bakkerd98030e2009-05-02 15:13:40 +00001130/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001131 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001133int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001134{
Paul Bakker23986e52011-04-24 08:57:21 +00001135 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001136 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001137 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Paul Bakker320a4b52009-03-28 18:52:39 +00001139 /*
1140 * Check for valid input
1141 */
1142 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001143 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001144
Paul Bakker96743fc2011-02-12 14:30:57 +00001145 p = (unsigned char *) malloc( len = buflen );
1146
1147 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001148 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001149
1150 memcpy( p, buf, buflen );
1151
1152 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
1154 crt->raw.p = p;
1155 crt->raw.len = len;
1156 end = p + len;
1157
1158 /*
1159 * Certificate ::= SEQUENCE {
1160 * tbsCertificate TBSCertificate,
1161 * signatureAlgorithm AlgorithmIdentifier,
1162 * signatureValue BIT STRING }
1163 */
1164 if( ( ret = asn1_get_tag( &p, end, &len,
1165 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1166 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001167 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001168 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 }
1170
Paul Bakkerb00ca422012-09-25 12:10:00 +00001171 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001173 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001174 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001175 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001177 crt_end = p + len;
1178
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 /*
1180 * TBSCertificate ::= SEQUENCE {
1181 */
1182 crt->tbs.p = p;
1183
1184 if( ( ret = asn1_get_tag( &p, end, &len,
1185 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1186 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001187 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001188 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 }
1190
1191 end = p + len;
1192 crt->tbs.len = end - crt->tbs.p;
1193
1194 /*
1195 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1196 *
1197 * CertificateSerialNumber ::= INTEGER
1198 *
1199 * signature AlgorithmIdentifier
1200 */
1201 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1202 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1203 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1204 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001205 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 return( ret );
1207 }
1208
1209 crt->version++;
1210
1211 if( crt->version > 3 )
1212 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001213 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001214 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 }
1216
Paul Bakker27d66162010-03-17 06:56:01 +00001217 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001219 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001220 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 }
1222
1223 /*
1224 * issuer Name
1225 */
1226 crt->issuer_raw.p = p;
1227
1228 if( ( ret = asn1_get_tag( &p, end, &len,
1229 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1230 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001231 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001232 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 }
1234
1235 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1236 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001237 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 return( ret );
1239 }
1240
1241 crt->issuer_raw.len = p - crt->issuer_raw.p;
1242
1243 /*
1244 * Validity ::= SEQUENCE {
1245 * notBefore Time,
1246 * notAfter Time }
1247 *
1248 */
1249 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1250 &crt->valid_to ) ) != 0 )
1251 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001252 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 return( ret );
1254 }
1255
1256 /*
1257 * subject Name
1258 */
1259 crt->subject_raw.p = p;
1260
1261 if( ( ret = asn1_get_tag( &p, end, &len,
1262 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1263 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001264 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001265 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 }
1267
Paul Bakkercefb3962012-06-27 11:51:09 +00001268 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001270 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 return( ret );
1272 }
1273
1274 crt->subject_raw.len = p - crt->subject_raw.p;
1275
1276 /*
1277 * SubjectPublicKeyInfo ::= SEQUENCE
1278 * algorithm AlgorithmIdentifier,
1279 * subjectPublicKey BIT STRING }
1280 */
1281 if( ( ret = asn1_get_tag( &p, end, &len,
1282 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1283 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001284 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001285 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 }
1287
1288 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1289 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 return( ret );
1293 }
1294
1295 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 return( ret );
1299 }
1300
1301 crt->rsa.len = mpi_size( &crt->rsa.N );
1302
1303 /*
1304 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1305 * -- If present, version shall be v2 or v3
1306 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1307 * -- If present, version shall be v2 or v3
1308 * extensions [3] EXPLICIT Extensions OPTIONAL
1309 * -- If present, version shall be v3
1310 */
1311 if( crt->version == 2 || crt->version == 3 )
1312 {
1313 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1314 if( ret != 0 )
1315 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001316 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 return( ret );
1318 }
1319 }
1320
1321 if( crt->version == 2 || crt->version == 3 )
1322 {
1323 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1324 if( ret != 0 )
1325 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001326 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 return( ret );
1328 }
1329 }
1330
1331 if( crt->version == 3 )
1332 {
Paul Bakker74111d32011-01-15 16:57:55 +00001333 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 if( ret != 0 )
1335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 return( ret );
1338 }
1339 }
1340
1341 if( p != end )
1342 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001343 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001344 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001345 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
Paul Bakkerb00ca422012-09-25 12:10:00 +00001348 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
1350 /*
1351 * signatureAlgorithm AlgorithmIdentifier,
1352 * signatureValue BIT STRING
1353 */
1354 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1355 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001356 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 return( ret );
1358 }
1359
Paul Bakker535e97d2012-08-23 10:49:55 +00001360 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1361 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001363 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001364 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 }
1366
1367 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1368 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001369 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 return( ret );
1371 }
1372
1373 if( p != end )
1374 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001375 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001376 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001377 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 }
1379
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001380 return( 0 );
1381}
1382
1383/*
1384 * Parse one or more PEM certificates from a buffer and add them to the chained list
1385 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001386int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001387{
Paul Bakker69e095c2011-12-10 21:55:01 +00001388 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001389 x509_cert *crt, *prev = NULL;
1390 int buf_format = X509_FORMAT_DER;
1391
1392 crt = chain;
1393
1394 /*
1395 * Check for valid input
1396 */
1397 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001398 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001399
1400 while( crt->version != 0 && crt->next != NULL )
1401 {
1402 prev = crt;
1403 crt = crt->next;
1404 }
1405
1406 /*
1407 * Add new certificate on the end of the chain if needed.
1408 */
1409 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001410 {
1411 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1412
Paul Bakker7d06ad22009-05-02 15:53:56 +00001413 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001414 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001415
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001416 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001417 crt = crt->next;
1418 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001421 /*
1422 * Determine buffer content. Buffer contains either one DER certificate or
1423 * one or more PEM certificates.
1424 */
1425#if defined(POLARSSL_PEM_C)
1426 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1427 buf_format = X509_FORMAT_PEM;
1428#endif
1429
1430 if( buf_format == X509_FORMAT_DER )
1431 return x509parse_crt_der( crt, buf, buflen );
1432
1433#if defined(POLARSSL_PEM_C)
1434 if( buf_format == X509_FORMAT_PEM )
1435 {
1436 pem_context pem;
1437
1438 while( buflen > 0 )
1439 {
1440 size_t use_len;
1441 pem_init( &pem );
1442
1443 ret = pem_read_buffer( &pem,
1444 "-----BEGIN CERTIFICATE-----",
1445 "-----END CERTIFICATE-----",
1446 buf, NULL, 0, &use_len );
1447
1448 if( ret == 0 )
1449 {
1450 /*
1451 * Was PEM encoded
1452 */
1453 buflen -= use_len;
1454 buf += use_len;
1455 }
1456 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1457 {
1458 pem_free( &pem );
1459
1460 if( first_error == 0 )
1461 first_error = ret;
1462
1463 continue;
1464 }
1465 else
1466 break;
1467
1468 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1469
1470 pem_free( &pem );
1471
1472 if( ret != 0 )
1473 {
1474 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001475 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001476 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001477 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001478 {
1479 if( prev )
1480 prev->next = NULL;
1481
1482 if( crt != chain )
1483 free( crt );
1484
1485 return( ret );
1486 }
1487
1488 if( first_error == 0 )
1489 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001490
1491 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001492
1493 memset( crt, 0, sizeof( x509_cert ) );
1494 continue;
1495 }
1496
1497 success = 1;
1498
1499 /*
1500 * Add new certificate to the list
1501 */
1502 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1503
1504 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001505 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001506
1507 prev = crt;
1508 crt = crt->next;
1509 memset( crt, 0, sizeof( x509_cert ) );
1510 }
1511 }
1512#endif
1513
1514 if( crt->version == 0 )
1515 {
1516 if( prev )
1517 prev->next = NULL;
1518
1519 if( crt != chain )
1520 free( crt );
1521 }
1522
1523 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001524 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001525 else if( first_error )
1526 return( first_error );
1527 else
1528 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529}
1530
1531/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001532 * Parse one or more CRLs and add them to the chained list
1533 */
Paul Bakker23986e52011-04-24 08:57:21 +00001534int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001535{
Paul Bakker23986e52011-04-24 08:57:21 +00001536 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001537 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001538 unsigned char *p, *end;
1539 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001540#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001541 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001542 pem_context pem;
1543#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001544
1545 crl = chain;
1546
1547 /*
1548 * Check for valid input
1549 */
1550 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001551 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001552
1553 while( crl->version != 0 && crl->next != NULL )
1554 crl = crl->next;
1555
1556 /*
1557 * Add new CRL on the end of the chain if needed.
1558 */
1559 if ( crl->version != 0 && crl->next == NULL)
1560 {
1561 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1562
Paul Bakker7d06ad22009-05-02 15:53:56 +00001563 if( crl->next == NULL )
1564 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001565 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001566 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001567 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001568
Paul Bakker7d06ad22009-05-02 15:53:56 +00001569 crl = crl->next;
1570 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001571 }
1572
Paul Bakker96743fc2011-02-12 14:30:57 +00001573#if defined(POLARSSL_PEM_C)
1574 pem_init( &pem );
1575 ret = pem_read_buffer( &pem,
1576 "-----BEGIN X509 CRL-----",
1577 "-----END X509 CRL-----",
1578 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001579
Paul Bakker96743fc2011-02-12 14:30:57 +00001580 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001581 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001582 /*
1583 * Was PEM encoded
1584 */
1585 buflen -= use_len;
1586 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587
1588 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001589 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001590 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001591 p = pem.buf;
1592 pem.buf = NULL;
1593 len = pem.buflen;
1594 pem_free( &pem );
1595 }
1596 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1597 {
1598 pem_free( &pem );
1599 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001600 }
1601 else
1602 {
1603 /*
1604 * nope, copy the raw DER data
1605 */
1606 p = (unsigned char *) malloc( len = buflen );
1607
1608 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001609 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001610
1611 memcpy( p, buf, buflen );
1612
1613 buflen = 0;
1614 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001615#else
1616 p = (unsigned char *) malloc( len = buflen );
1617
1618 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001619 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001620
1621 memcpy( p, buf, buflen );
1622
1623 buflen = 0;
1624#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001625
1626 crl->raw.p = p;
1627 crl->raw.len = len;
1628 end = p + len;
1629
1630 /*
1631 * CertificateList ::= SEQUENCE {
1632 * tbsCertList TBSCertList,
1633 * signatureAlgorithm AlgorithmIdentifier,
1634 * signatureValue BIT STRING }
1635 */
1636 if( ( ret = asn1_get_tag( &p, end, &len,
1637 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1638 {
1639 x509_crl_free( crl );
1640 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1641 }
1642
Paul Bakker23986e52011-04-24 08:57:21 +00001643 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001644 {
1645 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001646 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001647 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1648 }
1649
1650 /*
1651 * TBSCertList ::= SEQUENCE {
1652 */
1653 crl->tbs.p = p;
1654
1655 if( ( ret = asn1_get_tag( &p, end, &len,
1656 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1657 {
1658 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001659 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001660 }
1661
1662 end = p + len;
1663 crl->tbs.len = end - crl->tbs.p;
1664
1665 /*
1666 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1667 * -- if present, MUST be v2
1668 *
1669 * signature AlgorithmIdentifier
1670 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001671 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1673 {
1674 x509_crl_free( crl );
1675 return( ret );
1676 }
1677
1678 crl->version++;
1679
1680 if( crl->version > 2 )
1681 {
1682 x509_crl_free( crl );
1683 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1684 }
1685
Paul Bakker27d66162010-03-17 06:56:01 +00001686 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687 {
1688 x509_crl_free( crl );
1689 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1690 }
1691
1692 /*
1693 * issuer Name
1694 */
1695 crl->issuer_raw.p = p;
1696
1697 if( ( ret = asn1_get_tag( &p, end, &len,
1698 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1699 {
1700 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001701 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001702 }
1703
1704 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1705 {
1706 x509_crl_free( crl );
1707 return( ret );
1708 }
1709
1710 crl->issuer_raw.len = p - crl->issuer_raw.p;
1711
1712 /*
1713 * thisUpdate Time
1714 * nextUpdate Time OPTIONAL
1715 */
Paul Bakker91200182010-02-18 21:26:15 +00001716 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001717 {
1718 x509_crl_free( crl );
1719 return( ret );
1720 }
1721
Paul Bakker91200182010-02-18 21:26:15 +00001722 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001723 {
Paul Bakker9d781402011-05-09 16:17:09 +00001724 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001725 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001726 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001727 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001728 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001729 x509_crl_free( crl );
1730 return( ret );
1731 }
1732 }
1733
1734 /*
1735 * revokedCertificates SEQUENCE OF SEQUENCE {
1736 * userCertificate CertificateSerialNumber,
1737 * revocationDate Time,
1738 * crlEntryExtensions Extensions OPTIONAL
1739 * -- if present, MUST be v2
1740 * } OPTIONAL
1741 */
1742 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1743 {
1744 x509_crl_free( crl );
1745 return( ret );
1746 }
1747
1748 /*
1749 * crlExtensions EXPLICIT Extensions OPTIONAL
1750 * -- if present, MUST be v2
1751 */
1752 if( crl->version == 2 )
1753 {
1754 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1755
1756 if( ret != 0 )
1757 {
1758 x509_crl_free( crl );
1759 return( ret );
1760 }
1761 }
1762
1763 if( p != end )
1764 {
1765 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001766 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001767 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1768 }
1769
1770 end = crl->raw.p + crl->raw.len;
1771
1772 /*
1773 * signatureAlgorithm AlgorithmIdentifier,
1774 * signatureValue BIT STRING
1775 */
1776 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1777 {
1778 x509_crl_free( crl );
1779 return( ret );
1780 }
1781
Paul Bakker535e97d2012-08-23 10:49:55 +00001782 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1783 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001784 {
1785 x509_crl_free( crl );
1786 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1787 }
1788
1789 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1790 {
1791 x509_crl_free( crl );
1792 return( ret );
1793 }
1794
1795 if( p != end )
1796 {
1797 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001798 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1800 }
1801
1802 if( buflen > 0 )
1803 {
1804 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1805
Paul Bakker7d06ad22009-05-02 15:53:56 +00001806 if( crl->next == NULL )
1807 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001808 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001809 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001810 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001811
Paul Bakker7d06ad22009-05-02 15:53:56 +00001812 crl = crl->next;
1813 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814
1815 return( x509parse_crl( crl, buf, buflen ) );
1816 }
1817
1818 return( 0 );
1819}
1820
Paul Bakker335db3f2011-04-25 15:28:35 +00001821#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001822/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001823 * Load all data from a file into a given buffer.
1824 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001825int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001826{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001827 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001828
Paul Bakkerd98030e2009-05-02 15:13:40 +00001829 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001830 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001831
Paul Bakkerd98030e2009-05-02 15:13:40 +00001832 fseek( f, 0, SEEK_END );
1833 *n = (size_t) ftell( f );
1834 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001835
Paul Bakkerd98030e2009-05-02 15:13:40 +00001836 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001837 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001838
Paul Bakkerd98030e2009-05-02 15:13:40 +00001839 if( fread( *buf, 1, *n, f ) != *n )
1840 {
1841 fclose( f );
1842 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001843 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001844 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001845
Paul Bakkerd98030e2009-05-02 15:13:40 +00001846 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001847
Paul Bakkerd98030e2009-05-02 15:13:40 +00001848 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001849
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001851}
1852
1853/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 * Load one or more certificates and add them to the chained list
1855 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001856int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001857{
1858 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 size_t n;
1860 unsigned char *buf;
1861
Paul Bakker69e095c2011-12-10 21:55:01 +00001862 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1863 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
Paul Bakker69e095c2011-12-10 21:55:01 +00001865 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001866
1867 memset( buf, 0, n + 1 );
1868 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
1870 return( ret );
1871}
1872
Paul Bakker8d914582012-06-04 12:46:42 +00001873int x509parse_crtpath( x509_cert *chain, const char *path )
1874{
1875 int ret = 0;
1876#if defined(_WIN32)
1877 int t_ret;
1878 TCHAR szDir[MAX_PATH];
1879 WIN32_FIND_DATA file_data;
1880 HANDLE hFind;
1881 DWORD dwError = 0;
1882
1883 StringCchCopy(szDir, MAX_PATH, path);
1884 StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
1885
1886 hFind = FindFirstFile( szDir, &file_data );
1887 if (hFind == INVALID_HANDLE_VALUE)
1888 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1889
1890 do
1891 {
Paul Bakkere4791f32012-06-04 21:29:15 +00001892 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001893 continue;
1894
1895 t_ret = x509parse_crtfile( chain, entry_name );
1896 if( t_ret < 0 )
1897 return( t_ret );
1898
1899 ret += t_ret;
1900 }
1901 while( FindNextFile( hFind, &file_data ) != 0 );
1902
1903 dwError = GetLastError();
1904 if (dwError != ERROR_NO_MORE_FILES)
1905 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1906
1907 FindClose( hFind );
1908#else
1909 int t_ret;
1910 struct dirent *entry;
1911 char entry_name[255];
1912 DIR *dir = opendir( path );
1913
1914 if( dir == NULL)
1915 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1916
1917 while( ( entry = readdir( dir ) ) != NULL )
1918 {
1919 if( entry->d_type != DT_REG )
1920 continue;
1921
1922 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1923 t_ret = x509parse_crtfile( chain, entry_name );
1924 if( t_ret < 0 )
1925 return( t_ret );
1926
1927 ret += t_ret;
1928 }
1929 closedir( dir );
1930#endif
1931
1932 return( ret );
1933}
1934
Paul Bakkerd98030e2009-05-02 15:13:40 +00001935/*
1936 * Load one or more CRLs and add them to the chained list
1937 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001938int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001939{
1940 int ret;
1941 size_t n;
1942 unsigned char *buf;
1943
Paul Bakker69e095c2011-12-10 21:55:01 +00001944 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1945 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001946
Paul Bakker27fdf462011-06-09 13:55:13 +00001947 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001948
1949 memset( buf, 0, n + 1 );
1950 free( buf );
1951
1952 return( ret );
1953}
1954
Paul Bakker5121ce52009-01-03 21:22:43 +00001955/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001956 * Load and parse a private RSA key
1957 */
1958int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1959{
1960 int ret;
1961 size_t n;
1962 unsigned char *buf;
1963
Paul Bakker69e095c2011-12-10 21:55:01 +00001964 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1965 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001966
1967 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001968 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001969 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001970 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001971 (unsigned char *) pwd, strlen( pwd ) );
1972
1973 memset( buf, 0, n + 1 );
1974 free( buf );
1975
1976 return( ret );
1977}
1978
1979/*
1980 * Load and parse a public RSA key
1981 */
1982int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1983{
1984 int ret;
1985 size_t n;
1986 unsigned char *buf;
1987
Paul Bakker69e095c2011-12-10 21:55:01 +00001988 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1989 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001990
Paul Bakker27fdf462011-06-09 13:55:13 +00001991 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001992
1993 memset( buf, 0, n + 1 );
1994 free( buf );
1995
1996 return( ret );
1997}
1998#endif /* POLARSSL_FS_IO */
1999
2000/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 * Parse a private RSA key
2002 */
Paul Bakker23986e52011-04-24 08:57:21 +00002003int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2004 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002005{
Paul Bakker23986e52011-04-24 08:57:21 +00002006 int ret;
2007 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002009 unsigned char *p_alt;
2010 x509_buf pk_alg_oid;
2011
Paul Bakker96743fc2011-02-12 14:30:57 +00002012#if defined(POLARSSL_PEM_C)
2013 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Paul Bakker96743fc2011-02-12 14:30:57 +00002015 pem_init( &pem );
2016 ret = pem_read_buffer( &pem,
2017 "-----BEGIN RSA PRIVATE KEY-----",
2018 "-----END RSA PRIVATE KEY-----",
2019 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
Paul Bakkered56b222011-07-13 11:26:43 +00002021 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2022 {
2023 ret = pem_read_buffer( &pem,
2024 "-----BEGIN PRIVATE KEY-----",
2025 "-----END PRIVATE KEY-----",
2026 key, pwd, pwdlen, &len );
2027 }
2028
Paul Bakker96743fc2011-02-12 14:30:57 +00002029 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002031 /*
2032 * Was PEM encoded
2033 */
2034 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002036 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002037 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002038 pem_free( &pem );
2039 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002041
Paul Bakker96743fc2011-02-12 14:30:57 +00002042 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2043#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002044 ((void) pwd);
2045 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002046 p = (unsigned char *) key;
2047#endif
2048 end = p + keylen;
2049
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002051 * Note: Depending on the type of private key file one can expect either a
2052 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2053 *
2054 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002055 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002056 * algorithm AlgorithmIdentifier,
2057 * PrivateKey BIT STRING
2058 * }
2059 *
2060 * AlgorithmIdentifier ::= SEQUENCE {
2061 * algorithm OBJECT IDENTIFIER,
2062 * parameters ANY DEFINED BY algorithm OPTIONAL
2063 * }
2064 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 * RSAPrivateKey ::= SEQUENCE {
2066 * version Version,
2067 * modulus INTEGER, -- n
2068 * publicExponent INTEGER, -- e
2069 * privateExponent INTEGER, -- d
2070 * prime1 INTEGER, -- p
2071 * prime2 INTEGER, -- q
2072 * exponent1 INTEGER, -- d mod (p-1)
2073 * exponent2 INTEGER, -- d mod (q-1)
2074 * coefficient INTEGER, -- (inverse of q) mod p
2075 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2076 * }
2077 */
2078 if( ( ret = asn1_get_tag( &p, end, &len,
2079 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2080 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002081#if defined(POLARSSL_PEM_C)
2082 pem_free( &pem );
2083#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002085 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 }
2087
2088 end = p + len;
2089
2090 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2091 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002092#if defined(POLARSSL_PEM_C)
2093 pem_free( &pem );
2094#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002096 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098
2099 if( rsa->ver != 0 )
2100 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002101#if defined(POLARSSL_PEM_C)
2102 pem_free( &pem );
2103#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002105 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 }
2107
Paul Bakkered56b222011-07-13 11:26:43 +00002108 p_alt = p;
2109
2110 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2111 {
2112 // Assume that we have the PKCS#1 format if wrong
2113 // tag was encountered
2114 //
2115 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2116 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2117 {
2118#if defined(POLARSSL_PEM_C)
2119 pem_free( &pem );
2120#endif
2121 rsa_free( rsa );
2122 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2123 }
2124 }
2125 else
2126 {
2127 int can_handle;
2128
2129 /*
2130 * only RSA keys handled at this time
2131 */
2132 can_handle = 0;
2133
2134 if( pk_alg_oid.len == 9 &&
2135 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2136 can_handle = 1;
2137
2138 if( pk_alg_oid.len == 9 &&
2139 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2140 {
2141 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2142 can_handle = 1;
2143
2144 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2145 can_handle = 1;
2146 }
2147
2148 if( pk_alg_oid.len == 5 &&
2149 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2150 can_handle = 1;
2151
2152 if( can_handle == 0 )
2153 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2154
2155 /*
2156 * Parse the PKCS#8 format
2157 */
2158
2159 p = p_alt;
2160 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2161 {
2162#if defined(POLARSSL_PEM_C)
2163 pem_free( &pem );
2164#endif
2165 rsa_free( rsa );
2166 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2167 }
2168
2169 if( ( end - p ) < 1 )
2170 {
2171#if defined(POLARSSL_PEM_C)
2172 pem_free( &pem );
2173#endif
2174 rsa_free( rsa );
2175 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2176 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2177 }
2178
2179 end = p + len;
2180
2181 if( ( ret = asn1_get_tag( &p, end, &len,
2182 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2183 {
2184#if defined(POLARSSL_PEM_C)
2185 pem_free( &pem );
2186#endif
2187 rsa_free( rsa );
2188 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2189 }
2190
2191 end = p + len;
2192
2193 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2194 {
2195#if defined(POLARSSL_PEM_C)
2196 pem_free( &pem );
2197#endif
2198 rsa_free( rsa );
2199 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2200 }
2201
2202 if( rsa->ver != 0 )
2203 {
2204#if defined(POLARSSL_PEM_C)
2205 pem_free( &pem );
2206#endif
2207 rsa_free( rsa );
2208 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2209 }
2210 }
2211
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2213 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2214 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2215 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2216 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2217 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2218 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2219 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2220 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002221#if defined(POLARSSL_PEM_C)
2222 pem_free( &pem );
2223#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002225 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 }
2227
2228 rsa->len = mpi_size( &rsa->N );
2229
2230 if( p != end )
2231 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002232#if defined(POLARSSL_PEM_C)
2233 pem_free( &pem );
2234#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002236 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002237 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
2240 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2241 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002242#if defined(POLARSSL_PEM_C)
2243 pem_free( &pem );
2244#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 rsa_free( rsa );
2246 return( ret );
2247 }
2248
Paul Bakker96743fc2011-02-12 14:30:57 +00002249#if defined(POLARSSL_PEM_C)
2250 pem_free( &pem );
2251#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002252
2253 return( 0 );
2254}
2255
2256/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002257 * Parse a public RSA key
2258 */
Paul Bakker23986e52011-04-24 08:57:21 +00002259int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002260{
Paul Bakker23986e52011-04-24 08:57:21 +00002261 int ret;
2262 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002263 unsigned char *p, *end;
2264 x509_buf alg_oid;
2265#if defined(POLARSSL_PEM_C)
2266 pem_context pem;
2267
2268 pem_init( &pem );
2269 ret = pem_read_buffer( &pem,
2270 "-----BEGIN PUBLIC KEY-----",
2271 "-----END PUBLIC KEY-----",
2272 key, NULL, 0, &len );
2273
2274 if( ret == 0 )
2275 {
2276 /*
2277 * Was PEM encoded
2278 */
2279 keylen = pem.buflen;
2280 }
2281 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2282 {
2283 pem_free( &pem );
2284 return( ret );
2285 }
2286
2287 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2288#else
2289 p = (unsigned char *) key;
2290#endif
2291 end = p + keylen;
2292
2293 /*
2294 * PublicKeyInfo ::= SEQUENCE {
2295 * algorithm AlgorithmIdentifier,
2296 * PublicKey BIT STRING
2297 * }
2298 *
2299 * AlgorithmIdentifier ::= SEQUENCE {
2300 * algorithm OBJECT IDENTIFIER,
2301 * parameters ANY DEFINED BY algorithm OPTIONAL
2302 * }
2303 *
2304 * RSAPublicKey ::= SEQUENCE {
2305 * modulus INTEGER, -- n
2306 * publicExponent INTEGER -- e
2307 * }
2308 */
2309
2310 if( ( ret = asn1_get_tag( &p, end, &len,
2311 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2312 {
2313#if defined(POLARSSL_PEM_C)
2314 pem_free( &pem );
2315#endif
2316 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002317 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002318 }
2319
2320 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2321 {
2322#if defined(POLARSSL_PEM_C)
2323 pem_free( &pem );
2324#endif
2325 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002326 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002327 }
2328
2329 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2330 {
2331#if defined(POLARSSL_PEM_C)
2332 pem_free( &pem );
2333#endif
2334 rsa_free( rsa );
2335 return( ret );
2336 }
2337
2338 rsa->len = mpi_size( &rsa->N );
2339
2340#if defined(POLARSSL_PEM_C)
2341 pem_free( &pem );
2342#endif
2343
2344 return( 0 );
2345}
2346
Paul Bakkereaa89f82011-04-04 21:36:15 +00002347#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002348/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002349 * Parse DHM parameters
2350 */
Paul Bakker23986e52011-04-24 08:57:21 +00002351int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002352{
Paul Bakker23986e52011-04-24 08:57:21 +00002353 int ret;
2354 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002355 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002356#if defined(POLARSSL_PEM_C)
2357 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002358
Paul Bakker96743fc2011-02-12 14:30:57 +00002359 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002360
Paul Bakker96743fc2011-02-12 14:30:57 +00002361 ret = pem_read_buffer( &pem,
2362 "-----BEGIN DH PARAMETERS-----",
2363 "-----END DH PARAMETERS-----",
2364 dhmin, NULL, 0, &dhminlen );
2365
2366 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002367 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002368 /*
2369 * Was PEM encoded
2370 */
2371 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002372 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002373 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002374 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002375 pem_free( &pem );
2376 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002377 }
2378
Paul Bakker96743fc2011-02-12 14:30:57 +00002379 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2380#else
2381 p = (unsigned char *) dhmin;
2382#endif
2383 end = p + dhminlen;
2384
Paul Bakker1b57b062011-01-06 15:48:19 +00002385 memset( dhm, 0, sizeof( dhm_context ) );
2386
Paul Bakker1b57b062011-01-06 15:48:19 +00002387 /*
2388 * DHParams ::= SEQUENCE {
2389 * prime INTEGER, -- P
2390 * generator INTEGER, -- g
2391 * }
2392 */
2393 if( ( ret = asn1_get_tag( &p, end, &len,
2394 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2395 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002396#if defined(POLARSSL_PEM_C)
2397 pem_free( &pem );
2398#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002399 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002400 }
2401
2402 end = p + len;
2403
2404 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2405 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2406 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002407#if defined(POLARSSL_PEM_C)
2408 pem_free( &pem );
2409#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002410 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002411 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002412 }
2413
2414 if( p != end )
2415 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002416#if defined(POLARSSL_PEM_C)
2417 pem_free( &pem );
2418#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002419 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002420 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002421 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2422 }
2423
Paul Bakker96743fc2011-02-12 14:30:57 +00002424#if defined(POLARSSL_PEM_C)
2425 pem_free( &pem );
2426#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002427
2428 return( 0 );
2429}
2430
Paul Bakker335db3f2011-04-25 15:28:35 +00002431#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002432/*
2433 * Load and parse a private RSA key
2434 */
2435int x509parse_dhmfile( dhm_context *dhm, const char *path )
2436{
2437 int ret;
2438 size_t n;
2439 unsigned char *buf;
2440
Paul Bakker69e095c2011-12-10 21:55:01 +00002441 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2442 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002443
Paul Bakker27fdf462011-06-09 13:55:13 +00002444 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002445
2446 memset( buf, 0, n + 1 );
2447 free( buf );
2448
2449 return( ret );
2450}
Paul Bakker335db3f2011-04-25 15:28:35 +00002451#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002452#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002453
Paul Bakker5121ce52009-01-03 21:22:43 +00002454#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002455#include <stdarg.h>
2456
2457#if !defined vsnprintf
2458#define vsnprintf _vsnprintf
2459#endif // vsnprintf
2460
2461/*
2462 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2463 * Result value is not size of buffer needed, but -1 if no fit is possible.
2464 *
2465 * This fuction tries to 'fix' this by at least suggesting enlarging the
2466 * size by 20.
2467 */
2468int compat_snprintf(char *str, size_t size, const char *format, ...)
2469{
2470 va_list ap;
2471 int res = -1;
2472
2473 va_start( ap, format );
2474
2475 res = vsnprintf( str, size, format, ap );
2476
2477 va_end( ap );
2478
2479 // No quick fix possible
2480 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002481 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002482
2483 return res;
2484}
2485
2486#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002487#endif
2488
Paul Bakkerd98030e2009-05-02 15:13:40 +00002489#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2490
2491#define SAFE_SNPRINTF() \
2492{ \
2493 if( ret == -1 ) \
2494 return( -1 ); \
2495 \
Paul Bakker23986e52011-04-24 08:57:21 +00002496 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002497 p[n - 1] = '\0'; \
2498 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2499 } \
2500 \
Paul Bakker23986e52011-04-24 08:57:21 +00002501 n -= (unsigned int) ret; \
2502 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002503}
2504
Paul Bakker5121ce52009-01-03 21:22:43 +00002505/*
2506 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002507 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002509int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002510{
Paul Bakker23986e52011-04-24 08:57:21 +00002511 int ret;
2512 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002514 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 char s[128], *p;
2516
2517 memset( s, 0, sizeof( s ) );
2518
2519 name = dn;
2520 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002521 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
2523 while( name != NULL )
2524 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002525 if( !name->oid.p )
2526 {
2527 name = name->next;
2528 continue;
2529 }
2530
Paul Bakker74111d32011-01-15 16:57:55 +00002531 if( name != dn )
2532 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002533 ret = snprintf( p, n, ", " );
2534 SAFE_SNPRINTF();
2535 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Paul Bakker535e97d2012-08-23 10:49:55 +00002537 if( name->oid.len == 3 &&
2538 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 {
2540 switch( name->oid.p[2] )
2541 {
2542 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002543 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
2545 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002546 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
2548 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002549 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
2551 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002552 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
2554 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002555 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
2557 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002558 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
2560 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002561 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 name->oid.p[2] );
2563 break;
2564 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002565 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002567 else if( name->oid.len == 9 &&
2568 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 {
2570 switch( name->oid.p[8] )
2571 {
2572 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002573 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
2575 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002576 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 name->oid.p[8] );
2578 break;
2579 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002580 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 }
2582 else
Paul Bakker74111d32011-01-15 16:57:55 +00002583 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002584 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002585 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
2588 for( i = 0; i < name->val.len; i++ )
2589 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002590 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 break;
2592
2593 c = name->val.p[i];
2594 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2595 s[i] = '?';
2596 else s[i] = c;
2597 }
2598 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002599 ret = snprintf( p, n, "%s", s );
2600 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 name = name->next;
2602 }
2603
Paul Bakker23986e52011-04-24 08:57:21 +00002604 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605}
2606
2607/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002608 * Store the serial in printable form into buf; no more
2609 * than size characters will be written
2610 */
2611int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2612{
Paul Bakker23986e52011-04-24 08:57:21 +00002613 int ret;
2614 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002615 char *p;
2616
2617 p = buf;
2618 n = size;
2619
2620 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002621 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002622
2623 for( i = 0; i < nr; i++ )
2624 {
Paul Bakker93048802011-12-05 14:38:06 +00002625 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002626 continue;
2627
Paul Bakkerdd476992011-01-16 21:34:59 +00002628 ret = snprintf( p, n, "%02X%s",
2629 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2630 SAFE_SNPRINTF();
2631 }
2632
Paul Bakker03c7c252011-11-25 12:37:37 +00002633 if( nr != serial->len )
2634 {
2635 ret = snprintf( p, n, "...." );
2636 SAFE_SNPRINTF();
2637 }
2638
Paul Bakker23986e52011-04-24 08:57:21 +00002639 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002640}
2641
2642/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002643 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002645int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2646 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002647{
Paul Bakker23986e52011-04-24 08:57:21 +00002648 int ret;
2649 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002650 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
2652 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002653 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Paul Bakkerd98030e2009-05-02 15:13:40 +00002655 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002657 SAFE_SNPRINTF();
2658 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002660 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Paul Bakkerdd476992011-01-16 21:34:59 +00002662 ret = x509parse_serial_gets( p, n, &crt->serial);
2663 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Paul Bakkerd98030e2009-05-02 15:13:40 +00002665 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2666 SAFE_SNPRINTF();
2667 ret = x509parse_dn_gets( p, n, &crt->issuer );
2668 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Paul Bakkerd98030e2009-05-02 15:13:40 +00002670 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2671 SAFE_SNPRINTF();
2672 ret = x509parse_dn_gets( p, n, &crt->subject );
2673 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
Paul Bakkerd98030e2009-05-02 15:13:40 +00002675 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2677 crt->valid_from.year, crt->valid_from.mon,
2678 crt->valid_from.day, crt->valid_from.hour,
2679 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002680 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Paul Bakkerd98030e2009-05-02 15:13:40 +00002682 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2684 crt->valid_to.year, crt->valid_to.mon,
2685 crt->valid_to.day, crt->valid_to.hour,
2686 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002687 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Paul Bakkerd98030e2009-05-02 15:13:40 +00002689 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2690 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Paul Bakker27d66162010-03-17 06:56:01 +00002692 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002694 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2695 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2696 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2697 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2698 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2699 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2700 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2701 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2702 default: ret = snprintf( p, n, "???" ); break;
2703 }
2704 SAFE_SNPRINTF();
2705
2706 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002707 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002708 SAFE_SNPRINTF();
2709
Paul Bakker23986e52011-04-24 08:57:21 +00002710 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002711}
2712
Paul Bakker74111d32011-01-15 16:57:55 +00002713/* Compare a given OID string with an OID x509_buf * */
2714#define OID_CMP(oid_str, oid_buf) \
2715 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2716 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2717
2718/*
2719 * Return an informational string describing the given OID
2720 */
2721const char *x509_oid_get_description( x509_buf *oid )
2722{
2723 if ( oid == NULL )
2724 return ( NULL );
2725
2726 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2727 return( STRING_SERVER_AUTH );
2728
2729 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2730 return( STRING_CLIENT_AUTH );
2731
2732 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2733 return( STRING_CODE_SIGNING );
2734
2735 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2736 return( STRING_EMAIL_PROTECTION );
2737
2738 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2739 return( STRING_TIME_STAMPING );
2740
2741 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2742 return( STRING_OCSP_SIGNING );
2743
2744 return( NULL );
2745}
2746
2747/* Return the x.y.z.... style numeric string for the given OID */
2748int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2749{
Paul Bakker23986e52011-04-24 08:57:21 +00002750 int ret;
2751 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002752 unsigned int value;
2753 char *p;
2754
2755 p = buf;
2756 n = size;
2757
2758 /* First byte contains first two dots */
2759 if( oid->len > 0 )
2760 {
2761 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2762 SAFE_SNPRINTF();
2763 }
2764
2765 /* TODO: value can overflow in value. */
2766 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002767 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002768 {
2769 value <<= 7;
2770 value += oid->p[i] & 0x7F;
2771
2772 if( !( oid->p[i] & 0x80 ) )
2773 {
2774 /* Last byte */
2775 ret = snprintf( p, n, ".%d", value );
2776 SAFE_SNPRINTF();
2777 value = 0;
2778 }
2779 }
2780
Paul Bakker23986e52011-04-24 08:57:21 +00002781 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002782}
2783
Paul Bakkerd98030e2009-05-02 15:13:40 +00002784/*
2785 * Return an informational string about the CRL.
2786 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002787int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2788 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002789{
Paul Bakker23986e52011-04-24 08:57:21 +00002790 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002791 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002792 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002793 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002794
2795 p = buf;
2796 n = size;
2797
2798 ret = snprintf( p, n, "%sCRL version : %d",
2799 prefix, crl->version );
2800 SAFE_SNPRINTF();
2801
2802 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2803 SAFE_SNPRINTF();
2804 ret = x509parse_dn_gets( p, n, &crl->issuer );
2805 SAFE_SNPRINTF();
2806
2807 ret = snprintf( p, n, "\n%sthis update : " \
2808 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2809 crl->this_update.year, crl->this_update.mon,
2810 crl->this_update.day, crl->this_update.hour,
2811 crl->this_update.min, crl->this_update.sec );
2812 SAFE_SNPRINTF();
2813
2814 ret = snprintf( p, n, "\n%snext update : " \
2815 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2816 crl->next_update.year, crl->next_update.mon,
2817 crl->next_update.day, crl->next_update.hour,
2818 crl->next_update.min, crl->next_update.sec );
2819 SAFE_SNPRINTF();
2820
2821 entry = &crl->entry;
2822
2823 ret = snprintf( p, n, "\n%sRevoked certificates:",
2824 prefix );
2825 SAFE_SNPRINTF();
2826
Paul Bakker9be19372009-07-27 20:21:53 +00002827 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002828 {
2829 ret = snprintf( p, n, "\n%sserial number: ",
2830 prefix );
2831 SAFE_SNPRINTF();
2832
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002833 ret = x509parse_serial_gets( p, n, &entry->serial);
2834 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002835
Paul Bakkerd98030e2009-05-02 15:13:40 +00002836 ret = snprintf( p, n, " revocation date: " \
2837 "%04d-%02d-%02d %02d:%02d:%02d",
2838 entry->revocation_date.year, entry->revocation_date.mon,
2839 entry->revocation_date.day, entry->revocation_date.hour,
2840 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002841 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002842
2843 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 }
2845
Paul Bakkerd98030e2009-05-02 15:13:40 +00002846 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2847 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
Paul Bakker27d66162010-03-17 06:56:01 +00002849 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002850 {
2851 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2852 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2853 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2854 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2855 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2856 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2857 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2858 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2859 default: ret = snprintf( p, n, "???" ); break;
2860 }
2861 SAFE_SNPRINTF();
2862
Paul Bakker1e27bb22009-07-19 20:25:25 +00002863 ret = snprintf( p, n, "\n" );
2864 SAFE_SNPRINTF();
2865
Paul Bakker23986e52011-04-24 08:57:21 +00002866 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867}
2868
2869/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002870 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002872int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002873{
Paul Bakkercce9d772011-11-18 14:26:47 +00002874 int year, mon, day;
2875 int hour, min, sec;
2876
2877#if defined(_WIN32)
2878 SYSTEMTIME st;
2879
2880 GetLocalTime(&st);
2881
2882 year = st.wYear;
2883 mon = st.wMonth;
2884 day = st.wDay;
2885 hour = st.wHour;
2886 min = st.wMinute;
2887 sec = st.wSecond;
2888#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 struct tm *lt;
2890 time_t tt;
2891
2892 tt = time( NULL );
2893 lt = localtime( &tt );
2894
Paul Bakkercce9d772011-11-18 14:26:47 +00002895 year = lt->tm_year + 1900;
2896 mon = lt->tm_mon + 1;
2897 day = lt->tm_mday;
2898 hour = lt->tm_hour;
2899 min = lt->tm_min;
2900 sec = lt->tm_sec;
2901#endif
2902
2903 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002904 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakkercce9d772011-11-18 14:26:47 +00002906 if( year == to->year &&
2907 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002908 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Paul Bakkercce9d772011-11-18 14:26:47 +00002910 if( year == to->year &&
2911 mon == to->mon &&
2912 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002913 return( 1 );
2914
Paul Bakkercce9d772011-11-18 14:26:47 +00002915 if( year == to->year &&
2916 mon == to->mon &&
2917 day == to->day &&
2918 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002919 return( 1 );
2920
Paul Bakkercce9d772011-11-18 14:26:47 +00002921 if( year == to->year &&
2922 mon == to->mon &&
2923 day == to->day &&
2924 hour == to->hour &&
2925 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002926 return( 1 );
2927
Paul Bakkercce9d772011-11-18 14:26:47 +00002928 if( year == to->year &&
2929 mon == to->mon &&
2930 day == to->day &&
2931 hour == to->hour &&
2932 min == to->min &&
2933 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002934 return( 1 );
2935
Paul Bakker40ea7de2009-05-03 10:18:48 +00002936 return( 0 );
2937}
2938
2939/*
2940 * Return 1 if the certificate is revoked, or 0 otherwise.
2941 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002942int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002943{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002944 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002945
2946 while( cur != NULL && cur->serial.len != 0 )
2947 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002948 if( crt->serial.len == cur->serial.len &&
2949 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002950 {
2951 if( x509parse_time_expired( &cur->revocation_date ) )
2952 return( 1 );
2953 }
2954
2955 cur = cur->next;
2956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
2958 return( 0 );
2959}
2960
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002961/*
2962 * Wrapper for x509 hashes.
2963 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002964 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002965 */
Paul Bakker23986e52011-04-24 08:57:21 +00002966static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002967 unsigned char *out )
2968{
2969 switch( alg )
2970 {
Paul Bakker40e46942009-01-03 21:51:57 +00002971#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002972 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002973#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002974#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002975 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002976#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002977#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002978 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002979#endif
2980#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002981 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002982#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002983#if defined(POLARSSL_SHA2_C)
2984 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2985 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2986#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002987#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002988 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2989 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2990#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002992 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 break;
2994 }
2995}
2996
2997/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002998 * Check that the given certificate is valid accoring to the CRL.
2999 */
3000static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3001 x509_crl *crl_list)
3002{
3003 int flags = 0;
3004 int hash_id;
3005 unsigned char hash[64];
3006
3007 /*
3008 * TODO: What happens if no CRL is present?
3009 * Suggestion: Revocation state should be unknown if no CRL is present.
3010 * For backwards compatibility this is not yet implemented.
3011 */
3012
3013 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
3014 {
3015 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
3016 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3017 crl_list->issuer_raw.len ) != 0 )
3018 {
3019 crl_list = crl_list->next;
3020 continue;
3021 }
3022
3023 /*
3024 * Check if CRL is correctly signed by the trusted CA
3025 */
3026 hash_id = crl_list->sig_alg;
3027
3028 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3029
3030 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
3031 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 Bakker76fd75a2011-01-16 21:12:10 +00003089/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 * Verify the certificate validity
3091 */
3092int x509parse_verify( x509_cert *crt,
3093 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003094 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003095 const char *cn, int *flags,
3096 int (*f_vrfy)(void *, x509_cert *, int, int),
3097 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003098{
Paul Bakker23986e52011-04-24 08:57:21 +00003099 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 int hash_id;
3101 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003102 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00003104 unsigned char hash[64];
Paul Bakkera8cd2392012-02-11 16:09:32 +00003105 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003106
Paul Bakker40ea7de2009-05-03 10:18:48 +00003107 *flags = 0;
3108
3109 if( x509parse_time_expired( &crt->valid_to ) )
3110 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
3112 if( cn != NULL )
3113 {
3114 name = &crt->subject;
3115 cn_len = strlen( cn );
3116
Paul Bakker4d2c1242012-05-10 14:12:46 +00003117 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003119 cur = &crt->subject_alt_names;
3120
3121 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003122 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003123 if( cur->buf.len == cn_len &&
3124 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003125 break;
3126
Paul Bakker535e97d2012-08-23 10:49:55 +00003127 if( cur->buf.len > 2 &&
3128 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003129 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003130 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003131
Paul Bakker4d2c1242012-05-10 14:12:46 +00003132 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003133 }
3134
3135 if( cur == NULL )
3136 *flags |= BADCERT_CN_MISMATCH;
3137 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003138 else
3139 {
3140 while( name != NULL )
3141 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003142 if( name->oid.len == 3 &&
3143 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003144 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003145 if( name->val.len == cn_len &&
3146 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003147 break;
3148
Paul Bakker535e97d2012-08-23 10:49:55 +00003149 if( name->val.len > 2 &&
3150 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003151 x509_wildcard_verify( cn, &name->val ) )
3152 break;
3153 }
3154
3155 name = name->next;
3156 }
3157
3158 if( name == NULL )
3159 *flags |= BADCERT_CN_MISMATCH;
3160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 }
3162
Paul Bakker5121ce52009-01-03 21:22:43 +00003163 /*
3164 * Iterate upwards in the given cert chain,
3165 * ignoring any upper cert with CA != TRUE.
3166 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003167 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
3169 pathlen = 1;
3170
Paul Bakker76fd75a2011-01-16 21:12:10 +00003171 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003172 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003173 if( parent->ca_istrue == 0 ||
3174 crt->issuer_raw.len != parent->subject_raw.len ||
3175 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 crt->issuer_raw.len ) != 0 )
3177 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003178 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003179 continue;
3180 }
3181
Paul Bakker27d66162010-03-17 06:56:01 +00003182 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003183
3184 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3185
Paul Bakker76fd75a2011-01-16 21:12:10 +00003186 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3187 crt->sig.p ) != 0 )
3188 *flags |= BADCERT_NOT_TRUSTED;
3189
3190 /* Check trusted CA's CRL for the given crt */
3191 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003192
3193 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00003194 if( NULL != f_vrfy )
3195 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003196 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003197 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003198 else
3199 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003200 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003201 else if( *flags != 0 )
3202 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
3204 pathlen++;
3205
Paul Bakker76fd75a2011-01-16 21:12:10 +00003206 crt = parent;
3207 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 }
3209
3210 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003211 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003213 *flags |= BADCERT_NOT_TRUSTED;
3214
Paul Bakker7c6d4a42009-03-28 20:35:47 +00003215 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 {
3217 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
3218 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
3219 crt->issuer_raw.len ) != 0 )
3220 {
3221 trust_ca = trust_ca->next;
3222 continue;
3223 }
3224
3225 if( trust_ca->max_pathlen > 0 &&
3226 trust_ca->max_pathlen < pathlen )
3227 break;
3228
Paul Bakker27d66162010-03-17 06:56:01 +00003229 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
3231 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3232
3233 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3234 0, hash, crt->sig.p ) == 0 )
3235 {
3236 /*
3237 * cert. is signed by a trusted CA
3238 */
3239 *flags &= ~BADCERT_NOT_TRUSTED;
3240 break;
3241 }
3242
3243 trust_ca = trust_ca->next;
3244 }
3245
Paul Bakker76fd75a2011-01-16 21:12:10 +00003246 /* Check trusted CA's CRL for the given crt */
3247 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003248
3249 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003250 if( NULL != f_vrfy )
3251 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003252 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003253 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003254 else
3255 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003256 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003257 else if( *flags != 0 )
3258 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003259
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 return( 0 );
3261}
3262
3263/*
3264 * Unallocate all certificate data
3265 */
3266void x509_free( x509_cert *crt )
3267{
3268 x509_cert *cert_cur = crt;
3269 x509_cert *cert_prv;
3270 x509_name *name_cur;
3271 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003272 x509_sequence *seq_cur;
3273 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003274
3275 if( crt == NULL )
3276 return;
3277
3278 do
3279 {
3280 rsa_free( &cert_cur->rsa );
3281
3282 name_cur = cert_cur->issuer.next;
3283 while( name_cur != NULL )
3284 {
3285 name_prv = name_cur;
3286 name_cur = name_cur->next;
3287 memset( name_prv, 0, sizeof( x509_name ) );
3288 free( name_prv );
3289 }
3290
3291 name_cur = cert_cur->subject.next;
3292 while( name_cur != NULL )
3293 {
3294 name_prv = name_cur;
3295 name_cur = name_cur->next;
3296 memset( name_prv, 0, sizeof( x509_name ) );
3297 free( name_prv );
3298 }
3299
Paul Bakker74111d32011-01-15 16:57:55 +00003300 seq_cur = cert_cur->ext_key_usage.next;
3301 while( seq_cur != NULL )
3302 {
3303 seq_prv = seq_cur;
3304 seq_cur = seq_cur->next;
3305 memset( seq_prv, 0, sizeof( x509_sequence ) );
3306 free( seq_prv );
3307 }
3308
Paul Bakker8afa70d2012-02-11 18:42:45 +00003309 seq_cur = cert_cur->subject_alt_names.next;
3310 while( seq_cur != NULL )
3311 {
3312 seq_prv = seq_cur;
3313 seq_cur = seq_cur->next;
3314 memset( seq_prv, 0, sizeof( x509_sequence ) );
3315 free( seq_prv );
3316 }
3317
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 if( cert_cur->raw.p != NULL )
3319 {
3320 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3321 free( cert_cur->raw.p );
3322 }
3323
3324 cert_cur = cert_cur->next;
3325 }
3326 while( cert_cur != NULL );
3327
3328 cert_cur = crt;
3329 do
3330 {
3331 cert_prv = cert_cur;
3332 cert_cur = cert_cur->next;
3333
3334 memset( cert_prv, 0, sizeof( x509_cert ) );
3335 if( cert_prv != crt )
3336 free( cert_prv );
3337 }
3338 while( cert_cur != NULL );
3339}
3340
Paul Bakkerd98030e2009-05-02 15:13:40 +00003341/*
3342 * Unallocate all CRL data
3343 */
3344void x509_crl_free( x509_crl *crl )
3345{
3346 x509_crl *crl_cur = crl;
3347 x509_crl *crl_prv;
3348 x509_name *name_cur;
3349 x509_name *name_prv;
3350 x509_crl_entry *entry_cur;
3351 x509_crl_entry *entry_prv;
3352
3353 if( crl == NULL )
3354 return;
3355
3356 do
3357 {
3358 name_cur = crl_cur->issuer.next;
3359 while( name_cur != NULL )
3360 {
3361 name_prv = name_cur;
3362 name_cur = name_cur->next;
3363 memset( name_prv, 0, sizeof( x509_name ) );
3364 free( name_prv );
3365 }
3366
3367 entry_cur = crl_cur->entry.next;
3368 while( entry_cur != NULL )
3369 {
3370 entry_prv = entry_cur;
3371 entry_cur = entry_cur->next;
3372 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3373 free( entry_prv );
3374 }
3375
3376 if( crl_cur->raw.p != NULL )
3377 {
3378 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3379 free( crl_cur->raw.p );
3380 }
3381
3382 crl_cur = crl_cur->next;
3383 }
3384 while( crl_cur != NULL );
3385
3386 crl_cur = crl;
3387 do
3388 {
3389 crl_prv = crl_cur;
3390 crl_cur = crl_cur->next;
3391
3392 memset( crl_prv, 0, sizeof( x509_crl ) );
3393 if( crl_prv != crl )
3394 free( crl_prv );
3395 }
3396 while( crl_cur != NULL );
3397}
3398
Paul Bakker40e46942009-01-03 21:51:57 +00003399#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003400
Paul Bakker40e46942009-01-03 21:51:57 +00003401#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003402
3403/*
3404 * Checkup routine
3405 */
3406int x509_self_test( int verbose )
3407{
Paul Bakker5690efc2011-05-26 13:16:06 +00003408#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003409 int ret;
3410 int flags;
3411 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 x509_cert cacert;
3413 x509_cert clicert;
3414 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003415#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003416 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003417#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003418
3419 if( verbose != 0 )
3420 printf( " X.509 certificate load: " );
3421
3422 memset( &clicert, 0, sizeof( x509_cert ) );
3423
3424 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003425 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 if( ret != 0 )
3427 {
3428 if( verbose != 0 )
3429 printf( "failed\n" );
3430
3431 return( ret );
3432 }
3433
3434 memset( &cacert, 0, sizeof( x509_cert ) );
3435
3436 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003437 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 if( ret != 0 )
3439 {
3440 if( verbose != 0 )
3441 printf( "failed\n" );
3442
3443 return( ret );
3444 }
3445
3446 if( verbose != 0 )
3447 printf( "passed\n X.509 private key load: " );
3448
3449 i = strlen( test_ca_key );
3450 j = strlen( test_ca_pwd );
3451
Paul Bakker66b78b22011-03-25 14:22:50 +00003452 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3453
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 if( ( ret = x509parse_key( &rsa,
3455 (unsigned char *) test_ca_key, i,
3456 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3457 {
3458 if( verbose != 0 )
3459 printf( "failed\n" );
3460
3461 return( ret );
3462 }
3463
3464 if( verbose != 0 )
3465 printf( "passed\n X.509 signature verify: ");
3466
Paul Bakker23986e52011-04-24 08:57:21 +00003467 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 if( ret != 0 )
3469 {
Paul Bakker23986e52011-04-24 08:57:21 +00003470 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 if( verbose != 0 )
3472 printf( "failed\n" );
3473
3474 return( ret );
3475 }
3476
Paul Bakker5690efc2011-05-26 13:16:06 +00003477#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003479 printf( "passed\n X.509 DHM parameter load: " );
3480
3481 i = strlen( test_dhm_params );
3482 j = strlen( test_ca_pwd );
3483
3484 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3485 {
3486 if( verbose != 0 )
3487 printf( "failed\n" );
3488
3489 return( ret );
3490 }
3491
3492 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003494#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003495
3496 x509_free( &cacert );
3497 x509_free( &clicert );
3498 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003499#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003500 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003501#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
3503 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003504#else
3505 ((void) verbose);
3506 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003508}
3509
3510#endif
3511
3512#endif