blob: ab633be99fab2b8cd22b04dfa929880451fbd812 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerefc30292011-11-10 14:43:23 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000043#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/des.h"
45#include "polarssl/md2.h"
46#include "polarssl/md4.h"
47#include "polarssl/md5.h"
48#include "polarssl/sha1.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000049#include "polarssl/sha2.h"
50#include "polarssl/sha4.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000051#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53#include <string.h>
54#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000055#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000056#include <windows.h>
57#else
Paul Bakker5121ce52009-01-03 21:22:43 +000058#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakker335db3f2011-04-25 15:28:35 +000061#if defined(POLARSSL_FS_IO)
62#include <stdio.h>
Paul Bakkere4791f32012-06-04 21:29:15 +000063#if defined(_WIN32)
64#include <strsafe.h>
65#else
Paul Bakker8d914582012-06-04 12:46:42 +000066#include <sys/types.h>
67#include <dirent.h>
68#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000069#endif
70
Paul Bakker5121ce52009-01-03 21:22:43 +000071/*
Paul Bakker5121ce52009-01-03 21:22:43 +000072 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
73 */
74static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000075 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000076 int *ver )
77{
Paul Bakker23986e52011-04-24 08:57:21 +000078 int ret;
79 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 if( ( ret = asn1_get_tag( p, end, &len,
82 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
83 {
Paul Bakker40e46942009-01-03 21:51:57 +000084 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000085 {
86 *ver = 0;
87 return( 0 );
88 }
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90 return( ret );
91 }
92
93 end = *p + len;
94
95 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000096 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +000099 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000100 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102 return( 0 );
103}
104
105/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000106 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000107 */
108static int x509_crl_get_version( unsigned char **p,
109 const unsigned char *end,
110 int *ver )
111{
112 int ret;
113
114 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
115 {
116 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000117 {
118 *ver = 0;
119 return( 0 );
120 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000121
122 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
123 }
124
125 return( 0 );
126}
127
128/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 * CertificateSerialNumber ::= INTEGER
130 */
131static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000132 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 x509_buf *serial )
134{
135 int ret;
136
137 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000138 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000139 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
142 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000143 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000144 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 serial->tag = *(*p)++;
147
148 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 serial->p = *p;
152 *p += serial->len;
153
154 return( 0 );
155}
156
157/*
158 * AlgorithmIdentifier ::= SEQUENCE {
159 * algorithm OBJECT IDENTIFIER,
160 * parameters ANY DEFINED BY algorithm OPTIONAL }
161 */
162static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000163 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 x509_buf *alg )
165{
Paul Bakker23986e52011-04-24 08:57:21 +0000166 int ret;
167 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 if( ( ret = asn1_get_tag( p, end, &len,
170 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000171 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 end = *p + len;
174 alg->tag = **p;
175
176 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000177 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 alg->p = *p;
180 *p += alg->len;
181
182 if( *p == end )
183 return( 0 );
184
185 /*
186 * assume the algorithm parameters must be NULL
187 */
188 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000189 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000192 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000193 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 return( 0 );
196}
197
198/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * AttributeTypeAndValue ::= SEQUENCE {
200 * type AttributeType,
201 * value AttributeValue }
202 *
203 * AttributeType ::= OBJECT IDENTIFIER
204 *
205 * AttributeValue ::= ANY DEFINED BY AttributeType
206 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000207static int x509_get_attr_type_value( unsigned char **p,
208 const unsigned char *end,
209 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210{
Paul Bakker23986e52011-04-24 08:57:21 +0000211 int ret;
212 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 x509_buf *oid;
214 x509_buf *val;
215
216 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000218 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 oid = &cur->oid;
221 oid->tag = **p;
222
223 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
226 oid->p = *p;
227 *p += oid->len;
228
229 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000230 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000231 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
234 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
235 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000236 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000237 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 val = &cur->val;
240 val->tag = *(*p)++;
241
242 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000243 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245 val->p = *p;
246 *p += val->len;
247
248 cur->next = NULL;
249
Paul Bakker400ff6f2011-02-20 10:40:16 +0000250 return( 0 );
251}
252
253/*
254 * RelativeDistinguishedName ::=
255 * SET OF AttributeTypeAndValue
256 *
257 * AttributeTypeAndValue ::= SEQUENCE {
258 * type AttributeType,
259 * value AttributeValue }
260 *
261 * AttributeType ::= OBJECT IDENTIFIER
262 *
263 * AttributeValue ::= ANY DEFINED BY AttributeType
264 */
265static int x509_get_name( unsigned char **p,
266 const unsigned char *end,
267 x509_name *cur )
268{
Paul Bakker23986e52011-04-24 08:57:21 +0000269 int ret;
270 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000271 const unsigned char *end2;
272 x509_name *use;
273
274 if( ( ret = asn1_get_tag( p, end, &len,
275 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000276 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277
278 end2 = end;
279 end = *p + len;
280 use = cur;
281
282 do
283 {
284 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
285 return( ret );
286
287 if( *p != end )
288 {
289 use->next = (x509_name *) malloc(
290 sizeof( x509_name ) );
291
292 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000293 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000294
295 memset( use->next, 0, sizeof( x509_name ) );
296
297 use = use->next;
298 }
299 }
300 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302 /*
303 * recurse until end of SEQUENCE is reached
304 */
305 if( *p == end2 )
306 return( 0 );
307
308 cur->next = (x509_name *) malloc(
309 sizeof( x509_name ) );
310
311 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000312 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
Paul Bakker430ffbe2012-05-01 08:14:20 +0000314 memset( cur->next, 0, sizeof( x509_name ) );
315
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 return( x509_get_name( p, end2, cur->next ) );
317}
318
319/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 * Time ::= CHOICE {
321 * utcTime UTCTime,
322 * generalTime GeneralizedTime }
323 */
Paul Bakker91200182010-02-18 21:26:15 +0000324static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000325 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000326 x509_time *time )
327{
Paul Bakker23986e52011-04-24 08:57:21 +0000328 int ret;
329 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000331 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332
Paul Bakker91200182010-02-18 21:26:15 +0000333 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000334 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
335 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336
Paul Bakker91200182010-02-18 21:26:15 +0000337 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 if ( tag == ASN1_UTC_TIME )
340 {
341 (*p)++;
342 ret = asn1_get_len( p, end, &len );
343
344 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000345 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000346
Paul Bakker91200182010-02-18 21:26:15 +0000347 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000348 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
349 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000350
Paul Bakker91200182010-02-18 21:26:15 +0000351 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
352 &time->year, &time->mon, &time->day,
353 &time->hour, &time->min, &time->sec ) < 5 )
354 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000355
Paul Bakker400ff6f2011-02-20 10:40:16 +0000356 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000357 time->year += 1900;
358
359 *p += len;
360
361 return( 0 );
362 }
363 else if ( tag == ASN1_GENERALIZED_TIME )
364 {
365 (*p)++;
366 ret = asn1_get_len( p, end, &len );
367
368 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000369 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000370
371 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000372 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
373 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000374
375 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
376 &time->year, &time->mon, &time->day,
377 &time->hour, &time->min, &time->sec ) < 5 )
378 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
379
380 *p += len;
381
382 return( 0 );
383 }
384 else
Paul Bakker9d781402011-05-09 16:17:09 +0000385 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000386}
387
388
389/*
390 * Validity ::= SEQUENCE {
391 * notBefore Time,
392 * notAfter Time }
393 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000394static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000395 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 x509_time *from,
397 x509_time *to )
398{
Paul Bakker23986e52011-04-24 08:57:21 +0000399 int ret;
400 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402 if( ( ret = asn1_get_tag( p, end, &len,
403 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000404 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 end = *p + len;
407
Paul Bakker91200182010-02-18 21:26:15 +0000408 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000409 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
Paul Bakker91200182010-02-18 21:26:15 +0000411 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000415 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000416 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418 return( 0 );
419}
420
421/*
422 * SubjectPublicKeyInfo ::= SEQUENCE {
423 * algorithm AlgorithmIdentifier,
424 * subjectPublicKey BIT STRING }
425 */
426static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000427 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 x509_buf *pk_alg_oid,
429 mpi *N, mpi *E )
430{
Paul Bakker23986e52011-04-24 08:57:21 +0000431 int ret, can_handle;
432 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 unsigned char *end2;
434
435 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
436 return( ret );
437
438 /*
439 * only RSA public keys handled at this time
440 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000441 can_handle = 0;
442
443 if( pk_alg_oid->len == 9 &&
444 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
445 can_handle = 1;
446
447 if( pk_alg_oid->len == 9 &&
448 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
449 {
450 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
451 can_handle = 1;
452
453 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
454 can_handle = 1;
455 }
456
457 if( pk_alg_oid->len == 5 &&
458 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
459 can_handle = 1;
460
461 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000462 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000465 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000468 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000469 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 end2 = *p + len;
472
473 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000474 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 /*
477 * RSAPublicKey ::= SEQUENCE {
478 * modulus INTEGER, -- n
479 * publicExponent INTEGER -- e
480 * }
481 */
482 if( ( ret = asn1_get_tag( p, end2, &len,
483 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
491 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000492 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000495 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000496 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 return( 0 );
499}
500
501static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000502 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 x509_buf *sig )
504{
Paul Bakker23986e52011-04-24 08:57:21 +0000505 int ret;
506 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakker8afa70d2012-02-11 18:42:45 +0000508 if( ( end - *p ) < 1 )
509 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
510 POLARSSL_ERR_ASN1_OUT_OF_DATA );
511
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 sig->tag = **p;
513
514 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000515 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker74111d32011-01-15 16:57:55 +0000517
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000519 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521 sig->len = len;
522 sig->p = *p;
523
524 *p += len;
525
526 return( 0 );
527}
528
529/*
530 * X.509 v2/v3 unique identifier (not parsed)
531 */
532static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000533 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 x509_buf *uid, int n )
535{
536 int ret;
537
538 if( *p == end )
539 return( 0 );
540
541 uid->tag = **p;
542
543 if( ( ret = asn1_get_tag( p, end, &uid->len,
544 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
545 {
Paul Bakker40e46942009-01-03 21:51:57 +0000546 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 return( 0 );
548
549 return( ret );
550 }
551
552 uid->p = *p;
553 *p += uid->len;
554
555 return( 0 );
556}
557
558/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000559 * X.509 Extensions (No parsing of extensions, pointer should
560 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
562static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000563 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000564 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565{
Paul Bakker23986e52011-04-24 08:57:21 +0000566 int ret;
567 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 if( *p == end )
570 return( 0 );
571
572 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000573
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000575 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 ext->p = *p;
579 end = *p + ext->len;
580
581 /*
582 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
583 *
584 * Extension ::= SEQUENCE {
585 * extnID OBJECT IDENTIFIER,
586 * critical BOOLEAN DEFAULT FALSE,
587 * extnValue OCTET STRING }
588 */
589 if( ( ret = asn1_get_tag( p, end, &len,
590 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000591 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000594 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000595 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Paul Bakkerd98030e2009-05-02 15:13:40 +0000597 return( 0 );
598}
599
600/*
601 * X.509 CRL v2 extensions (no extensions parsed yet.)
602 */
603static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000604 const unsigned char *end,
605 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000606{
Paul Bakker23986e52011-04-24 08:57:21 +0000607 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000608 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000609
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000610 /* Get explicit tag */
611 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000612 {
613 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
614 return( 0 );
615
616 return( ret );
617 }
618
619 while( *p < end )
620 {
621 if( ( ret = asn1_get_tag( p, end, &len,
622 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000623 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000624
625 *p += len;
626 }
627
628 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000629 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000630 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
631
632 return( 0 );
633}
634
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000635/*
636 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
637 */
638static int x509_get_crl_entry_ext( unsigned char **p,
639 const unsigned char *end,
640 x509_buf *ext )
641{
642 int ret;
643 size_t len = 0;
644
645 /* OPTIONAL */
646 if (end <= *p)
647 return( 0 );
648
649 ext->tag = **p;
650 ext->p = *p;
651
652 /*
653 * Get CRL-entry extension sequence header
654 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
655 */
656 if( ( ret = asn1_get_tag( p, end, &ext->len,
657 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
658 {
659 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
660 {
661 ext->p = NULL;
662 return( 0 );
663 }
664 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
665 }
666
667 end = *p + ext->len;
668
669 if( end != *p + ext->len )
670 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
671 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
672
673 while( *p < end )
674 {
675 if( ( ret = asn1_get_tag( p, end, &len,
676 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
677 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
678
679 *p += len;
680 }
681
682 if( *p != end )
683 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
684 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
685
686 return( 0 );
687}
688
Paul Bakker74111d32011-01-15 16:57:55 +0000689static int x509_get_basic_constraints( unsigned char **p,
690 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000691 int *ca_istrue,
692 int *max_pathlen )
693{
Paul Bakker23986e52011-04-24 08:57:21 +0000694 int ret;
695 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000696
697 /*
698 * BasicConstraints ::= SEQUENCE {
699 * cA BOOLEAN DEFAULT FALSE,
700 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
701 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000702 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000703 *max_pathlen = 0; /* endless */
704
705 if( ( ret = asn1_get_tag( p, end, &len,
706 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000707 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000708
709 if( *p == end )
710 return 0;
711
Paul Bakker3cccddb2011-01-16 21:46:31 +0000712 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000713 {
714 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000715 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000716
717 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000718 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000719
Paul Bakker3cccddb2011-01-16 21:46:31 +0000720 if( *ca_istrue != 0 )
721 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000722 }
723
724 if( *p == end )
725 return 0;
726
727 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000728 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000729
730 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000731 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000732 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
733
734 (*max_pathlen)++;
735
Paul Bakker74111d32011-01-15 16:57:55 +0000736 return 0;
737}
738
739static int x509_get_ns_cert_type( unsigned char **p,
740 const unsigned char *end,
741 unsigned char *ns_cert_type)
742{
743 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000744 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000745
746 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000747 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000748
749 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000750 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000751 POLARSSL_ERR_ASN1_INVALID_LENGTH );
752
753 /* Get actual bitstring */
754 *ns_cert_type = *bs.p;
755 return 0;
756}
757
758static int x509_get_key_usage( unsigned char **p,
759 const unsigned char *end,
760 unsigned char *key_usage)
761{
762 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000763 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000764
765 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000766 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000767
Paul Bakker94a67962012-08-23 13:03:52 +0000768 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000769 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000770 POLARSSL_ERR_ASN1_INVALID_LENGTH );
771
772 /* Get actual bitstring */
773 *key_usage = *bs.p;
774 return 0;
775}
776
Paul Bakkerd98030e2009-05-02 15:13:40 +0000777/*
Paul Bakker74111d32011-01-15 16:57:55 +0000778 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
779 *
780 * KeyPurposeId ::= OBJECT IDENTIFIER
781 */
782static int x509_get_ext_key_usage( unsigned char **p,
783 const unsigned char *end,
784 x509_sequence *ext_key_usage)
785{
786 int ret;
787
788 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000789 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000790
791 /* Sequence length must be >= 1 */
792 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000793 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000794 POLARSSL_ERR_ASN1_INVALID_LENGTH );
795
796 return 0;
797}
798
799/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000800 * SubjectAltName ::= GeneralNames
801 *
802 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
803 *
804 * GeneralName ::= CHOICE {
805 * otherName [0] OtherName,
806 * rfc822Name [1] IA5String,
807 * dNSName [2] IA5String,
808 * x400Address [3] ORAddress,
809 * directoryName [4] Name,
810 * ediPartyName [5] EDIPartyName,
811 * uniformResourceIdentifier [6] IA5String,
812 * iPAddress [7] OCTET STRING,
813 * registeredID [8] OBJECT IDENTIFIER }
814 *
815 * OtherName ::= SEQUENCE {
816 * type-id OBJECT IDENTIFIER,
817 * value [0] EXPLICIT ANY DEFINED BY type-id }
818 *
819 * EDIPartyName ::= SEQUENCE {
820 * nameAssigner [0] DirectoryString OPTIONAL,
821 * partyName [1] DirectoryString }
822 *
823 * NOTE: PolarSSL only parses and uses dNSName at this point.
824 */
825static int x509_get_subject_alt_name( unsigned char **p,
826 const unsigned char *end,
827 x509_sequence *subject_alt_name )
828{
829 int ret;
830 size_t len, tag_len;
831 asn1_buf *buf;
832 unsigned char tag;
833 asn1_sequence *cur = subject_alt_name;
834
835 /* Get main sequence tag */
836 if( ( ret = asn1_get_tag( p, end, &len,
837 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
838 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
839
840 if( *p + len != end )
841 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
842 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
843
844 while( *p < end )
845 {
846 if( ( end - *p ) < 1 )
847 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
848 POLARSSL_ERR_ASN1_OUT_OF_DATA );
849
850 tag = **p;
851 (*p)++;
852 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
853 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
854
855 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
856 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
857 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
858
859 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
860 {
861 *p += tag_len;
862 continue;
863 }
864
865 buf = &(cur->buf);
866 buf->tag = tag;
867 buf->p = *p;
868 buf->len = tag_len;
869 *p += buf->len;
870
871 /* Allocate and assign next pointer */
872 if (*p < end)
873 {
874 cur->next = (asn1_sequence *) malloc(
875 sizeof( asn1_sequence ) );
876
877 if( cur->next == NULL )
878 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
879 POLARSSL_ERR_ASN1_MALLOC_FAILED );
880
Paul Bakker535e97d2012-08-23 10:49:55 +0000881 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000882 cur = cur->next;
883 }
884 }
885
886 /* Set final sequence entry's next pointer to NULL */
887 cur->next = NULL;
888
889 if( *p != end )
890 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
891 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
892
893 return( 0 );
894}
895
896/*
Paul Bakker74111d32011-01-15 16:57:55 +0000897 * X.509 v3 extensions
898 *
899 * TODO: Perform all of the basic constraints tests required by the RFC
900 * TODO: Set values for undetected extensions to a sane default?
901 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000902 */
903static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000904 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000905 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000906{
Paul Bakker23986e52011-04-24 08:57:21 +0000907 int ret;
908 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000909 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000910
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000911 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000912 {
913 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
914 return( 0 );
915
916 return( ret );
917 }
918
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 while( *p < end )
920 {
Paul Bakker74111d32011-01-15 16:57:55 +0000921 /*
922 * Extension ::= SEQUENCE {
923 * extnID OBJECT IDENTIFIER,
924 * critical BOOLEAN DEFAULT FALSE,
925 * extnValue OCTET STRING }
926 */
927 x509_buf extn_oid = {0, 0, NULL};
928 int is_critical = 0; /* DEFAULT FALSE */
929
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 if( ( ret = asn1_get_tag( p, end, &len,
931 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000932 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000934 end_ext_data = *p + len;
935
Paul Bakker74111d32011-01-15 16:57:55 +0000936 /* Get extension ID */
937 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Paul Bakker74111d32011-01-15 16:57:55 +0000939 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000940 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Paul Bakker74111d32011-01-15 16:57:55 +0000942 extn_oid.p = *p;
943 *p += extn_oid.len;
944
945 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000946 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000947 POLARSSL_ERR_ASN1_OUT_OF_DATA );
948
949 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000950 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000951 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000952 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Paul Bakker74111d32011-01-15 16:57:55 +0000954 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000955 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000957 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000959 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000960
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000961 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000962 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000963 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Paul Bakker74111d32011-01-15 16:57:55 +0000965 /*
966 * Detect supported extensions
967 */
968 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
969 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 {
Paul Bakker74111d32011-01-15 16:57:55 +0000971 /* Parse basic constraints */
972 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000973 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000974 return ( ret );
975 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 }
Paul Bakker74111d32011-01-15 16:57:55 +0000977 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
978 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
979 {
980 /* Parse netscape certificate type */
981 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
982 &crt->ns_cert_type ) ) != 0 )
983 return ( ret );
984 crt->ext_types |= EXT_NS_CERT_TYPE;
985 }
986 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
987 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
988 {
989 /* Parse key usage */
990 if( ( ret = x509_get_key_usage( p, end_ext_octet,
991 &crt->key_usage ) ) != 0 )
992 return ( ret );
993 crt->ext_types |= EXT_KEY_USAGE;
994 }
995 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
996 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
997 {
998 /* Parse extended key usage */
999 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1000 &crt->ext_key_usage ) ) != 0 )
1001 return ( ret );
1002 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1003 }
Paul Bakkera8cd2392012-02-11 16:09:32 +00001004 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == extn_oid.len ) &&
1005 memcmp( extn_oid.p, OID_SUBJECT_ALT_NAME, extn_oid.len ) == 0 )
1006 {
1007 /* Parse extended key usage */
1008 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1009 &crt->subject_alt_names ) ) != 0 )
1010 return ( ret );
1011 crt->ext_types |= EXT_SUBJECT_ALT_NAME;
1012 }
Paul Bakker74111d32011-01-15 16:57:55 +00001013 else
1014 {
1015 /* No parser found, skip extension */
1016 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Paul Bakker5c721f92011-07-27 16:51:09 +00001018#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001019 if( is_critical )
1020 {
1021 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001022 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001023 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1024 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001025#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001026 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 }
1028
1029 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001030 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001031 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 return( 0 );
1034}
1035
1036/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001037 * X.509 CRL Entries
1038 */
1039static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001040 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001041 x509_crl_entry *entry )
1042{
Paul Bakker23986e52011-04-24 08:57:21 +00001043 int ret;
1044 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001045 x509_crl_entry *cur_entry = entry;
1046
1047 if( *p == end )
1048 return( 0 );
1049
Paul Bakker9be19372009-07-27 20:21:53 +00001050 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001051 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1052 {
1053 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1054 return( 0 );
1055
1056 return( ret );
1057 }
1058
Paul Bakker9be19372009-07-27 20:21:53 +00001059 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001060
1061 while( *p < end )
1062 {
Paul Bakker23986e52011-04-24 08:57:21 +00001063 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001064 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001065
1066 if( ( ret = asn1_get_tag( p, end, &len2,
1067 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1068 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001069 return( ret );
1070 }
1071
Paul Bakker9be19372009-07-27 20:21:53 +00001072 cur_entry->raw.tag = **p;
1073 cur_entry->raw.p = *p;
1074 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001075 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001076
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001077 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001078 return( ret );
1079
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001080 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001081 return( ret );
1082
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001083 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001084 return( ret );
1085
Paul Bakker74111d32011-01-15 16:57:55 +00001086 if ( *p < end )
1087 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001088 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001089
1090 if( cur_entry->next == NULL )
1091 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1092
Paul Bakkerd98030e2009-05-02 15:13:40 +00001093 cur_entry = cur_entry->next;
1094 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1095 }
1096 }
1097
1098 return( 0 );
1099}
1100
Paul Bakker27d66162010-03-17 06:56:01 +00001101static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1102{
1103 if( sig_oid->len == 9 &&
1104 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1105 {
1106 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1107 {
1108 *sig_alg = sig_oid->p[8];
1109 return( 0 );
1110 }
1111
1112 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1113 {
1114 *sig_alg = sig_oid->p[8];
1115 return( 0 );
1116 }
1117
1118 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1119 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001120 if( sig_oid->len == 5 &&
1121 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1122 {
1123 *sig_alg = SIG_RSA_SHA1;
1124 return( 0 );
1125 }
Paul Bakker27d66162010-03-17 06:56:01 +00001126
1127 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1128}
1129
Paul Bakkerd98030e2009-05-02 15:13:40 +00001130/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001131 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001133int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001134{
Paul Bakker23986e52011-04-24 08:57:21 +00001135 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001136 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001137 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Paul Bakker320a4b52009-03-28 18:52:39 +00001139 /*
1140 * Check for valid input
1141 */
1142 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001143 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001144
Paul Bakker96743fc2011-02-12 14:30:57 +00001145 p = (unsigned char *) malloc( len = buflen );
1146
1147 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001148 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001149
1150 memcpy( p, buf, buflen );
1151
1152 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
1154 crt->raw.p = p;
1155 crt->raw.len = len;
1156 end = p + len;
1157
1158 /*
1159 * Certificate ::= SEQUENCE {
1160 * tbsCertificate TBSCertificate,
1161 * signatureAlgorithm AlgorithmIdentifier,
1162 * signatureValue BIT STRING }
1163 */
1164 if( ( ret = asn1_get_tag( &p, end, &len,
1165 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1166 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001167 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001168 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 }
1170
Paul Bakkerb00ca422012-09-25 12:10:00 +00001171 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001173 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001174 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001175 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001177 crt_end = p + len;
1178
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 /*
1180 * TBSCertificate ::= SEQUENCE {
1181 */
1182 crt->tbs.p = p;
1183
1184 if( ( ret = asn1_get_tag( &p, end, &len,
1185 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1186 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001187 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001188 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 }
1190
1191 end = p + len;
1192 crt->tbs.len = end - crt->tbs.p;
1193
1194 /*
1195 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1196 *
1197 * CertificateSerialNumber ::= INTEGER
1198 *
1199 * signature AlgorithmIdentifier
1200 */
1201 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1202 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1203 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1204 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001205 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 return( ret );
1207 }
1208
1209 crt->version++;
1210
1211 if( crt->version > 3 )
1212 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001213 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001214 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 }
1216
Paul Bakker27d66162010-03-17 06:56:01 +00001217 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001219 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001220 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 }
1222
1223 /*
1224 * issuer Name
1225 */
1226 crt->issuer_raw.p = p;
1227
1228 if( ( ret = asn1_get_tag( &p, end, &len,
1229 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1230 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001231 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001232 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 }
1234
1235 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1236 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001237 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 return( ret );
1239 }
1240
1241 crt->issuer_raw.len = p - crt->issuer_raw.p;
1242
1243 /*
1244 * Validity ::= SEQUENCE {
1245 * notBefore Time,
1246 * notAfter Time }
1247 *
1248 */
1249 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1250 &crt->valid_to ) ) != 0 )
1251 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001252 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 return( ret );
1254 }
1255
1256 /*
1257 * subject Name
1258 */
1259 crt->subject_raw.p = p;
1260
1261 if( ( ret = asn1_get_tag( &p, end, &len,
1262 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1263 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001264 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001265 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 }
1267
Paul Bakkercefb3962012-06-27 11:51:09 +00001268 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001270 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 return( ret );
1272 }
1273
1274 crt->subject_raw.len = p - crt->subject_raw.p;
1275
1276 /*
1277 * SubjectPublicKeyInfo ::= SEQUENCE
1278 * algorithm AlgorithmIdentifier,
1279 * subjectPublicKey BIT STRING }
1280 */
1281 if( ( ret = asn1_get_tag( &p, end, &len,
1282 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1283 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001284 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001285 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 }
1287
1288 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1289 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 return( ret );
1293 }
1294
1295 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 return( ret );
1299 }
1300
1301 crt->rsa.len = mpi_size( &crt->rsa.N );
1302
1303 /*
1304 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1305 * -- If present, version shall be v2 or v3
1306 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1307 * -- If present, version shall be v2 or v3
1308 * extensions [3] EXPLICIT Extensions OPTIONAL
1309 * -- If present, version shall be v3
1310 */
1311 if( crt->version == 2 || crt->version == 3 )
1312 {
1313 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1314 if( ret != 0 )
1315 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001316 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 return( ret );
1318 }
1319 }
1320
1321 if( crt->version == 2 || crt->version == 3 )
1322 {
1323 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1324 if( ret != 0 )
1325 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001326 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 return( ret );
1328 }
1329 }
1330
1331 if( crt->version == 3 )
1332 {
Paul Bakker74111d32011-01-15 16:57:55 +00001333 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 if( ret != 0 )
1335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 return( ret );
1338 }
1339 }
1340
1341 if( p != end )
1342 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001343 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001344 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001345 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
Paul Bakkerb00ca422012-09-25 12:10:00 +00001348 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
1350 /*
1351 * signatureAlgorithm AlgorithmIdentifier,
1352 * signatureValue BIT STRING
1353 */
1354 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1355 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001356 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 return( ret );
1358 }
1359
Paul Bakker535e97d2012-08-23 10:49:55 +00001360 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1361 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001363 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001364 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 }
1366
1367 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1368 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001369 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 return( ret );
1371 }
1372
1373 if( p != end )
1374 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001375 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001376 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001377 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 }
1379
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001380 return( 0 );
1381}
1382
1383/*
1384 * Parse one or more PEM certificates from a buffer and add them to the chained list
1385 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001386int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001387{
Paul Bakker69e095c2011-12-10 21:55:01 +00001388 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001389 x509_cert *crt, *prev = NULL;
1390 int buf_format = X509_FORMAT_DER;
1391
1392 crt = chain;
1393
1394 /*
1395 * Check for valid input
1396 */
1397 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001398 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001399
1400 while( crt->version != 0 && crt->next != NULL )
1401 {
1402 prev = crt;
1403 crt = crt->next;
1404 }
1405
1406 /*
1407 * Add new certificate on the end of the chain if needed.
1408 */
1409 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001410 {
1411 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1412
Paul Bakker7d06ad22009-05-02 15:53:56 +00001413 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001414 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001415
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001416 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001417 crt = crt->next;
1418 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001421 /*
1422 * Determine buffer content. Buffer contains either one DER certificate or
1423 * one or more PEM certificates.
1424 */
1425#if defined(POLARSSL_PEM_C)
1426 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1427 buf_format = X509_FORMAT_PEM;
1428#endif
1429
1430 if( buf_format == X509_FORMAT_DER )
1431 return x509parse_crt_der( crt, buf, buflen );
1432
1433#if defined(POLARSSL_PEM_C)
1434 if( buf_format == X509_FORMAT_PEM )
1435 {
1436 pem_context pem;
1437
1438 while( buflen > 0 )
1439 {
1440 size_t use_len;
1441 pem_init( &pem );
1442
1443 ret = pem_read_buffer( &pem,
1444 "-----BEGIN CERTIFICATE-----",
1445 "-----END CERTIFICATE-----",
1446 buf, NULL, 0, &use_len );
1447
1448 if( ret == 0 )
1449 {
1450 /*
1451 * Was PEM encoded
1452 */
1453 buflen -= use_len;
1454 buf += use_len;
1455 }
1456 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1457 {
1458 pem_free( &pem );
1459
1460 if( first_error == 0 )
1461 first_error = ret;
1462
1463 continue;
1464 }
1465 else
1466 break;
1467
1468 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1469
1470 pem_free( &pem );
1471
1472 if( ret != 0 )
1473 {
1474 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001475 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001476 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001477 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001478 {
1479 if( prev )
1480 prev->next = NULL;
1481
1482 if( crt != chain )
1483 free( crt );
1484
1485 return( ret );
1486 }
1487
1488 if( first_error == 0 )
1489 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001490
1491 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001492
1493 memset( crt, 0, sizeof( x509_cert ) );
1494 continue;
1495 }
1496
1497 success = 1;
1498
1499 /*
1500 * Add new certificate to the list
1501 */
1502 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1503
1504 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001505 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001506
1507 prev = crt;
1508 crt = crt->next;
1509 memset( crt, 0, sizeof( x509_cert ) );
1510 }
1511 }
1512#endif
1513
1514 if( crt->version == 0 )
1515 {
1516 if( prev )
1517 prev->next = NULL;
1518
1519 if( crt != chain )
1520 free( crt );
1521 }
1522
1523 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001524 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001525 else if( first_error )
1526 return( first_error );
1527 else
1528 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529}
1530
1531/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001532 * Parse one or more CRLs and add them to the chained list
1533 */
Paul Bakker23986e52011-04-24 08:57:21 +00001534int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001535{
Paul Bakker23986e52011-04-24 08:57:21 +00001536 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001537 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001538 unsigned char *p, *end;
1539 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001540#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001541 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001542 pem_context pem;
1543#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001544
1545 crl = chain;
1546
1547 /*
1548 * Check for valid input
1549 */
1550 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001551 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001552
1553 while( crl->version != 0 && crl->next != NULL )
1554 crl = crl->next;
1555
1556 /*
1557 * Add new CRL on the end of the chain if needed.
1558 */
1559 if ( crl->version != 0 && crl->next == NULL)
1560 {
1561 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1562
Paul Bakker7d06ad22009-05-02 15:53:56 +00001563 if( crl->next == NULL )
1564 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001565 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001566 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001567 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001568
Paul Bakker7d06ad22009-05-02 15:53:56 +00001569 crl = crl->next;
1570 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001571 }
1572
Paul Bakker96743fc2011-02-12 14:30:57 +00001573#if defined(POLARSSL_PEM_C)
1574 pem_init( &pem );
1575 ret = pem_read_buffer( &pem,
1576 "-----BEGIN X509 CRL-----",
1577 "-----END X509 CRL-----",
1578 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001579
Paul Bakker96743fc2011-02-12 14:30:57 +00001580 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001581 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001582 /*
1583 * Was PEM encoded
1584 */
1585 buflen -= use_len;
1586 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587
1588 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001589 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001590 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001591 p = pem.buf;
1592 pem.buf = NULL;
1593 len = pem.buflen;
1594 pem_free( &pem );
1595 }
1596 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1597 {
1598 pem_free( &pem );
1599 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001600 }
1601 else
1602 {
1603 /*
1604 * nope, copy the raw DER data
1605 */
1606 p = (unsigned char *) malloc( len = buflen );
1607
1608 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001609 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001610
1611 memcpy( p, buf, buflen );
1612
1613 buflen = 0;
1614 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001615#else
1616 p = (unsigned char *) malloc( len = buflen );
1617
1618 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001619 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001620
1621 memcpy( p, buf, buflen );
1622
1623 buflen = 0;
1624#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001625
1626 crl->raw.p = p;
1627 crl->raw.len = len;
1628 end = p + len;
1629
1630 /*
1631 * CertificateList ::= SEQUENCE {
1632 * tbsCertList TBSCertList,
1633 * signatureAlgorithm AlgorithmIdentifier,
1634 * signatureValue BIT STRING }
1635 */
1636 if( ( ret = asn1_get_tag( &p, end, &len,
1637 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1638 {
1639 x509_crl_free( crl );
1640 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1641 }
1642
Paul Bakker23986e52011-04-24 08:57:21 +00001643 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001644 {
1645 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001646 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001647 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1648 }
1649
1650 /*
1651 * TBSCertList ::= SEQUENCE {
1652 */
1653 crl->tbs.p = p;
1654
1655 if( ( ret = asn1_get_tag( &p, end, &len,
1656 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1657 {
1658 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001659 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001660 }
1661
1662 end = p + len;
1663 crl->tbs.len = end - crl->tbs.p;
1664
1665 /*
1666 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1667 * -- if present, MUST be v2
1668 *
1669 * signature AlgorithmIdentifier
1670 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001671 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1673 {
1674 x509_crl_free( crl );
1675 return( ret );
1676 }
1677
1678 crl->version++;
1679
1680 if( crl->version > 2 )
1681 {
1682 x509_crl_free( crl );
1683 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1684 }
1685
Paul Bakker27d66162010-03-17 06:56:01 +00001686 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687 {
1688 x509_crl_free( crl );
1689 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1690 }
1691
1692 /*
1693 * issuer Name
1694 */
1695 crl->issuer_raw.p = p;
1696
1697 if( ( ret = asn1_get_tag( &p, end, &len,
1698 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1699 {
1700 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001701 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001702 }
1703
1704 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1705 {
1706 x509_crl_free( crl );
1707 return( ret );
1708 }
1709
1710 crl->issuer_raw.len = p - crl->issuer_raw.p;
1711
1712 /*
1713 * thisUpdate Time
1714 * nextUpdate Time OPTIONAL
1715 */
Paul Bakker91200182010-02-18 21:26:15 +00001716 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001717 {
1718 x509_crl_free( crl );
1719 return( ret );
1720 }
1721
Paul Bakker91200182010-02-18 21:26:15 +00001722 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001723 {
Paul Bakker9d781402011-05-09 16:17:09 +00001724 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001725 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001726 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001727 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001728 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001729 x509_crl_free( crl );
1730 return( ret );
1731 }
1732 }
1733
1734 /*
1735 * revokedCertificates SEQUENCE OF SEQUENCE {
1736 * userCertificate CertificateSerialNumber,
1737 * revocationDate Time,
1738 * crlEntryExtensions Extensions OPTIONAL
1739 * -- if present, MUST be v2
1740 * } OPTIONAL
1741 */
1742 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1743 {
1744 x509_crl_free( crl );
1745 return( ret );
1746 }
1747
1748 /*
1749 * crlExtensions EXPLICIT Extensions OPTIONAL
1750 * -- if present, MUST be v2
1751 */
1752 if( crl->version == 2 )
1753 {
1754 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1755
1756 if( ret != 0 )
1757 {
1758 x509_crl_free( crl );
1759 return( ret );
1760 }
1761 }
1762
1763 if( p != end )
1764 {
1765 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001766 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001767 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1768 }
1769
1770 end = crl->raw.p + crl->raw.len;
1771
1772 /*
1773 * signatureAlgorithm AlgorithmIdentifier,
1774 * signatureValue BIT STRING
1775 */
1776 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1777 {
1778 x509_crl_free( crl );
1779 return( ret );
1780 }
1781
Paul Bakker535e97d2012-08-23 10:49:55 +00001782 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1783 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001784 {
1785 x509_crl_free( crl );
1786 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1787 }
1788
1789 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1790 {
1791 x509_crl_free( crl );
1792 return( ret );
1793 }
1794
1795 if( p != end )
1796 {
1797 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001798 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1800 }
1801
1802 if( buflen > 0 )
1803 {
1804 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1805
Paul Bakker7d06ad22009-05-02 15:53:56 +00001806 if( crl->next == NULL )
1807 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001808 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001809 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001810 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001811
Paul Bakker7d06ad22009-05-02 15:53:56 +00001812 crl = crl->next;
1813 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814
1815 return( x509parse_crl( crl, buf, buflen ) );
1816 }
1817
1818 return( 0 );
1819}
1820
Paul Bakker335db3f2011-04-25 15:28:35 +00001821#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001822/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001823 * Load all data from a file into a given buffer.
1824 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001825int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001826{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001827 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001828
Paul Bakkerd98030e2009-05-02 15:13:40 +00001829 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001830 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001831
Paul Bakkerd98030e2009-05-02 15:13:40 +00001832 fseek( f, 0, SEEK_END );
1833 *n = (size_t) ftell( f );
1834 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001835
Paul Bakkerd98030e2009-05-02 15:13:40 +00001836 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001837 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001838
Paul Bakkerd98030e2009-05-02 15:13:40 +00001839 if( fread( *buf, 1, *n, f ) != *n )
1840 {
1841 fclose( f );
1842 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001843 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001844 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001845
Paul Bakkerd98030e2009-05-02 15:13:40 +00001846 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001847
Paul Bakkerd98030e2009-05-02 15:13:40 +00001848 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001849
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001851}
1852
1853/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 * Load one or more certificates and add them to the chained list
1855 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001856int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001857{
1858 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 size_t n;
1860 unsigned char *buf;
1861
Paul Bakker69e095c2011-12-10 21:55:01 +00001862 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1863 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
Paul Bakker69e095c2011-12-10 21:55:01 +00001865 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001866
1867 memset( buf, 0, n + 1 );
1868 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
1870 return( ret );
1871}
1872
Paul Bakker8d914582012-06-04 12:46:42 +00001873int x509parse_crtpath( x509_cert *chain, const char *path )
1874{
1875 int ret = 0;
1876#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001877 int w_ret;
1878 WCHAR szDir[MAX_PATH];
1879 char filename[MAX_PATH];
1880 char *p;
1881
1882 WIN32_FIND_DATA file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001883 HANDLE hFind;
1884 DWORD dwError = 0;
1885
Paul Bakker3338b792012-10-01 21:13:10 +00001886 memset( szDir, 0, sizeof(szDir) );
1887 memset( filename, 0, MAX_PATH );
1888 memcpy( filename, path, strlen( path ) );
1889 filename[strlen( path )] = '\\';
1890 p = filename + strlen( path ) + 1;
1891
1892 w_ret = MultiByteToWideChar( CP_ACP, 0, path, strlen(path), szDir, MAX_PATH - 3 );
1893
1894 StringCchCopyW(szDir, MAX_PATH, szDir);
1895 StringCchCatW(szDir, MAX_PATH, TEXT("\\*"));
Paul Bakker8d914582012-06-04 12:46:42 +00001896
1897 hFind = FindFirstFile( szDir, &file_data );
1898 if (hFind == INVALID_HANDLE_VALUE)
1899 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1900
1901 do
1902 {
Paul Bakker3338b792012-10-01 21:13:10 +00001903 memset( p, 0, filename + MAX_PATH - p - 1 );
1904
Paul Bakkere4791f32012-06-04 21:29:15 +00001905 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001906 continue;
1907
Paul Bakker3338b792012-10-01 21:13:10 +00001908 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1909 lstrlenW(file_data.cFileName),
1910 p,
1911 filename + MAX_PATH - p - 2, NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001912
Paul Bakker3338b792012-10-01 21:13:10 +00001913 w_ret = x509parse_crtfile( chain, filename );
1914 if( w_ret < 0 )
1915 return( w_ret );
1916
1917 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001918 }
1919 while( FindNextFile( hFind, &file_data ) != 0 );
1920
1921 dwError = GetLastError();
1922 if (dwError != ERROR_NO_MORE_FILES)
1923 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1924
1925 FindClose( hFind );
1926#else
1927 int t_ret;
1928 struct dirent *entry;
1929 char entry_name[255];
1930 DIR *dir = opendir( path );
1931
1932 if( dir == NULL)
1933 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1934
1935 while( ( entry = readdir( dir ) ) != NULL )
1936 {
1937 if( entry->d_type != DT_REG )
1938 continue;
1939
1940 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1941 t_ret = x509parse_crtfile( chain, entry_name );
1942 if( t_ret < 0 )
1943 return( t_ret );
1944
1945 ret += t_ret;
1946 }
1947 closedir( dir );
1948#endif
1949
1950 return( ret );
1951}
1952
Paul Bakkerd98030e2009-05-02 15:13:40 +00001953/*
1954 * Load one or more CRLs and add them to the chained list
1955 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001956int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001957{
1958 int ret;
1959 size_t n;
1960 unsigned char *buf;
1961
Paul Bakker69e095c2011-12-10 21:55:01 +00001962 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1963 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001964
Paul Bakker27fdf462011-06-09 13:55:13 +00001965 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001966
1967 memset( buf, 0, n + 1 );
1968 free( buf );
1969
1970 return( ret );
1971}
1972
Paul Bakker5121ce52009-01-03 21:22:43 +00001973/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001974 * Load and parse a private RSA key
1975 */
1976int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1977{
1978 int ret;
1979 size_t n;
1980 unsigned char *buf;
1981
Paul Bakker69e095c2011-12-10 21:55:01 +00001982 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1983 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001984
1985 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001986 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001987 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001988 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001989 (unsigned char *) pwd, strlen( pwd ) );
1990
1991 memset( buf, 0, n + 1 );
1992 free( buf );
1993
1994 return( ret );
1995}
1996
1997/*
1998 * Load and parse a public RSA key
1999 */
2000int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2001{
2002 int ret;
2003 size_t n;
2004 unsigned char *buf;
2005
Paul Bakker69e095c2011-12-10 21:55:01 +00002006 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2007 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002008
Paul Bakker27fdf462011-06-09 13:55:13 +00002009 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002010
2011 memset( buf, 0, n + 1 );
2012 free( buf );
2013
2014 return( ret );
2015}
2016#endif /* POLARSSL_FS_IO */
2017
2018/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 * Parse a private RSA key
2020 */
Paul Bakker23986e52011-04-24 08:57:21 +00002021int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2022 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002023{
Paul Bakker23986e52011-04-24 08:57:21 +00002024 int ret;
2025 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002027 unsigned char *p_alt;
2028 x509_buf pk_alg_oid;
2029
Paul Bakker96743fc2011-02-12 14:30:57 +00002030#if defined(POLARSSL_PEM_C)
2031 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00002032
Paul Bakker96743fc2011-02-12 14:30:57 +00002033 pem_init( &pem );
2034 ret = pem_read_buffer( &pem,
2035 "-----BEGIN RSA PRIVATE KEY-----",
2036 "-----END RSA PRIVATE KEY-----",
2037 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
Paul Bakkered56b222011-07-13 11:26:43 +00002039 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2040 {
2041 ret = pem_read_buffer( &pem,
2042 "-----BEGIN PRIVATE KEY-----",
2043 "-----END PRIVATE KEY-----",
2044 key, pwd, pwdlen, &len );
2045 }
2046
Paul Bakker96743fc2011-02-12 14:30:57 +00002047 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002049 /*
2050 * Was PEM encoded
2051 */
2052 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002054 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002055 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002056 pem_free( &pem );
2057 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
Paul Bakker96743fc2011-02-12 14:30:57 +00002060 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2061#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002062 ((void) pwd);
2063 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002064 p = (unsigned char *) key;
2065#endif
2066 end = p + keylen;
2067
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002069 * Note: Depending on the type of private key file one can expect either a
2070 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2071 *
2072 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002073 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002074 * algorithm AlgorithmIdentifier,
2075 * PrivateKey BIT STRING
2076 * }
2077 *
2078 * AlgorithmIdentifier ::= SEQUENCE {
2079 * algorithm OBJECT IDENTIFIER,
2080 * parameters ANY DEFINED BY algorithm OPTIONAL
2081 * }
2082 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 * RSAPrivateKey ::= SEQUENCE {
2084 * version Version,
2085 * modulus INTEGER, -- n
2086 * publicExponent INTEGER, -- e
2087 * privateExponent INTEGER, -- d
2088 * prime1 INTEGER, -- p
2089 * prime2 INTEGER, -- q
2090 * exponent1 INTEGER, -- d mod (p-1)
2091 * exponent2 INTEGER, -- d mod (q-1)
2092 * coefficient INTEGER, -- (inverse of q) mod p
2093 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2094 * }
2095 */
2096 if( ( ret = asn1_get_tag( &p, end, &len,
2097 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2098 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002099#if defined(POLARSSL_PEM_C)
2100 pem_free( &pem );
2101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002103 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
2105
2106 end = p + len;
2107
2108 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2109 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002110#if defined(POLARSSL_PEM_C)
2111 pem_free( &pem );
2112#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002114 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 }
2116
2117 if( rsa->ver != 0 )
2118 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002119#if defined(POLARSSL_PEM_C)
2120 pem_free( &pem );
2121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002123 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
2125
Paul Bakkered56b222011-07-13 11:26:43 +00002126 p_alt = p;
2127
2128 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2129 {
2130 // Assume that we have the PKCS#1 format if wrong
2131 // tag was encountered
2132 //
2133 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2134 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2135 {
2136#if defined(POLARSSL_PEM_C)
2137 pem_free( &pem );
2138#endif
2139 rsa_free( rsa );
2140 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2141 }
2142 }
2143 else
2144 {
2145 int can_handle;
2146
2147 /*
2148 * only RSA keys handled at this time
2149 */
2150 can_handle = 0;
2151
2152 if( pk_alg_oid.len == 9 &&
2153 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2154 can_handle = 1;
2155
2156 if( pk_alg_oid.len == 9 &&
2157 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2158 {
2159 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2160 can_handle = 1;
2161
2162 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2163 can_handle = 1;
2164 }
2165
2166 if( pk_alg_oid.len == 5 &&
2167 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2168 can_handle = 1;
2169
2170 if( can_handle == 0 )
2171 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2172
2173 /*
2174 * Parse the PKCS#8 format
2175 */
2176
2177 p = p_alt;
2178 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2179 {
2180#if defined(POLARSSL_PEM_C)
2181 pem_free( &pem );
2182#endif
2183 rsa_free( rsa );
2184 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2185 }
2186
2187 if( ( end - p ) < 1 )
2188 {
2189#if defined(POLARSSL_PEM_C)
2190 pem_free( &pem );
2191#endif
2192 rsa_free( rsa );
2193 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2194 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2195 }
2196
2197 end = p + len;
2198
2199 if( ( ret = asn1_get_tag( &p, end, &len,
2200 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2201 {
2202#if defined(POLARSSL_PEM_C)
2203 pem_free( &pem );
2204#endif
2205 rsa_free( rsa );
2206 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2207 }
2208
2209 end = p + len;
2210
2211 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2212 {
2213#if defined(POLARSSL_PEM_C)
2214 pem_free( &pem );
2215#endif
2216 rsa_free( rsa );
2217 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2218 }
2219
2220 if( rsa->ver != 0 )
2221 {
2222#if defined(POLARSSL_PEM_C)
2223 pem_free( &pem );
2224#endif
2225 rsa_free( rsa );
2226 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2227 }
2228 }
2229
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2231 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2232 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2233 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2234 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2235 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2236 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2237 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2238 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002239#if defined(POLARSSL_PEM_C)
2240 pem_free( &pem );
2241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002243 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 }
2245
2246 rsa->len = mpi_size( &rsa->N );
2247
2248 if( p != end )
2249 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002250#if defined(POLARSSL_PEM_C)
2251 pem_free( &pem );
2252#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002254 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002255 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 }
2257
2258 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2259 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002260#if defined(POLARSSL_PEM_C)
2261 pem_free( &pem );
2262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 rsa_free( rsa );
2264 return( ret );
2265 }
2266
Paul Bakker96743fc2011-02-12 14:30:57 +00002267#if defined(POLARSSL_PEM_C)
2268 pem_free( &pem );
2269#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
2271 return( 0 );
2272}
2273
2274/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002275 * Parse a public RSA key
2276 */
Paul Bakker23986e52011-04-24 08:57:21 +00002277int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002278{
Paul Bakker23986e52011-04-24 08:57:21 +00002279 int ret;
2280 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002281 unsigned char *p, *end;
2282 x509_buf alg_oid;
2283#if defined(POLARSSL_PEM_C)
2284 pem_context pem;
2285
2286 pem_init( &pem );
2287 ret = pem_read_buffer( &pem,
2288 "-----BEGIN PUBLIC KEY-----",
2289 "-----END PUBLIC KEY-----",
2290 key, NULL, 0, &len );
2291
2292 if( ret == 0 )
2293 {
2294 /*
2295 * Was PEM encoded
2296 */
2297 keylen = pem.buflen;
2298 }
2299 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2300 {
2301 pem_free( &pem );
2302 return( ret );
2303 }
2304
2305 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2306#else
2307 p = (unsigned char *) key;
2308#endif
2309 end = p + keylen;
2310
2311 /*
2312 * PublicKeyInfo ::= SEQUENCE {
2313 * algorithm AlgorithmIdentifier,
2314 * PublicKey BIT STRING
2315 * }
2316 *
2317 * AlgorithmIdentifier ::= SEQUENCE {
2318 * algorithm OBJECT IDENTIFIER,
2319 * parameters ANY DEFINED BY algorithm OPTIONAL
2320 * }
2321 *
2322 * RSAPublicKey ::= SEQUENCE {
2323 * modulus INTEGER, -- n
2324 * publicExponent INTEGER -- e
2325 * }
2326 */
2327
2328 if( ( ret = asn1_get_tag( &p, end, &len,
2329 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2330 {
2331#if defined(POLARSSL_PEM_C)
2332 pem_free( &pem );
2333#endif
2334 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002335 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002336 }
2337
2338 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2339 {
2340#if defined(POLARSSL_PEM_C)
2341 pem_free( &pem );
2342#endif
2343 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002344 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002345 }
2346
2347 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2348 {
2349#if defined(POLARSSL_PEM_C)
2350 pem_free( &pem );
2351#endif
2352 rsa_free( rsa );
2353 return( ret );
2354 }
2355
2356 rsa->len = mpi_size( &rsa->N );
2357
2358#if defined(POLARSSL_PEM_C)
2359 pem_free( &pem );
2360#endif
2361
2362 return( 0 );
2363}
2364
Paul Bakkereaa89f82011-04-04 21:36:15 +00002365#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002366/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002367 * Parse DHM parameters
2368 */
Paul Bakker23986e52011-04-24 08:57:21 +00002369int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002370{
Paul Bakker23986e52011-04-24 08:57:21 +00002371 int ret;
2372 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002373 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002374#if defined(POLARSSL_PEM_C)
2375 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002376
Paul Bakker96743fc2011-02-12 14:30:57 +00002377 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002378
Paul Bakker96743fc2011-02-12 14:30:57 +00002379 ret = pem_read_buffer( &pem,
2380 "-----BEGIN DH PARAMETERS-----",
2381 "-----END DH PARAMETERS-----",
2382 dhmin, NULL, 0, &dhminlen );
2383
2384 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002385 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002386 /*
2387 * Was PEM encoded
2388 */
2389 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002390 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002391 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002392 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002393 pem_free( &pem );
2394 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002395 }
2396
Paul Bakker96743fc2011-02-12 14:30:57 +00002397 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2398#else
2399 p = (unsigned char *) dhmin;
2400#endif
2401 end = p + dhminlen;
2402
Paul Bakker1b57b062011-01-06 15:48:19 +00002403 memset( dhm, 0, sizeof( dhm_context ) );
2404
Paul Bakker1b57b062011-01-06 15:48:19 +00002405 /*
2406 * DHParams ::= SEQUENCE {
2407 * prime INTEGER, -- P
2408 * generator INTEGER, -- g
2409 * }
2410 */
2411 if( ( ret = asn1_get_tag( &p, end, &len,
2412 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2413 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002414#if defined(POLARSSL_PEM_C)
2415 pem_free( &pem );
2416#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002417 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002418 }
2419
2420 end = p + len;
2421
2422 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2423 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2424 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002425#if defined(POLARSSL_PEM_C)
2426 pem_free( &pem );
2427#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002428 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002429 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002430 }
2431
2432 if( p != end )
2433 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002434#if defined(POLARSSL_PEM_C)
2435 pem_free( &pem );
2436#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002437 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002438 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002439 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2440 }
2441
Paul Bakker96743fc2011-02-12 14:30:57 +00002442#if defined(POLARSSL_PEM_C)
2443 pem_free( &pem );
2444#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002445
2446 return( 0 );
2447}
2448
Paul Bakker335db3f2011-04-25 15:28:35 +00002449#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002450/*
2451 * Load and parse a private RSA key
2452 */
2453int x509parse_dhmfile( dhm_context *dhm, const char *path )
2454{
2455 int ret;
2456 size_t n;
2457 unsigned char *buf;
2458
Paul Bakker69e095c2011-12-10 21:55:01 +00002459 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2460 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002461
Paul Bakker27fdf462011-06-09 13:55:13 +00002462 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002463
2464 memset( buf, 0, n + 1 );
2465 free( buf );
2466
2467 return( ret );
2468}
Paul Bakker335db3f2011-04-25 15:28:35 +00002469#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002470#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002471
Paul Bakker5121ce52009-01-03 21:22:43 +00002472#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002473#include <stdarg.h>
2474
2475#if !defined vsnprintf
2476#define vsnprintf _vsnprintf
2477#endif // vsnprintf
2478
2479/*
2480 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2481 * Result value is not size of buffer needed, but -1 if no fit is possible.
2482 *
2483 * This fuction tries to 'fix' this by at least suggesting enlarging the
2484 * size by 20.
2485 */
2486int compat_snprintf(char *str, size_t size, const char *format, ...)
2487{
2488 va_list ap;
2489 int res = -1;
2490
2491 va_start( ap, format );
2492
2493 res = vsnprintf( str, size, format, ap );
2494
2495 va_end( ap );
2496
2497 // No quick fix possible
2498 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002499 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002500
2501 return res;
2502}
2503
2504#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002505#endif
2506
Paul Bakkerd98030e2009-05-02 15:13:40 +00002507#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2508
2509#define SAFE_SNPRINTF() \
2510{ \
2511 if( ret == -1 ) \
2512 return( -1 ); \
2513 \
Paul Bakker23986e52011-04-24 08:57:21 +00002514 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002515 p[n - 1] = '\0'; \
2516 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2517 } \
2518 \
Paul Bakker23986e52011-04-24 08:57:21 +00002519 n -= (unsigned int) ret; \
2520 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002521}
2522
Paul Bakker5121ce52009-01-03 21:22:43 +00002523/*
2524 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002525 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002527int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002528{
Paul Bakker23986e52011-04-24 08:57:21 +00002529 int ret;
2530 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002532 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 char s[128], *p;
2534
2535 memset( s, 0, sizeof( s ) );
2536
2537 name = dn;
2538 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002539 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
2541 while( name != NULL )
2542 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002543 if( !name->oid.p )
2544 {
2545 name = name->next;
2546 continue;
2547 }
2548
Paul Bakker74111d32011-01-15 16:57:55 +00002549 if( name != dn )
2550 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002551 ret = snprintf( p, n, ", " );
2552 SAFE_SNPRINTF();
2553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Paul Bakker535e97d2012-08-23 10:49:55 +00002555 if( name->oid.len == 3 &&
2556 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 {
2558 switch( name->oid.p[2] )
2559 {
2560 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002561 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002564 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
2566 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002567 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
2569 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002570 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
2572 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002573 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
2575 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002576 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
2578 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002579 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 name->oid.p[2] );
2581 break;
2582 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002583 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002585 else if( name->oid.len == 9 &&
2586 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 {
2588 switch( name->oid.p[8] )
2589 {
2590 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002591 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
2593 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002594 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 name->oid.p[8] );
2596 break;
2597 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002598 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600 else
Paul Bakker74111d32011-01-15 16:57:55 +00002601 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002602 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002603 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
2606 for( i = 0; i < name->val.len; i++ )
2607 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002608 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 break;
2610
2611 c = name->val.p[i];
2612 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2613 s[i] = '?';
2614 else s[i] = c;
2615 }
2616 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002617 ret = snprintf( p, n, "%s", s );
2618 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 name = name->next;
2620 }
2621
Paul Bakker23986e52011-04-24 08:57:21 +00002622 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623}
2624
2625/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002626 * Store the serial in printable form into buf; no more
2627 * than size characters will be written
2628 */
2629int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2630{
Paul Bakker23986e52011-04-24 08:57:21 +00002631 int ret;
2632 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002633 char *p;
2634
2635 p = buf;
2636 n = size;
2637
2638 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002639 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002640
2641 for( i = 0; i < nr; i++ )
2642 {
Paul Bakker93048802011-12-05 14:38:06 +00002643 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002644 continue;
2645
Paul Bakkerdd476992011-01-16 21:34:59 +00002646 ret = snprintf( p, n, "%02X%s",
2647 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2648 SAFE_SNPRINTF();
2649 }
2650
Paul Bakker03c7c252011-11-25 12:37:37 +00002651 if( nr != serial->len )
2652 {
2653 ret = snprintf( p, n, "...." );
2654 SAFE_SNPRINTF();
2655 }
2656
Paul Bakker23986e52011-04-24 08:57:21 +00002657 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002658}
2659
2660/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002661 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002663int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2664 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002665{
Paul Bakker23986e52011-04-24 08:57:21 +00002666 int ret;
2667 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002668 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
2670 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002671 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Paul Bakkerd98030e2009-05-02 15:13:40 +00002673 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002675 SAFE_SNPRINTF();
2676 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002678 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Paul Bakkerdd476992011-01-16 21:34:59 +00002680 ret = x509parse_serial_gets( p, n, &crt->serial);
2681 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Paul Bakkerd98030e2009-05-02 15:13:40 +00002683 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2684 SAFE_SNPRINTF();
2685 ret = x509parse_dn_gets( p, n, &crt->issuer );
2686 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Paul Bakkerd98030e2009-05-02 15:13:40 +00002688 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2689 SAFE_SNPRINTF();
2690 ret = x509parse_dn_gets( p, n, &crt->subject );
2691 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Paul Bakkerd98030e2009-05-02 15:13:40 +00002693 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2695 crt->valid_from.year, crt->valid_from.mon,
2696 crt->valid_from.day, crt->valid_from.hour,
2697 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002698 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Paul Bakkerd98030e2009-05-02 15:13:40 +00002700 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2702 crt->valid_to.year, crt->valid_to.mon,
2703 crt->valid_to.day, crt->valid_to.hour,
2704 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002705 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Paul Bakkerd98030e2009-05-02 15:13:40 +00002707 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2708 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Paul Bakker27d66162010-03-17 06:56:01 +00002710 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002712 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2713 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2714 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2715 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2716 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2717 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2718 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2719 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2720 default: ret = snprintf( p, n, "???" ); break;
2721 }
2722 SAFE_SNPRINTF();
2723
2724 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002725 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002726 SAFE_SNPRINTF();
2727
Paul Bakker23986e52011-04-24 08:57:21 +00002728 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002729}
2730
Paul Bakker74111d32011-01-15 16:57:55 +00002731/* Compare a given OID string with an OID x509_buf * */
2732#define OID_CMP(oid_str, oid_buf) \
2733 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2734 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2735
2736/*
2737 * Return an informational string describing the given OID
2738 */
2739const char *x509_oid_get_description( x509_buf *oid )
2740{
2741 if ( oid == NULL )
2742 return ( NULL );
2743
2744 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2745 return( STRING_SERVER_AUTH );
2746
2747 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2748 return( STRING_CLIENT_AUTH );
2749
2750 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2751 return( STRING_CODE_SIGNING );
2752
2753 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2754 return( STRING_EMAIL_PROTECTION );
2755
2756 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2757 return( STRING_TIME_STAMPING );
2758
2759 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2760 return( STRING_OCSP_SIGNING );
2761
2762 return( NULL );
2763}
2764
2765/* Return the x.y.z.... style numeric string for the given OID */
2766int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2767{
Paul Bakker23986e52011-04-24 08:57:21 +00002768 int ret;
2769 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002770 unsigned int value;
2771 char *p;
2772
2773 p = buf;
2774 n = size;
2775
2776 /* First byte contains first two dots */
2777 if( oid->len > 0 )
2778 {
2779 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2780 SAFE_SNPRINTF();
2781 }
2782
2783 /* TODO: value can overflow in value. */
2784 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002785 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002786 {
2787 value <<= 7;
2788 value += oid->p[i] & 0x7F;
2789
2790 if( !( oid->p[i] & 0x80 ) )
2791 {
2792 /* Last byte */
2793 ret = snprintf( p, n, ".%d", value );
2794 SAFE_SNPRINTF();
2795 value = 0;
2796 }
2797 }
2798
Paul Bakker23986e52011-04-24 08:57:21 +00002799 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002800}
2801
Paul Bakkerd98030e2009-05-02 15:13:40 +00002802/*
2803 * Return an informational string about the CRL.
2804 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002805int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2806 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002807{
Paul Bakker23986e52011-04-24 08:57:21 +00002808 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002809 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002810 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002811 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002812
2813 p = buf;
2814 n = size;
2815
2816 ret = snprintf( p, n, "%sCRL version : %d",
2817 prefix, crl->version );
2818 SAFE_SNPRINTF();
2819
2820 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2821 SAFE_SNPRINTF();
2822 ret = x509parse_dn_gets( p, n, &crl->issuer );
2823 SAFE_SNPRINTF();
2824
2825 ret = snprintf( p, n, "\n%sthis update : " \
2826 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2827 crl->this_update.year, crl->this_update.mon,
2828 crl->this_update.day, crl->this_update.hour,
2829 crl->this_update.min, crl->this_update.sec );
2830 SAFE_SNPRINTF();
2831
2832 ret = snprintf( p, n, "\n%snext update : " \
2833 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2834 crl->next_update.year, crl->next_update.mon,
2835 crl->next_update.day, crl->next_update.hour,
2836 crl->next_update.min, crl->next_update.sec );
2837 SAFE_SNPRINTF();
2838
2839 entry = &crl->entry;
2840
2841 ret = snprintf( p, n, "\n%sRevoked certificates:",
2842 prefix );
2843 SAFE_SNPRINTF();
2844
Paul Bakker9be19372009-07-27 20:21:53 +00002845 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002846 {
2847 ret = snprintf( p, n, "\n%sserial number: ",
2848 prefix );
2849 SAFE_SNPRINTF();
2850
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002851 ret = x509parse_serial_gets( p, n, &entry->serial);
2852 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002853
Paul Bakkerd98030e2009-05-02 15:13:40 +00002854 ret = snprintf( p, n, " revocation date: " \
2855 "%04d-%02d-%02d %02d:%02d:%02d",
2856 entry->revocation_date.year, entry->revocation_date.mon,
2857 entry->revocation_date.day, entry->revocation_date.hour,
2858 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002859 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002860
2861 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 }
2863
Paul Bakkerd98030e2009-05-02 15:13:40 +00002864 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2865 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Paul Bakker27d66162010-03-17 06:56:01 +00002867 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002868 {
2869 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2870 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2871 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2872 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2873 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2874 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2875 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2876 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2877 default: ret = snprintf( p, n, "???" ); break;
2878 }
2879 SAFE_SNPRINTF();
2880
Paul Bakker1e27bb22009-07-19 20:25:25 +00002881 ret = snprintf( p, n, "\n" );
2882 SAFE_SNPRINTF();
2883
Paul Bakker23986e52011-04-24 08:57:21 +00002884 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002885}
2886
2887/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002888 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002890int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002891{
Paul Bakkercce9d772011-11-18 14:26:47 +00002892 int year, mon, day;
2893 int hour, min, sec;
2894
2895#if defined(_WIN32)
2896 SYSTEMTIME st;
2897
2898 GetLocalTime(&st);
2899
2900 year = st.wYear;
2901 mon = st.wMonth;
2902 day = st.wDay;
2903 hour = st.wHour;
2904 min = st.wMinute;
2905 sec = st.wSecond;
2906#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 struct tm *lt;
2908 time_t tt;
2909
2910 tt = time( NULL );
2911 lt = localtime( &tt );
2912
Paul Bakkercce9d772011-11-18 14:26:47 +00002913 year = lt->tm_year + 1900;
2914 mon = lt->tm_mon + 1;
2915 day = lt->tm_mday;
2916 hour = lt->tm_hour;
2917 min = lt->tm_min;
2918 sec = lt->tm_sec;
2919#endif
2920
2921 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002922 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Paul Bakkercce9d772011-11-18 14:26:47 +00002924 if( year == to->year &&
2925 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002926 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002927
Paul Bakkercce9d772011-11-18 14:26:47 +00002928 if( year == to->year &&
2929 mon == to->mon &&
2930 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002931 return( 1 );
2932
Paul Bakkercce9d772011-11-18 14:26:47 +00002933 if( year == to->year &&
2934 mon == to->mon &&
2935 day == to->day &&
2936 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002937 return( 1 );
2938
Paul Bakkercce9d772011-11-18 14:26:47 +00002939 if( year == to->year &&
2940 mon == to->mon &&
2941 day == to->day &&
2942 hour == to->hour &&
2943 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002944 return( 1 );
2945
Paul Bakkercce9d772011-11-18 14:26:47 +00002946 if( year == to->year &&
2947 mon == to->mon &&
2948 day == to->day &&
2949 hour == to->hour &&
2950 min == to->min &&
2951 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002952 return( 1 );
2953
Paul Bakker40ea7de2009-05-03 10:18:48 +00002954 return( 0 );
2955}
2956
2957/*
2958 * Return 1 if the certificate is revoked, or 0 otherwise.
2959 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002960int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002961{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002962 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002963
2964 while( cur != NULL && cur->serial.len != 0 )
2965 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002966 if( crt->serial.len == cur->serial.len &&
2967 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002968 {
2969 if( x509parse_time_expired( &cur->revocation_date ) )
2970 return( 1 );
2971 }
2972
2973 cur = cur->next;
2974 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
2976 return( 0 );
2977}
2978
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002979/*
2980 * Wrapper for x509 hashes.
2981 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002982 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002983 */
Paul Bakker23986e52011-04-24 08:57:21 +00002984static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002985 unsigned char *out )
2986{
2987 switch( alg )
2988 {
Paul Bakker40e46942009-01-03 21:51:57 +00002989#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002990 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002991#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002992#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002993 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002994#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002995#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002996 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002997#endif
2998#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002999 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003000#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003001#if defined(POLARSSL_SHA2_C)
3002 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3003 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3004#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003005#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003006 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3007 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3008#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003010 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 break;
3012 }
3013}
3014
3015/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003016 * Check that the given certificate is valid accoring to the CRL.
3017 */
3018static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3019 x509_crl *crl_list)
3020{
3021 int flags = 0;
3022 int hash_id;
3023 unsigned char hash[64];
3024
Paul Bakker915275b2012-09-28 07:10:55 +00003025 if( ca == NULL )
3026 return( flags );
3027
Paul Bakker76fd75a2011-01-16 21:12:10 +00003028 /*
3029 * TODO: What happens if no CRL is present?
3030 * Suggestion: Revocation state should be unknown if no CRL is present.
3031 * For backwards compatibility this is not yet implemented.
3032 */
3033
Paul Bakker915275b2012-09-28 07:10:55 +00003034 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003035 {
Paul Bakker915275b2012-09-28 07:10:55 +00003036 if( crl_list->version == 0 ||
3037 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003038 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3039 crl_list->issuer_raw.len ) != 0 )
3040 {
3041 crl_list = crl_list->next;
3042 continue;
3043 }
3044
3045 /*
3046 * Check if CRL is correctly signed by the trusted CA
3047 */
3048 hash_id = crl_list->sig_alg;
3049
3050 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3051
3052 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
3053 0, hash, crl_list->sig.p ) == 0 )
3054 {
3055 /*
3056 * CRL is not trusted
3057 */
3058 flags |= BADCRL_NOT_TRUSTED;
3059 break;
3060 }
3061
3062 /*
3063 * Check for validity of CRL (Do not drop out)
3064 */
3065 if( x509parse_time_expired( &crl_list->next_update ) )
3066 flags |= BADCRL_EXPIRED;
3067
3068 /*
3069 * Check if certificate is revoked
3070 */
3071 if( x509parse_revoked(crt, crl_list) )
3072 {
3073 flags |= BADCERT_REVOKED;
3074 break;
3075 }
3076
3077 crl_list = crl_list->next;
3078 }
3079 return flags;
3080}
3081
Paul Bakker57b12982012-02-11 17:38:38 +00003082int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003083{
3084 size_t i;
3085 size_t cn_idx = 0;
3086
Paul Bakker57b12982012-02-11 17:38:38 +00003087 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003088 return( 0 );
3089
3090 for( i = 0; i < strlen( cn ); ++i )
3091 {
3092 if( cn[i] == '.' )
3093 {
3094 cn_idx = i;
3095 break;
3096 }
3097 }
3098
3099 if( cn_idx == 0 )
3100 return( 0 );
3101
Paul Bakker535e97d2012-08-23 10:49:55 +00003102 if( strlen( cn ) - cn_idx == name->len - 1 &&
3103 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003104 {
3105 return( 1 );
3106 }
3107
3108 return( 0 );
3109}
3110
Paul Bakker915275b2012-09-28 07:10:55 +00003111static int x509parse_verify_top(
3112 x509_cert *child, x509_cert *trust_ca,
3113 x509_crl *ca_crl, int *path_cnt, int *flags,
3114 int (*f_vrfy)(void *, x509_cert *, int, int *),
3115 void *p_vrfy )
3116{
3117 int hash_id, ret;
3118 int ca_flags = 0;
3119 unsigned char hash[64];
3120
3121 if( x509parse_time_expired( &child->valid_to ) )
3122 *flags |= BADCERT_EXPIRED;
3123
3124 /*
3125 * Child is the top of the chain. Check against the trust_ca list.
3126 */
3127 *flags |= BADCERT_NOT_TRUSTED;
3128
3129 while( trust_ca != NULL )
3130 {
3131 if( trust_ca->version == 0 ||
3132 child->issuer_raw.len != trust_ca->subject_raw.len ||
3133 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3134 child->issuer_raw.len ) != 0 )
3135 {
3136 trust_ca = trust_ca->next;
3137 continue;
3138 }
3139
3140 if( trust_ca->max_pathlen > 0 &&
3141 trust_ca->max_pathlen < *path_cnt )
3142 {
3143 trust_ca = trust_ca->next;
3144 continue;
3145 }
3146
3147 hash_id = child->sig_alg;
3148
3149 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3150
3151 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3152 0, hash, child->sig.p ) != 0 )
3153 {
3154 trust_ca = trust_ca->next;
3155 continue;
3156 }
3157
3158 /*
3159 * Top of chain is signed by a trusted CA
3160 */
3161 *flags &= ~BADCERT_NOT_TRUSTED;
3162 break;
3163 }
3164
3165 if( trust_ca != NULL )
3166 {
3167 /* Check trusted CA's CRL for then chain's top crt */
3168 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3169
3170 if( x509parse_time_expired( &trust_ca->valid_to ) )
3171 ca_flags |= BADCERT_EXPIRED;
3172
3173 hash_id = trust_ca->sig_alg;
3174
3175 x509_hash( trust_ca->tbs.p, trust_ca->tbs.len, hash_id, hash );
3176
3177 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3178 0, hash, trust_ca->sig.p ) != 0 )
3179 {
3180 ca_flags |= BADCERT_NOT_TRUSTED;
3181 }
3182
3183 if( NULL != f_vrfy )
3184 {
3185 if( ( ret = f_vrfy( p_vrfy, trust_ca, 0, &ca_flags ) ) != 0 )
3186 return( ret );
3187 }
3188 }
3189
3190 /* Call callback on top cert */
3191 if( NULL != f_vrfy )
3192 {
3193 if( ( ret = f_vrfy(p_vrfy, child, 1, flags ) ) != 0 )
3194 return( ret );
3195 }
3196
3197 *path_cnt = 2;
3198
3199 *flags |= ca_flags;
3200
3201 return( 0 );
3202}
3203
3204static int x509parse_verify_child(
3205 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
3206 x509_crl *ca_crl, int *path_cnt, int *flags,
3207 int (*f_vrfy)(void *, x509_cert *, int, int *),
3208 void *p_vrfy )
3209{
3210 int hash_id, ret;
3211 int parent_flags = 0;
3212 unsigned char hash[64];
3213 x509_cert *grandparent;
3214
3215 if( x509parse_time_expired( &child->valid_to ) )
3216 *flags |= BADCERT_EXPIRED;
3217
3218 hash_id = child->sig_alg;
3219
3220 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3221
3222 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3223 child->sig.p ) != 0 )
3224 *flags |= BADCERT_NOT_TRUSTED;
3225
3226 /* Check trusted CA's CRL for the given crt */
3227 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3228
3229 grandparent = parent->next;
3230
3231 while( grandparent != NULL )
3232 {
3233 if( grandparent->version == 0 ||
3234 grandparent->ca_istrue == 0 ||
3235 parent->issuer_raw.len != grandparent->subject_raw.len ||
3236 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3237 parent->issuer_raw.len ) != 0 )
3238 {
3239 grandparent = grandparent->next;
3240 continue;
3241 }
3242 break;
3243 }
3244
3245 (*path_cnt)++;
3246 if( grandparent != NULL )
3247 {
3248 /*
3249 * Part of the chain
3250 */
3251 ret = x509parse_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt, &parent_flags, f_vrfy, p_vrfy );
3252 if( ret != 0 )
3253 return( ret );
3254 }
3255 else
3256 {
3257 ret = x509parse_verify_top( parent, trust_ca, ca_crl, path_cnt, &parent_flags, f_vrfy, p_vrfy );
3258 if( ret != 0 )
3259 return( ret );
3260 }
3261
3262 /* child is verified to be a child of the parent, call verify callback */
3263 if( NULL != f_vrfy )
3264 if( ( ret = f_vrfy( p_vrfy, child, *path_cnt, flags ) ) != 0 )
3265 return( ret );
3266 (*path_cnt)++;
3267
3268 *flags |= parent_flags;
3269
3270 return( 0 );
3271}
3272
Paul Bakker76fd75a2011-01-16 21:12:10 +00003273/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 * Verify the certificate validity
3275 */
3276int x509parse_verify( x509_cert *crt,
3277 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003278 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003279 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003280 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003281 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003282{
Paul Bakker23986e52011-04-24 08:57:21 +00003283 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003284 int ret;
3285 int pathlen = 1;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003286 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003288 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003289
Paul Bakker40ea7de2009-05-03 10:18:48 +00003290 *flags = 0;
3291
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 if( cn != NULL )
3293 {
3294 name = &crt->subject;
3295 cn_len = strlen( cn );
3296
Paul Bakker4d2c1242012-05-10 14:12:46 +00003297 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003299 cur = &crt->subject_alt_names;
3300
3301 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003302 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003303 if( cur->buf.len == cn_len &&
3304 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003305 break;
3306
Paul Bakker535e97d2012-08-23 10:49:55 +00003307 if( cur->buf.len > 2 &&
3308 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003309 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003310 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003311
Paul Bakker4d2c1242012-05-10 14:12:46 +00003312 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003313 }
3314
3315 if( cur == NULL )
3316 *flags |= BADCERT_CN_MISMATCH;
3317 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003318 else
3319 {
3320 while( name != NULL )
3321 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003322 if( name->oid.len == 3 &&
3323 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003324 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003325 if( name->val.len == cn_len &&
3326 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003327 break;
3328
Paul Bakker535e97d2012-08-23 10:49:55 +00003329 if( name->val.len > 2 &&
3330 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003331 x509_wildcard_verify( cn, &name->val ) )
3332 break;
3333 }
3334
3335 name = name->next;
3336 }
3337
3338 if( name == NULL )
3339 *flags |= BADCERT_CN_MISMATCH;
3340 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003341 }
3342
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003344 * Iterate upwards in the given cert chain, to find our crt parent.
3345 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003347 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003348
Paul Bakker76fd75a2011-01-16 21:12:10 +00003349 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003351 if( parent->ca_istrue == 0 ||
3352 crt->issuer_raw.len != parent->subject_raw.len ||
3353 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 crt->issuer_raw.len ) != 0 )
3355 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003356 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 continue;
3358 }
Paul Bakker915275b2012-09-28 07:10:55 +00003359 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 }
3361
Paul Bakker915275b2012-09-28 07:10:55 +00003362 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 {
Paul Bakker915275b2012-09-28 07:10:55 +00003364 /*
3365 * Part of the chain
3366 */
3367 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, &pathlen, flags, f_vrfy, p_vrfy );
3368 if( ret != 0 )
3369 return( ret );
3370 }
3371 else
Paul Bakker74111d32011-01-15 16:57:55 +00003372 {
Paul Bakker915275b2012-09-28 07:10:55 +00003373 ret = x509parse_verify_top( crt, trust_ca, ca_crl, &pathlen, flags, f_vrfy, p_vrfy );
3374 if( ret != 0 )
3375 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003376 }
Paul Bakker915275b2012-09-28 07:10:55 +00003377
3378 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003379 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003380
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 return( 0 );
3382}
3383
3384/*
3385 * Unallocate all certificate data
3386 */
3387void x509_free( x509_cert *crt )
3388{
3389 x509_cert *cert_cur = crt;
3390 x509_cert *cert_prv;
3391 x509_name *name_cur;
3392 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003393 x509_sequence *seq_cur;
3394 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003395
3396 if( crt == NULL )
3397 return;
3398
3399 do
3400 {
3401 rsa_free( &cert_cur->rsa );
3402
3403 name_cur = cert_cur->issuer.next;
3404 while( name_cur != NULL )
3405 {
3406 name_prv = name_cur;
3407 name_cur = name_cur->next;
3408 memset( name_prv, 0, sizeof( x509_name ) );
3409 free( name_prv );
3410 }
3411
3412 name_cur = cert_cur->subject.next;
3413 while( name_cur != NULL )
3414 {
3415 name_prv = name_cur;
3416 name_cur = name_cur->next;
3417 memset( name_prv, 0, sizeof( x509_name ) );
3418 free( name_prv );
3419 }
3420
Paul Bakker74111d32011-01-15 16:57:55 +00003421 seq_cur = cert_cur->ext_key_usage.next;
3422 while( seq_cur != NULL )
3423 {
3424 seq_prv = seq_cur;
3425 seq_cur = seq_cur->next;
3426 memset( seq_prv, 0, sizeof( x509_sequence ) );
3427 free( seq_prv );
3428 }
3429
Paul Bakker8afa70d2012-02-11 18:42:45 +00003430 seq_cur = cert_cur->subject_alt_names.next;
3431 while( seq_cur != NULL )
3432 {
3433 seq_prv = seq_cur;
3434 seq_cur = seq_cur->next;
3435 memset( seq_prv, 0, sizeof( x509_sequence ) );
3436 free( seq_prv );
3437 }
3438
Paul Bakker5121ce52009-01-03 21:22:43 +00003439 if( cert_cur->raw.p != NULL )
3440 {
3441 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3442 free( cert_cur->raw.p );
3443 }
3444
3445 cert_cur = cert_cur->next;
3446 }
3447 while( cert_cur != NULL );
3448
3449 cert_cur = crt;
3450 do
3451 {
3452 cert_prv = cert_cur;
3453 cert_cur = cert_cur->next;
3454
3455 memset( cert_prv, 0, sizeof( x509_cert ) );
3456 if( cert_prv != crt )
3457 free( cert_prv );
3458 }
3459 while( cert_cur != NULL );
3460}
3461
Paul Bakkerd98030e2009-05-02 15:13:40 +00003462/*
3463 * Unallocate all CRL data
3464 */
3465void x509_crl_free( x509_crl *crl )
3466{
3467 x509_crl *crl_cur = crl;
3468 x509_crl *crl_prv;
3469 x509_name *name_cur;
3470 x509_name *name_prv;
3471 x509_crl_entry *entry_cur;
3472 x509_crl_entry *entry_prv;
3473
3474 if( crl == NULL )
3475 return;
3476
3477 do
3478 {
3479 name_cur = crl_cur->issuer.next;
3480 while( name_cur != NULL )
3481 {
3482 name_prv = name_cur;
3483 name_cur = name_cur->next;
3484 memset( name_prv, 0, sizeof( x509_name ) );
3485 free( name_prv );
3486 }
3487
3488 entry_cur = crl_cur->entry.next;
3489 while( entry_cur != NULL )
3490 {
3491 entry_prv = entry_cur;
3492 entry_cur = entry_cur->next;
3493 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3494 free( entry_prv );
3495 }
3496
3497 if( crl_cur->raw.p != NULL )
3498 {
3499 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3500 free( crl_cur->raw.p );
3501 }
3502
3503 crl_cur = crl_cur->next;
3504 }
3505 while( crl_cur != NULL );
3506
3507 crl_cur = crl;
3508 do
3509 {
3510 crl_prv = crl_cur;
3511 crl_cur = crl_cur->next;
3512
3513 memset( crl_prv, 0, sizeof( x509_crl ) );
3514 if( crl_prv != crl )
3515 free( crl_prv );
3516 }
3517 while( crl_cur != NULL );
3518}
3519
Paul Bakker40e46942009-01-03 21:51:57 +00003520#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003521
Paul Bakker40e46942009-01-03 21:51:57 +00003522#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
3524/*
3525 * Checkup routine
3526 */
3527int x509_self_test( int verbose )
3528{
Paul Bakker5690efc2011-05-26 13:16:06 +00003529#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003530 int ret;
3531 int flags;
3532 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003533 x509_cert cacert;
3534 x509_cert clicert;
3535 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003536#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003537 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003538#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
3540 if( verbose != 0 )
3541 printf( " X.509 certificate load: " );
3542
3543 memset( &clicert, 0, sizeof( x509_cert ) );
3544
3545 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003546 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 if( ret != 0 )
3548 {
3549 if( verbose != 0 )
3550 printf( "failed\n" );
3551
3552 return( ret );
3553 }
3554
3555 memset( &cacert, 0, sizeof( x509_cert ) );
3556
3557 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003558 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 if( ret != 0 )
3560 {
3561 if( verbose != 0 )
3562 printf( "failed\n" );
3563
3564 return( ret );
3565 }
3566
3567 if( verbose != 0 )
3568 printf( "passed\n X.509 private key load: " );
3569
3570 i = strlen( test_ca_key );
3571 j = strlen( test_ca_pwd );
3572
Paul Bakker66b78b22011-03-25 14:22:50 +00003573 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3574
Paul Bakker5121ce52009-01-03 21:22:43 +00003575 if( ( ret = x509parse_key( &rsa,
3576 (unsigned char *) test_ca_key, i,
3577 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3578 {
3579 if( verbose != 0 )
3580 printf( "failed\n" );
3581
3582 return( ret );
3583 }
3584
3585 if( verbose != 0 )
3586 printf( "passed\n X.509 signature verify: ");
3587
Paul Bakker23986e52011-04-24 08:57:21 +00003588 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003589 if( ret != 0 )
3590 {
Paul Bakker23986e52011-04-24 08:57:21 +00003591 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003592 if( verbose != 0 )
3593 printf( "failed\n" );
3594
3595 return( ret );
3596 }
3597
Paul Bakker5690efc2011-05-26 13:16:06 +00003598#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003600 printf( "passed\n X.509 DHM parameter load: " );
3601
3602 i = strlen( test_dhm_params );
3603 j = strlen( test_ca_pwd );
3604
3605 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3606 {
3607 if( verbose != 0 )
3608 printf( "failed\n" );
3609
3610 return( ret );
3611 }
3612
3613 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003615#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003616
3617 x509_free( &cacert );
3618 x509_free( &clicert );
3619 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003620#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003621 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003622#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003623
3624 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003625#else
3626 ((void) verbose);
3627 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3628#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003629}
3630
3631#endif
3632
3633#endif