blob: 3513f1b34cc3f0cc6fa73075e166a9fead03c7ce [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 Bakker5121ce52009-01-03 21:22:43 +00001137 unsigned char *p, *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 Bakker23986e52011-04-24 08:57:21 +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 }
1177
1178 /*
1179 * TBSCertificate ::= SEQUENCE {
1180 */
1181 crt->tbs.p = p;
1182
1183 if( ( ret = asn1_get_tag( &p, end, &len,
1184 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1185 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001186 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001187 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
1189
1190 end = p + len;
1191 crt->tbs.len = end - crt->tbs.p;
1192
1193 /*
1194 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1195 *
1196 * CertificateSerialNumber ::= INTEGER
1197 *
1198 * signature AlgorithmIdentifier
1199 */
1200 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1201 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1202 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1203 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001204 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 return( ret );
1206 }
1207
1208 crt->version++;
1209
1210 if( crt->version > 3 )
1211 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001212 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001213 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 }
1215
Paul Bakker27d66162010-03-17 06:56:01 +00001216 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001218 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001219 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 }
1221
1222 /*
1223 * issuer Name
1224 */
1225 crt->issuer_raw.p = p;
1226
1227 if( ( ret = asn1_get_tag( &p, end, &len,
1228 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1229 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001230 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001231 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 }
1233
1234 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1235 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001236 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 return( ret );
1238 }
1239
1240 crt->issuer_raw.len = p - crt->issuer_raw.p;
1241
1242 /*
1243 * Validity ::= SEQUENCE {
1244 * notBefore Time,
1245 * notAfter Time }
1246 *
1247 */
1248 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1249 &crt->valid_to ) ) != 0 )
1250 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001251 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 return( ret );
1253 }
1254
1255 /*
1256 * subject Name
1257 */
1258 crt->subject_raw.p = p;
1259
1260 if( ( ret = asn1_get_tag( &p, end, &len,
1261 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1262 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001263 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001264 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 }
1266
Paul Bakkercefb3962012-06-27 11:51:09 +00001267 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001269 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 return( ret );
1271 }
1272
1273 crt->subject_raw.len = p - crt->subject_raw.p;
1274
1275 /*
1276 * SubjectPublicKeyInfo ::= SEQUENCE
1277 * algorithm AlgorithmIdentifier,
1278 * subjectPublicKey BIT STRING }
1279 */
1280 if( ( ret = asn1_get_tag( &p, end, &len,
1281 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1282 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001283 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001284 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 }
1286
1287 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1288 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1289 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001290 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 return( ret );
1292 }
1293
1294 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1295 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001296 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 return( ret );
1298 }
1299
1300 crt->rsa.len = mpi_size( &crt->rsa.N );
1301
1302 /*
1303 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1304 * -- If present, version shall be v2 or v3
1305 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1306 * -- If present, version shall be v2 or v3
1307 * extensions [3] EXPLICIT Extensions OPTIONAL
1308 * -- If present, version shall be v3
1309 */
1310 if( crt->version == 2 || crt->version == 3 )
1311 {
1312 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1313 if( ret != 0 )
1314 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001315 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 return( ret );
1317 }
1318 }
1319
1320 if( crt->version == 2 || crt->version == 3 )
1321 {
1322 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1323 if( ret != 0 )
1324 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001325 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 return( ret );
1327 }
1328 }
1329
1330 if( crt->version == 3 )
1331 {
Paul Bakker74111d32011-01-15 16:57:55 +00001332 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 if( ret != 0 )
1334 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001335 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 return( ret );
1337 }
1338 }
1339
1340 if( p != end )
1341 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001342 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001343 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001344 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
1346
1347 end = crt->raw.p + crt->raw.len;
1348
1349 /*
1350 * signatureAlgorithm AlgorithmIdentifier,
1351 * signatureValue BIT STRING
1352 */
1353 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1354 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001355 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 return( ret );
1357 }
1358
Paul Bakker535e97d2012-08-23 10:49:55 +00001359 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1360 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001362 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001363 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
1365
1366 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1367 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001368 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 return( ret );
1370 }
1371
1372 if( p != end )
1373 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001374 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001375 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001376 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 }
1378
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001379 return( 0 );
1380}
1381
1382/*
1383 * Parse one or more PEM certificates from a buffer and add them to the chained list
1384 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001385int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001386{
Paul Bakker69e095c2011-12-10 21:55:01 +00001387 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001388 x509_cert *crt, *prev = NULL;
1389 int buf_format = X509_FORMAT_DER;
1390
1391 crt = chain;
1392
1393 /*
1394 * Check for valid input
1395 */
1396 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001397 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001398
1399 while( crt->version != 0 && crt->next != NULL )
1400 {
1401 prev = crt;
1402 crt = crt->next;
1403 }
1404
1405 /*
1406 * Add new certificate on the end of the chain if needed.
1407 */
1408 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001409 {
1410 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1411
Paul Bakker7d06ad22009-05-02 15:53:56 +00001412 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001413 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001414
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001415 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001416 crt = crt->next;
1417 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001420 /*
1421 * Determine buffer content. Buffer contains either one DER certificate or
1422 * one or more PEM certificates.
1423 */
1424#if defined(POLARSSL_PEM_C)
1425 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1426 buf_format = X509_FORMAT_PEM;
1427#endif
1428
1429 if( buf_format == X509_FORMAT_DER )
1430 return x509parse_crt_der( crt, buf, buflen );
1431
1432#if defined(POLARSSL_PEM_C)
1433 if( buf_format == X509_FORMAT_PEM )
1434 {
1435 pem_context pem;
1436
1437 while( buflen > 0 )
1438 {
1439 size_t use_len;
1440 pem_init( &pem );
1441
1442 ret = pem_read_buffer( &pem,
1443 "-----BEGIN CERTIFICATE-----",
1444 "-----END CERTIFICATE-----",
1445 buf, NULL, 0, &use_len );
1446
1447 if( ret == 0 )
1448 {
1449 /*
1450 * Was PEM encoded
1451 */
1452 buflen -= use_len;
1453 buf += use_len;
1454 }
1455 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1456 {
1457 pem_free( &pem );
1458
1459 if( first_error == 0 )
1460 first_error = ret;
1461
1462 continue;
1463 }
1464 else
1465 break;
1466
1467 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1468
1469 pem_free( &pem );
1470
1471 if( ret != 0 )
1472 {
1473 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001474 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001475 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001476 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001477 {
1478 if( prev )
1479 prev->next = NULL;
1480
1481 if( crt != chain )
1482 free( crt );
1483
1484 return( ret );
1485 }
1486
1487 if( first_error == 0 )
1488 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001489
1490 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001491
1492 memset( crt, 0, sizeof( x509_cert ) );
1493 continue;
1494 }
1495
1496 success = 1;
1497
1498 /*
1499 * Add new certificate to the list
1500 */
1501 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1502
1503 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001504 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001505
1506 prev = crt;
1507 crt = crt->next;
1508 memset( crt, 0, sizeof( x509_cert ) );
1509 }
1510 }
1511#endif
1512
1513 if( crt->version == 0 )
1514 {
1515 if( prev )
1516 prev->next = NULL;
1517
1518 if( crt != chain )
1519 free( crt );
1520 }
1521
1522 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001523 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001524 else if( first_error )
1525 return( first_error );
1526 else
1527 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528}
1529
1530/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001531 * Parse one or more CRLs and add them to the chained list
1532 */
Paul Bakker23986e52011-04-24 08:57:21 +00001533int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001534{
Paul Bakker23986e52011-04-24 08:57:21 +00001535 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001536 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001537 unsigned char *p, *end;
1538 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001539#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001540 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001541 pem_context pem;
1542#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001543
1544 crl = chain;
1545
1546 /*
1547 * Check for valid input
1548 */
1549 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001550 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001551
1552 while( crl->version != 0 && crl->next != NULL )
1553 crl = crl->next;
1554
1555 /*
1556 * Add new CRL on the end of the chain if needed.
1557 */
1558 if ( crl->version != 0 && crl->next == NULL)
1559 {
1560 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1561
Paul Bakker7d06ad22009-05-02 15:53:56 +00001562 if( crl->next == NULL )
1563 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001564 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001565 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001566 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001567
Paul Bakker7d06ad22009-05-02 15:53:56 +00001568 crl = crl->next;
1569 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001570 }
1571
Paul Bakker96743fc2011-02-12 14:30:57 +00001572#if defined(POLARSSL_PEM_C)
1573 pem_init( &pem );
1574 ret = pem_read_buffer( &pem,
1575 "-----BEGIN X509 CRL-----",
1576 "-----END X509 CRL-----",
1577 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001578
Paul Bakker96743fc2011-02-12 14:30:57 +00001579 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001580 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001581 /*
1582 * Was PEM encoded
1583 */
1584 buflen -= use_len;
1585 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001586
1587 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001588 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001589 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001590 p = pem.buf;
1591 pem.buf = NULL;
1592 len = pem.buflen;
1593 pem_free( &pem );
1594 }
1595 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1596 {
1597 pem_free( &pem );
1598 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001599 }
1600 else
1601 {
1602 /*
1603 * nope, copy the raw DER data
1604 */
1605 p = (unsigned char *) malloc( len = buflen );
1606
1607 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001608 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609
1610 memcpy( p, buf, buflen );
1611
1612 buflen = 0;
1613 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001614#else
1615 p = (unsigned char *) malloc( len = buflen );
1616
1617 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001618 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001619
1620 memcpy( p, buf, buflen );
1621
1622 buflen = 0;
1623#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001624
1625 crl->raw.p = p;
1626 crl->raw.len = len;
1627 end = p + len;
1628
1629 /*
1630 * CertificateList ::= SEQUENCE {
1631 * tbsCertList TBSCertList,
1632 * signatureAlgorithm AlgorithmIdentifier,
1633 * signatureValue BIT STRING }
1634 */
1635 if( ( ret = asn1_get_tag( &p, end, &len,
1636 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1637 {
1638 x509_crl_free( crl );
1639 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1640 }
1641
Paul Bakker23986e52011-04-24 08:57:21 +00001642 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001643 {
1644 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001645 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001646 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1647 }
1648
1649 /*
1650 * TBSCertList ::= SEQUENCE {
1651 */
1652 crl->tbs.p = p;
1653
1654 if( ( ret = asn1_get_tag( &p, end, &len,
1655 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1656 {
1657 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001658 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001659 }
1660
1661 end = p + len;
1662 crl->tbs.len = end - crl->tbs.p;
1663
1664 /*
1665 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1666 * -- if present, MUST be v2
1667 *
1668 * signature AlgorithmIdentifier
1669 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001670 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001671 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1672 {
1673 x509_crl_free( crl );
1674 return( ret );
1675 }
1676
1677 crl->version++;
1678
1679 if( crl->version > 2 )
1680 {
1681 x509_crl_free( crl );
1682 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1683 }
1684
Paul Bakker27d66162010-03-17 06:56:01 +00001685 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001686 {
1687 x509_crl_free( crl );
1688 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1689 }
1690
1691 /*
1692 * issuer Name
1693 */
1694 crl->issuer_raw.p = p;
1695
1696 if( ( ret = asn1_get_tag( &p, end, &len,
1697 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1698 {
1699 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001700 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001701 }
1702
1703 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1704 {
1705 x509_crl_free( crl );
1706 return( ret );
1707 }
1708
1709 crl->issuer_raw.len = p - crl->issuer_raw.p;
1710
1711 /*
1712 * thisUpdate Time
1713 * nextUpdate Time OPTIONAL
1714 */
Paul Bakker91200182010-02-18 21:26:15 +00001715 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001716 {
1717 x509_crl_free( crl );
1718 return( ret );
1719 }
1720
Paul Bakker91200182010-02-18 21:26:15 +00001721 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001722 {
Paul Bakker9d781402011-05-09 16:17:09 +00001723 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001724 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001725 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001726 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001727 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001728 x509_crl_free( crl );
1729 return( ret );
1730 }
1731 }
1732
1733 /*
1734 * revokedCertificates SEQUENCE OF SEQUENCE {
1735 * userCertificate CertificateSerialNumber,
1736 * revocationDate Time,
1737 * crlEntryExtensions Extensions OPTIONAL
1738 * -- if present, MUST be v2
1739 * } OPTIONAL
1740 */
1741 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1742 {
1743 x509_crl_free( crl );
1744 return( ret );
1745 }
1746
1747 /*
1748 * crlExtensions EXPLICIT Extensions OPTIONAL
1749 * -- if present, MUST be v2
1750 */
1751 if( crl->version == 2 )
1752 {
1753 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1754
1755 if( ret != 0 )
1756 {
1757 x509_crl_free( crl );
1758 return( ret );
1759 }
1760 }
1761
1762 if( p != end )
1763 {
1764 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001765 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001766 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1767 }
1768
1769 end = crl->raw.p + crl->raw.len;
1770
1771 /*
1772 * signatureAlgorithm AlgorithmIdentifier,
1773 * signatureValue BIT STRING
1774 */
1775 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1776 {
1777 x509_crl_free( crl );
1778 return( ret );
1779 }
1780
Paul Bakker535e97d2012-08-23 10:49:55 +00001781 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1782 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001783 {
1784 x509_crl_free( crl );
1785 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1786 }
1787
1788 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1789 {
1790 x509_crl_free( crl );
1791 return( ret );
1792 }
1793
1794 if( p != end )
1795 {
1796 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001797 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001798 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1799 }
1800
1801 if( buflen > 0 )
1802 {
1803 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1804
Paul Bakker7d06ad22009-05-02 15:53:56 +00001805 if( crl->next == NULL )
1806 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001807 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001808 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001809 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810
Paul Bakker7d06ad22009-05-02 15:53:56 +00001811 crl = crl->next;
1812 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001813
1814 return( x509parse_crl( crl, buf, buflen ) );
1815 }
1816
1817 return( 0 );
1818}
1819
Paul Bakker335db3f2011-04-25 15:28:35 +00001820#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001821/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001822 * Load all data from a file into a given buffer.
1823 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001824int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001825{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001826 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001827
Paul Bakkerd98030e2009-05-02 15:13:40 +00001828 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001829 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001830
Paul Bakkerd98030e2009-05-02 15:13:40 +00001831 fseek( f, 0, SEEK_END );
1832 *n = (size_t) ftell( f );
1833 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001834
Paul Bakkerd98030e2009-05-02 15:13:40 +00001835 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001836 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001837
Paul Bakkerd98030e2009-05-02 15:13:40 +00001838 if( fread( *buf, 1, *n, f ) != *n )
1839 {
1840 fclose( f );
1841 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001842 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001843 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001844
Paul Bakkerd98030e2009-05-02 15:13:40 +00001845 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001846
Paul Bakkerd98030e2009-05-02 15:13:40 +00001847 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001848
Paul Bakkerd98030e2009-05-02 15:13:40 +00001849 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001850}
1851
1852/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 * Load one or more certificates and add them to the chained list
1854 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001855int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001856{
1857 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 size_t n;
1859 unsigned char *buf;
1860
Paul Bakker69e095c2011-12-10 21:55:01 +00001861 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1862 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
Paul Bakker69e095c2011-12-10 21:55:01 +00001864 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
1866 memset( buf, 0, n + 1 );
1867 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
1869 return( ret );
1870}
1871
Paul Bakker8d914582012-06-04 12:46:42 +00001872int x509parse_crtpath( x509_cert *chain, const char *path )
1873{
1874 int ret = 0;
1875#if defined(_WIN32)
1876 int t_ret;
1877 TCHAR szDir[MAX_PATH];
1878 WIN32_FIND_DATA file_data;
1879 HANDLE hFind;
1880 DWORD dwError = 0;
1881
1882 StringCchCopy(szDir, MAX_PATH, path);
1883 StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
1884
1885 hFind = FindFirstFile( szDir, &file_data );
1886 if (hFind == INVALID_HANDLE_VALUE)
1887 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1888
1889 do
1890 {
Paul Bakkere4791f32012-06-04 21:29:15 +00001891 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001892 continue;
1893
1894 t_ret = x509parse_crtfile( chain, entry_name );
1895 if( t_ret < 0 )
1896 return( t_ret );
1897
1898 ret += t_ret;
1899 }
1900 while( FindNextFile( hFind, &file_data ) != 0 );
1901
1902 dwError = GetLastError();
1903 if (dwError != ERROR_NO_MORE_FILES)
1904 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1905
1906 FindClose( hFind );
1907#else
1908 int t_ret;
1909 struct dirent *entry;
1910 char entry_name[255];
1911 DIR *dir = opendir( path );
1912
1913 if( dir == NULL)
1914 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1915
1916 while( ( entry = readdir( dir ) ) != NULL )
1917 {
1918 if( entry->d_type != DT_REG )
1919 continue;
1920
1921 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1922 t_ret = x509parse_crtfile( chain, entry_name );
1923 if( t_ret < 0 )
1924 return( t_ret );
1925
1926 ret += t_ret;
1927 }
1928 closedir( dir );
1929#endif
1930
1931 return( ret );
1932}
1933
Paul Bakkerd98030e2009-05-02 15:13:40 +00001934/*
1935 * Load one or more CRLs and add them to the chained list
1936 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001937int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001938{
1939 int ret;
1940 size_t n;
1941 unsigned char *buf;
1942
Paul Bakker69e095c2011-12-10 21:55:01 +00001943 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1944 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001945
Paul Bakker27fdf462011-06-09 13:55:13 +00001946 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001947
1948 memset( buf, 0, n + 1 );
1949 free( buf );
1950
1951 return( ret );
1952}
1953
Paul Bakker5121ce52009-01-03 21:22:43 +00001954/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001955 * Load and parse a private RSA key
1956 */
1957int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1958{
1959 int ret;
1960 size_t n;
1961 unsigned char *buf;
1962
Paul Bakker69e095c2011-12-10 21:55:01 +00001963 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1964 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001965
1966 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001967 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001968 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001969 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001970 (unsigned char *) pwd, strlen( pwd ) );
1971
1972 memset( buf, 0, n + 1 );
1973 free( buf );
1974
1975 return( ret );
1976}
1977
1978/*
1979 * Load and parse a public RSA key
1980 */
1981int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1982{
1983 int ret;
1984 size_t n;
1985 unsigned char *buf;
1986
Paul Bakker69e095c2011-12-10 21:55:01 +00001987 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1988 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001989
Paul Bakker27fdf462011-06-09 13:55:13 +00001990 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001991
1992 memset( buf, 0, n + 1 );
1993 free( buf );
1994
1995 return( ret );
1996}
1997#endif /* POLARSSL_FS_IO */
1998
1999/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 * Parse a private RSA key
2001 */
Paul Bakker23986e52011-04-24 08:57:21 +00002002int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2003 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002004{
Paul Bakker23986e52011-04-24 08:57:21 +00002005 int ret;
2006 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002008 unsigned char *p_alt;
2009 x509_buf pk_alg_oid;
2010
Paul Bakker96743fc2011-02-12 14:30:57 +00002011#if defined(POLARSSL_PEM_C)
2012 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Paul Bakker96743fc2011-02-12 14:30:57 +00002014 pem_init( &pem );
2015 ret = pem_read_buffer( &pem,
2016 "-----BEGIN RSA PRIVATE KEY-----",
2017 "-----END RSA PRIVATE KEY-----",
2018 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
Paul Bakkered56b222011-07-13 11:26:43 +00002020 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2021 {
2022 ret = pem_read_buffer( &pem,
2023 "-----BEGIN PRIVATE KEY-----",
2024 "-----END PRIVATE KEY-----",
2025 key, pwd, pwdlen, &len );
2026 }
2027
Paul Bakker96743fc2011-02-12 14:30:57 +00002028 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002030 /*
2031 * Was PEM encoded
2032 */
2033 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002035 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002036 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002037 pem_free( &pem );
2038 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
Paul Bakker96743fc2011-02-12 14:30:57 +00002041 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2042#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002043 ((void) pwd);
2044 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002045 p = (unsigned char *) key;
2046#endif
2047 end = p + keylen;
2048
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002050 * Note: Depending on the type of private key file one can expect either a
2051 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2052 *
2053 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002054 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002055 * algorithm AlgorithmIdentifier,
2056 * PrivateKey BIT STRING
2057 * }
2058 *
2059 * AlgorithmIdentifier ::= SEQUENCE {
2060 * algorithm OBJECT IDENTIFIER,
2061 * parameters ANY DEFINED BY algorithm OPTIONAL
2062 * }
2063 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 * RSAPrivateKey ::= SEQUENCE {
2065 * version Version,
2066 * modulus INTEGER, -- n
2067 * publicExponent INTEGER, -- e
2068 * privateExponent INTEGER, -- d
2069 * prime1 INTEGER, -- p
2070 * prime2 INTEGER, -- q
2071 * exponent1 INTEGER, -- d mod (p-1)
2072 * exponent2 INTEGER, -- d mod (q-1)
2073 * coefficient INTEGER, -- (inverse of q) mod p
2074 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2075 * }
2076 */
2077 if( ( ret = asn1_get_tag( &p, end, &len,
2078 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2079 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002080#if defined(POLARSSL_PEM_C)
2081 pem_free( &pem );
2082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002084 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 }
2086
2087 end = p + len;
2088
2089 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2090 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002091#if defined(POLARSSL_PEM_C)
2092 pem_free( &pem );
2093#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002095 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 }
2097
2098 if( rsa->ver != 0 )
2099 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002100#if defined(POLARSSL_PEM_C)
2101 pem_free( &pem );
2102#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002104 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
2106
Paul Bakkered56b222011-07-13 11:26:43 +00002107 p_alt = p;
2108
2109 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2110 {
2111 // Assume that we have the PKCS#1 format if wrong
2112 // tag was encountered
2113 //
2114 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2115 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2116 {
2117#if defined(POLARSSL_PEM_C)
2118 pem_free( &pem );
2119#endif
2120 rsa_free( rsa );
2121 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2122 }
2123 }
2124 else
2125 {
2126 int can_handle;
2127
2128 /*
2129 * only RSA keys handled at this time
2130 */
2131 can_handle = 0;
2132
2133 if( pk_alg_oid.len == 9 &&
2134 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2135 can_handle = 1;
2136
2137 if( pk_alg_oid.len == 9 &&
2138 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2139 {
2140 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2141 can_handle = 1;
2142
2143 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2144 can_handle = 1;
2145 }
2146
2147 if( pk_alg_oid.len == 5 &&
2148 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2149 can_handle = 1;
2150
2151 if( can_handle == 0 )
2152 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2153
2154 /*
2155 * Parse the PKCS#8 format
2156 */
2157
2158 p = p_alt;
2159 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2160 {
2161#if defined(POLARSSL_PEM_C)
2162 pem_free( &pem );
2163#endif
2164 rsa_free( rsa );
2165 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2166 }
2167
2168 if( ( end - p ) < 1 )
2169 {
2170#if defined(POLARSSL_PEM_C)
2171 pem_free( &pem );
2172#endif
2173 rsa_free( rsa );
2174 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2175 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2176 }
2177
2178 end = p + len;
2179
2180 if( ( ret = asn1_get_tag( &p, end, &len,
2181 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2182 {
2183#if defined(POLARSSL_PEM_C)
2184 pem_free( &pem );
2185#endif
2186 rsa_free( rsa );
2187 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2188 }
2189
2190 end = p + len;
2191
2192 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2193 {
2194#if defined(POLARSSL_PEM_C)
2195 pem_free( &pem );
2196#endif
2197 rsa_free( rsa );
2198 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2199 }
2200
2201 if( rsa->ver != 0 )
2202 {
2203#if defined(POLARSSL_PEM_C)
2204 pem_free( &pem );
2205#endif
2206 rsa_free( rsa );
2207 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2208 }
2209 }
2210
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2212 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2213 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2214 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2215 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2216 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2217 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2218 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2219 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002220#if defined(POLARSSL_PEM_C)
2221 pem_free( &pem );
2222#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002224 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
2226
2227 rsa->len = mpi_size( &rsa->N );
2228
2229 if( p != end )
2230 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002231#if defined(POLARSSL_PEM_C)
2232 pem_free( &pem );
2233#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002235 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002236 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 }
2238
2239 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2240 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002241#if defined(POLARSSL_PEM_C)
2242 pem_free( &pem );
2243#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 rsa_free( rsa );
2245 return( ret );
2246 }
2247
Paul Bakker96743fc2011-02-12 14:30:57 +00002248#if defined(POLARSSL_PEM_C)
2249 pem_free( &pem );
2250#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002251
2252 return( 0 );
2253}
2254
2255/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002256 * Parse a public RSA key
2257 */
Paul Bakker23986e52011-04-24 08:57:21 +00002258int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002259{
Paul Bakker23986e52011-04-24 08:57:21 +00002260 int ret;
2261 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002262 unsigned char *p, *end;
2263 x509_buf alg_oid;
2264#if defined(POLARSSL_PEM_C)
2265 pem_context pem;
2266
2267 pem_init( &pem );
2268 ret = pem_read_buffer( &pem,
2269 "-----BEGIN PUBLIC KEY-----",
2270 "-----END PUBLIC KEY-----",
2271 key, NULL, 0, &len );
2272
2273 if( ret == 0 )
2274 {
2275 /*
2276 * Was PEM encoded
2277 */
2278 keylen = pem.buflen;
2279 }
2280 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2281 {
2282 pem_free( &pem );
2283 return( ret );
2284 }
2285
2286 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2287#else
2288 p = (unsigned char *) key;
2289#endif
2290 end = p + keylen;
2291
2292 /*
2293 * PublicKeyInfo ::= SEQUENCE {
2294 * algorithm AlgorithmIdentifier,
2295 * PublicKey BIT STRING
2296 * }
2297 *
2298 * AlgorithmIdentifier ::= SEQUENCE {
2299 * algorithm OBJECT IDENTIFIER,
2300 * parameters ANY DEFINED BY algorithm OPTIONAL
2301 * }
2302 *
2303 * RSAPublicKey ::= SEQUENCE {
2304 * modulus INTEGER, -- n
2305 * publicExponent INTEGER -- e
2306 * }
2307 */
2308
2309 if( ( ret = asn1_get_tag( &p, end, &len,
2310 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2311 {
2312#if defined(POLARSSL_PEM_C)
2313 pem_free( &pem );
2314#endif
2315 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002316 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002317 }
2318
2319 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2320 {
2321#if defined(POLARSSL_PEM_C)
2322 pem_free( &pem );
2323#endif
2324 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002325 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002326 }
2327
2328 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2329 {
2330#if defined(POLARSSL_PEM_C)
2331 pem_free( &pem );
2332#endif
2333 rsa_free( rsa );
2334 return( ret );
2335 }
2336
2337 rsa->len = mpi_size( &rsa->N );
2338
2339#if defined(POLARSSL_PEM_C)
2340 pem_free( &pem );
2341#endif
2342
2343 return( 0 );
2344}
2345
Paul Bakkereaa89f82011-04-04 21:36:15 +00002346#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002347/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002348 * Parse DHM parameters
2349 */
Paul Bakker23986e52011-04-24 08:57:21 +00002350int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002351{
Paul Bakker23986e52011-04-24 08:57:21 +00002352 int ret;
2353 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002354 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002355#if defined(POLARSSL_PEM_C)
2356 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002357
Paul Bakker96743fc2011-02-12 14:30:57 +00002358 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002359
Paul Bakker96743fc2011-02-12 14:30:57 +00002360 ret = pem_read_buffer( &pem,
2361 "-----BEGIN DH PARAMETERS-----",
2362 "-----END DH PARAMETERS-----",
2363 dhmin, NULL, 0, &dhminlen );
2364
2365 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002366 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002367 /*
2368 * Was PEM encoded
2369 */
2370 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002371 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002372 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002373 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002374 pem_free( &pem );
2375 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002376 }
2377
Paul Bakker96743fc2011-02-12 14:30:57 +00002378 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2379#else
2380 p = (unsigned char *) dhmin;
2381#endif
2382 end = p + dhminlen;
2383
Paul Bakker1b57b062011-01-06 15:48:19 +00002384 memset( dhm, 0, sizeof( dhm_context ) );
2385
Paul Bakker1b57b062011-01-06 15:48:19 +00002386 /*
2387 * DHParams ::= SEQUENCE {
2388 * prime INTEGER, -- P
2389 * generator INTEGER, -- g
2390 * }
2391 */
2392 if( ( ret = asn1_get_tag( &p, end, &len,
2393 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2394 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002395#if defined(POLARSSL_PEM_C)
2396 pem_free( &pem );
2397#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002398 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002399 }
2400
2401 end = p + len;
2402
2403 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2404 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2405 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002406#if defined(POLARSSL_PEM_C)
2407 pem_free( &pem );
2408#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002409 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002410 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002411 }
2412
2413 if( p != end )
2414 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002415#if defined(POLARSSL_PEM_C)
2416 pem_free( &pem );
2417#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002418 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002419 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002420 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2421 }
2422
Paul Bakker96743fc2011-02-12 14:30:57 +00002423#if defined(POLARSSL_PEM_C)
2424 pem_free( &pem );
2425#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002426
2427 return( 0 );
2428}
2429
Paul Bakker335db3f2011-04-25 15:28:35 +00002430#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002431/*
2432 * Load and parse a private RSA key
2433 */
2434int x509parse_dhmfile( dhm_context *dhm, const char *path )
2435{
2436 int ret;
2437 size_t n;
2438 unsigned char *buf;
2439
Paul Bakker69e095c2011-12-10 21:55:01 +00002440 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2441 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002442
Paul Bakker27fdf462011-06-09 13:55:13 +00002443 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002444
2445 memset( buf, 0, n + 1 );
2446 free( buf );
2447
2448 return( ret );
2449}
Paul Bakker335db3f2011-04-25 15:28:35 +00002450#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002451#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002452
Paul Bakker5121ce52009-01-03 21:22:43 +00002453#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002454#include <stdarg.h>
2455
2456#if !defined vsnprintf
2457#define vsnprintf _vsnprintf
2458#endif // vsnprintf
2459
2460/*
2461 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2462 * Result value is not size of buffer needed, but -1 if no fit is possible.
2463 *
2464 * This fuction tries to 'fix' this by at least suggesting enlarging the
2465 * size by 20.
2466 */
2467int compat_snprintf(char *str, size_t size, const char *format, ...)
2468{
2469 va_list ap;
2470 int res = -1;
2471
2472 va_start( ap, format );
2473
2474 res = vsnprintf( str, size, format, ap );
2475
2476 va_end( ap );
2477
2478 // No quick fix possible
2479 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002480 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002481
2482 return res;
2483}
2484
2485#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002486#endif
2487
Paul Bakkerd98030e2009-05-02 15:13:40 +00002488#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2489
2490#define SAFE_SNPRINTF() \
2491{ \
2492 if( ret == -1 ) \
2493 return( -1 ); \
2494 \
Paul Bakker23986e52011-04-24 08:57:21 +00002495 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002496 p[n - 1] = '\0'; \
2497 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2498 } \
2499 \
Paul Bakker23986e52011-04-24 08:57:21 +00002500 n -= (unsigned int) ret; \
2501 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002502}
2503
Paul Bakker5121ce52009-01-03 21:22:43 +00002504/*
2505 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002506 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002508int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002509{
Paul Bakker23986e52011-04-24 08:57:21 +00002510 int ret;
2511 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002513 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 char s[128], *p;
2515
2516 memset( s, 0, sizeof( s ) );
2517
2518 name = dn;
2519 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002520 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
2522 while( name != NULL )
2523 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002524 if( !name->oid.p )
2525 {
2526 name = name->next;
2527 continue;
2528 }
2529
Paul Bakker74111d32011-01-15 16:57:55 +00002530 if( name != dn )
2531 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002532 ret = snprintf( p, n, ", " );
2533 SAFE_SNPRINTF();
2534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Paul Bakker535e97d2012-08-23 10:49:55 +00002536 if( name->oid.len == 3 &&
2537 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 {
2539 switch( name->oid.p[2] )
2540 {
2541 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002542 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
2544 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002545 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
2547 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002548 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
2550 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002551 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
2553 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002554 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
2556 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002557 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
2559 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002560 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 name->oid.p[2] );
2562 break;
2563 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002564 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002566 else if( name->oid.len == 9 &&
2567 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 {
2569 switch( name->oid.p[8] )
2570 {
2571 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002572 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
2574 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002575 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 name->oid.p[8] );
2577 break;
2578 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002579 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
2581 else
Paul Bakker74111d32011-01-15 16:57:55 +00002582 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002583 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002584 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 for( i = 0; i < name->val.len; i++ )
2588 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002589 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 break;
2591
2592 c = name->val.p[i];
2593 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2594 s[i] = '?';
2595 else s[i] = c;
2596 }
2597 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002598 ret = snprintf( p, n, "%s", s );
2599 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 name = name->next;
2601 }
2602
Paul Bakker23986e52011-04-24 08:57:21 +00002603 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604}
2605
2606/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002607 * Store the serial in printable form into buf; no more
2608 * than size characters will be written
2609 */
2610int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2611{
Paul Bakker23986e52011-04-24 08:57:21 +00002612 int ret;
2613 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002614 char *p;
2615
2616 p = buf;
2617 n = size;
2618
2619 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002620 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002621
2622 for( i = 0; i < nr; i++ )
2623 {
Paul Bakker93048802011-12-05 14:38:06 +00002624 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002625 continue;
2626
Paul Bakkerdd476992011-01-16 21:34:59 +00002627 ret = snprintf( p, n, "%02X%s",
2628 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2629 SAFE_SNPRINTF();
2630 }
2631
Paul Bakker03c7c252011-11-25 12:37:37 +00002632 if( nr != serial->len )
2633 {
2634 ret = snprintf( p, n, "...." );
2635 SAFE_SNPRINTF();
2636 }
2637
Paul Bakker23986e52011-04-24 08:57:21 +00002638 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002639}
2640
2641/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002642 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002644int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2645 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002646{
Paul Bakker23986e52011-04-24 08:57:21 +00002647 int ret;
2648 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002649 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
2651 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002652 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
Paul Bakkerd98030e2009-05-02 15:13:40 +00002654 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002656 SAFE_SNPRINTF();
2657 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002659 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Paul Bakkerdd476992011-01-16 21:34:59 +00002661 ret = x509parse_serial_gets( p, n, &crt->serial);
2662 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Paul Bakkerd98030e2009-05-02 15:13:40 +00002664 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2665 SAFE_SNPRINTF();
2666 ret = x509parse_dn_gets( p, n, &crt->issuer );
2667 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Paul Bakkerd98030e2009-05-02 15:13:40 +00002669 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2670 SAFE_SNPRINTF();
2671 ret = x509parse_dn_gets( p, n, &crt->subject );
2672 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Paul Bakkerd98030e2009-05-02 15:13:40 +00002674 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2676 crt->valid_from.year, crt->valid_from.mon,
2677 crt->valid_from.day, crt->valid_from.hour,
2678 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002679 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Paul Bakkerd98030e2009-05-02 15:13:40 +00002681 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2683 crt->valid_to.year, crt->valid_to.mon,
2684 crt->valid_to.day, crt->valid_to.hour,
2685 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002686 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Paul Bakkerd98030e2009-05-02 15:13:40 +00002688 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2689 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Paul Bakker27d66162010-03-17 06:56:01 +00002691 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002693 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2694 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2695 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2696 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2697 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2698 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2699 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2700 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2701 default: ret = snprintf( p, n, "???" ); break;
2702 }
2703 SAFE_SNPRINTF();
2704
2705 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002706 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002707 SAFE_SNPRINTF();
2708
Paul Bakker23986e52011-04-24 08:57:21 +00002709 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002710}
2711
Paul Bakker74111d32011-01-15 16:57:55 +00002712/* Compare a given OID string with an OID x509_buf * */
2713#define OID_CMP(oid_str, oid_buf) \
2714 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2715 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2716
2717/*
2718 * Return an informational string describing the given OID
2719 */
2720const char *x509_oid_get_description( x509_buf *oid )
2721{
2722 if ( oid == NULL )
2723 return ( NULL );
2724
2725 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2726 return( STRING_SERVER_AUTH );
2727
2728 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2729 return( STRING_CLIENT_AUTH );
2730
2731 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2732 return( STRING_CODE_SIGNING );
2733
2734 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2735 return( STRING_EMAIL_PROTECTION );
2736
2737 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2738 return( STRING_TIME_STAMPING );
2739
2740 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2741 return( STRING_OCSP_SIGNING );
2742
2743 return( NULL );
2744}
2745
2746/* Return the x.y.z.... style numeric string for the given OID */
2747int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2748{
Paul Bakker23986e52011-04-24 08:57:21 +00002749 int ret;
2750 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002751 unsigned int value;
2752 char *p;
2753
2754 p = buf;
2755 n = size;
2756
2757 /* First byte contains first two dots */
2758 if( oid->len > 0 )
2759 {
2760 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2761 SAFE_SNPRINTF();
2762 }
2763
2764 /* TODO: value can overflow in value. */
2765 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002766 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002767 {
2768 value <<= 7;
2769 value += oid->p[i] & 0x7F;
2770
2771 if( !( oid->p[i] & 0x80 ) )
2772 {
2773 /* Last byte */
2774 ret = snprintf( p, n, ".%d", value );
2775 SAFE_SNPRINTF();
2776 value = 0;
2777 }
2778 }
2779
Paul Bakker23986e52011-04-24 08:57:21 +00002780 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002781}
2782
Paul Bakkerd98030e2009-05-02 15:13:40 +00002783/*
2784 * Return an informational string about the CRL.
2785 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002786int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2787 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002788{
Paul Bakker23986e52011-04-24 08:57:21 +00002789 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002790 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002791 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002792 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002793
2794 p = buf;
2795 n = size;
2796
2797 ret = snprintf( p, n, "%sCRL version : %d",
2798 prefix, crl->version );
2799 SAFE_SNPRINTF();
2800
2801 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2802 SAFE_SNPRINTF();
2803 ret = x509parse_dn_gets( p, n, &crl->issuer );
2804 SAFE_SNPRINTF();
2805
2806 ret = snprintf( p, n, "\n%sthis update : " \
2807 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2808 crl->this_update.year, crl->this_update.mon,
2809 crl->this_update.day, crl->this_update.hour,
2810 crl->this_update.min, crl->this_update.sec );
2811 SAFE_SNPRINTF();
2812
2813 ret = snprintf( p, n, "\n%snext update : " \
2814 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2815 crl->next_update.year, crl->next_update.mon,
2816 crl->next_update.day, crl->next_update.hour,
2817 crl->next_update.min, crl->next_update.sec );
2818 SAFE_SNPRINTF();
2819
2820 entry = &crl->entry;
2821
2822 ret = snprintf( p, n, "\n%sRevoked certificates:",
2823 prefix );
2824 SAFE_SNPRINTF();
2825
Paul Bakker9be19372009-07-27 20:21:53 +00002826 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002827 {
2828 ret = snprintf( p, n, "\n%sserial number: ",
2829 prefix );
2830 SAFE_SNPRINTF();
2831
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002832 ret = x509parse_serial_gets( p, n, &entry->serial);
2833 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002834
Paul Bakkerd98030e2009-05-02 15:13:40 +00002835 ret = snprintf( p, n, " revocation date: " \
2836 "%04d-%02d-%02d %02d:%02d:%02d",
2837 entry->revocation_date.year, entry->revocation_date.mon,
2838 entry->revocation_date.day, entry->revocation_date.hour,
2839 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002840 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002841
2842 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 }
2844
Paul Bakkerd98030e2009-05-02 15:13:40 +00002845 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2846 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakker27d66162010-03-17 06:56:01 +00002848 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002849 {
2850 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2851 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2852 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2853 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2854 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2855 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2856 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2857 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2858 default: ret = snprintf( p, n, "???" ); break;
2859 }
2860 SAFE_SNPRINTF();
2861
Paul Bakker1e27bb22009-07-19 20:25:25 +00002862 ret = snprintf( p, n, "\n" );
2863 SAFE_SNPRINTF();
2864
Paul Bakker23986e52011-04-24 08:57:21 +00002865 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002866}
2867
2868/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002869 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002871int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002872{
Paul Bakkercce9d772011-11-18 14:26:47 +00002873 int year, mon, day;
2874 int hour, min, sec;
2875
2876#if defined(_WIN32)
2877 SYSTEMTIME st;
2878
2879 GetLocalTime(&st);
2880
2881 year = st.wYear;
2882 mon = st.wMonth;
2883 day = st.wDay;
2884 hour = st.wHour;
2885 min = st.wMinute;
2886 sec = st.wSecond;
2887#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002888 struct tm *lt;
2889 time_t tt;
2890
2891 tt = time( NULL );
2892 lt = localtime( &tt );
2893
Paul Bakkercce9d772011-11-18 14:26:47 +00002894 year = lt->tm_year + 1900;
2895 mon = lt->tm_mon + 1;
2896 day = lt->tm_mday;
2897 hour = lt->tm_hour;
2898 min = lt->tm_min;
2899 sec = lt->tm_sec;
2900#endif
2901
2902 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002903 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Paul Bakkercce9d772011-11-18 14:26:47 +00002905 if( year == to->year &&
2906 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002907 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Paul Bakkercce9d772011-11-18 14:26:47 +00002909 if( year == to->year &&
2910 mon == to->mon &&
2911 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002912 return( 1 );
2913
Paul Bakkercce9d772011-11-18 14:26:47 +00002914 if( year == to->year &&
2915 mon == to->mon &&
2916 day == to->day &&
2917 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002918 return( 1 );
2919
Paul Bakkercce9d772011-11-18 14:26:47 +00002920 if( year == to->year &&
2921 mon == to->mon &&
2922 day == to->day &&
2923 hour == to->hour &&
2924 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002925 return( 1 );
2926
Paul Bakkercce9d772011-11-18 14:26:47 +00002927 if( year == to->year &&
2928 mon == to->mon &&
2929 day == to->day &&
2930 hour == to->hour &&
2931 min == to->min &&
2932 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002933 return( 1 );
2934
Paul Bakker40ea7de2009-05-03 10:18:48 +00002935 return( 0 );
2936}
2937
2938/*
2939 * Return 1 if the certificate is revoked, or 0 otherwise.
2940 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002941int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002942{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002943 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002944
2945 while( cur != NULL && cur->serial.len != 0 )
2946 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002947 if( crt->serial.len == cur->serial.len &&
2948 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002949 {
2950 if( x509parse_time_expired( &cur->revocation_date ) )
2951 return( 1 );
2952 }
2953
2954 cur = cur->next;
2955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
2957 return( 0 );
2958}
2959
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002960/*
2961 * Wrapper for x509 hashes.
2962 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002963 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002964 */
Paul Bakker23986e52011-04-24 08:57:21 +00002965static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 unsigned char *out )
2967{
2968 switch( alg )
2969 {
Paul Bakker40e46942009-01-03 21:51:57 +00002970#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002971 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002972#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002973#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002974 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002975#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002976#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002977 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002978#endif
2979#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002980 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002981#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002982#if defined(POLARSSL_SHA2_C)
2983 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2984 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2985#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002986#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002987 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2988 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2989#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002991 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 break;
2993 }
2994}
2995
2996/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002997 * Check that the given certificate is valid accoring to the CRL.
2998 */
2999static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3000 x509_crl *crl_list)
3001{
3002 int flags = 0;
3003 int hash_id;
3004 unsigned char hash[64];
3005
3006 /*
3007 * TODO: What happens if no CRL is present?
3008 * Suggestion: Revocation state should be unknown if no CRL is present.
3009 * For backwards compatibility this is not yet implemented.
3010 */
3011
3012 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
3013 {
3014 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
3015 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3016 crl_list->issuer_raw.len ) != 0 )
3017 {
3018 crl_list = crl_list->next;
3019 continue;
3020 }
3021
3022 /*
3023 * Check if CRL is correctly signed by the trusted CA
3024 */
3025 hash_id = crl_list->sig_alg;
3026
3027 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3028
3029 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
3030 0, hash, crl_list->sig.p ) == 0 )
3031 {
3032 /*
3033 * CRL is not trusted
3034 */
3035 flags |= BADCRL_NOT_TRUSTED;
3036 break;
3037 }
3038
3039 /*
3040 * Check for validity of CRL (Do not drop out)
3041 */
3042 if( x509parse_time_expired( &crl_list->next_update ) )
3043 flags |= BADCRL_EXPIRED;
3044
3045 /*
3046 * Check if certificate is revoked
3047 */
3048 if( x509parse_revoked(crt, crl_list) )
3049 {
3050 flags |= BADCERT_REVOKED;
3051 break;
3052 }
3053
3054 crl_list = crl_list->next;
3055 }
3056 return flags;
3057}
3058
Paul Bakker57b12982012-02-11 17:38:38 +00003059int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003060{
3061 size_t i;
3062 size_t cn_idx = 0;
3063
Paul Bakker57b12982012-02-11 17:38:38 +00003064 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003065 return( 0 );
3066
3067 for( i = 0; i < strlen( cn ); ++i )
3068 {
3069 if( cn[i] == '.' )
3070 {
3071 cn_idx = i;
3072 break;
3073 }
3074 }
3075
3076 if( cn_idx == 0 )
3077 return( 0 );
3078
Paul Bakker535e97d2012-08-23 10:49:55 +00003079 if( strlen( cn ) - cn_idx == name->len - 1 &&
3080 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003081 {
3082 return( 1 );
3083 }
3084
3085 return( 0 );
3086}
3087
Paul Bakker76fd75a2011-01-16 21:12:10 +00003088/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 * Verify the certificate validity
3090 */
3091int x509parse_verify( x509_cert *crt,
3092 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003093 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003094 const char *cn, int *flags,
3095 int (*f_vrfy)(void *, x509_cert *, int, int),
3096 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003097{
Paul Bakker23986e52011-04-24 08:57:21 +00003098 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 int hash_id;
3100 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003101 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00003103 unsigned char hash[64];
Paul Bakkera8cd2392012-02-11 16:09:32 +00003104 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
Paul Bakker40ea7de2009-05-03 10:18:48 +00003106 *flags = 0;
3107
3108 if( x509parse_time_expired( &crt->valid_to ) )
3109 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003110
3111 if( cn != NULL )
3112 {
3113 name = &crt->subject;
3114 cn_len = strlen( cn );
3115
Paul Bakker4d2c1242012-05-10 14:12:46 +00003116 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003118 cur = &crt->subject_alt_names;
3119
3120 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003121 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003122 if( cur->buf.len == cn_len &&
3123 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003124 break;
3125
Paul Bakker535e97d2012-08-23 10:49:55 +00003126 if( cur->buf.len > 2 &&
3127 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003128 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003129 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003130
Paul Bakker4d2c1242012-05-10 14:12:46 +00003131 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003132 }
3133
3134 if( cur == NULL )
3135 *flags |= BADCERT_CN_MISMATCH;
3136 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003137 else
3138 {
3139 while( name != NULL )
3140 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003141 if( name->oid.len == 3 &&
3142 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003143 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003144 if( name->val.len == cn_len &&
3145 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003146 break;
3147
Paul Bakker535e97d2012-08-23 10:49:55 +00003148 if( name->val.len > 2 &&
3149 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003150 x509_wildcard_verify( cn, &name->val ) )
3151 break;
3152 }
3153
3154 name = name->next;
3155 }
3156
3157 if( name == NULL )
3158 *flags |= BADCERT_CN_MISMATCH;
3159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003160 }
3161
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 /*
3163 * Iterate upwards in the given cert chain,
3164 * ignoring any upper cert with CA != TRUE.
3165 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003166 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003167
3168 pathlen = 1;
3169
Paul Bakker76fd75a2011-01-16 21:12:10 +00003170 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003171 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003172 if( parent->ca_istrue == 0 ||
3173 crt->issuer_raw.len != parent->subject_raw.len ||
3174 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 crt->issuer_raw.len ) != 0 )
3176 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003177 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 continue;
3179 }
3180
Paul Bakker27d66162010-03-17 06:56:01 +00003181 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
3183 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3184
Paul Bakker76fd75a2011-01-16 21:12:10 +00003185 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3186 crt->sig.p ) != 0 )
3187 *flags |= BADCERT_NOT_TRUSTED;
3188
3189 /* Check trusted CA's CRL for the given crt */
3190 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003191
3192 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00003193 if( NULL != f_vrfy )
3194 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003195 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003196 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003197 else
3198 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003199 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003200 else if( *flags != 0 )
3201 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003202
3203 pathlen++;
3204
Paul Bakker76fd75a2011-01-16 21:12:10 +00003205 crt = parent;
3206 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 }
3208
3209 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003210 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003212 *flags |= BADCERT_NOT_TRUSTED;
3213
Paul Bakker7c6d4a42009-03-28 20:35:47 +00003214 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 {
3216 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
3217 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
3218 crt->issuer_raw.len ) != 0 )
3219 {
3220 trust_ca = trust_ca->next;
3221 continue;
3222 }
3223
3224 if( trust_ca->max_pathlen > 0 &&
3225 trust_ca->max_pathlen < pathlen )
3226 break;
3227
Paul Bakker27d66162010-03-17 06:56:01 +00003228 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003229
3230 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3231
3232 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3233 0, hash, crt->sig.p ) == 0 )
3234 {
3235 /*
3236 * cert. is signed by a trusted CA
3237 */
3238 *flags &= ~BADCERT_NOT_TRUSTED;
3239 break;
3240 }
3241
3242 trust_ca = trust_ca->next;
3243 }
3244
Paul Bakker76fd75a2011-01-16 21:12:10 +00003245 /* Check trusted CA's CRL for the given crt */
3246 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003247
3248 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003249 if( NULL != f_vrfy )
3250 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003251 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003252 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003253 else
3254 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003255 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003256 else if( *flags != 0 )
3257 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003258
Paul Bakker5121ce52009-01-03 21:22:43 +00003259 return( 0 );
3260}
3261
3262/*
3263 * Unallocate all certificate data
3264 */
3265void x509_free( x509_cert *crt )
3266{
3267 x509_cert *cert_cur = crt;
3268 x509_cert *cert_prv;
3269 x509_name *name_cur;
3270 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003271 x509_sequence *seq_cur;
3272 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
3274 if( crt == NULL )
3275 return;
3276
3277 do
3278 {
3279 rsa_free( &cert_cur->rsa );
3280
3281 name_cur = cert_cur->issuer.next;
3282 while( name_cur != NULL )
3283 {
3284 name_prv = name_cur;
3285 name_cur = name_cur->next;
3286 memset( name_prv, 0, sizeof( x509_name ) );
3287 free( name_prv );
3288 }
3289
3290 name_cur = cert_cur->subject.next;
3291 while( name_cur != NULL )
3292 {
3293 name_prv = name_cur;
3294 name_cur = name_cur->next;
3295 memset( name_prv, 0, sizeof( x509_name ) );
3296 free( name_prv );
3297 }
3298
Paul Bakker74111d32011-01-15 16:57:55 +00003299 seq_cur = cert_cur->ext_key_usage.next;
3300 while( seq_cur != NULL )
3301 {
3302 seq_prv = seq_cur;
3303 seq_cur = seq_cur->next;
3304 memset( seq_prv, 0, sizeof( x509_sequence ) );
3305 free( seq_prv );
3306 }
3307
Paul Bakker8afa70d2012-02-11 18:42:45 +00003308 seq_cur = cert_cur->subject_alt_names.next;
3309 while( seq_cur != NULL )
3310 {
3311 seq_prv = seq_cur;
3312 seq_cur = seq_cur->next;
3313 memset( seq_prv, 0, sizeof( x509_sequence ) );
3314 free( seq_prv );
3315 }
3316
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 if( cert_cur->raw.p != NULL )
3318 {
3319 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3320 free( cert_cur->raw.p );
3321 }
3322
3323 cert_cur = cert_cur->next;
3324 }
3325 while( cert_cur != NULL );
3326
3327 cert_cur = crt;
3328 do
3329 {
3330 cert_prv = cert_cur;
3331 cert_cur = cert_cur->next;
3332
3333 memset( cert_prv, 0, sizeof( x509_cert ) );
3334 if( cert_prv != crt )
3335 free( cert_prv );
3336 }
3337 while( cert_cur != NULL );
3338}
3339
Paul Bakkerd98030e2009-05-02 15:13:40 +00003340/*
3341 * Unallocate all CRL data
3342 */
3343void x509_crl_free( x509_crl *crl )
3344{
3345 x509_crl *crl_cur = crl;
3346 x509_crl *crl_prv;
3347 x509_name *name_cur;
3348 x509_name *name_prv;
3349 x509_crl_entry *entry_cur;
3350 x509_crl_entry *entry_prv;
3351
3352 if( crl == NULL )
3353 return;
3354
3355 do
3356 {
3357 name_cur = crl_cur->issuer.next;
3358 while( name_cur != NULL )
3359 {
3360 name_prv = name_cur;
3361 name_cur = name_cur->next;
3362 memset( name_prv, 0, sizeof( x509_name ) );
3363 free( name_prv );
3364 }
3365
3366 entry_cur = crl_cur->entry.next;
3367 while( entry_cur != NULL )
3368 {
3369 entry_prv = entry_cur;
3370 entry_cur = entry_cur->next;
3371 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3372 free( entry_prv );
3373 }
3374
3375 if( crl_cur->raw.p != NULL )
3376 {
3377 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3378 free( crl_cur->raw.p );
3379 }
3380
3381 crl_cur = crl_cur->next;
3382 }
3383 while( crl_cur != NULL );
3384
3385 crl_cur = crl;
3386 do
3387 {
3388 crl_prv = crl_cur;
3389 crl_cur = crl_cur->next;
3390
3391 memset( crl_prv, 0, sizeof( x509_crl ) );
3392 if( crl_prv != crl )
3393 free( crl_prv );
3394 }
3395 while( crl_cur != NULL );
3396}
3397
Paul Bakker40e46942009-01-03 21:51:57 +00003398#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003399
Paul Bakker40e46942009-01-03 21:51:57 +00003400#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003401
3402/*
3403 * Checkup routine
3404 */
3405int x509_self_test( int verbose )
3406{
Paul Bakker5690efc2011-05-26 13:16:06 +00003407#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003408 int ret;
3409 int flags;
3410 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 x509_cert cacert;
3412 x509_cert clicert;
3413 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003414#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003415 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003416#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003417
3418 if( verbose != 0 )
3419 printf( " X.509 certificate load: " );
3420
3421 memset( &clicert, 0, sizeof( x509_cert ) );
3422
3423 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003424 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003425 if( ret != 0 )
3426 {
3427 if( verbose != 0 )
3428 printf( "failed\n" );
3429
3430 return( ret );
3431 }
3432
3433 memset( &cacert, 0, sizeof( x509_cert ) );
3434
3435 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003436 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003437 if( ret != 0 )
3438 {
3439 if( verbose != 0 )
3440 printf( "failed\n" );
3441
3442 return( ret );
3443 }
3444
3445 if( verbose != 0 )
3446 printf( "passed\n X.509 private key load: " );
3447
3448 i = strlen( test_ca_key );
3449 j = strlen( test_ca_pwd );
3450
Paul Bakker66b78b22011-03-25 14:22:50 +00003451 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3452
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 if( ( ret = x509parse_key( &rsa,
3454 (unsigned char *) test_ca_key, i,
3455 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3456 {
3457 if( verbose != 0 )
3458 printf( "failed\n" );
3459
3460 return( ret );
3461 }
3462
3463 if( verbose != 0 )
3464 printf( "passed\n X.509 signature verify: ");
3465
Paul Bakker23986e52011-04-24 08:57:21 +00003466 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 if( ret != 0 )
3468 {
Paul Bakker23986e52011-04-24 08:57:21 +00003469 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 if( verbose != 0 )
3471 printf( "failed\n" );
3472
3473 return( ret );
3474 }
3475
Paul Bakker5690efc2011-05-26 13:16:06 +00003476#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003478 printf( "passed\n X.509 DHM parameter load: " );
3479
3480 i = strlen( test_dhm_params );
3481 j = strlen( test_ca_pwd );
3482
3483 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3484 {
3485 if( verbose != 0 )
3486 printf( "failed\n" );
3487
3488 return( ret );
3489 }
3490
3491 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003492 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003493#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003494
3495 x509_free( &cacert );
3496 x509_free( &clicert );
3497 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003498#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003499 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003500#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003501
3502 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003503#else
3504 ((void) verbose);
3505 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003507}
3508
3509#endif
3510
3511#endif