blob: c6e25275200007609a68622a023f32cb92f7931f [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"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010045#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000046#include "polarssl/md2.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010047#endif
48#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/md4.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010050#endif
51#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/md5.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010053#endif
54#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000055#include "polarssl/sha1.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010056#endif
57#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000058#include "polarssl/sha2.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010059#endif
60#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000061#include "polarssl/sha4.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010062#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000063#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65#include <string.h>
66#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000067#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000068#include <windows.h>
69#else
Paul Bakker5121ce52009-01-03 21:22:43 +000070#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Paul Bakker335db3f2011-04-25 15:28:35 +000073#if defined(POLARSSL_FS_IO)
74#include <stdio.h>
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
163/*
164 * AlgorithmIdentifier ::= SEQUENCE {
165 * algorithm OBJECT IDENTIFIER,
166 * parameters ANY DEFINED BY algorithm OPTIONAL }
167 */
168static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000169 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 x509_buf *alg )
171{
Paul Bakker23986e52011-04-24 08:57:21 +0000172 int ret;
173 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175 if( ( ret = asn1_get_tag( p, end, &len,
176 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000177 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 end = *p + len;
180 alg->tag = **p;
181
182 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 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 alg->p = *p;
186 *p += alg->len;
187
188 if( *p == end )
189 return( 0 );
190
191 /*
192 * assume the algorithm parameters must be NULL
193 */
194 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000195 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000198 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000199 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201 return( 0 );
202}
203
204/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 * AttributeTypeAndValue ::= SEQUENCE {
206 * type AttributeType,
207 * value AttributeValue }
208 *
209 * AttributeType ::= OBJECT IDENTIFIER
210 *
211 * AttributeValue ::= ANY DEFINED BY AttributeType
212 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000213static int x509_get_attr_type_value( unsigned char **p,
214 const unsigned char *end,
215 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216{
Paul Bakker23986e52011-04-24 08:57:21 +0000217 int ret;
218 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 x509_buf *oid;
220 x509_buf *val;
221
222 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 oid = &cur->oid;
227 oid->tag = **p;
228
229 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000230 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232 oid->p = *p;
233 *p += oid->len;
234
235 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000236 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000237 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
240 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
241 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000242 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000243 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245 val = &cur->val;
246 val->tag = *(*p)++;
247
248 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000249 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251 val->p = *p;
252 *p += val->len;
253
254 cur->next = NULL;
255
Paul Bakker400ff6f2011-02-20 10:40:16 +0000256 return( 0 );
257}
258
259/*
260 * RelativeDistinguishedName ::=
261 * SET OF AttributeTypeAndValue
262 *
263 * AttributeTypeAndValue ::= SEQUENCE {
264 * type AttributeType,
265 * value AttributeValue }
266 *
267 * AttributeType ::= OBJECT IDENTIFIER
268 *
269 * AttributeValue ::= ANY DEFINED BY AttributeType
270 */
271static int x509_get_name( unsigned char **p,
272 const unsigned char *end,
273 x509_name *cur )
274{
Paul Bakker23986e52011-04-24 08:57:21 +0000275 int ret;
276 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277 const unsigned char *end2;
278 x509_name *use;
279
280 if( ( ret = asn1_get_tag( p, end, &len,
281 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000282 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000283
284 end2 = end;
285 end = *p + len;
286 use = cur;
287
288 do
289 {
290 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
291 return( ret );
292
293 if( *p != end )
294 {
295 use->next = (x509_name *) malloc(
296 sizeof( x509_name ) );
297
298 if( use->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +0000299 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000300
301 memset( use->next, 0, sizeof( x509_name ) );
302
303 use = use->next;
304 }
305 }
306 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308 /*
309 * recurse until end of SEQUENCE is reached
310 */
311 if( *p == end2 )
312 return( 0 );
313
314 cur->next = (x509_name *) malloc(
315 sizeof( x509_name ) );
316
317 if( cur->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +0000318 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Paul Bakker07156682012-05-30 07:33:30 +0000320 memset( cur->next, 0, sizeof( x509_name ) );
321
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 return( x509_get_name( p, end2, cur->next ) );
323}
324
325/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 * Time ::= CHOICE {
327 * utcTime UTCTime,
328 * generalTime GeneralizedTime }
329 */
Paul Bakker91200182010-02-18 21:26:15 +0000330static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000331 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332 x509_time *time )
333{
Paul Bakker23986e52011-04-24 08:57:21 +0000334 int ret;
335 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000337 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000340 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
341 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000342
Paul Bakker91200182010-02-18 21:26:15 +0000343 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344
Paul Bakker91200182010-02-18 21:26:15 +0000345 if ( tag == ASN1_UTC_TIME )
346 {
347 (*p)++;
348 ret = asn1_get_len( p, end, &len );
349
350 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000351 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000352
Paul Bakker91200182010-02-18 21:26:15 +0000353 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000354 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
355 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000356
Paul Bakker91200182010-02-18 21:26:15 +0000357 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
358 &time->year, &time->mon, &time->day,
359 &time->hour, &time->min, &time->sec ) < 5 )
360 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000361
Paul Bakker400ff6f2011-02-20 10:40:16 +0000362 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000363 time->year += 1900;
364
365 *p += len;
366
367 return( 0 );
368 }
369 else if ( tag == ASN1_GENERALIZED_TIME )
370 {
371 (*p)++;
372 ret = asn1_get_len( p, end, &len );
373
374 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000375 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000376
377 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000378 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
379 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000380
381 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
382 &time->year, &time->mon, &time->day,
383 &time->hour, &time->min, &time->sec ) < 5 )
384 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
385
386 *p += len;
387
388 return( 0 );
389 }
390 else
Paul Bakker9d781402011-05-09 16:17:09 +0000391 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000392}
393
394
395/*
396 * Validity ::= SEQUENCE {
397 * notBefore Time,
398 * notAfter Time }
399 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000400static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000401 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 x509_time *from,
403 x509_time *to )
404{
Paul Bakker23986e52011-04-24 08:57:21 +0000405 int ret;
406 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
408 if( ( ret = asn1_get_tag( p, end, &len,
409 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000410 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 end = *p + len;
413
Paul Bakker91200182010-02-18 21:26:15 +0000414 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000415 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
Paul Bakker91200182010-02-18 21:26:15 +0000417 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000421 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000422 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 return( 0 );
425}
426
427/*
428 * SubjectPublicKeyInfo ::= SEQUENCE {
429 * algorithm AlgorithmIdentifier,
430 * subjectPublicKey BIT STRING }
431 */
432static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000433 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 x509_buf *pk_alg_oid,
435 mpi *N, mpi *E )
436{
Paul Bakker23986e52011-04-24 08:57:21 +0000437 int ret, can_handle;
438 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 unsigned char *end2;
440
441 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
442 return( ret );
443
444 /*
445 * only RSA public keys handled at this time
446 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000447 can_handle = 0;
448
449 if( pk_alg_oid->len == 9 &&
450 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
451 can_handle = 1;
452
453 if( pk_alg_oid->len == 9 &&
454 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
455 {
456 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
457 can_handle = 1;
458
459 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
460 can_handle = 1;
461 }
462
463 if( pk_alg_oid->len == 5 &&
464 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
465 can_handle = 1;
466
467 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000468 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000471 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000474 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000475 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477 end2 = *p + len;
478
479 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000480 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 /*
483 * RSAPublicKey ::= SEQUENCE {
484 * modulus INTEGER, -- n
485 * publicExponent INTEGER -- e
486 * }
487 */
488 if( ( ret = asn1_get_tag( p, end2, &len,
489 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000490 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000493 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000494 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
497 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000498 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000501 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000502 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 return( 0 );
505}
506
507static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000508 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 x509_buf *sig )
510{
Paul Bakker23986e52011-04-24 08:57:21 +0000511 int ret;
512 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514 sig->tag = **p;
515
516 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000517 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Paul Bakker74111d32011-01-15 16:57:55 +0000519
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000521 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523 sig->len = len;
524 sig->p = *p;
525
526 *p += len;
527
528 return( 0 );
529}
530
531/*
532 * X.509 v2/v3 unique identifier (not parsed)
533 */
534static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000535 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 x509_buf *uid, int n )
537{
538 int ret;
539
540 if( *p == end )
541 return( 0 );
542
543 uid->tag = **p;
544
545 if( ( ret = asn1_get_tag( p, end, &uid->len,
546 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
547 {
Paul Bakker40e46942009-01-03 21:51:57 +0000548 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 return( 0 );
550
551 return( ret );
552 }
553
554 uid->p = *p;
555 *p += uid->len;
556
557 return( 0 );
558}
559
560/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000561 * X.509 Extensions (No parsing of extensions, pointer should
562 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 */
564static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000565 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000566 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000567{
Paul Bakker23986e52011-04-24 08:57:21 +0000568 int ret;
569 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571 if( *p == end )
572 return( 0 );
573
574 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000575
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000577 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 ext->p = *p;
581 end = *p + ext->len;
582
583 /*
584 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
585 *
586 * Extension ::= SEQUENCE {
587 * extnID OBJECT IDENTIFIER,
588 * critical BOOLEAN DEFAULT FALSE,
589 * extnValue OCTET STRING }
590 */
591 if( ( ret = asn1_get_tag( p, end, &len,
592 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000593 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000596 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000597 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Paul Bakkerd98030e2009-05-02 15:13:40 +0000599 return( 0 );
600}
601
602/*
603 * X.509 CRL v2 extensions (no extensions parsed yet.)
604 */
605static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000606 const unsigned char *end,
607 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000608{
Paul Bakker23986e52011-04-24 08:57:21 +0000609 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000610 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000611
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000612 /* Get explicit tag */
613 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614 {
615 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
616 return( 0 );
617
618 return( ret );
619 }
620
621 while( *p < end )
622 {
623 if( ( ret = asn1_get_tag( p, end, &len,
624 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000625 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000626
627 *p += len;
628 }
629
630 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000631 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000632 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
633
634 return( 0 );
635}
636
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000637/*
638 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
639 */
640static int x509_get_crl_entry_ext( unsigned char **p,
641 const unsigned char *end,
642 x509_buf *ext )
643{
644 int ret;
645 size_t len = 0;
646
647 /* OPTIONAL */
648 if (end <= *p)
649 return( 0 );
650
651 ext->tag = **p;
652 ext->p = *p;
653
654 /*
655 * Get CRL-entry extension sequence header
656 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
657 */
658 if( ( ret = asn1_get_tag( p, end, &ext->len,
659 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
660 {
661 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
662 {
663 ext->p = NULL;
664 return( 0 );
665 }
666 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
667 }
668
669 end = *p + ext->len;
670
671 if( end != *p + ext->len )
672 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
673 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
674
675 while( *p < end )
676 {
677 if( ( ret = asn1_get_tag( p, end, &len,
678 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
679 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
680
681 *p += len;
682 }
683
684 if( *p != end )
685 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
686 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
687
688 return( 0 );
689}
690
Paul Bakker74111d32011-01-15 16:57:55 +0000691static int x509_get_basic_constraints( unsigned char **p,
692 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000693 int *ca_istrue,
694 int *max_pathlen )
695{
Paul Bakker23986e52011-04-24 08:57:21 +0000696 int ret;
697 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000698
699 /*
700 * BasicConstraints ::= SEQUENCE {
701 * cA BOOLEAN DEFAULT FALSE,
702 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
703 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000704 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000705 *max_pathlen = 0; /* endless */
706
707 if( ( ret = asn1_get_tag( p, end, &len,
708 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000709 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000710
711 if( *p == end )
712 return 0;
713
Paul Bakker3cccddb2011-01-16 21:46:31 +0000714 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000715 {
716 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000717 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000718
719 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000720 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000721
Paul Bakker3cccddb2011-01-16 21:46:31 +0000722 if( *ca_istrue != 0 )
723 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000724 }
725
726 if( *p == end )
727 return 0;
728
729 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000730 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000731
732 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000733 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000734 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
735
736 (*max_pathlen)++;
737
Paul Bakker74111d32011-01-15 16:57:55 +0000738 return 0;
739}
740
741static int x509_get_ns_cert_type( unsigned char **p,
742 const unsigned char *end,
743 unsigned char *ns_cert_type)
744{
745 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000746 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000747
748 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000749 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000750
751 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000752 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000753 POLARSSL_ERR_ASN1_INVALID_LENGTH );
754
755 /* Get actual bitstring */
756 *ns_cert_type = *bs.p;
757 return 0;
758}
759
760static int x509_get_key_usage( unsigned char **p,
761 const unsigned char *end,
762 unsigned char *key_usage)
763{
764 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000765 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000766
767 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000768 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000769
Paul Bakkercebdf172011-11-11 15:01:31 +0000770 if( bs.len > 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000771 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000772 POLARSSL_ERR_ASN1_INVALID_LENGTH );
773
774 /* Get actual bitstring */
775 *key_usage = *bs.p;
776 return 0;
777}
778
Paul Bakkerd98030e2009-05-02 15:13:40 +0000779/*
Paul Bakker74111d32011-01-15 16:57:55 +0000780 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
781 *
782 * KeyPurposeId ::= OBJECT IDENTIFIER
783 */
784static int x509_get_ext_key_usage( unsigned char **p,
785 const unsigned char *end,
786 x509_sequence *ext_key_usage)
787{
788 int ret;
789
790 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000791 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000792
793 /* Sequence length must be >= 1 */
794 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000795 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000796 POLARSSL_ERR_ASN1_INVALID_LENGTH );
797
798 return 0;
799}
800
801/*
802 * X.509 v3 extensions
803 *
804 * TODO: Perform all of the basic constraints tests required by the RFC
805 * TODO: Set values for undetected extensions to a sane default?
806 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000807 */
808static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000809 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000810 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000811{
Paul Bakker23986e52011-04-24 08:57:21 +0000812 int ret;
813 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000814 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000815
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000816 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000817 {
818 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
819 return( 0 );
820
821 return( ret );
822 }
823
Paul Bakker5121ce52009-01-03 21:22:43 +0000824 while( *p < end )
825 {
Paul Bakker74111d32011-01-15 16:57:55 +0000826 /*
827 * Extension ::= SEQUENCE {
828 * extnID OBJECT IDENTIFIER,
829 * critical BOOLEAN DEFAULT FALSE,
830 * extnValue OCTET STRING }
831 */
832 x509_buf extn_oid = {0, 0, NULL};
833 int is_critical = 0; /* DEFAULT FALSE */
834
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 if( ( ret = asn1_get_tag( p, end, &len,
836 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000837 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000839 end_ext_data = *p + len;
840
Paul Bakker74111d32011-01-15 16:57:55 +0000841 /* Get extension ID */
842 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Paul Bakker74111d32011-01-15 16:57:55 +0000844 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Paul Bakker74111d32011-01-15 16:57:55 +0000847 extn_oid.p = *p;
848 *p += extn_oid.len;
849
850 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000852 POLARSSL_ERR_ASN1_OUT_OF_DATA );
853
854 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000855 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000856 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000857 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Paul Bakker74111d32011-01-15 16:57:55 +0000859 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000860 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000862 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000864 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000865
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000866 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000867 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000868 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Paul Bakker74111d32011-01-15 16:57:55 +0000870 /*
871 * Detect supported extensions
872 */
873 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
874 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 {
Paul Bakker74111d32011-01-15 16:57:55 +0000876 /* Parse basic constraints */
877 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000878 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000879 return ( ret );
880 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000881 }
Paul Bakker74111d32011-01-15 16:57:55 +0000882 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
883 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
884 {
885 /* Parse netscape certificate type */
886 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
887 &crt->ns_cert_type ) ) != 0 )
888 return ( ret );
889 crt->ext_types |= EXT_NS_CERT_TYPE;
890 }
891 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
892 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
893 {
894 /* Parse key usage */
895 if( ( ret = x509_get_key_usage( p, end_ext_octet,
896 &crt->key_usage ) ) != 0 )
897 return ( ret );
898 crt->ext_types |= EXT_KEY_USAGE;
899 }
900 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
901 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
902 {
903 /* Parse extended key usage */
904 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
905 &crt->ext_key_usage ) ) != 0 )
906 return ( ret );
907 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
908 }
909 else
910 {
911 /* No parser found, skip extension */
912 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker5c721f92011-07-27 16:51:09 +0000914#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000915 if( is_critical )
916 {
917 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000918 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000919 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
920 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000921#endif
Paul Bakker74111d32011-01-15 16:57:55 +0000922 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 }
924
925 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000926 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000927 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 return( 0 );
930}
931
932/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000933 * X.509 CRL Entries
934 */
935static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000936 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000937 x509_crl_entry *entry )
938{
Paul Bakker23986e52011-04-24 08:57:21 +0000939 int ret;
940 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000941 x509_crl_entry *cur_entry = entry;
942
943 if( *p == end )
944 return( 0 );
945
Paul Bakker9be19372009-07-27 20:21:53 +0000946 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000947 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
948 {
949 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
950 return( 0 );
951
952 return( ret );
953 }
954
Paul Bakker9be19372009-07-27 20:21:53 +0000955 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000956
957 while( *p < end )
958 {
Paul Bakker23986e52011-04-24 08:57:21 +0000959 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000960 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000961
962 if( ( ret = asn1_get_tag( p, end, &len2,
963 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
964 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000965 return( ret );
966 }
967
Paul Bakker9be19372009-07-27 20:21:53 +0000968 cur_entry->raw.tag = **p;
969 cur_entry->raw.p = *p;
970 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000971 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +0000972
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000973 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000974 return( ret );
975
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000976 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000977 return( ret );
978
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000979 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000980 return( ret );
981
Paul Bakker74111d32011-01-15 16:57:55 +0000982 if ( *p < end )
983 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000984 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkere2e36d32012-01-23 09:56:51 +0000985
986 if( cur_entry->next == NULL )
987 return( POLARSSL_ERR_X509_MALLOC_FAILED );
988
Paul Bakkerd98030e2009-05-02 15:13:40 +0000989 cur_entry = cur_entry->next;
990 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
991 }
992 }
993
994 return( 0 );
995}
996
Paul Bakker27d66162010-03-17 06:56:01 +0000997static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
998{
999 if( sig_oid->len == 9 &&
1000 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1001 {
1002 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1003 {
1004 *sig_alg = sig_oid->p[8];
1005 return( 0 );
1006 }
1007
1008 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1009 {
1010 *sig_alg = sig_oid->p[8];
1011 return( 0 );
1012 }
1013
1014 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1015 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001016 if( sig_oid->len == 5 &&
1017 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1018 {
1019 *sig_alg = SIG_RSA_SHA1;
1020 return( 0 );
1021 }
Paul Bakker27d66162010-03-17 06:56:01 +00001022
1023 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1024}
1025
Paul Bakkerd98030e2009-05-02 15:13:40 +00001026/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001027 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 */
Paul Bakker03437fc2013-06-19 12:10:31 +02001029int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1030 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001031{
Paul Bakker23986e52011-04-24 08:57:21 +00001032 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001033 size_t len;
Paul Bakker47f62612013-01-14 16:40:55 +01001034 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001035
Paul Bakker320a4b52009-03-28 18:52:39 +00001036 /*
1037 * Check for valid input
1038 */
1039 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001040 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001041
Paul Bakker96743fc2011-02-12 14:30:57 +00001042 p = (unsigned char *) malloc( len = buflen );
1043
1044 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001045 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001046
1047 memcpy( p, buf, buflen );
1048
1049 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
1051 crt->raw.p = p;
1052 crt->raw.len = len;
1053 end = p + len;
1054
1055 /*
1056 * Certificate ::= SEQUENCE {
1057 * tbsCertificate TBSCertificate,
1058 * signatureAlgorithm AlgorithmIdentifier,
1059 * signatureValue BIT STRING }
1060 */
1061 if( ( ret = asn1_get_tag( &p, end, &len,
1062 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1063 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001064 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001065 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 }
1067
Paul Bakker47f62612013-01-14 16:40:55 +01001068 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001070 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001071 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001072 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 }
Paul Bakker47f62612013-01-14 16:40:55 +01001074 crt_end = p + len;
Paul Bakker03437fc2013-06-19 12:10:31 +02001075
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 /*
1077 * TBSCertificate ::= SEQUENCE {
1078 */
1079 crt->tbs.p = p;
1080
1081 if( ( ret = asn1_get_tag( &p, end, &len,
1082 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1083 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001084 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001085 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 }
1087
1088 end = p + len;
1089 crt->tbs.len = end - crt->tbs.p;
1090
1091 /*
1092 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1093 *
1094 * CertificateSerialNumber ::= INTEGER
1095 *
1096 * signature AlgorithmIdentifier
1097 */
1098 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1099 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1100 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1101 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001102 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 return( ret );
1104 }
1105
1106 crt->version++;
1107
1108 if( crt->version > 3 )
1109 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001110 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001111 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 }
1113
Paul Bakker27d66162010-03-17 06:56:01 +00001114 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001116 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001117 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 }
1119
1120 /*
1121 * issuer Name
1122 */
1123 crt->issuer_raw.p = p;
1124
1125 if( ( ret = asn1_get_tag( &p, end, &len,
1126 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1127 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001128 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001129 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
1131
1132 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1133 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001134 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 return( ret );
1136 }
1137
1138 crt->issuer_raw.len = p - crt->issuer_raw.p;
1139
1140 /*
1141 * Validity ::= SEQUENCE {
1142 * notBefore Time,
1143 * notAfter Time }
1144 *
1145 */
1146 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1147 &crt->valid_to ) ) != 0 )
1148 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001149 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 return( ret );
1151 }
1152
1153 /*
1154 * subject Name
1155 */
1156 crt->subject_raw.p = p;
1157
1158 if( ( ret = asn1_get_tag( &p, end, &len,
1159 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1160 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001161 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001162 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 }
1164
1165 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1166 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001167 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 return( ret );
1169 }
1170
1171 crt->subject_raw.len = p - crt->subject_raw.p;
1172
1173 /*
1174 * SubjectPublicKeyInfo ::= SEQUENCE
1175 * algorithm AlgorithmIdentifier,
1176 * subjectPublicKey BIT STRING }
1177 */
1178 if( ( ret = asn1_get_tag( &p, end, &len,
1179 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1180 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001181 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001182 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 }
1184
1185 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1186 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1187 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001188 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 return( ret );
1190 }
1191
1192 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1193 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001194 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 return( ret );
1196 }
1197
1198 crt->rsa.len = mpi_size( &crt->rsa.N );
1199
1200 /*
1201 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1202 * -- If present, version shall be v2 or v3
1203 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1204 * -- If present, version shall be v2 or v3
1205 * extensions [3] EXPLICIT Extensions OPTIONAL
1206 * -- If present, version shall be v3
1207 */
1208 if( crt->version == 2 || crt->version == 3 )
1209 {
1210 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1211 if( ret != 0 )
1212 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001213 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 return( ret );
1215 }
1216 }
1217
1218 if( crt->version == 2 || crt->version == 3 )
1219 {
1220 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1221 if( ret != 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
1228 if( crt->version == 3 )
1229 {
Paul Bakker74111d32011-01-15 16:57:55 +00001230 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 if( ret != 0 )
1232 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001233 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 return( ret );
1235 }
1236 }
1237
1238 if( p != end )
1239 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001240 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001241 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001242 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 }
1244
Paul Bakker47f62612013-01-14 16:40:55 +01001245 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
1247 /*
1248 * signatureAlgorithm AlgorithmIdentifier,
1249 * signatureValue BIT STRING
1250 */
1251 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1252 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001253 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 return( ret );
1255 }
1256
Paul Bakker7261cba2013-01-16 12:39:54 +01001257 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1258 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001260 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001261 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 }
1263
1264 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1265 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001266 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 return( ret );
1268 }
1269
1270 if( p != end )
1271 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001272 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001273 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001274 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 }
1276
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001277 return( 0 );
1278}
1279
1280/*
Paul Bakker03437fc2013-06-19 12:10:31 +02001281 * Parse one X.509 certificate in DER format from a buffer and add them to a
1282 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001283 */
Paul Bakker03437fc2013-06-19 12:10:31 +02001284int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001285{
Paul Bakker03437fc2013-06-19 12:10:31 +02001286 int ret;
1287 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001288
1289 /*
1290 * Check for valid input
1291 */
1292 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001293 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001294
1295 while( crt->version != 0 && crt->next != NULL )
1296 {
1297 prev = crt;
1298 crt = crt->next;
1299 }
1300
1301 /*
1302 * Add new certificate on the end of the chain if needed.
1303 */
1304 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001305 {
1306 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1307
Paul Bakker7d06ad22009-05-02 15:53:56 +00001308 if( crt->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001309 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001310
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001311 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 crt = crt->next;
1313 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001314 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
Paul Bakker03437fc2013-06-19 12:10:31 +02001316 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1317 {
1318 if( prev )
1319 prev->next = NULL;
1320
1321 if( crt != chain )
1322 free( crt );
1323
1324 return( ret );
1325 }
1326
1327 return( 0 );
1328}
1329
1330/*
1331 * Parse one or more PEM certificates from a buffer and add them to the chained list
1332 */
1333int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1334{
1335 int ret, success = 0, first_error = 0, total_failed = 0;
1336 int buf_format = X509_FORMAT_DER;
1337
1338 /*
1339 * Check for valid input
1340 */
1341 if( chain == NULL || buf == NULL )
1342 return( POLARSSL_ERR_X509_INVALID_INPUT );
1343
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001344 /*
1345 * Determine buffer content. Buffer contains either one DER certificate or
1346 * one or more PEM certificates.
1347 */
1348#if defined(POLARSSL_PEM_C)
1349 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1350 buf_format = X509_FORMAT_PEM;
1351#endif
1352
1353 if( buf_format == X509_FORMAT_DER )
Paul Bakker03437fc2013-06-19 12:10:31 +02001354 return x509parse_crt_der( chain, buf, buflen );
1355
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001356#if defined(POLARSSL_PEM_C)
1357 if( buf_format == X509_FORMAT_PEM )
1358 {
1359 pem_context pem;
1360
1361 while( buflen > 0 )
1362 {
1363 size_t use_len;
1364 pem_init( &pem );
1365
1366 ret = pem_read_buffer( &pem,
1367 "-----BEGIN CERTIFICATE-----",
1368 "-----END CERTIFICATE-----",
1369 buf, NULL, 0, &use_len );
1370
1371 if( ret == 0 )
1372 {
1373 /*
1374 * Was PEM encoded
1375 */
1376 buflen -= use_len;
1377 buf += use_len;
1378 }
Paul Bakker721f06d2013-06-19 12:07:42 +02001379 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1380 {
1381 return( ret );
1382 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001383 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001384 {
1385 pem_free( &pem );
1386
Paul Bakker721f06d2013-06-19 12:07:42 +02001387 /*
1388 * PEM header and footer were found
1389 */
1390 buflen -= use_len;
1391 buf += use_len;
1392
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001393 if( first_error == 0 )
1394 first_error = ret;
1395
1396 continue;
1397 }
1398 else
1399 break;
1400
Paul Bakker03437fc2013-06-19 12:10:31 +02001401 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001402
1403 pem_free( &pem );
1404
1405 if( ret != 0 )
1406 {
1407 /*
Paul Bakker03437fc2013-06-19 12:10:31 +02001408 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001409 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001410 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001411 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001412
1413 if( first_error == 0 )
1414 first_error = ret;
1415
Paul Bakker03437fc2013-06-19 12:10:31 +02001416 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001417 continue;
1418 }
1419
1420 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001421 }
1422 }
1423#endif
1424
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001425 if( success )
Paul Bakker732e1a82011-12-11 16:35:09 +00001426 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001427 else if( first_error )
1428 return( first_error );
1429 else
1430 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431}
1432
1433/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001434 * Parse one or more CRLs and add them to the chained list
1435 */
Paul Bakker23986e52011-04-24 08:57:21 +00001436int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001437{
Paul Bakker23986e52011-04-24 08:57:21 +00001438 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001439 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001440 unsigned char *p, *end;
1441 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001442#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001443 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001444 pem_context pem;
1445#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001446
1447 crl = chain;
1448
1449 /*
1450 * Check for valid input
1451 */
1452 if( crl == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001453 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001454
1455 while( crl->version != 0 && crl->next != NULL )
1456 crl = crl->next;
1457
1458 /*
1459 * Add new CRL on the end of the chain if needed.
1460 */
1461 if ( crl->version != 0 && crl->next == NULL)
1462 {
1463 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1464
Paul Bakker7d06ad22009-05-02 15:53:56 +00001465 if( crl->next == NULL )
1466 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001467 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001468 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001469 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001470
Paul Bakker7d06ad22009-05-02 15:53:56 +00001471 crl = crl->next;
1472 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001473 }
1474
Paul Bakker96743fc2011-02-12 14:30:57 +00001475#if defined(POLARSSL_PEM_C)
1476 pem_init( &pem );
1477 ret = pem_read_buffer( &pem,
1478 "-----BEGIN X509 CRL-----",
1479 "-----END X509 CRL-----",
1480 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001481
Paul Bakker96743fc2011-02-12 14:30:57 +00001482 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001483 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001484 /*
1485 * Was PEM encoded
1486 */
1487 buflen -= use_len;
1488 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001489
1490 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001491 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001492 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001493 p = pem.buf;
1494 pem.buf = NULL;
1495 len = pem.buflen;
1496 pem_free( &pem );
1497 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001498 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001499 {
1500 pem_free( &pem );
1501 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001502 }
1503 else
1504 {
1505 /*
1506 * nope, copy the raw DER data
1507 */
1508 p = (unsigned char *) malloc( len = buflen );
1509
1510 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001511 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001512
1513 memcpy( p, buf, buflen );
1514
1515 buflen = 0;
1516 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001517#else
1518 p = (unsigned char *) malloc( len = buflen );
1519
1520 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001521 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001522
1523 memcpy( p, buf, buflen );
1524
1525 buflen = 0;
1526#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001527
1528 crl->raw.p = p;
1529 crl->raw.len = len;
1530 end = p + len;
1531
1532 /*
1533 * CertificateList ::= SEQUENCE {
1534 * tbsCertList TBSCertList,
1535 * signatureAlgorithm AlgorithmIdentifier,
1536 * signatureValue BIT STRING }
1537 */
1538 if( ( ret = asn1_get_tag( &p, end, &len,
1539 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1540 {
1541 x509_crl_free( crl );
1542 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1543 }
1544
Paul Bakker23986e52011-04-24 08:57:21 +00001545 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001546 {
1547 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001548 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001549 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1550 }
1551
1552 /*
1553 * TBSCertList ::= SEQUENCE {
1554 */
1555 crl->tbs.p = p;
1556
1557 if( ( ret = asn1_get_tag( &p, end, &len,
1558 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1559 {
1560 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001561 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001562 }
1563
1564 end = p + len;
1565 crl->tbs.len = end - crl->tbs.p;
1566
1567 /*
1568 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1569 * -- if present, MUST be v2
1570 *
1571 * signature AlgorithmIdentifier
1572 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001573 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001574 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1575 {
1576 x509_crl_free( crl );
1577 return( ret );
1578 }
1579
1580 crl->version++;
1581
1582 if( crl->version > 2 )
1583 {
1584 x509_crl_free( crl );
1585 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1586 }
1587
Paul Bakker27d66162010-03-17 06:56:01 +00001588 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001589 {
1590 x509_crl_free( crl );
1591 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1592 }
1593
1594 /*
1595 * issuer Name
1596 */
1597 crl->issuer_raw.p = p;
1598
1599 if( ( ret = asn1_get_tag( &p, end, &len,
1600 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1601 {
1602 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001603 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001604 }
1605
1606 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1607 {
1608 x509_crl_free( crl );
1609 return( ret );
1610 }
1611
1612 crl->issuer_raw.len = p - crl->issuer_raw.p;
1613
1614 /*
1615 * thisUpdate Time
1616 * nextUpdate Time OPTIONAL
1617 */
Paul Bakker91200182010-02-18 21:26:15 +00001618 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001619 {
1620 x509_crl_free( crl );
1621 return( ret );
1622 }
1623
Paul Bakker91200182010-02-18 21:26:15 +00001624 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001625 {
Paul Bakker9d781402011-05-09 16:17:09 +00001626 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001627 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001628 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001629 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001630 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631 x509_crl_free( crl );
1632 return( ret );
1633 }
1634 }
1635
1636 /*
1637 * revokedCertificates SEQUENCE OF SEQUENCE {
1638 * userCertificate CertificateSerialNumber,
1639 * revocationDate Time,
1640 * crlEntryExtensions Extensions OPTIONAL
1641 * -- if present, MUST be v2
1642 * } OPTIONAL
1643 */
1644 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1645 {
1646 x509_crl_free( crl );
1647 return( ret );
1648 }
1649
1650 /*
1651 * crlExtensions EXPLICIT Extensions OPTIONAL
1652 * -- if present, MUST be v2
1653 */
1654 if( crl->version == 2 )
1655 {
1656 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1657
1658 if( ret != 0 )
1659 {
1660 x509_crl_free( crl );
1661 return( ret );
1662 }
1663 }
1664
1665 if( p != end )
1666 {
1667 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001668 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001669 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1670 }
1671
1672 end = crl->raw.p + crl->raw.len;
1673
1674 /*
1675 * signatureAlgorithm AlgorithmIdentifier,
1676 * signatureValue BIT STRING
1677 */
1678 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1679 {
1680 x509_crl_free( crl );
1681 return( ret );
1682 }
1683
Paul Bakker7261cba2013-01-16 12:39:54 +01001684 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1685 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001686 {
1687 x509_crl_free( crl );
1688 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1689 }
1690
1691 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1692 {
1693 x509_crl_free( crl );
1694 return( ret );
1695 }
1696
1697 if( p != end )
1698 {
1699 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001700 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001701 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1702 }
1703
1704 if( buflen > 0 )
1705 {
1706 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1707
Paul Bakker7d06ad22009-05-02 15:53:56 +00001708 if( crl->next == NULL )
1709 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001710 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001711 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001712 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001713
Paul Bakker7d06ad22009-05-02 15:53:56 +00001714 crl = crl->next;
1715 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001716
1717 return( x509parse_crl( crl, buf, buflen ) );
1718 }
1719
1720 return( 0 );
1721}
1722
Paul Bakker335db3f2011-04-25 15:28:35 +00001723#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001724/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001725 * Load all data from a file into a given buffer.
1726 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001727int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001728{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001729 FILE *f;
Paul Bakker16e5f812013-09-11 11:37:33 +02001730 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001731
Paul Bakkerd98030e2009-05-02 15:13:40 +00001732 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001733 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001734
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 fseek( f, 0, SEEK_END );
Paul Bakker16e5f812013-09-11 11:37:33 +02001736 if( ( size = ftell( f ) ) == -1 )
1737 {
1738 fclose( f );
1739 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1740 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001741 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001742
Paul Bakker16e5f812013-09-11 11:37:33 +02001743 *n = (size_t) size;
1744
1745 if( *n + 1 == 0 ||
1746 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1747 {
1748 fclose( f );
Paul Bakker732e1a82011-12-11 16:35:09 +00001749 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker16e5f812013-09-11 11:37:33 +02001750 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001751
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752 if( fread( *buf, 1, *n, f ) != *n )
1753 {
1754 fclose( f );
1755 free( *buf );
Paul Bakker732e1a82011-12-11 16:35:09 +00001756 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001757 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001758
Paul Bakkerd98030e2009-05-02 15:13:40 +00001759 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001760
Paul Bakkerd98030e2009-05-02 15:13:40 +00001761 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001762
Paul Bakkerd98030e2009-05-02 15:13:40 +00001763 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001764}
1765
1766/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 * Load one or more certificates and add them to the chained list
1768 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001769int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001770{
1771 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 size_t n;
1773 unsigned char *buf;
1774
Paul Bakker732e1a82011-12-11 16:35:09 +00001775 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1776 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
Paul Bakker732e1a82011-12-11 16:35:09 +00001778 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
1780 memset( buf, 0, n + 1 );
1781 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
1783 return( ret );
1784}
1785
Paul Bakkerd98030e2009-05-02 15:13:40 +00001786/*
1787 * Load one or more CRLs and add them to the chained list
1788 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001789int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790{
1791 int ret;
1792 size_t n;
1793 unsigned char *buf;
1794
Paul Bakker732e1a82011-12-11 16:35:09 +00001795 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1796 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001797
Paul Bakker27fdf462011-06-09 13:55:13 +00001798 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799
1800 memset( buf, 0, n + 1 );
1801 free( buf );
1802
1803 return( ret );
1804}
1805
Paul Bakker5121ce52009-01-03 21:22:43 +00001806/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001807 * Load and parse a private RSA key
1808 */
1809int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1810{
1811 int ret;
1812 size_t n;
1813 unsigned char *buf;
1814
Paul Bakker732e1a82011-12-11 16:35:09 +00001815 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1816 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001817
1818 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001819 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001820 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001821 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001822 (unsigned char *) pwd, strlen( pwd ) );
1823
1824 memset( buf, 0, n + 1 );
1825 free( buf );
1826
1827 return( ret );
1828}
1829
1830/*
1831 * Load and parse a public RSA key
1832 */
1833int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1834{
1835 int ret;
1836 size_t n;
1837 unsigned char *buf;
1838
Paul Bakker732e1a82011-12-11 16:35:09 +00001839 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1840 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001841
Paul Bakker27fdf462011-06-09 13:55:13 +00001842 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001843
1844 memset( buf, 0, n + 1 );
1845 free( buf );
1846
1847 return( ret );
1848}
1849#endif /* POLARSSL_FS_IO */
1850
1851/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 * Parse a private RSA key
1853 */
Paul Bakker23986e52011-04-24 08:57:21 +00001854int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1855 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001856{
Paul Bakker23986e52011-04-24 08:57:21 +00001857 int ret;
1858 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001860 unsigned char *p_alt;
1861 x509_buf pk_alg_oid;
1862
Paul Bakker96743fc2011-02-12 14:30:57 +00001863#if defined(POLARSSL_PEM_C)
1864 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
Paul Bakker96743fc2011-02-12 14:30:57 +00001866 pem_init( &pem );
1867 ret = pem_read_buffer( &pem,
1868 "-----BEGIN RSA PRIVATE KEY-----",
1869 "-----END RSA PRIVATE KEY-----",
1870 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001871
Paul Bakker03a85bc2013-06-19 12:06:00 +02001872 if( ret == POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkered56b222011-07-13 11:26:43 +00001873 {
1874 ret = pem_read_buffer( &pem,
1875 "-----BEGIN PRIVATE KEY-----",
1876 "-----END PRIVATE KEY-----",
1877 key, pwd, pwdlen, &len );
1878 }
1879
Paul Bakker96743fc2011-02-12 14:30:57 +00001880 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001882 /*
1883 * Was PEM encoded
1884 */
1885 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001887 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001888 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001889 pem_free( &pem );
1890 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001891 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001892
Paul Bakker96743fc2011-02-12 14:30:57 +00001893 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1894#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001895 ((void) pwd);
1896 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001897 p = (unsigned char *) key;
1898#endif
1899 end = p + keylen;
1900
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001902 * Note: Depending on the type of private key file one can expect either a
1903 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1904 *
1905 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001906 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001907 * algorithm AlgorithmIdentifier,
1908 * PrivateKey BIT STRING
1909 * }
1910 *
1911 * AlgorithmIdentifier ::= SEQUENCE {
1912 * algorithm OBJECT IDENTIFIER,
1913 * parameters ANY DEFINED BY algorithm OPTIONAL
1914 * }
1915 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 * RSAPrivateKey ::= SEQUENCE {
1917 * version Version,
1918 * modulus INTEGER, -- n
1919 * publicExponent INTEGER, -- e
1920 * privateExponent INTEGER, -- d
1921 * prime1 INTEGER, -- p
1922 * prime2 INTEGER, -- q
1923 * exponent1 INTEGER, -- d mod (p-1)
1924 * exponent2 INTEGER, -- d mod (q-1)
1925 * coefficient INTEGER, -- (inverse of q) mod p
1926 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1927 * }
1928 */
1929 if( ( ret = asn1_get_tag( &p, end, &len,
1930 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1931 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001932#if defined(POLARSSL_PEM_C)
1933 pem_free( &pem );
1934#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001936 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 }
1938
1939 end = p + len;
1940
1941 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1942 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001943#if defined(POLARSSL_PEM_C)
1944 pem_free( &pem );
1945#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001947 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 }
1949
1950 if( rsa->ver != 0 )
1951 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001952#if defined(POLARSSL_PEM_C)
1953 pem_free( &pem );
1954#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001956 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 }
1958
Paul Bakkered56b222011-07-13 11:26:43 +00001959 p_alt = p;
1960
1961 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1962 {
1963 // Assume that we have the PKCS#1 format if wrong
1964 // tag was encountered
1965 //
1966 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1967 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1968 {
1969#if defined(POLARSSL_PEM_C)
1970 pem_free( &pem );
1971#endif
1972 rsa_free( rsa );
1973 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1974 }
1975 }
1976 else
1977 {
1978 int can_handle;
1979
1980 /*
1981 * only RSA keys handled at this time
1982 */
1983 can_handle = 0;
1984
1985 if( pk_alg_oid.len == 9 &&
1986 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1987 can_handle = 1;
1988
1989 if( pk_alg_oid.len == 9 &&
1990 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1991 {
1992 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1993 can_handle = 1;
1994
1995 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1996 can_handle = 1;
1997 }
1998
1999 if( pk_alg_oid.len == 5 &&
2000 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2001 can_handle = 1;
2002
2003 if( can_handle == 0 )
2004 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2005
2006 /*
2007 * Parse the PKCS#8 format
2008 */
2009
2010 p = p_alt;
2011 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2012 {
2013#if defined(POLARSSL_PEM_C)
2014 pem_free( &pem );
2015#endif
2016 rsa_free( rsa );
2017 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2018 }
2019
2020 if( ( end - p ) < 1 )
2021 {
2022#if defined(POLARSSL_PEM_C)
2023 pem_free( &pem );
2024#endif
2025 rsa_free( rsa );
2026 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2027 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2028 }
2029
2030 end = p + len;
2031
2032 if( ( ret = asn1_get_tag( &p, end, &len,
2033 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2034 {
2035#if defined(POLARSSL_PEM_C)
2036 pem_free( &pem );
2037#endif
2038 rsa_free( rsa );
2039 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2040 }
2041
2042 end = p + len;
2043
2044 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2045 {
2046#if defined(POLARSSL_PEM_C)
2047 pem_free( &pem );
2048#endif
2049 rsa_free( rsa );
2050 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2051 }
2052
2053 if( rsa->ver != 0 )
2054 {
2055#if defined(POLARSSL_PEM_C)
2056 pem_free( &pem );
2057#endif
2058 rsa_free( rsa );
2059 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2060 }
2061 }
2062
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2064 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2065 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2066 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2067 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2068 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2069 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2070 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2071 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002072#if defined(POLARSSL_PEM_C)
2073 pem_free( &pem );
2074#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002076 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 }
2078
2079 rsa->len = mpi_size( &rsa->N );
2080
2081 if( p != end )
2082 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002083#if defined(POLARSSL_PEM_C)
2084 pem_free( &pem );
2085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002087 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002088 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
2090
2091 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2092 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002093#if defined(POLARSSL_PEM_C)
2094 pem_free( &pem );
2095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 rsa_free( rsa );
2097 return( ret );
2098 }
2099
Paul Bakker96743fc2011-02-12 14:30:57 +00002100#if defined(POLARSSL_PEM_C)
2101 pem_free( &pem );
2102#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002103
2104 return( 0 );
2105}
2106
2107/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002108 * Parse a public RSA key
2109 */
Paul Bakker23986e52011-04-24 08:57:21 +00002110int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002111{
Paul Bakker23986e52011-04-24 08:57:21 +00002112 int ret;
2113 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002114 unsigned char *p, *end;
2115 x509_buf alg_oid;
2116#if defined(POLARSSL_PEM_C)
2117 pem_context pem;
2118
2119 pem_init( &pem );
2120 ret = pem_read_buffer( &pem,
2121 "-----BEGIN PUBLIC KEY-----",
2122 "-----END PUBLIC KEY-----",
2123 key, NULL, 0, &len );
2124
2125 if( ret == 0 )
2126 {
2127 /*
2128 * Was PEM encoded
2129 */
2130 keylen = pem.buflen;
2131 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02002132 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002133 {
2134 pem_free( &pem );
2135 return( ret );
2136 }
2137
2138 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2139#else
2140 p = (unsigned char *) key;
2141#endif
2142 end = p + keylen;
2143
2144 /*
2145 * PublicKeyInfo ::= SEQUENCE {
2146 * algorithm AlgorithmIdentifier,
2147 * PublicKey BIT STRING
2148 * }
2149 *
2150 * AlgorithmIdentifier ::= SEQUENCE {
2151 * algorithm OBJECT IDENTIFIER,
2152 * parameters ANY DEFINED BY algorithm OPTIONAL
2153 * }
2154 *
2155 * RSAPublicKey ::= SEQUENCE {
2156 * modulus INTEGER, -- n
2157 * publicExponent INTEGER -- e
2158 * }
2159 */
2160
2161 if( ( ret = asn1_get_tag( &p, end, &len,
2162 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2163 {
2164#if defined(POLARSSL_PEM_C)
2165 pem_free( &pem );
2166#endif
2167 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002168 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002169 }
2170
2171 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2172 {
2173#if defined(POLARSSL_PEM_C)
2174 pem_free( &pem );
2175#endif
2176 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002177 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002178 }
2179
2180 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2181 {
2182#if defined(POLARSSL_PEM_C)
2183 pem_free( &pem );
2184#endif
2185 rsa_free( rsa );
2186 return( ret );
2187 }
2188
2189 rsa->len = mpi_size( &rsa->N );
2190
2191#if defined(POLARSSL_PEM_C)
2192 pem_free( &pem );
2193#endif
2194
2195 return( 0 );
2196}
2197
Paul Bakkereaa89f82011-04-04 21:36:15 +00002198#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002199/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002200 * Parse DHM parameters
2201 */
Paul Bakker23986e52011-04-24 08:57:21 +00002202int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002203{
Paul Bakker23986e52011-04-24 08:57:21 +00002204 int ret;
2205 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002206 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002207#if defined(POLARSSL_PEM_C)
2208 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002209
Paul Bakker96743fc2011-02-12 14:30:57 +00002210 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002211
Paul Bakker96743fc2011-02-12 14:30:57 +00002212 ret = pem_read_buffer( &pem,
2213 "-----BEGIN DH PARAMETERS-----",
2214 "-----END DH PARAMETERS-----",
2215 dhmin, NULL, 0, &dhminlen );
2216
2217 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002218 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002219 /*
2220 * Was PEM encoded
2221 */
2222 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002223 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02002224 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002225 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002226 pem_free( &pem );
2227 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002228 }
2229
Paul Bakker96743fc2011-02-12 14:30:57 +00002230 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2231#else
2232 p = (unsigned char *) dhmin;
2233#endif
2234 end = p + dhminlen;
2235
Paul Bakker1b57b062011-01-06 15:48:19 +00002236 memset( dhm, 0, sizeof( dhm_context ) );
2237
Paul Bakker1b57b062011-01-06 15:48:19 +00002238 /*
2239 * DHParams ::= SEQUENCE {
2240 * prime INTEGER, -- P
2241 * generator INTEGER, -- g
2242 * }
2243 */
2244 if( ( ret = asn1_get_tag( &p, end, &len,
2245 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2246 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002247#if defined(POLARSSL_PEM_C)
2248 pem_free( &pem );
2249#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002250 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002251 }
2252
2253 end = p + len;
2254
2255 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2256 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2257 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002258#if defined(POLARSSL_PEM_C)
2259 pem_free( &pem );
2260#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002261 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002262 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002263 }
2264
2265 if( p != end )
2266 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002267#if defined(POLARSSL_PEM_C)
2268 pem_free( &pem );
2269#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002270 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002271 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002272 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2273 }
2274
Paul Bakker96743fc2011-02-12 14:30:57 +00002275#if defined(POLARSSL_PEM_C)
2276 pem_free( &pem );
2277#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002278
2279 return( 0 );
2280}
2281
Paul Bakker335db3f2011-04-25 15:28:35 +00002282#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002283/*
2284 * Load and parse a private RSA key
2285 */
2286int x509parse_dhmfile( dhm_context *dhm, const char *path )
2287{
2288 int ret;
2289 size_t n;
2290 unsigned char *buf;
2291
Paul Bakker732e1a82011-12-11 16:35:09 +00002292 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2293 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002294
Paul Bakker27fdf462011-06-09 13:55:13 +00002295 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002296
2297 memset( buf, 0, n + 1 );
2298 free( buf );
2299
2300 return( ret );
2301}
Paul Bakker335db3f2011-04-25 15:28:35 +00002302#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002303#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002304
Paul Bakker5121ce52009-01-03 21:22:43 +00002305#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002306#include <stdarg.h>
2307
2308#if !defined vsnprintf
2309#define vsnprintf _vsnprintf
2310#endif // vsnprintf
2311
2312/*
2313 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2314 * Result value is not size of buffer needed, but -1 if no fit is possible.
2315 *
2316 * This fuction tries to 'fix' this by at least suggesting enlarging the
2317 * size by 20.
2318 */
2319int compat_snprintf(char *str, size_t size, const char *format, ...)
2320{
2321 va_list ap;
2322 int res = -1;
2323
2324 va_start( ap, format );
2325
2326 res = vsnprintf( str, size, format, ap );
2327
2328 va_end( ap );
2329
2330 // No quick fix possible
2331 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002332 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002333
2334 return res;
2335}
2336
2337#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002338#endif
2339
Paul Bakkerd98030e2009-05-02 15:13:40 +00002340#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2341
2342#define SAFE_SNPRINTF() \
2343{ \
2344 if( ret == -1 ) \
2345 return( -1 ); \
2346 \
Paul Bakker23986e52011-04-24 08:57:21 +00002347 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002348 p[n - 1] = '\0'; \
2349 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2350 } \
2351 \
Paul Bakker23986e52011-04-24 08:57:21 +00002352 n -= (unsigned int) ret; \
2353 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002354}
2355
Paul Bakker5121ce52009-01-03 21:22:43 +00002356/*
2357 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002358 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002359 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002360int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002361{
Paul Bakker23986e52011-04-24 08:57:21 +00002362 int ret;
2363 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002365 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 char s[128], *p;
2367
2368 memset( s, 0, sizeof( s ) );
2369
2370 name = dn;
2371 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002372 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
2374 while( name != NULL )
2375 {
Paul Bakker74111d32011-01-15 16:57:55 +00002376 if( name != dn )
2377 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002378 ret = snprintf( p, n, ", " );
2379 SAFE_SNPRINTF();
2380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Paul Bakker7261cba2013-01-16 12:39:54 +01002382 if( name->oid.len == 3 &&
2383 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 {
2385 switch( name->oid.p[2] )
2386 {
2387 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002388 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
2390 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002391 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
2393 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002394 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
2396 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002397 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
2399 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002400 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
2402 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002403 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
2405 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002406 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 name->oid.p[2] );
2408 break;
2409 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002410 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 }
Paul Bakker7261cba2013-01-16 12:39:54 +01002412 else if( name->oid.len == 9 &&
2413 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 {
2415 switch( name->oid.p[8] )
2416 {
2417 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002418 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
2420 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002421 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 name->oid.p[8] );
2423 break;
2424 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002425 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 }
2427 else
Paul Bakker74111d32011-01-15 16:57:55 +00002428 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002429 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002430 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002431 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
2433 for( i = 0; i < name->val.len; i++ )
2434 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002435 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 break;
2437
2438 c = name->val.p[i];
2439 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2440 s[i] = '?';
2441 else s[i] = c;
2442 }
2443 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002444 ret = snprintf( p, n, "%s", s );
2445 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 name = name->next;
2447 }
2448
Paul Bakker23986e52011-04-24 08:57:21 +00002449 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002450}
2451
2452/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002453 * Store the serial in printable form into buf; no more
2454 * than size characters will be written
2455 */
2456int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2457{
Paul Bakker23986e52011-04-24 08:57:21 +00002458 int ret;
2459 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002460 char *p;
2461
2462 p = buf;
2463 n = size;
2464
2465 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002466 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002467
2468 for( i = 0; i < nr; i++ )
2469 {
Paul Bakker93048802011-12-05 14:38:06 +00002470 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002471 continue;
2472
Paul Bakkerdd476992011-01-16 21:34:59 +00002473 ret = snprintf( p, n, "%02X%s",
2474 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2475 SAFE_SNPRINTF();
2476 }
2477
Paul Bakker03c7c252011-11-25 12:37:37 +00002478 if( nr != serial->len )
2479 {
2480 ret = snprintf( p, n, "...." );
2481 SAFE_SNPRINTF();
2482 }
2483
Paul Bakker23986e52011-04-24 08:57:21 +00002484 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002485}
2486
2487/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002488 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002490int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2491 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002492{
Paul Bakker23986e52011-04-24 08:57:21 +00002493 int ret;
2494 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002495 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
2497 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002498 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Paul Bakkerd98030e2009-05-02 15:13:40 +00002500 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002502 SAFE_SNPRINTF();
2503 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002505 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002506
Paul Bakkerdd476992011-01-16 21:34:59 +00002507 ret = x509parse_serial_gets( p, n, &crt->serial);
2508 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Paul Bakkerd98030e2009-05-02 15:13:40 +00002510 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2511 SAFE_SNPRINTF();
2512 ret = x509parse_dn_gets( p, n, &crt->issuer );
2513 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
Paul Bakkerd98030e2009-05-02 15:13:40 +00002515 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2516 SAFE_SNPRINTF();
2517 ret = x509parse_dn_gets( p, n, &crt->subject );
2518 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Paul Bakkerd98030e2009-05-02 15:13:40 +00002520 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2522 crt->valid_from.year, crt->valid_from.mon,
2523 crt->valid_from.day, crt->valid_from.hour,
2524 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002525 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Paul Bakkerd98030e2009-05-02 15:13:40 +00002527 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2529 crt->valid_to.year, crt->valid_to.mon,
2530 crt->valid_to.day, crt->valid_to.hour,
2531 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002532 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Paul Bakkerd98030e2009-05-02 15:13:40 +00002534 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2535 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Paul Bakker27d66162010-03-17 06:56:01 +00002537 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002539 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2540 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2541 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2542 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2543 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2544 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2545 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2546 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2547 default: ret = snprintf( p, n, "???" ); break;
2548 }
2549 SAFE_SNPRINTF();
2550
2551 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002552 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002553 SAFE_SNPRINTF();
2554
Paul Bakker23986e52011-04-24 08:57:21 +00002555 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002556}
2557
Paul Bakker74111d32011-01-15 16:57:55 +00002558/* Compare a given OID string with an OID x509_buf * */
2559#define OID_CMP(oid_str, oid_buf) \
2560 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2561 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2562
2563/*
2564 * Return an informational string describing the given OID
2565 */
2566const char *x509_oid_get_description( x509_buf *oid )
2567{
2568 if ( oid == NULL )
2569 return ( NULL );
2570
2571 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2572 return( STRING_SERVER_AUTH );
2573
2574 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2575 return( STRING_CLIENT_AUTH );
2576
2577 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2578 return( STRING_CODE_SIGNING );
2579
2580 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2581 return( STRING_EMAIL_PROTECTION );
2582
2583 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2584 return( STRING_TIME_STAMPING );
2585
2586 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2587 return( STRING_OCSP_SIGNING );
2588
2589 return( NULL );
2590}
2591
2592/* Return the x.y.z.... style numeric string for the given OID */
2593int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2594{
Paul Bakker23986e52011-04-24 08:57:21 +00002595 int ret;
2596 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002597 unsigned int value;
2598 char *p;
2599
2600 p = buf;
2601 n = size;
2602
2603 /* First byte contains first two dots */
2604 if( oid->len > 0 )
2605 {
2606 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2607 SAFE_SNPRINTF();
2608 }
2609
2610 /* TODO: value can overflow in value. */
2611 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002612 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002613 {
2614 value <<= 7;
2615 value += oid->p[i] & 0x7F;
2616
2617 if( !( oid->p[i] & 0x80 ) )
2618 {
2619 /* Last byte */
2620 ret = snprintf( p, n, ".%d", value );
2621 SAFE_SNPRINTF();
2622 value = 0;
2623 }
2624 }
2625
Paul Bakker23986e52011-04-24 08:57:21 +00002626 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002627}
2628
Paul Bakkerd98030e2009-05-02 15:13:40 +00002629/*
2630 * Return an informational string about the CRL.
2631 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002632int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2633 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002634{
Paul Bakker23986e52011-04-24 08:57:21 +00002635 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002636 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002637 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002638 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002639
2640 p = buf;
2641 n = size;
2642
2643 ret = snprintf( p, n, "%sCRL version : %d",
2644 prefix, crl->version );
2645 SAFE_SNPRINTF();
2646
2647 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2648 SAFE_SNPRINTF();
2649 ret = x509parse_dn_gets( p, n, &crl->issuer );
2650 SAFE_SNPRINTF();
2651
2652 ret = snprintf( p, n, "\n%sthis update : " \
2653 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2654 crl->this_update.year, crl->this_update.mon,
2655 crl->this_update.day, crl->this_update.hour,
2656 crl->this_update.min, crl->this_update.sec );
2657 SAFE_SNPRINTF();
2658
2659 ret = snprintf( p, n, "\n%snext update : " \
2660 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2661 crl->next_update.year, crl->next_update.mon,
2662 crl->next_update.day, crl->next_update.hour,
2663 crl->next_update.min, crl->next_update.sec );
2664 SAFE_SNPRINTF();
2665
2666 entry = &crl->entry;
2667
2668 ret = snprintf( p, n, "\n%sRevoked certificates:",
2669 prefix );
2670 SAFE_SNPRINTF();
2671
Paul Bakker9be19372009-07-27 20:21:53 +00002672 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002673 {
2674 ret = snprintf( p, n, "\n%sserial number: ",
2675 prefix );
2676 SAFE_SNPRINTF();
2677
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002678 ret = x509parse_serial_gets( p, n, &entry->serial);
2679 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002680
Paul Bakkerd98030e2009-05-02 15:13:40 +00002681 ret = snprintf( p, n, " revocation date: " \
2682 "%04d-%02d-%02d %02d:%02d:%02d",
2683 entry->revocation_date.year, entry->revocation_date.mon,
2684 entry->revocation_date.day, entry->revocation_date.hour,
2685 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002686 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002687
2688 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 }
2690
Paul Bakkerd98030e2009-05-02 15:13:40 +00002691 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2692 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Paul Bakker27d66162010-03-17 06:56:01 +00002694 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002695 {
2696 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2697 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2698 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2699 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2700 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2701 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2702 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2703 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2704 default: ret = snprintf( p, n, "???" ); break;
2705 }
2706 SAFE_SNPRINTF();
2707
Paul Bakker1e27bb22009-07-19 20:25:25 +00002708 ret = snprintf( p, n, "\n" );
2709 SAFE_SNPRINTF();
2710
Paul Bakker23986e52011-04-24 08:57:21 +00002711 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712}
2713
2714/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002715 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002717int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002718{
Paul Bakkercce9d772011-11-18 14:26:47 +00002719 int year, mon, day;
2720 int hour, min, sec;
2721
2722#if defined(_WIN32)
2723 SYSTEMTIME st;
2724
2725 GetLocalTime(&st);
2726
2727 year = st.wYear;
2728 mon = st.wMonth;
2729 day = st.wDay;
2730 hour = st.wHour;
2731 min = st.wMinute;
2732 sec = st.wSecond;
2733#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 struct tm *lt;
2735 time_t tt;
2736
2737 tt = time( NULL );
2738 lt = localtime( &tt );
2739
Paul Bakkercce9d772011-11-18 14:26:47 +00002740 year = lt->tm_year + 1900;
2741 mon = lt->tm_mon + 1;
2742 day = lt->tm_mday;
2743 hour = lt->tm_hour;
2744 min = lt->tm_min;
2745 sec = lt->tm_sec;
2746#endif
2747
2748 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002749 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Paul Bakkercce9d772011-11-18 14:26:47 +00002751 if( year == to->year &&
2752 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002753 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
Paul Bakkercce9d772011-11-18 14:26:47 +00002755 if( year == to->year &&
2756 mon == to->mon &&
2757 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002758 return( 1 );
2759
Paul Bakkercce9d772011-11-18 14:26:47 +00002760 if( year == to->year &&
2761 mon == to->mon &&
2762 day == to->day &&
2763 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002764 return( 1 );
2765
Paul Bakkercce9d772011-11-18 14:26:47 +00002766 if( year == to->year &&
2767 mon == to->mon &&
2768 day == to->day &&
2769 hour == to->hour &&
2770 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002771 return( 1 );
2772
Paul Bakkercce9d772011-11-18 14:26:47 +00002773 if( year == to->year &&
2774 mon == to->mon &&
2775 day == to->day &&
2776 hour == to->hour &&
2777 min == to->min &&
2778 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002779 return( 1 );
2780
Paul Bakker40ea7de2009-05-03 10:18:48 +00002781 return( 0 );
2782}
2783
2784/*
2785 * Return 1 if the certificate is revoked, or 0 otherwise.
2786 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002787int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002788{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002789 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002790
2791 while( cur != NULL && cur->serial.len != 0 )
2792 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002793 if( crt->serial.len == cur->serial.len &&
2794 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002795 {
2796 if( x509parse_time_expired( &cur->revocation_date ) )
2797 return( 1 );
2798 }
2799
2800 cur = cur->next;
2801 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002802
2803 return( 0 );
2804}
2805
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002806/*
2807 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002808 */
Paul Bakker23986e52011-04-24 08:57:21 +00002809static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002810 unsigned char *out )
2811{
2812 switch( alg )
2813 {
Paul Bakker40e46942009-01-03 21:51:57 +00002814#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002815 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002816#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002817#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002818 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002820#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002821 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002822#endif
2823#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002824 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002825#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002826#if defined(POLARSSL_SHA2_C)
2827 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2828 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2829#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002830#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002831 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2832 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002835 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 break;
2837 }
2838}
2839
2840/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002841 * Check that the given certificate is valid accoring to the CRL.
2842 */
2843static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2844 x509_crl *crl_list)
2845{
2846 int flags = 0;
2847 int hash_id;
2848 unsigned char hash[64];
2849
2850 /*
2851 * TODO: What happens if no CRL is present?
2852 * Suggestion: Revocation state should be unknown if no CRL is present.
2853 * For backwards compatibility this is not yet implemented.
2854 */
2855
2856 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2857 {
2858 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2859 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2860 crl_list->issuer_raw.len ) != 0 )
2861 {
2862 crl_list = crl_list->next;
2863 continue;
2864 }
2865
2866 /*
2867 * Check if CRL is correctly signed by the trusted CA
2868 */
2869 hash_id = crl_list->sig_alg;
2870
2871 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2872
2873 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2874 0, hash, crl_list->sig.p ) == 0 )
2875 {
2876 /*
2877 * CRL is not trusted
2878 */
2879 flags |= BADCRL_NOT_TRUSTED;
2880 break;
2881 }
2882
2883 /*
2884 * Check for validity of CRL (Do not drop out)
2885 */
2886 if( x509parse_time_expired( &crl_list->next_update ) )
2887 flags |= BADCRL_EXPIRED;
2888
2889 /*
2890 * Check if certificate is revoked
2891 */
2892 if( x509parse_revoked(crt, crl_list) )
2893 {
2894 flags |= BADCERT_REVOKED;
2895 break;
2896 }
2897
2898 crl_list = crl_list->next;
2899 }
2900 return flags;
2901}
2902
2903/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 * Verify the certificate validity
2905 */
2906int x509parse_verify( x509_cert *crt,
2907 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002908 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002909 const char *cn, int *flags,
2910 int (*f_vrfy)(void *, x509_cert *, int, int),
2911 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002912{
Paul Bakker23986e52011-04-24 08:57:21 +00002913 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 int hash_id;
2915 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002916 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002918 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
Paul Bakker40ea7de2009-05-03 10:18:48 +00002920 *flags = 0;
2921
2922 if( x509parse_time_expired( &crt->valid_to ) )
2923 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
2925 if( cn != NULL )
2926 {
2927 name = &crt->subject;
2928 cn_len = strlen( cn );
2929
2930 while( name != NULL )
2931 {
Paul Bakker7261cba2013-01-16 12:39:54 +01002932 if( name->oid.len == 3 &&
2933 memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2934 name->val.len == cn_len &&
2935 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 break;
2937
2938 name = name->next;
2939 }
2940
2941 if( name == NULL )
2942 *flags |= BADCERT_CN_MISMATCH;
2943 }
2944
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 /*
2946 * Iterate upwards in the given cert chain,
2947 * ignoring any upper cert with CA != TRUE.
2948 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002949 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
2951 pathlen = 1;
2952
Paul Bakker76fd75a2011-01-16 21:12:10 +00002953 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002955 if( parent->ca_istrue == 0 ||
2956 crt->issuer_raw.len != parent->subject_raw.len ||
2957 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 crt->issuer_raw.len ) != 0 )
2959 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002960 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 continue;
2962 }
2963
Paul Bakker27d66162010-03-17 06:56:01 +00002964 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
2966 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2967
Paul Bakker76fd75a2011-01-16 21:12:10 +00002968 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2969 crt->sig.p ) != 0 )
2970 *flags |= BADCERT_NOT_TRUSTED;
2971
2972 /* Check trusted CA's CRL for the given crt */
2973 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002974
2975 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002976 if( NULL != f_vrfy )
2977 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002978 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002979 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002980 else
2981 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002982 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002983 else if( *flags != 0 )
2984 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
2986 pathlen++;
2987
Paul Bakker76fd75a2011-01-16 21:12:10 +00002988 crt = parent;
2989 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 }
2991
2992 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002993 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002995 *flags |= BADCERT_NOT_TRUSTED;
2996
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002997 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002998 {
2999 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
3000 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
3001 crt->issuer_raw.len ) != 0 )
3002 {
3003 trust_ca = trust_ca->next;
3004 continue;
3005 }
3006
3007 if( trust_ca->max_pathlen > 0 &&
3008 trust_ca->max_pathlen < pathlen )
3009 break;
3010
Paul Bakker27d66162010-03-17 06:56:01 +00003011 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
3013 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3014
3015 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3016 0, hash, crt->sig.p ) == 0 )
3017 {
3018 /*
3019 * cert. is signed by a trusted CA
3020 */
3021 *flags &= ~BADCERT_NOT_TRUSTED;
3022 break;
3023 }
3024
3025 trust_ca = trust_ca->next;
3026 }
3027
Paul Bakker76fd75a2011-01-16 21:12:10 +00003028 /* Check trusted CA's CRL for the given crt */
3029 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003030
3031 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003032 if( NULL != f_vrfy )
3033 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003034 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003035 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003036 else
3037 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003038 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003039 else if( *flags != 0 )
3040 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003041
Paul Bakker5121ce52009-01-03 21:22:43 +00003042 return( 0 );
3043}
3044
3045/*
3046 * Unallocate all certificate data
3047 */
3048void x509_free( x509_cert *crt )
3049{
3050 x509_cert *cert_cur = crt;
3051 x509_cert *cert_prv;
3052 x509_name *name_cur;
3053 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003054 x509_sequence *seq_cur;
3055 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
3057 if( crt == NULL )
3058 return;
3059
3060 do
3061 {
3062 rsa_free( &cert_cur->rsa );
3063
3064 name_cur = cert_cur->issuer.next;
3065 while( name_cur != NULL )
3066 {
3067 name_prv = name_cur;
3068 name_cur = name_cur->next;
3069 memset( name_prv, 0, sizeof( x509_name ) );
3070 free( name_prv );
3071 }
3072
3073 name_cur = cert_cur->subject.next;
3074 while( name_cur != NULL )
3075 {
3076 name_prv = name_cur;
3077 name_cur = name_cur->next;
3078 memset( name_prv, 0, sizeof( x509_name ) );
3079 free( name_prv );
3080 }
3081
Paul Bakker74111d32011-01-15 16:57:55 +00003082 seq_cur = cert_cur->ext_key_usage.next;
3083 while( seq_cur != NULL )
3084 {
3085 seq_prv = seq_cur;
3086 seq_cur = seq_cur->next;
3087 memset( seq_prv, 0, sizeof( x509_sequence ) );
3088 free( seq_prv );
3089 }
3090
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 if( cert_cur->raw.p != NULL )
3092 {
3093 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3094 free( cert_cur->raw.p );
3095 }
3096
3097 cert_cur = cert_cur->next;
3098 }
3099 while( cert_cur != NULL );
3100
3101 cert_cur = crt;
3102 do
3103 {
3104 cert_prv = cert_cur;
3105 cert_cur = cert_cur->next;
3106
3107 memset( cert_prv, 0, sizeof( x509_cert ) );
3108 if( cert_prv != crt )
3109 free( cert_prv );
3110 }
3111 while( cert_cur != NULL );
3112}
3113
Paul Bakkerd98030e2009-05-02 15:13:40 +00003114/*
3115 * Unallocate all CRL data
3116 */
3117void x509_crl_free( x509_crl *crl )
3118{
3119 x509_crl *crl_cur = crl;
3120 x509_crl *crl_prv;
3121 x509_name *name_cur;
3122 x509_name *name_prv;
3123 x509_crl_entry *entry_cur;
3124 x509_crl_entry *entry_prv;
3125
3126 if( crl == NULL )
3127 return;
3128
3129 do
3130 {
3131 name_cur = crl_cur->issuer.next;
3132 while( name_cur != NULL )
3133 {
3134 name_prv = name_cur;
3135 name_cur = name_cur->next;
3136 memset( name_prv, 0, sizeof( x509_name ) );
3137 free( name_prv );
3138 }
3139
3140 entry_cur = crl_cur->entry.next;
3141 while( entry_cur != NULL )
3142 {
3143 entry_prv = entry_cur;
3144 entry_cur = entry_cur->next;
3145 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3146 free( entry_prv );
3147 }
3148
3149 if( crl_cur->raw.p != NULL )
3150 {
3151 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3152 free( crl_cur->raw.p );
3153 }
3154
3155 crl_cur = crl_cur->next;
3156 }
3157 while( crl_cur != NULL );
3158
3159 crl_cur = crl;
3160 do
3161 {
3162 crl_prv = crl_cur;
3163 crl_cur = crl_cur->next;
3164
3165 memset( crl_prv, 0, sizeof( x509_crl ) );
3166 if( crl_prv != crl )
3167 free( crl_prv );
3168 }
3169 while( crl_cur != NULL );
3170}
3171
Paul Bakker40e46942009-01-03 21:51:57 +00003172#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
Paul Bakker40e46942009-01-03 21:51:57 +00003174#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003175
3176/*
3177 * Checkup routine
3178 */
3179int x509_self_test( int verbose )
3180{
Paul Bakker5690efc2011-05-26 13:16:06 +00003181#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003182 int ret;
3183 int flags;
3184 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003185 x509_cert cacert;
3186 x509_cert clicert;
3187 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003188#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003189 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003190#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
3192 if( verbose != 0 )
3193 printf( " X.509 certificate load: " );
3194
3195 memset( &clicert, 0, sizeof( x509_cert ) );
3196
3197 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003198 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 if( ret != 0 )
3200 {
3201 if( verbose != 0 )
3202 printf( "failed\n" );
3203
3204 return( ret );
3205 }
3206
3207 memset( &cacert, 0, sizeof( x509_cert ) );
3208
3209 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003210 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 if( ret != 0 )
3212 {
3213 if( verbose != 0 )
3214 printf( "failed\n" );
3215
3216 return( ret );
3217 }
3218
3219 if( verbose != 0 )
3220 printf( "passed\n X.509 private key load: " );
3221
3222 i = strlen( test_ca_key );
3223 j = strlen( test_ca_pwd );
3224
Paul Bakker66b78b22011-03-25 14:22:50 +00003225 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3226
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 if( ( ret = x509parse_key( &rsa,
3228 (unsigned char *) test_ca_key, i,
3229 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3230 {
3231 if( verbose != 0 )
3232 printf( "failed\n" );
3233
3234 return( ret );
3235 }
3236
3237 if( verbose != 0 )
3238 printf( "passed\n X.509 signature verify: ");
3239
Paul Bakker23986e52011-04-24 08:57:21 +00003240 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003241 if( ret != 0 )
3242 {
Paul Bakker23986e52011-04-24 08:57:21 +00003243 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 if( verbose != 0 )
3245 printf( "failed\n" );
3246
3247 return( ret );
3248 }
3249
Paul Bakker5690efc2011-05-26 13:16:06 +00003250#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003252 printf( "passed\n X.509 DHM parameter load: " );
3253
3254 i = strlen( test_dhm_params );
3255 j = strlen( test_ca_pwd );
3256
3257 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3258 {
3259 if( verbose != 0 )
3260 printf( "failed\n" );
3261
3262 return( ret );
3263 }
3264
3265 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003267#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003268
3269 x509_free( &cacert );
3270 x509_free( &clicert );
3271 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003272#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003273 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003275
3276 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003277#else
3278 ((void) verbose);
3279 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003281}
3282
3283#endif
3284
3285#endif