blob: af9884300b9bb3ce0ad17dfedb0b6d7558a9f385 [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/*
26 * The ITU-T X.509 standard defines a certificat format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc2459.txt
29 * http://www.ietf.org/rfc/rfc3279.txt
30 *
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>
63#endif
64
Paul Bakker5121ce52009-01-03 21:22:43 +000065/*
Paul Bakker5121ce52009-01-03 21:22:43 +000066 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
67 */
68static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000069 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000070 int *ver )
71{
Paul Bakker23986e52011-04-24 08:57:21 +000072 int ret;
73 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75 if( ( ret = asn1_get_tag( p, end, &len,
76 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
77 {
Paul Bakker40e46942009-01-03 21:51:57 +000078 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000079 {
80 *ver = 0;
81 return( 0 );
82 }
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84 return( ret );
85 }
86
87 end = *p + len;
88
89 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000090 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +000093 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +000094 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( 0 );
97}
98
99/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000100 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000101 */
102static int x509_crl_get_version( unsigned char **p,
103 const unsigned char *end,
104 int *ver )
105{
106 int ret;
107
108 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
109 {
110 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000111 {
112 *ver = 0;
113 return( 0 );
114 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000115
116 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
117 }
118
119 return( 0 );
120}
121
122/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 * CertificateSerialNumber ::= INTEGER
124 */
125static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000126 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 x509_buf *serial )
128{
129 int ret;
130
131 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000132 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000133 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
136 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000137 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000138 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 serial->tag = *(*p)++;
141
142 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000143 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145 serial->p = *p;
146 *p += serial->len;
147
148 return( 0 );
149}
150
151/*
152 * AlgorithmIdentifier ::= SEQUENCE {
153 * algorithm OBJECT IDENTIFIER,
154 * parameters ANY DEFINED BY algorithm OPTIONAL }
155 */
156static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000157 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 x509_buf *alg )
159{
Paul Bakker23986e52011-04-24 08:57:21 +0000160 int ret;
161 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 if( ( ret = asn1_get_tag( p, end, &len,
164 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000165 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167 end = *p + len;
168 alg->tag = **p;
169
170 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 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 alg->p = *p;
174 *p += alg->len;
175
176 if( *p == end )
177 return( 0 );
178
179 /*
180 * assume the algorithm parameters must be NULL
181 */
182 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000183 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000186 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000187 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189 return( 0 );
190}
191
192/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 * AttributeTypeAndValue ::= SEQUENCE {
194 * type AttributeType,
195 * value AttributeValue }
196 *
197 * AttributeType ::= OBJECT IDENTIFIER
198 *
199 * AttributeValue ::= ANY DEFINED BY AttributeType
200 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000201static int x509_get_attr_type_value( unsigned char **p,
202 const unsigned char *end,
203 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204{
Paul Bakker23986e52011-04-24 08:57:21 +0000205 int ret;
206 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 x509_buf *oid;
208 x509_buf *val;
209
210 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000212 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 oid = &cur->oid;
215 oid->tag = **p;
216
217 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 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
220 oid->p = *p;
221 *p += oid->len;
222
223 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000225 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
228 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
229 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
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_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 val = &cur->val;
234 val->tag = *(*p)++;
235
236 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000237 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 val->p = *p;
240 *p += val->len;
241
242 cur->next = NULL;
243
Paul Bakker400ff6f2011-02-20 10:40:16 +0000244 return( 0 );
245}
246
247/*
248 * RelativeDistinguishedName ::=
249 * SET OF AttributeTypeAndValue
250 *
251 * AttributeTypeAndValue ::= SEQUENCE {
252 * type AttributeType,
253 * value AttributeValue }
254 *
255 * AttributeType ::= OBJECT IDENTIFIER
256 *
257 * AttributeValue ::= ANY DEFINED BY AttributeType
258 */
259static int x509_get_name( unsigned char **p,
260 const unsigned char *end,
261 x509_name *cur )
262{
Paul Bakker23986e52011-04-24 08:57:21 +0000263 int ret;
264 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000265 const unsigned char *end2;
266 x509_name *use;
267
268 if( ( ret = asn1_get_tag( p, end, &len,
269 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000270 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000271
272 end2 = end;
273 end = *p + len;
274 use = cur;
275
276 do
277 {
278 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
279 return( ret );
280
281 if( *p != end )
282 {
283 use->next = (x509_name *) malloc(
284 sizeof( x509_name ) );
285
286 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000287 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000288
289 memset( use->next, 0, sizeof( x509_name ) );
290
291 use = use->next;
292 }
293 }
294 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296 /*
297 * recurse until end of SEQUENCE is reached
298 */
299 if( *p == end2 )
300 return( 0 );
301
302 cur->next = (x509_name *) malloc(
303 sizeof( x509_name ) );
304
305 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000306 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308 return( x509_get_name( p, end2, cur->next ) );
309}
310
311/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 * Time ::= CHOICE {
313 * utcTime UTCTime,
314 * generalTime GeneralizedTime }
315 */
Paul Bakker91200182010-02-18 21:26:15 +0000316static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000317 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000318 x509_time *time )
319{
Paul Bakker23986e52011-04-24 08:57:21 +0000320 int ret;
321 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000322 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000323 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000324
Paul Bakker91200182010-02-18 21:26:15 +0000325 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000326 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
327 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000328
Paul Bakker91200182010-02-18 21:26:15 +0000329 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330
Paul Bakker91200182010-02-18 21:26:15 +0000331 if ( tag == ASN1_UTC_TIME )
332 {
333 (*p)++;
334 ret = asn1_get_len( p, end, &len );
335
336 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000337 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000340 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
341 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000342
Paul Bakker91200182010-02-18 21:26:15 +0000343 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
344 &time->year, &time->mon, &time->day,
345 &time->hour, &time->min, &time->sec ) < 5 )
346 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000347
Paul Bakker400ff6f2011-02-20 10:40:16 +0000348 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000349 time->year += 1900;
350
351 *p += len;
352
353 return( 0 );
354 }
355 else if ( tag == ASN1_GENERALIZED_TIME )
356 {
357 (*p)++;
358 ret = asn1_get_len( p, end, &len );
359
360 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000361 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000362
363 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000364 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
365 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000366
367 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
368 &time->year, &time->mon, &time->day,
369 &time->hour, &time->min, &time->sec ) < 5 )
370 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
371
372 *p += len;
373
374 return( 0 );
375 }
376 else
Paul Bakker9d781402011-05-09 16:17:09 +0000377 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000378}
379
380
381/*
382 * Validity ::= SEQUENCE {
383 * notBefore Time,
384 * notAfter Time }
385 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000386static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000387 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 x509_time *from,
389 x509_time *to )
390{
Paul Bakker23986e52011-04-24 08:57:21 +0000391 int ret;
392 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394 if( ( ret = asn1_get_tag( p, end, &len,
395 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000396 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
398 end = *p + len;
399
Paul Bakker91200182010-02-18 21:26:15 +0000400 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000401 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
Paul Bakker91200182010-02-18 21:26:15 +0000403 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000407 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000408 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
410 return( 0 );
411}
412
413/*
414 * SubjectPublicKeyInfo ::= SEQUENCE {
415 * algorithm AlgorithmIdentifier,
416 * subjectPublicKey BIT STRING }
417 */
418static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000419 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 x509_buf *pk_alg_oid,
421 mpi *N, mpi *E )
422{
Paul Bakker23986e52011-04-24 08:57:21 +0000423 int ret, can_handle;
424 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 unsigned char *end2;
426
427 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
428 return( ret );
429
430 /*
431 * only RSA public keys handled at this time
432 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000433 can_handle = 0;
434
435 if( pk_alg_oid->len == 9 &&
436 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
437 can_handle = 1;
438
439 if( pk_alg_oid->len == 9 &&
440 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
441 {
442 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
443 can_handle = 1;
444
445 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
446 can_handle = 1;
447 }
448
449 if( pk_alg_oid->len == 5 &&
450 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
451 can_handle = 1;
452
453 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000454 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000457 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000460 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000461 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463 end2 = *p + len;
464
465 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000466 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 /*
469 * RSAPublicKey ::= SEQUENCE {
470 * modulus INTEGER, -- n
471 * publicExponent INTEGER -- e
472 * }
473 */
474 if( ( ret = asn1_get_tag( p, end2, &len,
475 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000476 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000479 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000480 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
483 ( ret = asn1_get_mpi( p, end2, E ) ) != 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 != end )
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 return( 0 );
491}
492
493static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000494 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 x509_buf *sig )
496{
Paul Bakker23986e52011-04-24 08:57:21 +0000497 int ret;
498 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 sig->tag = **p;
501
502 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000503 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Paul Bakker74111d32011-01-15 16:57:55 +0000505
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000507 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 sig->len = len;
510 sig->p = *p;
511
512 *p += len;
513
514 return( 0 );
515}
516
517/*
518 * X.509 v2/v3 unique identifier (not parsed)
519 */
520static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000521 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 x509_buf *uid, int n )
523{
524 int ret;
525
526 if( *p == end )
527 return( 0 );
528
529 uid->tag = **p;
530
531 if( ( ret = asn1_get_tag( p, end, &uid->len,
532 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
533 {
Paul Bakker40e46942009-01-03 21:51:57 +0000534 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 return( 0 );
536
537 return( ret );
538 }
539
540 uid->p = *p;
541 *p += uid->len;
542
543 return( 0 );
544}
545
546/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000547 * X.509 Extensions (No parsing of extensions, pointer should
548 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 */
550static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000551 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000552 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000553{
Paul Bakker23986e52011-04-24 08:57:21 +0000554 int ret;
555 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 if( *p == end )
558 return( 0 );
559
560 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000561
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000563 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 ext->p = *p;
567 end = *p + ext->len;
568
569 /*
570 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
571 *
572 * Extension ::= SEQUENCE {
573 * extnID OBJECT IDENTIFIER,
574 * critical BOOLEAN DEFAULT FALSE,
575 * extnValue OCTET STRING }
576 */
577 if( ( ret = asn1_get_tag( p, end, &len,
578 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000579 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000582 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000583 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Paul Bakkerd98030e2009-05-02 15:13:40 +0000585 return( 0 );
586}
587
588/*
589 * X.509 CRL v2 extensions (no extensions parsed yet.)
590 */
591static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000592 const unsigned char *end,
593 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000594{
Paul Bakker23986e52011-04-24 08:57:21 +0000595 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000596 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000597
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000598 /* Get explicit tag */
599 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000600 {
601 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
602 return( 0 );
603
604 return( ret );
605 }
606
607 while( *p < end )
608 {
609 if( ( ret = asn1_get_tag( p, end, &len,
610 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000611 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000612
613 *p += len;
614 }
615
616 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000617 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000618 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
619
620 return( 0 );
621}
622
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000623/*
624 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
625 */
626static int x509_get_crl_entry_ext( unsigned char **p,
627 const unsigned char *end,
628 x509_buf *ext )
629{
630 int ret;
631 size_t len = 0;
632
633 /* OPTIONAL */
634 if (end <= *p)
635 return( 0 );
636
637 ext->tag = **p;
638 ext->p = *p;
639
640 /*
641 * Get CRL-entry extension sequence header
642 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
643 */
644 if( ( ret = asn1_get_tag( p, end, &ext->len,
645 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
646 {
647 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
648 {
649 ext->p = NULL;
650 return( 0 );
651 }
652 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
653 }
654
655 end = *p + ext->len;
656
657 if( end != *p + ext->len )
658 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
659 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
660
661 while( *p < end )
662 {
663 if( ( ret = asn1_get_tag( p, end, &len,
664 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
665 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
666
667 *p += len;
668 }
669
670 if( *p != end )
671 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
672 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
673
674 return( 0 );
675}
676
Paul Bakker74111d32011-01-15 16:57:55 +0000677static int x509_get_basic_constraints( unsigned char **p,
678 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000679 int *ca_istrue,
680 int *max_pathlen )
681{
Paul Bakker23986e52011-04-24 08:57:21 +0000682 int ret;
683 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000684
685 /*
686 * BasicConstraints ::= SEQUENCE {
687 * cA BOOLEAN DEFAULT FALSE,
688 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
689 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000690 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000691 *max_pathlen = 0; /* endless */
692
693 if( ( ret = asn1_get_tag( p, end, &len,
694 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000695 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000696
697 if( *p == end )
698 return 0;
699
Paul Bakker3cccddb2011-01-16 21:46:31 +0000700 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000701 {
702 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000703 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000704
705 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000706 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000707
Paul Bakker3cccddb2011-01-16 21:46:31 +0000708 if( *ca_istrue != 0 )
709 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000710 }
711
712 if( *p == end )
713 return 0;
714
715 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000716 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000717
718 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000719 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000720 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
721
722 (*max_pathlen)++;
723
Paul Bakker74111d32011-01-15 16:57:55 +0000724 return 0;
725}
726
727static int x509_get_ns_cert_type( unsigned char **p,
728 const unsigned char *end,
729 unsigned char *ns_cert_type)
730{
731 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000732 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000733
734 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000735 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000736
737 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000738 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000739 POLARSSL_ERR_ASN1_INVALID_LENGTH );
740
741 /* Get actual bitstring */
742 *ns_cert_type = *bs.p;
743 return 0;
744}
745
746static int x509_get_key_usage( unsigned char **p,
747 const unsigned char *end,
748 unsigned char *key_usage)
749{
750 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000751 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000752
753 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000754 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000755
Paul Bakkercebdf172011-11-11 15:01:31 +0000756 if( bs.len > 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000757 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000758 POLARSSL_ERR_ASN1_INVALID_LENGTH );
759
760 /* Get actual bitstring */
761 *key_usage = *bs.p;
762 return 0;
763}
764
Paul Bakkerd98030e2009-05-02 15:13:40 +0000765/*
Paul Bakker74111d32011-01-15 16:57:55 +0000766 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
767 *
768 * KeyPurposeId ::= OBJECT IDENTIFIER
769 */
770static int x509_get_ext_key_usage( unsigned char **p,
771 const unsigned char *end,
772 x509_sequence *ext_key_usage)
773{
774 int ret;
775
776 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000777 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000778
779 /* Sequence length must be >= 1 */
780 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000782 POLARSSL_ERR_ASN1_INVALID_LENGTH );
783
784 return 0;
785}
786
787/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000788 * SubjectAltName ::= GeneralNames
789 *
790 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
791 *
792 * GeneralName ::= CHOICE {
793 * otherName [0] OtherName,
794 * rfc822Name [1] IA5String,
795 * dNSName [2] IA5String,
796 * x400Address [3] ORAddress,
797 * directoryName [4] Name,
798 * ediPartyName [5] EDIPartyName,
799 * uniformResourceIdentifier [6] IA5String,
800 * iPAddress [7] OCTET STRING,
801 * registeredID [8] OBJECT IDENTIFIER }
802 *
803 * OtherName ::= SEQUENCE {
804 * type-id OBJECT IDENTIFIER,
805 * value [0] EXPLICIT ANY DEFINED BY type-id }
806 *
807 * EDIPartyName ::= SEQUENCE {
808 * nameAssigner [0] DirectoryString OPTIONAL,
809 * partyName [1] DirectoryString }
810 *
811 * NOTE: PolarSSL only parses and uses dNSName at this point.
812 */
813static int x509_get_subject_alt_name( unsigned char **p,
814 const unsigned char *end,
815 x509_sequence *subject_alt_name )
816{
817 int ret;
818 size_t len, tag_len;
819 asn1_buf *buf;
820 unsigned char tag;
821 asn1_sequence *cur = subject_alt_name;
822
823 /* Get main sequence tag */
824 if( ( ret = asn1_get_tag( p, end, &len,
825 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
826 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
827
828 if( *p + len != end )
829 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
830 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
831
832 while( *p < end )
833 {
834 if( ( end - *p ) < 1 )
835 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
836 POLARSSL_ERR_ASN1_OUT_OF_DATA );
837
838 tag = **p;
839 (*p)++;
840 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
841 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
842
843 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
844 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
845 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
846
847 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
848 {
849 *p += tag_len;
850 continue;
851 }
852
853 buf = &(cur->buf);
854 buf->tag = tag;
855 buf->p = *p;
856 buf->len = tag_len;
857 *p += buf->len;
858
859 /* Allocate and assign next pointer */
860 if (*p < end)
861 {
862 cur->next = (asn1_sequence *) malloc(
863 sizeof( asn1_sequence ) );
864
865 if( cur->next == NULL )
866 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
867 POLARSSL_ERR_ASN1_MALLOC_FAILED );
868
869 cur = cur->next;
870 }
871 }
872
873 /* Set final sequence entry's next pointer to NULL */
874 cur->next = NULL;
875
876 if( *p != end )
877 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
878 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
879
880 return( 0 );
881}
882
883/*
Paul Bakker74111d32011-01-15 16:57:55 +0000884 * X.509 v3 extensions
885 *
886 * TODO: Perform all of the basic constraints tests required by the RFC
887 * TODO: Set values for undetected extensions to a sane default?
888 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000889 */
890static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000891 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000892 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000893{
Paul Bakker23986e52011-04-24 08:57:21 +0000894 int ret;
895 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000896 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000897
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000898 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000899 {
900 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
901 return( 0 );
902
903 return( ret );
904 }
905
Paul Bakker5121ce52009-01-03 21:22:43 +0000906 while( *p < end )
907 {
Paul Bakker74111d32011-01-15 16:57:55 +0000908 /*
909 * Extension ::= SEQUENCE {
910 * extnID OBJECT IDENTIFIER,
911 * critical BOOLEAN DEFAULT FALSE,
912 * extnValue OCTET STRING }
913 */
914 x509_buf extn_oid = {0, 0, NULL};
915 int is_critical = 0; /* DEFAULT FALSE */
916
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 if( ( ret = asn1_get_tag( p, end, &len,
918 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000919 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000920
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000921 end_ext_data = *p + len;
922
Paul Bakker74111d32011-01-15 16:57:55 +0000923 /* Get extension ID */
924 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Paul Bakker74111d32011-01-15 16:57:55 +0000926 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000927 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Paul Bakker74111d32011-01-15 16:57:55 +0000929 extn_oid.p = *p;
930 *p += extn_oid.len;
931
932 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000933 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000934 POLARSSL_ERR_ASN1_OUT_OF_DATA );
935
936 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000937 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000938 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000939 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
Paul Bakker74111d32011-01-15 16:57:55 +0000941 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000942 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000944 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000946 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000947
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000948 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000949 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000950 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Paul Bakker74111d32011-01-15 16:57:55 +0000952 /*
953 * Detect supported extensions
954 */
955 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
956 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 {
Paul Bakker74111d32011-01-15 16:57:55 +0000958 /* Parse basic constraints */
959 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000960 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000961 return ( ret );
962 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 }
Paul Bakker74111d32011-01-15 16:57:55 +0000964 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
965 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
966 {
967 /* Parse netscape certificate type */
968 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
969 &crt->ns_cert_type ) ) != 0 )
970 return ( ret );
971 crt->ext_types |= EXT_NS_CERT_TYPE;
972 }
973 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
974 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
975 {
976 /* Parse key usage */
977 if( ( ret = x509_get_key_usage( p, end_ext_octet,
978 &crt->key_usage ) ) != 0 )
979 return ( ret );
980 crt->ext_types |= EXT_KEY_USAGE;
981 }
982 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
983 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
984 {
985 /* Parse extended key usage */
986 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
987 &crt->ext_key_usage ) ) != 0 )
988 return ( ret );
989 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
990 }
Paul Bakkera8cd2392012-02-11 16:09:32 +0000991 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == extn_oid.len ) &&
992 memcmp( extn_oid.p, OID_SUBJECT_ALT_NAME, extn_oid.len ) == 0 )
993 {
994 /* Parse extended key usage */
995 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
996 &crt->subject_alt_names ) ) != 0 )
997 return ( ret );
998 crt->ext_types |= EXT_SUBJECT_ALT_NAME;
999 }
Paul Bakker74111d32011-01-15 16:57:55 +00001000 else
1001 {
1002 /* No parser found, skip extension */
1003 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker5c721f92011-07-27 16:51:09 +00001005#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001006 if( is_critical )
1007 {
1008 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001009 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001010 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1011 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001012#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001013 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 }
1015
1016 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001017 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001018 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 return( 0 );
1021}
1022
1023/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001024 * X.509 CRL Entries
1025 */
1026static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001027 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001028 x509_crl_entry *entry )
1029{
Paul Bakker23986e52011-04-24 08:57:21 +00001030 int ret;
1031 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001032 x509_crl_entry *cur_entry = entry;
1033
1034 if( *p == end )
1035 return( 0 );
1036
Paul Bakker9be19372009-07-27 20:21:53 +00001037 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001038 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1039 {
1040 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1041 return( 0 );
1042
1043 return( ret );
1044 }
1045
Paul Bakker9be19372009-07-27 20:21:53 +00001046 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001047
1048 while( *p < end )
1049 {
Paul Bakker23986e52011-04-24 08:57:21 +00001050 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001051 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001052
1053 if( ( ret = asn1_get_tag( p, end, &len2,
1054 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1055 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001056 return( ret );
1057 }
1058
Paul Bakker9be19372009-07-27 20:21:53 +00001059 cur_entry->raw.tag = **p;
1060 cur_entry->raw.p = *p;
1061 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001062 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001063
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001064 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001065 return( ret );
1066
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001067 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001068 return( ret );
1069
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001070 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001071 return( ret );
1072
Paul Bakker74111d32011-01-15 16:57:55 +00001073 if ( *p < end )
1074 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001075 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001076
1077 if( cur_entry->next == NULL )
1078 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1079
Paul Bakkerd98030e2009-05-02 15:13:40 +00001080 cur_entry = cur_entry->next;
1081 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1082 }
1083 }
1084
1085 return( 0 );
1086}
1087
Paul Bakker27d66162010-03-17 06:56:01 +00001088static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1089{
1090 if( sig_oid->len == 9 &&
1091 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1092 {
1093 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1094 {
1095 *sig_alg = sig_oid->p[8];
1096 return( 0 );
1097 }
1098
1099 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1100 {
1101 *sig_alg = sig_oid->p[8];
1102 return( 0 );
1103 }
1104
1105 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1106 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001107 if( sig_oid->len == 5 &&
1108 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1109 {
1110 *sig_alg = SIG_RSA_SHA1;
1111 return( 0 );
1112 }
Paul Bakker27d66162010-03-17 06:56:01 +00001113
1114 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1115}
1116
Paul Bakkerd98030e2009-05-02 15:13:40 +00001117/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001118 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001120int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121{
Paul Bakker23986e52011-04-24 08:57:21 +00001122 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001123 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Paul Bakker320a4b52009-03-28 18:52:39 +00001126 /*
1127 * Check for valid input
1128 */
1129 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001130 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001131
Paul Bakker96743fc2011-02-12 14:30:57 +00001132 p = (unsigned char *) malloc( len = buflen );
1133
1134 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001135 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001136
1137 memcpy( p, buf, buflen );
1138
1139 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
1141 crt->raw.p = p;
1142 crt->raw.len = len;
1143 end = p + len;
1144
1145 /*
1146 * Certificate ::= SEQUENCE {
1147 * tbsCertificate TBSCertificate,
1148 * signatureAlgorithm AlgorithmIdentifier,
1149 * signatureValue BIT STRING }
1150 */
1151 if( ( ret = asn1_get_tag( &p, end, &len,
1152 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1153 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001154 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001155 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 }
1157
Paul Bakker23986e52011-04-24 08:57:21 +00001158 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001160 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001161 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001162 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 }
1164
1165 /*
1166 * TBSCertificate ::= SEQUENCE {
1167 */
1168 crt->tbs.p = p;
1169
1170 if( ( ret = asn1_get_tag( &p, end, &len,
1171 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1172 {
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 + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 }
1176
1177 end = p + len;
1178 crt->tbs.len = end - crt->tbs.p;
1179
1180 /*
1181 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1182 *
1183 * CertificateSerialNumber ::= INTEGER
1184 *
1185 * signature AlgorithmIdentifier
1186 */
1187 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1188 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1189 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1190 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001191 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 return( ret );
1193 }
1194
1195 crt->version++;
1196
1197 if( crt->version > 3 )
1198 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001199 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001200 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 }
1202
Paul Bakker27d66162010-03-17 06:56:01 +00001203 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001205 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001206 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
1208
1209 /*
1210 * issuer Name
1211 */
1212 crt->issuer_raw.p = p;
1213
1214 if( ( ret = asn1_get_tag( &p, end, &len,
1215 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1216 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001217 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001218 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 }
1220
1221 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1222 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001223 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 return( ret );
1225 }
1226
1227 crt->issuer_raw.len = p - crt->issuer_raw.p;
1228
1229 /*
1230 * Validity ::= SEQUENCE {
1231 * notBefore Time,
1232 * notAfter Time }
1233 *
1234 */
1235 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1236 &crt->valid_to ) ) != 0 )
1237 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001238 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 return( ret );
1240 }
1241
1242 /*
1243 * subject Name
1244 */
1245 crt->subject_raw.p = p;
1246
1247 if( ( ret = asn1_get_tag( &p, end, &len,
1248 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1249 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001250 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001251 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 }
1253
1254 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1255 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001256 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 return( ret );
1258 }
1259
1260 crt->subject_raw.len = p - crt->subject_raw.p;
1261
1262 /*
1263 * SubjectPublicKeyInfo ::= SEQUENCE
1264 * algorithm AlgorithmIdentifier,
1265 * subjectPublicKey BIT STRING }
1266 */
1267 if( ( ret = asn1_get_tag( &p, end, &len,
1268 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1269 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001270 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001271 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 }
1273
1274 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1275 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1276 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001277 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 return( ret );
1279 }
1280
1281 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1282 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001283 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 return( ret );
1285 }
1286
1287 crt->rsa.len = mpi_size( &crt->rsa.N );
1288
1289 /*
1290 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1291 * -- If present, version shall be v2 or v3
1292 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1293 * -- If present, version shall be v2 or v3
1294 * extensions [3] EXPLICIT Extensions OPTIONAL
1295 * -- If present, version shall be v3
1296 */
1297 if( crt->version == 2 || crt->version == 3 )
1298 {
1299 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1300 if( ret != 0 )
1301 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001302 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 return( ret );
1304 }
1305 }
1306
1307 if( crt->version == 2 || crt->version == 3 )
1308 {
1309 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1310 if( ret != 0 )
1311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 return( ret );
1314 }
1315 }
1316
1317 if( crt->version == 3 )
1318 {
Paul Bakker74111d32011-01-15 16:57:55 +00001319 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 if( ret != 0 )
1321 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001322 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 return( ret );
1324 }
1325 }
1326
1327 if( p != end )
1328 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001329 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001330 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001331 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
1333
1334 end = crt->raw.p + crt->raw.len;
1335
1336 /*
1337 * signatureAlgorithm AlgorithmIdentifier,
1338 * signatureValue BIT STRING
1339 */
1340 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1341 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001342 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 return( ret );
1344 }
1345
Paul Bakker320a4b52009-03-28 18:52:39 +00001346 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001348 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001349 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
1351
1352 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1353 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001354 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 return( ret );
1356 }
1357
1358 if( p != end )
1359 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001361 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001362 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 }
1364
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001365 return( 0 );
1366}
1367
1368/*
1369 * Parse one or more PEM certificates from a buffer and add them to the chained list
1370 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001371int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001372{
Paul Bakker69e095c2011-12-10 21:55:01 +00001373 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001374 x509_cert *crt, *prev = NULL;
1375 int buf_format = X509_FORMAT_DER;
1376
1377 crt = chain;
1378
1379 /*
1380 * Check for valid input
1381 */
1382 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001383 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001384
1385 while( crt->version != 0 && crt->next != NULL )
1386 {
1387 prev = crt;
1388 crt = crt->next;
1389 }
1390
1391 /*
1392 * Add new certificate on the end of the chain if needed.
1393 */
1394 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001395 {
1396 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1397
Paul Bakker7d06ad22009-05-02 15:53:56 +00001398 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001399 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001400
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001401 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001402 crt = crt->next;
1403 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001406 /*
1407 * Determine buffer content. Buffer contains either one DER certificate or
1408 * one or more PEM certificates.
1409 */
1410#if defined(POLARSSL_PEM_C)
1411 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1412 buf_format = X509_FORMAT_PEM;
1413#endif
1414
1415 if( buf_format == X509_FORMAT_DER )
1416 return x509parse_crt_der( crt, buf, buflen );
1417
1418#if defined(POLARSSL_PEM_C)
1419 if( buf_format == X509_FORMAT_PEM )
1420 {
1421 pem_context pem;
1422
1423 while( buflen > 0 )
1424 {
1425 size_t use_len;
1426 pem_init( &pem );
1427
1428 ret = pem_read_buffer( &pem,
1429 "-----BEGIN CERTIFICATE-----",
1430 "-----END CERTIFICATE-----",
1431 buf, NULL, 0, &use_len );
1432
1433 if( ret == 0 )
1434 {
1435 /*
1436 * Was PEM encoded
1437 */
1438 buflen -= use_len;
1439 buf += use_len;
1440 }
1441 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1442 {
1443 pem_free( &pem );
1444
1445 if( first_error == 0 )
1446 first_error = ret;
1447
1448 continue;
1449 }
1450 else
1451 break;
1452
1453 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1454
1455 pem_free( &pem );
1456
1457 if( ret != 0 )
1458 {
1459 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001460 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001461 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001462 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001463 {
1464 if( prev )
1465 prev->next = NULL;
1466
1467 if( crt != chain )
1468 free( crt );
1469
1470 return( ret );
1471 }
1472
1473 if( first_error == 0 )
1474 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001475
1476 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001477
1478 memset( crt, 0, sizeof( x509_cert ) );
1479 continue;
1480 }
1481
1482 success = 1;
1483
1484 /*
1485 * Add new certificate to the list
1486 */
1487 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1488
1489 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001490 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001491
1492 prev = crt;
1493 crt = crt->next;
1494 memset( crt, 0, sizeof( x509_cert ) );
1495 }
1496 }
1497#endif
1498
1499 if( crt->version == 0 )
1500 {
1501 if( prev )
1502 prev->next = NULL;
1503
1504 if( crt != chain )
1505 free( crt );
1506 }
1507
1508 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001509 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001510 else if( first_error )
1511 return( first_error );
1512 else
1513 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514}
1515
1516/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001517 * Parse one or more CRLs and add them to the chained list
1518 */
Paul Bakker23986e52011-04-24 08:57:21 +00001519int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001520{
Paul Bakker23986e52011-04-24 08:57:21 +00001521 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001522 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001523 unsigned char *p, *end;
1524 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001525#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001526 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001527 pem_context pem;
1528#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001529
1530 crl = chain;
1531
1532 /*
1533 * Check for valid input
1534 */
1535 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001536 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001537
1538 while( crl->version != 0 && crl->next != NULL )
1539 crl = crl->next;
1540
1541 /*
1542 * Add new CRL on the end of the chain if needed.
1543 */
1544 if ( crl->version != 0 && crl->next == NULL)
1545 {
1546 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1547
Paul Bakker7d06ad22009-05-02 15:53:56 +00001548 if( crl->next == NULL )
1549 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001550 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001551 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001552 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001553
Paul Bakker7d06ad22009-05-02 15:53:56 +00001554 crl = crl->next;
1555 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001556 }
1557
Paul Bakker96743fc2011-02-12 14:30:57 +00001558#if defined(POLARSSL_PEM_C)
1559 pem_init( &pem );
1560 ret = pem_read_buffer( &pem,
1561 "-----BEGIN X509 CRL-----",
1562 "-----END X509 CRL-----",
1563 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001564
Paul Bakker96743fc2011-02-12 14:30:57 +00001565 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001566 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001567 /*
1568 * Was PEM encoded
1569 */
1570 buflen -= use_len;
1571 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001572
1573 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001574 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001575 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001576 p = pem.buf;
1577 pem.buf = NULL;
1578 len = pem.buflen;
1579 pem_free( &pem );
1580 }
1581 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1582 {
1583 pem_free( &pem );
1584 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585 }
1586 else
1587 {
1588 /*
1589 * nope, copy the raw DER data
1590 */
1591 p = (unsigned char *) malloc( len = buflen );
1592
1593 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001594 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001595
1596 memcpy( p, buf, buflen );
1597
1598 buflen = 0;
1599 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001600#else
1601 p = (unsigned char *) malloc( len = buflen );
1602
1603 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001604 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001605
1606 memcpy( p, buf, buflen );
1607
1608 buflen = 0;
1609#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001610
1611 crl->raw.p = p;
1612 crl->raw.len = len;
1613 end = p + len;
1614
1615 /*
1616 * CertificateList ::= SEQUENCE {
1617 * tbsCertList TBSCertList,
1618 * signatureAlgorithm AlgorithmIdentifier,
1619 * signatureValue BIT STRING }
1620 */
1621 if( ( ret = asn1_get_tag( &p, end, &len,
1622 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1623 {
1624 x509_crl_free( crl );
1625 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1626 }
1627
Paul Bakker23986e52011-04-24 08:57:21 +00001628 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001629 {
1630 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001631 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001632 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1633 }
1634
1635 /*
1636 * TBSCertList ::= SEQUENCE {
1637 */
1638 crl->tbs.p = p;
1639
1640 if( ( ret = asn1_get_tag( &p, end, &len,
1641 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1642 {
1643 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001644 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001645 }
1646
1647 end = p + len;
1648 crl->tbs.len = end - crl->tbs.p;
1649
1650 /*
1651 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1652 * -- if present, MUST be v2
1653 *
1654 * signature AlgorithmIdentifier
1655 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001656 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001657 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1658 {
1659 x509_crl_free( crl );
1660 return( ret );
1661 }
1662
1663 crl->version++;
1664
1665 if( crl->version > 2 )
1666 {
1667 x509_crl_free( crl );
1668 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1669 }
1670
Paul Bakker27d66162010-03-17 06:56:01 +00001671 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 {
1673 x509_crl_free( crl );
1674 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1675 }
1676
1677 /*
1678 * issuer Name
1679 */
1680 crl->issuer_raw.p = p;
1681
1682 if( ( ret = asn1_get_tag( &p, end, &len,
1683 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1684 {
1685 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001686 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687 }
1688
1689 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1690 {
1691 x509_crl_free( crl );
1692 return( ret );
1693 }
1694
1695 crl->issuer_raw.len = p - crl->issuer_raw.p;
1696
1697 /*
1698 * thisUpdate Time
1699 * nextUpdate Time OPTIONAL
1700 */
Paul Bakker91200182010-02-18 21:26:15 +00001701 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001702 {
1703 x509_crl_free( crl );
1704 return( ret );
1705 }
1706
Paul Bakker91200182010-02-18 21:26:15 +00001707 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001708 {
Paul Bakker9d781402011-05-09 16:17:09 +00001709 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001710 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001711 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001712 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001713 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001714 x509_crl_free( crl );
1715 return( ret );
1716 }
1717 }
1718
1719 /*
1720 * revokedCertificates SEQUENCE OF SEQUENCE {
1721 * userCertificate CertificateSerialNumber,
1722 * revocationDate Time,
1723 * crlEntryExtensions Extensions OPTIONAL
1724 * -- if present, MUST be v2
1725 * } OPTIONAL
1726 */
1727 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1728 {
1729 x509_crl_free( crl );
1730 return( ret );
1731 }
1732
1733 /*
1734 * crlExtensions EXPLICIT Extensions OPTIONAL
1735 * -- if present, MUST be v2
1736 */
1737 if( crl->version == 2 )
1738 {
1739 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1740
1741 if( ret != 0 )
1742 {
1743 x509_crl_free( crl );
1744 return( ret );
1745 }
1746 }
1747
1748 if( p != end )
1749 {
1750 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001751 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1753 }
1754
1755 end = crl->raw.p + crl->raw.len;
1756
1757 /*
1758 * signatureAlgorithm AlgorithmIdentifier,
1759 * signatureValue BIT STRING
1760 */
1761 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1762 {
1763 x509_crl_free( crl );
1764 return( ret );
1765 }
1766
1767 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1768 {
1769 x509_crl_free( crl );
1770 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1771 }
1772
1773 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1774 {
1775 x509_crl_free( crl );
1776 return( ret );
1777 }
1778
1779 if( p != end )
1780 {
1781 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001782 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001783 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1784 }
1785
1786 if( buflen > 0 )
1787 {
1788 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1789
Paul Bakker7d06ad22009-05-02 15:53:56 +00001790 if( crl->next == NULL )
1791 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001792 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001793 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001794 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001795
Paul Bakker7d06ad22009-05-02 15:53:56 +00001796 crl = crl->next;
1797 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001798
1799 return( x509parse_crl( crl, buf, buflen ) );
1800 }
1801
1802 return( 0 );
1803}
1804
Paul Bakker335db3f2011-04-25 15:28:35 +00001805#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001806/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001807 * Load all data from a file into a given buffer.
1808 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001809int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001810{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001811 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001812
Paul Bakkerd98030e2009-05-02 15:13:40 +00001813 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001814 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001815
Paul Bakkerd98030e2009-05-02 15:13:40 +00001816 fseek( f, 0, SEEK_END );
1817 *n = (size_t) ftell( f );
1818 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001819
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001821 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001822
Paul Bakkerd98030e2009-05-02 15:13:40 +00001823 if( fread( *buf, 1, *n, f ) != *n )
1824 {
1825 fclose( f );
1826 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001827 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001828 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001829
Paul Bakkerd98030e2009-05-02 15:13:40 +00001830 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001831
Paul Bakkerd98030e2009-05-02 15:13:40 +00001832 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001833
Paul Bakkerd98030e2009-05-02 15:13:40 +00001834 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001835}
1836
1837/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 * Load one or more certificates and add them to the chained list
1839 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001840int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001841{
1842 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 size_t n;
1844 unsigned char *buf;
1845
Paul Bakker69e095c2011-12-10 21:55:01 +00001846 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1847 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Paul Bakker69e095c2011-12-10 21:55:01 +00001849 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
1851 memset( buf, 0, n + 1 );
1852 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
1854 return( ret );
1855}
1856
Paul Bakkerd98030e2009-05-02 15:13:40 +00001857/*
1858 * Load one or more CRLs and add them to the chained list
1859 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001860int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001861{
1862 int ret;
1863 size_t n;
1864 unsigned char *buf;
1865
Paul Bakker69e095c2011-12-10 21:55:01 +00001866 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1867 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001868
Paul Bakker27fdf462011-06-09 13:55:13 +00001869 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001870
1871 memset( buf, 0, n + 1 );
1872 free( buf );
1873
1874 return( ret );
1875}
1876
Paul Bakker5121ce52009-01-03 21:22:43 +00001877/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001878 * Load and parse a private RSA key
1879 */
1880int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1881{
1882 int ret;
1883 size_t n;
1884 unsigned char *buf;
1885
Paul Bakker69e095c2011-12-10 21:55:01 +00001886 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1887 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001888
1889 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001890 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001891 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001892 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001893 (unsigned char *) pwd, strlen( pwd ) );
1894
1895 memset( buf, 0, n + 1 );
1896 free( buf );
1897
1898 return( ret );
1899}
1900
1901/*
1902 * Load and parse a public RSA key
1903 */
1904int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1905{
1906 int ret;
1907 size_t n;
1908 unsigned char *buf;
1909
Paul Bakker69e095c2011-12-10 21:55:01 +00001910 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1911 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001912
Paul Bakker27fdf462011-06-09 13:55:13 +00001913 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001914
1915 memset( buf, 0, n + 1 );
1916 free( buf );
1917
1918 return( ret );
1919}
1920#endif /* POLARSSL_FS_IO */
1921
1922/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 * Parse a private RSA key
1924 */
Paul Bakker23986e52011-04-24 08:57:21 +00001925int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1926 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001927{
Paul Bakker23986e52011-04-24 08:57:21 +00001928 int ret;
1929 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001931 unsigned char *p_alt;
1932 x509_buf pk_alg_oid;
1933
Paul Bakker96743fc2011-02-12 14:30:57 +00001934#if defined(POLARSSL_PEM_C)
1935 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
Paul Bakker96743fc2011-02-12 14:30:57 +00001937 pem_init( &pem );
1938 ret = pem_read_buffer( &pem,
1939 "-----BEGIN RSA PRIVATE KEY-----",
1940 "-----END RSA PRIVATE KEY-----",
1941 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
Paul Bakkered56b222011-07-13 11:26:43 +00001943 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1944 {
1945 ret = pem_read_buffer( &pem,
1946 "-----BEGIN PRIVATE KEY-----",
1947 "-----END PRIVATE KEY-----",
1948 key, pwd, pwdlen, &len );
1949 }
1950
Paul Bakker96743fc2011-02-12 14:30:57 +00001951 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001953 /*
1954 * Was PEM encoded
1955 */
1956 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001958 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001959 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001960 pem_free( &pem );
1961 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001963
Paul Bakker96743fc2011-02-12 14:30:57 +00001964 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1965#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001966 ((void) pwd);
1967 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001968 p = (unsigned char *) key;
1969#endif
1970 end = p + keylen;
1971
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001973 * Note: Depending on the type of private key file one can expect either a
1974 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1975 *
1976 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001977 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001978 * algorithm AlgorithmIdentifier,
1979 * PrivateKey BIT STRING
1980 * }
1981 *
1982 * AlgorithmIdentifier ::= SEQUENCE {
1983 * algorithm OBJECT IDENTIFIER,
1984 * parameters ANY DEFINED BY algorithm OPTIONAL
1985 * }
1986 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 * RSAPrivateKey ::= SEQUENCE {
1988 * version Version,
1989 * modulus INTEGER, -- n
1990 * publicExponent INTEGER, -- e
1991 * privateExponent INTEGER, -- d
1992 * prime1 INTEGER, -- p
1993 * prime2 INTEGER, -- q
1994 * exponent1 INTEGER, -- d mod (p-1)
1995 * exponent2 INTEGER, -- d mod (q-1)
1996 * coefficient INTEGER, -- (inverse of q) mod p
1997 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1998 * }
1999 */
2000 if( ( ret = asn1_get_tag( &p, end, &len,
2001 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2002 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002003#if defined(POLARSSL_PEM_C)
2004 pem_free( &pem );
2005#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002007 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 }
2009
2010 end = p + len;
2011
2012 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2013 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002014#if defined(POLARSSL_PEM_C)
2015 pem_free( &pem );
2016#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002018 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 }
2020
2021 if( rsa->ver != 0 )
2022 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002023#if defined(POLARSSL_PEM_C)
2024 pem_free( &pem );
2025#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002027 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
2029
Paul Bakkered56b222011-07-13 11:26:43 +00002030 p_alt = p;
2031
2032 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2033 {
2034 // Assume that we have the PKCS#1 format if wrong
2035 // tag was encountered
2036 //
2037 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2038 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2039 {
2040#if defined(POLARSSL_PEM_C)
2041 pem_free( &pem );
2042#endif
2043 rsa_free( rsa );
2044 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2045 }
2046 }
2047 else
2048 {
2049 int can_handle;
2050
2051 /*
2052 * only RSA keys handled at this time
2053 */
2054 can_handle = 0;
2055
2056 if( pk_alg_oid.len == 9 &&
2057 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2058 can_handle = 1;
2059
2060 if( pk_alg_oid.len == 9 &&
2061 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2062 {
2063 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2064 can_handle = 1;
2065
2066 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2067 can_handle = 1;
2068 }
2069
2070 if( pk_alg_oid.len == 5 &&
2071 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2072 can_handle = 1;
2073
2074 if( can_handle == 0 )
2075 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2076
2077 /*
2078 * Parse the PKCS#8 format
2079 */
2080
2081 p = p_alt;
2082 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2083 {
2084#if defined(POLARSSL_PEM_C)
2085 pem_free( &pem );
2086#endif
2087 rsa_free( rsa );
2088 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2089 }
2090
2091 if( ( end - p ) < 1 )
2092 {
2093#if defined(POLARSSL_PEM_C)
2094 pem_free( &pem );
2095#endif
2096 rsa_free( rsa );
2097 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2098 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2099 }
2100
2101 end = p + len;
2102
2103 if( ( ret = asn1_get_tag( &p, end, &len,
2104 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2105 {
2106#if defined(POLARSSL_PEM_C)
2107 pem_free( &pem );
2108#endif
2109 rsa_free( rsa );
2110 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2111 }
2112
2113 end = p + len;
2114
2115 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2116 {
2117#if defined(POLARSSL_PEM_C)
2118 pem_free( &pem );
2119#endif
2120 rsa_free( rsa );
2121 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2122 }
2123
2124 if( rsa->ver != 0 )
2125 {
2126#if defined(POLARSSL_PEM_C)
2127 pem_free( &pem );
2128#endif
2129 rsa_free( rsa );
2130 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2131 }
2132 }
2133
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2135 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2136 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2137 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2138 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2139 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2140 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2141 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2142 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002143#if defined(POLARSSL_PEM_C)
2144 pem_free( &pem );
2145#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002147 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 }
2149
2150 rsa->len = mpi_size( &rsa->N );
2151
2152 if( p != end )
2153 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002154#if defined(POLARSSL_PEM_C)
2155 pem_free( &pem );
2156#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002158 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002159 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 }
2161
2162 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2163 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002164#if defined(POLARSSL_PEM_C)
2165 pem_free( &pem );
2166#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 rsa_free( rsa );
2168 return( ret );
2169 }
2170
Paul Bakker96743fc2011-02-12 14:30:57 +00002171#if defined(POLARSSL_PEM_C)
2172 pem_free( &pem );
2173#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002174
2175 return( 0 );
2176}
2177
2178/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002179 * Parse a public RSA key
2180 */
Paul Bakker23986e52011-04-24 08:57:21 +00002181int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002182{
Paul Bakker23986e52011-04-24 08:57:21 +00002183 int ret;
2184 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002185 unsigned char *p, *end;
2186 x509_buf alg_oid;
2187#if defined(POLARSSL_PEM_C)
2188 pem_context pem;
2189
2190 pem_init( &pem );
2191 ret = pem_read_buffer( &pem,
2192 "-----BEGIN PUBLIC KEY-----",
2193 "-----END PUBLIC KEY-----",
2194 key, NULL, 0, &len );
2195
2196 if( ret == 0 )
2197 {
2198 /*
2199 * Was PEM encoded
2200 */
2201 keylen = pem.buflen;
2202 }
2203 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2204 {
2205 pem_free( &pem );
2206 return( ret );
2207 }
2208
2209 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2210#else
2211 p = (unsigned char *) key;
2212#endif
2213 end = p + keylen;
2214
2215 /*
2216 * PublicKeyInfo ::= SEQUENCE {
2217 * algorithm AlgorithmIdentifier,
2218 * PublicKey BIT STRING
2219 * }
2220 *
2221 * AlgorithmIdentifier ::= SEQUENCE {
2222 * algorithm OBJECT IDENTIFIER,
2223 * parameters ANY DEFINED BY algorithm OPTIONAL
2224 * }
2225 *
2226 * RSAPublicKey ::= SEQUENCE {
2227 * modulus INTEGER, -- n
2228 * publicExponent INTEGER -- e
2229 * }
2230 */
2231
2232 if( ( ret = asn1_get_tag( &p, end, &len,
2233 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2234 {
2235#if defined(POLARSSL_PEM_C)
2236 pem_free( &pem );
2237#endif
2238 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002239 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002240 }
2241
2242 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2243 {
2244#if defined(POLARSSL_PEM_C)
2245 pem_free( &pem );
2246#endif
2247 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002248 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002249 }
2250
2251 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2252 {
2253#if defined(POLARSSL_PEM_C)
2254 pem_free( &pem );
2255#endif
2256 rsa_free( rsa );
2257 return( ret );
2258 }
2259
2260 rsa->len = mpi_size( &rsa->N );
2261
2262#if defined(POLARSSL_PEM_C)
2263 pem_free( &pem );
2264#endif
2265
2266 return( 0 );
2267}
2268
Paul Bakkereaa89f82011-04-04 21:36:15 +00002269#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002270/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002271 * Parse DHM parameters
2272 */
Paul Bakker23986e52011-04-24 08:57:21 +00002273int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002274{
Paul Bakker23986e52011-04-24 08:57:21 +00002275 int ret;
2276 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002277 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002278#if defined(POLARSSL_PEM_C)
2279 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002280
Paul Bakker96743fc2011-02-12 14:30:57 +00002281 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002282
Paul Bakker96743fc2011-02-12 14:30:57 +00002283 ret = pem_read_buffer( &pem,
2284 "-----BEGIN DH PARAMETERS-----",
2285 "-----END DH PARAMETERS-----",
2286 dhmin, NULL, 0, &dhminlen );
2287
2288 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002289 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002290 /*
2291 * Was PEM encoded
2292 */
2293 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002294 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002295 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002296 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002297 pem_free( &pem );
2298 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002299 }
2300
Paul Bakker96743fc2011-02-12 14:30:57 +00002301 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2302#else
2303 p = (unsigned char *) dhmin;
2304#endif
2305 end = p + dhminlen;
2306
Paul Bakker1b57b062011-01-06 15:48:19 +00002307 memset( dhm, 0, sizeof( dhm_context ) );
2308
Paul Bakker1b57b062011-01-06 15:48:19 +00002309 /*
2310 * DHParams ::= SEQUENCE {
2311 * prime INTEGER, -- P
2312 * generator INTEGER, -- g
2313 * }
2314 */
2315 if( ( ret = asn1_get_tag( &p, end, &len,
2316 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2317 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002318#if defined(POLARSSL_PEM_C)
2319 pem_free( &pem );
2320#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002321 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002322 }
2323
2324 end = p + len;
2325
2326 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2327 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2328 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002329#if defined(POLARSSL_PEM_C)
2330 pem_free( &pem );
2331#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002332 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002333 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002334 }
2335
2336 if( p != end )
2337 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002338#if defined(POLARSSL_PEM_C)
2339 pem_free( &pem );
2340#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002341 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002342 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002343 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2344 }
2345
Paul Bakker96743fc2011-02-12 14:30:57 +00002346#if defined(POLARSSL_PEM_C)
2347 pem_free( &pem );
2348#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002349
2350 return( 0 );
2351}
2352
Paul Bakker335db3f2011-04-25 15:28:35 +00002353#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002354/*
2355 * Load and parse a private RSA key
2356 */
2357int x509parse_dhmfile( dhm_context *dhm, const char *path )
2358{
2359 int ret;
2360 size_t n;
2361 unsigned char *buf;
2362
Paul Bakker69e095c2011-12-10 21:55:01 +00002363 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2364 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002365
Paul Bakker27fdf462011-06-09 13:55:13 +00002366 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002367
2368 memset( buf, 0, n + 1 );
2369 free( buf );
2370
2371 return( ret );
2372}
Paul Bakker335db3f2011-04-25 15:28:35 +00002373#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002374#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002375
Paul Bakker5121ce52009-01-03 21:22:43 +00002376#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002377#include <stdarg.h>
2378
2379#if !defined vsnprintf
2380#define vsnprintf _vsnprintf
2381#endif // vsnprintf
2382
2383/*
2384 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2385 * Result value is not size of buffer needed, but -1 if no fit is possible.
2386 *
2387 * This fuction tries to 'fix' this by at least suggesting enlarging the
2388 * size by 20.
2389 */
2390int compat_snprintf(char *str, size_t size, const char *format, ...)
2391{
2392 va_list ap;
2393 int res = -1;
2394
2395 va_start( ap, format );
2396
2397 res = vsnprintf( str, size, format, ap );
2398
2399 va_end( ap );
2400
2401 // No quick fix possible
2402 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002403 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002404
2405 return res;
2406}
2407
2408#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002409#endif
2410
Paul Bakkerd98030e2009-05-02 15:13:40 +00002411#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2412
2413#define SAFE_SNPRINTF() \
2414{ \
2415 if( ret == -1 ) \
2416 return( -1 ); \
2417 \
Paul Bakker23986e52011-04-24 08:57:21 +00002418 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002419 p[n - 1] = '\0'; \
2420 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2421 } \
2422 \
Paul Bakker23986e52011-04-24 08:57:21 +00002423 n -= (unsigned int) ret; \
2424 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002425}
2426
Paul Bakker5121ce52009-01-03 21:22:43 +00002427/*
2428 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002429 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002431int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002432{
Paul Bakker23986e52011-04-24 08:57:21 +00002433 int ret;
2434 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002436 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 char s[128], *p;
2438
2439 memset( s, 0, sizeof( s ) );
2440
2441 name = dn;
2442 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002443 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 while( name != NULL )
2446 {
Paul Bakker74111d32011-01-15 16:57:55 +00002447 if( name != dn )
2448 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002449 ret = snprintf( p, n, ", " );
2450 SAFE_SNPRINTF();
2451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
2453 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2454 {
2455 switch( name->oid.p[2] )
2456 {
2457 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002458 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002459
2460 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002461 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
2463 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002464 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
2466 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002467 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002470 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
2472 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002473 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
2475 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002476 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 name->oid.p[2] );
2478 break;
2479 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002480 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 }
2482 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2483 {
2484 switch( name->oid.p[8] )
2485 {
2486 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002487 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
2489 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002490 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 name->oid.p[8] );
2492 break;
2493 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002494 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 }
2496 else
Paul Bakker74111d32011-01-15 16:57:55 +00002497 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002498 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002499 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
2502 for( i = 0; i < name->val.len; i++ )
2503 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002504 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 break;
2506
2507 c = name->val.p[i];
2508 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2509 s[i] = '?';
2510 else s[i] = c;
2511 }
2512 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002513 ret = snprintf( p, n, "%s", s );
2514 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 name = name->next;
2516 }
2517
Paul Bakker23986e52011-04-24 08:57:21 +00002518 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002519}
2520
2521/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002522 * Store the serial in printable form into buf; no more
2523 * than size characters will be written
2524 */
2525int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2526{
Paul Bakker23986e52011-04-24 08:57:21 +00002527 int ret;
2528 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002529 char *p;
2530
2531 p = buf;
2532 n = size;
2533
2534 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002535 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002536
2537 for( i = 0; i < nr; i++ )
2538 {
Paul Bakker93048802011-12-05 14:38:06 +00002539 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002540 continue;
2541
Paul Bakkerdd476992011-01-16 21:34:59 +00002542 ret = snprintf( p, n, "%02X%s",
2543 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2544 SAFE_SNPRINTF();
2545 }
2546
Paul Bakker03c7c252011-11-25 12:37:37 +00002547 if( nr != serial->len )
2548 {
2549 ret = snprintf( p, n, "...." );
2550 SAFE_SNPRINTF();
2551 }
2552
Paul Bakker23986e52011-04-24 08:57:21 +00002553 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002554}
2555
2556/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002557 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002559int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2560 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002561{
Paul Bakker23986e52011-04-24 08:57:21 +00002562 int ret;
2563 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002564 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
2566 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002567 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
Paul Bakkerd98030e2009-05-02 15:13:40 +00002569 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002571 SAFE_SNPRINTF();
2572 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002574 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Paul Bakkerdd476992011-01-16 21:34:59 +00002576 ret = x509parse_serial_gets( p, n, &crt->serial);
2577 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Paul Bakkerd98030e2009-05-02 15:13:40 +00002579 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2580 SAFE_SNPRINTF();
2581 ret = x509parse_dn_gets( p, n, &crt->issuer );
2582 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Paul Bakkerd98030e2009-05-02 15:13:40 +00002584 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2585 SAFE_SNPRINTF();
2586 ret = x509parse_dn_gets( p, n, &crt->subject );
2587 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Paul Bakkerd98030e2009-05-02 15:13:40 +00002589 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2591 crt->valid_from.year, crt->valid_from.mon,
2592 crt->valid_from.day, crt->valid_from.hour,
2593 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002594 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Paul Bakkerd98030e2009-05-02 15:13:40 +00002596 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2598 crt->valid_to.year, crt->valid_to.mon,
2599 crt->valid_to.day, crt->valid_to.hour,
2600 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002601 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Paul Bakkerd98030e2009-05-02 15:13:40 +00002603 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2604 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Paul Bakker27d66162010-03-17 06:56:01 +00002606 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002608 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2609 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2610 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2611 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2612 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2613 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2614 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2615 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2616 default: ret = snprintf( p, n, "???" ); break;
2617 }
2618 SAFE_SNPRINTF();
2619
2620 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002621 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002622 SAFE_SNPRINTF();
2623
Paul Bakker23986e52011-04-24 08:57:21 +00002624 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002625}
2626
Paul Bakker74111d32011-01-15 16:57:55 +00002627/* Compare a given OID string with an OID x509_buf * */
2628#define OID_CMP(oid_str, oid_buf) \
2629 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2630 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2631
2632/*
2633 * Return an informational string describing the given OID
2634 */
2635const char *x509_oid_get_description( x509_buf *oid )
2636{
2637 if ( oid == NULL )
2638 return ( NULL );
2639
2640 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2641 return( STRING_SERVER_AUTH );
2642
2643 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2644 return( STRING_CLIENT_AUTH );
2645
2646 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2647 return( STRING_CODE_SIGNING );
2648
2649 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2650 return( STRING_EMAIL_PROTECTION );
2651
2652 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2653 return( STRING_TIME_STAMPING );
2654
2655 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2656 return( STRING_OCSP_SIGNING );
2657
2658 return( NULL );
2659}
2660
2661/* Return the x.y.z.... style numeric string for the given OID */
2662int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2663{
Paul Bakker23986e52011-04-24 08:57:21 +00002664 int ret;
2665 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002666 unsigned int value;
2667 char *p;
2668
2669 p = buf;
2670 n = size;
2671
2672 /* First byte contains first two dots */
2673 if( oid->len > 0 )
2674 {
2675 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2676 SAFE_SNPRINTF();
2677 }
2678
2679 /* TODO: value can overflow in value. */
2680 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002681 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002682 {
2683 value <<= 7;
2684 value += oid->p[i] & 0x7F;
2685
2686 if( !( oid->p[i] & 0x80 ) )
2687 {
2688 /* Last byte */
2689 ret = snprintf( p, n, ".%d", value );
2690 SAFE_SNPRINTF();
2691 value = 0;
2692 }
2693 }
2694
Paul Bakker23986e52011-04-24 08:57:21 +00002695 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002696}
2697
Paul Bakkerd98030e2009-05-02 15:13:40 +00002698/*
2699 * Return an informational string about the CRL.
2700 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002701int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2702 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002703{
Paul Bakker23986e52011-04-24 08:57:21 +00002704 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002705 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002706 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002707 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002708
2709 p = buf;
2710 n = size;
2711
2712 ret = snprintf( p, n, "%sCRL version : %d",
2713 prefix, crl->version );
2714 SAFE_SNPRINTF();
2715
2716 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2717 SAFE_SNPRINTF();
2718 ret = x509parse_dn_gets( p, n, &crl->issuer );
2719 SAFE_SNPRINTF();
2720
2721 ret = snprintf( p, n, "\n%sthis update : " \
2722 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2723 crl->this_update.year, crl->this_update.mon,
2724 crl->this_update.day, crl->this_update.hour,
2725 crl->this_update.min, crl->this_update.sec );
2726 SAFE_SNPRINTF();
2727
2728 ret = snprintf( p, n, "\n%snext update : " \
2729 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2730 crl->next_update.year, crl->next_update.mon,
2731 crl->next_update.day, crl->next_update.hour,
2732 crl->next_update.min, crl->next_update.sec );
2733 SAFE_SNPRINTF();
2734
2735 entry = &crl->entry;
2736
2737 ret = snprintf( p, n, "\n%sRevoked certificates:",
2738 prefix );
2739 SAFE_SNPRINTF();
2740
Paul Bakker9be19372009-07-27 20:21:53 +00002741 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002742 {
2743 ret = snprintf( p, n, "\n%sserial number: ",
2744 prefix );
2745 SAFE_SNPRINTF();
2746
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002747 ret = x509parse_serial_gets( p, n, &entry->serial);
2748 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002749
Paul Bakkerd98030e2009-05-02 15:13:40 +00002750 ret = snprintf( p, n, " revocation date: " \
2751 "%04d-%02d-%02d %02d:%02d:%02d",
2752 entry->revocation_date.year, entry->revocation_date.mon,
2753 entry->revocation_date.day, entry->revocation_date.hour,
2754 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002755 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002756
2757 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 }
2759
Paul Bakkerd98030e2009-05-02 15:13:40 +00002760 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2761 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Paul Bakker27d66162010-03-17 06:56:01 +00002763 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002764 {
2765 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2766 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2767 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2768 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2769 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2770 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2771 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2772 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2773 default: ret = snprintf( p, n, "???" ); break;
2774 }
2775 SAFE_SNPRINTF();
2776
Paul Bakker1e27bb22009-07-19 20:25:25 +00002777 ret = snprintf( p, n, "\n" );
2778 SAFE_SNPRINTF();
2779
Paul Bakker23986e52011-04-24 08:57:21 +00002780 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781}
2782
2783/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002784 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002786int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002787{
Paul Bakkercce9d772011-11-18 14:26:47 +00002788 int year, mon, day;
2789 int hour, min, sec;
2790
2791#if defined(_WIN32)
2792 SYSTEMTIME st;
2793
2794 GetLocalTime(&st);
2795
2796 year = st.wYear;
2797 mon = st.wMonth;
2798 day = st.wDay;
2799 hour = st.wHour;
2800 min = st.wMinute;
2801 sec = st.wSecond;
2802#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 struct tm *lt;
2804 time_t tt;
2805
2806 tt = time( NULL );
2807 lt = localtime( &tt );
2808
Paul Bakkercce9d772011-11-18 14:26:47 +00002809 year = lt->tm_year + 1900;
2810 mon = lt->tm_mon + 1;
2811 day = lt->tm_mday;
2812 hour = lt->tm_hour;
2813 min = lt->tm_min;
2814 sec = lt->tm_sec;
2815#endif
2816
2817 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002818 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Paul Bakkercce9d772011-11-18 14:26:47 +00002820 if( year == to->year &&
2821 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002822 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
Paul Bakkercce9d772011-11-18 14:26:47 +00002824 if( year == to->year &&
2825 mon == to->mon &&
2826 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002827 return( 1 );
2828
Paul Bakkercce9d772011-11-18 14:26:47 +00002829 if( year == to->year &&
2830 mon == to->mon &&
2831 day == to->day &&
2832 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002833 return( 1 );
2834
Paul Bakkercce9d772011-11-18 14:26:47 +00002835 if( year == to->year &&
2836 mon == to->mon &&
2837 day == to->day &&
2838 hour == to->hour &&
2839 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002840 return( 1 );
2841
Paul Bakkercce9d772011-11-18 14:26:47 +00002842 if( year == to->year &&
2843 mon == to->mon &&
2844 day == to->day &&
2845 hour == to->hour &&
2846 min == to->min &&
2847 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002848 return( 1 );
2849
Paul Bakker40ea7de2009-05-03 10:18:48 +00002850 return( 0 );
2851}
2852
2853/*
2854 * Return 1 if the certificate is revoked, or 0 otherwise.
2855 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002856int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002857{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002858 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002859
2860 while( cur != NULL && cur->serial.len != 0 )
2861 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002862 if( crt->serial.len == cur->serial.len &&
2863 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002864 {
2865 if( x509parse_time_expired( &cur->revocation_date ) )
2866 return( 1 );
2867 }
2868
2869 cur = cur->next;
2870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
2872 return( 0 );
2873}
2874
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002875/*
2876 * Wrapper for x509 hashes.
2877 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002878 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002879 */
Paul Bakker23986e52011-04-24 08:57:21 +00002880static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 unsigned char *out )
2882{
2883 switch( alg )
2884 {
Paul Bakker40e46942009-01-03 21:51:57 +00002885#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002886 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002887#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002888#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002889 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002890#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002891#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002892 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002893#endif
2894#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002895 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002896#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002897#if defined(POLARSSL_SHA2_C)
2898 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2899 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2900#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002901#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002902 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2903 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2904#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002906 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 break;
2908 }
2909}
2910
2911/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002912 * Check that the given certificate is valid accoring to the CRL.
2913 */
2914static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2915 x509_crl *crl_list)
2916{
2917 int flags = 0;
2918 int hash_id;
2919 unsigned char hash[64];
2920
2921 /*
2922 * TODO: What happens if no CRL is present?
2923 * Suggestion: Revocation state should be unknown if no CRL is present.
2924 * For backwards compatibility this is not yet implemented.
2925 */
2926
2927 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2928 {
2929 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2930 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2931 crl_list->issuer_raw.len ) != 0 )
2932 {
2933 crl_list = crl_list->next;
2934 continue;
2935 }
2936
2937 /*
2938 * Check if CRL is correctly signed by the trusted CA
2939 */
2940 hash_id = crl_list->sig_alg;
2941
2942 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2943
2944 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2945 0, hash, crl_list->sig.p ) == 0 )
2946 {
2947 /*
2948 * CRL is not trusted
2949 */
2950 flags |= BADCRL_NOT_TRUSTED;
2951 break;
2952 }
2953
2954 /*
2955 * Check for validity of CRL (Do not drop out)
2956 */
2957 if( x509parse_time_expired( &crl_list->next_update ) )
2958 flags |= BADCRL_EXPIRED;
2959
2960 /*
2961 * Check if certificate is revoked
2962 */
2963 if( x509parse_revoked(crt, crl_list) )
2964 {
2965 flags |= BADCERT_REVOKED;
2966 break;
2967 }
2968
2969 crl_list = crl_list->next;
2970 }
2971 return flags;
2972}
2973
Paul Bakkera8cd2392012-02-11 16:09:32 +00002974int x509_wildcard_verify( const char *cn, x509_name *name )
2975{
2976 size_t i;
2977 size_t cn_idx = 0;
2978
2979 if( name->val.len < 3 || name->val.p[0] != '*' || name->val.p[1] != '.' )
2980 return( 0 );
2981
2982 for( i = 0; i < strlen( cn ); ++i )
2983 {
2984 if( cn[i] == '.' )
2985 {
2986 cn_idx = i;
2987 break;
2988 }
2989 }
2990
2991 if( cn_idx == 0 )
2992 return( 0 );
2993
2994 if( memcmp( name->val.p + 1, cn + cn_idx, name->val.len - 1 ) == 0 &&
2995 strlen( cn ) - cn_idx == name->val.len - 1 )
2996 {
2997 return( 1 );
2998 }
2999
3000 return( 0 );
3001}
3002
Paul Bakker76fd75a2011-01-16 21:12:10 +00003003/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 * Verify the certificate validity
3005 */
3006int x509parse_verify( x509_cert *crt,
3007 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003008 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003009 const char *cn, int *flags,
3010 int (*f_vrfy)(void *, x509_cert *, int, int),
3011 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003012{
Paul Bakker23986e52011-04-24 08:57:21 +00003013 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 int hash_id;
3015 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003016 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00003018 unsigned char hash[64];
Paul Bakkera8cd2392012-02-11 16:09:32 +00003019 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Paul Bakker40ea7de2009-05-03 10:18:48 +00003021 *flags = 0;
3022
3023 if( x509parse_time_expired( &crt->valid_to ) )
3024 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003025
3026 if( cn != NULL )
3027 {
3028 name = &crt->subject;
3029 cn_len = strlen( cn );
3030
3031 while( name != NULL )
3032 {
Paul Bakkera8cd2392012-02-11 16:09:32 +00003033 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 )
3034 {
3035 if( memcmp( name->val.p, cn, cn_len ) == 0 &&
3036 name->val.len == cn_len )
3037 break;
3038
3039 if( memcmp( name->val.p, "*.", 2 ) == 0 &&
3040 x509_wildcard_verify( cn, name ) )
3041 break;
3042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
3044 name = name->next;
3045 }
3046
3047 if( name == NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003048 {
3049 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
3050 {
3051 cur = &crt->subject_alt_names;
3052
3053 while( cur != NULL )
3054 {
3055 if( memcmp( cn, cur->buf.p, cn_len ) == 0 &&
3056 cur->buf.len == cn_len )
3057 break;
3058
3059 cur = cur->next;
3060 }
3061 }
3062
3063 if( cur == NULL )
3064 *flags |= BADCERT_CN_MISMATCH;
3065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003066 }
3067
Paul Bakker5121ce52009-01-03 21:22:43 +00003068 /*
3069 * Iterate upwards in the given cert chain,
3070 * ignoring any upper cert with CA != TRUE.
3071 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003072 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
3074 pathlen = 1;
3075
Paul Bakker76fd75a2011-01-16 21:12:10 +00003076 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003078 if( parent->ca_istrue == 0 ||
3079 crt->issuer_raw.len != parent->subject_raw.len ||
3080 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 crt->issuer_raw.len ) != 0 )
3082 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003083 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 continue;
3085 }
3086
Paul Bakker27d66162010-03-17 06:56:01 +00003087 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
3089 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3090
Paul Bakker76fd75a2011-01-16 21:12:10 +00003091 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3092 crt->sig.p ) != 0 )
3093 *flags |= BADCERT_NOT_TRUSTED;
3094
3095 /* Check trusted CA's CRL for the given crt */
3096 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003097
3098 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00003099 if( NULL != f_vrfy )
3100 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003101 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003102 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003103 else
3104 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003105 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003106 else if( *flags != 0 )
3107 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003108
3109 pathlen++;
3110
Paul Bakker76fd75a2011-01-16 21:12:10 +00003111 crt = parent;
3112 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 }
3114
3115 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003116 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003118 *flags |= BADCERT_NOT_TRUSTED;
3119
Paul Bakker7c6d4a42009-03-28 20:35:47 +00003120 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003121 {
3122 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
3123 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
3124 crt->issuer_raw.len ) != 0 )
3125 {
3126 trust_ca = trust_ca->next;
3127 continue;
3128 }
3129
3130 if( trust_ca->max_pathlen > 0 &&
3131 trust_ca->max_pathlen < pathlen )
3132 break;
3133
Paul Bakker27d66162010-03-17 06:56:01 +00003134 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003135
3136 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3137
3138 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3139 0, hash, crt->sig.p ) == 0 )
3140 {
3141 /*
3142 * cert. is signed by a trusted CA
3143 */
3144 *flags &= ~BADCERT_NOT_TRUSTED;
3145 break;
3146 }
3147
3148 trust_ca = trust_ca->next;
3149 }
3150
Paul Bakker76fd75a2011-01-16 21:12:10 +00003151 /* Check trusted CA's CRL for the given crt */
3152 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003153
3154 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003155 if( NULL != f_vrfy )
3156 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003157 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003158 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003159 else
3160 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003161 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003162 else if( *flags != 0 )
3163 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003164
Paul Bakker5121ce52009-01-03 21:22:43 +00003165 return( 0 );
3166}
3167
3168/*
3169 * Unallocate all certificate data
3170 */
3171void x509_free( x509_cert *crt )
3172{
3173 x509_cert *cert_cur = crt;
3174 x509_cert *cert_prv;
3175 x509_name *name_cur;
3176 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003177 x509_sequence *seq_cur;
3178 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003179
3180 if( crt == NULL )
3181 return;
3182
3183 do
3184 {
3185 rsa_free( &cert_cur->rsa );
3186
3187 name_cur = cert_cur->issuer.next;
3188 while( name_cur != NULL )
3189 {
3190 name_prv = name_cur;
3191 name_cur = name_cur->next;
3192 memset( name_prv, 0, sizeof( x509_name ) );
3193 free( name_prv );
3194 }
3195
3196 name_cur = cert_cur->subject.next;
3197 while( name_cur != NULL )
3198 {
3199 name_prv = name_cur;
3200 name_cur = name_cur->next;
3201 memset( name_prv, 0, sizeof( x509_name ) );
3202 free( name_prv );
3203 }
3204
Paul Bakker74111d32011-01-15 16:57:55 +00003205 seq_cur = cert_cur->ext_key_usage.next;
3206 while( seq_cur != NULL )
3207 {
3208 seq_prv = seq_cur;
3209 seq_cur = seq_cur->next;
3210 memset( seq_prv, 0, sizeof( x509_sequence ) );
3211 free( seq_prv );
3212 }
3213
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 if( cert_cur->raw.p != NULL )
3215 {
3216 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3217 free( cert_cur->raw.p );
3218 }
3219
3220 cert_cur = cert_cur->next;
3221 }
3222 while( cert_cur != NULL );
3223
3224 cert_cur = crt;
3225 do
3226 {
3227 cert_prv = cert_cur;
3228 cert_cur = cert_cur->next;
3229
3230 memset( cert_prv, 0, sizeof( x509_cert ) );
3231 if( cert_prv != crt )
3232 free( cert_prv );
3233 }
3234 while( cert_cur != NULL );
3235}
3236
Paul Bakkerd98030e2009-05-02 15:13:40 +00003237/*
3238 * Unallocate all CRL data
3239 */
3240void x509_crl_free( x509_crl *crl )
3241{
3242 x509_crl *crl_cur = crl;
3243 x509_crl *crl_prv;
3244 x509_name *name_cur;
3245 x509_name *name_prv;
3246 x509_crl_entry *entry_cur;
3247 x509_crl_entry *entry_prv;
3248
3249 if( crl == NULL )
3250 return;
3251
3252 do
3253 {
3254 name_cur = crl_cur->issuer.next;
3255 while( name_cur != NULL )
3256 {
3257 name_prv = name_cur;
3258 name_cur = name_cur->next;
3259 memset( name_prv, 0, sizeof( x509_name ) );
3260 free( name_prv );
3261 }
3262
3263 entry_cur = crl_cur->entry.next;
3264 while( entry_cur != NULL )
3265 {
3266 entry_prv = entry_cur;
3267 entry_cur = entry_cur->next;
3268 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3269 free( entry_prv );
3270 }
3271
3272 if( crl_cur->raw.p != NULL )
3273 {
3274 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3275 free( crl_cur->raw.p );
3276 }
3277
3278 crl_cur = crl_cur->next;
3279 }
3280 while( crl_cur != NULL );
3281
3282 crl_cur = crl;
3283 do
3284 {
3285 crl_prv = crl_cur;
3286 crl_cur = crl_cur->next;
3287
3288 memset( crl_prv, 0, sizeof( x509_crl ) );
3289 if( crl_prv != crl )
3290 free( crl_prv );
3291 }
3292 while( crl_cur != NULL );
3293}
3294
Paul Bakker40e46942009-01-03 21:51:57 +00003295#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Paul Bakker40e46942009-01-03 21:51:57 +00003297#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003298
3299/*
3300 * Checkup routine
3301 */
3302int x509_self_test( int verbose )
3303{
Paul Bakker5690efc2011-05-26 13:16:06 +00003304#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003305 int ret;
3306 int flags;
3307 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 x509_cert cacert;
3309 x509_cert clicert;
3310 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003311#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003312 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003313#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
3315 if( verbose != 0 )
3316 printf( " X.509 certificate load: " );
3317
3318 memset( &clicert, 0, sizeof( x509_cert ) );
3319
3320 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003321 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 if( ret != 0 )
3323 {
3324 if( verbose != 0 )
3325 printf( "failed\n" );
3326
3327 return( ret );
3328 }
3329
3330 memset( &cacert, 0, sizeof( x509_cert ) );
3331
3332 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003333 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 if( ret != 0 )
3335 {
3336 if( verbose != 0 )
3337 printf( "failed\n" );
3338
3339 return( ret );
3340 }
3341
3342 if( verbose != 0 )
3343 printf( "passed\n X.509 private key load: " );
3344
3345 i = strlen( test_ca_key );
3346 j = strlen( test_ca_pwd );
3347
Paul Bakker66b78b22011-03-25 14:22:50 +00003348 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3349
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 if( ( ret = x509parse_key( &rsa,
3351 (unsigned char *) test_ca_key, i,
3352 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3353 {
3354 if( verbose != 0 )
3355 printf( "failed\n" );
3356
3357 return( ret );
3358 }
3359
3360 if( verbose != 0 )
3361 printf( "passed\n X.509 signature verify: ");
3362
Paul Bakker23986e52011-04-24 08:57:21 +00003363 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 if( ret != 0 )
3365 {
Paul Bakker23986e52011-04-24 08:57:21 +00003366 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003367 if( verbose != 0 )
3368 printf( "failed\n" );
3369
3370 return( ret );
3371 }
3372
Paul Bakker5690efc2011-05-26 13:16:06 +00003373#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003375 printf( "passed\n X.509 DHM parameter load: " );
3376
3377 i = strlen( test_dhm_params );
3378 j = strlen( test_ca_pwd );
3379
3380 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3381 {
3382 if( verbose != 0 )
3383 printf( "failed\n" );
3384
3385 return( ret );
3386 }
3387
3388 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003389 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003390#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
3392 x509_free( &cacert );
3393 x509_free( &clicert );
3394 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003395#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003396 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003397#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003398
3399 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003400#else
3401 ((void) verbose);
3402 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3403#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003404}
3405
3406#endif
3407
3408#endif