blob: 12ad67c1d61883d1a66ca1d5e90afb2c687b5e8b [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 Bakker2b245eb2009-04-19 18:44:26 +00001730
Paul Bakkerd98030e2009-05-02 15:13:40 +00001731 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001732 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001733
Paul Bakkerd98030e2009-05-02 15:13:40 +00001734 fseek( f, 0, SEEK_END );
1735 *n = (size_t) ftell( f );
1736 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001737
Paul Bakkerd98030e2009-05-02 15:13:40 +00001738 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001739 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001740
Paul Bakkerd98030e2009-05-02 15:13:40 +00001741 if( fread( *buf, 1, *n, f ) != *n )
1742 {
1743 fclose( f );
1744 free( *buf );
Paul Bakker732e1a82011-12-11 16:35:09 +00001745 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001746 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001747
Paul Bakkerd98030e2009-05-02 15:13:40 +00001748 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001749
Paul Bakkerd98030e2009-05-02 15:13:40 +00001750 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001751
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001753}
1754
1755/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001756 * Load one or more certificates and add them to the chained list
1757 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001758int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001759{
1760 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 size_t n;
1762 unsigned char *buf;
1763
Paul Bakker732e1a82011-12-11 16:35:09 +00001764 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1765 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001766
Paul Bakker732e1a82011-12-11 16:35:09 +00001767 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
1769 memset( buf, 0, n + 1 );
1770 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
1772 return( ret );
1773}
1774
Paul Bakkerd98030e2009-05-02 15:13:40 +00001775/*
1776 * Load one or more CRLs and add them to the chained list
1777 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001778int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001779{
1780 int ret;
1781 size_t n;
1782 unsigned char *buf;
1783
Paul Bakker732e1a82011-12-11 16:35:09 +00001784 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1785 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001786
Paul Bakker27fdf462011-06-09 13:55:13 +00001787 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001788
1789 memset( buf, 0, n + 1 );
1790 free( buf );
1791
1792 return( ret );
1793}
1794
Paul Bakker5121ce52009-01-03 21:22:43 +00001795/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001796 * Load and parse a private RSA key
1797 */
1798int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1799{
1800 int ret;
1801 size_t n;
1802 unsigned char *buf;
1803
Paul Bakker732e1a82011-12-11 16:35:09 +00001804 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1805 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001806
1807 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001808 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001809 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001810 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001811 (unsigned char *) pwd, strlen( pwd ) );
1812
1813 memset( buf, 0, n + 1 );
1814 free( buf );
1815
1816 return( ret );
1817}
1818
1819/*
1820 * Load and parse a public RSA key
1821 */
1822int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1823{
1824 int ret;
1825 size_t n;
1826 unsigned char *buf;
1827
Paul Bakker732e1a82011-12-11 16:35:09 +00001828 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1829 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001830
Paul Bakker27fdf462011-06-09 13:55:13 +00001831 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001832
1833 memset( buf, 0, n + 1 );
1834 free( buf );
1835
1836 return( ret );
1837}
1838#endif /* POLARSSL_FS_IO */
1839
1840/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 * Parse a private RSA key
1842 */
Paul Bakker23986e52011-04-24 08:57:21 +00001843int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1844 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001845{
Paul Bakker23986e52011-04-24 08:57:21 +00001846 int ret;
1847 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001849 unsigned char *p_alt;
1850 x509_buf pk_alg_oid;
1851
Paul Bakker96743fc2011-02-12 14:30:57 +00001852#if defined(POLARSSL_PEM_C)
1853 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Paul Bakker96743fc2011-02-12 14:30:57 +00001855 pem_init( &pem );
1856 ret = pem_read_buffer( &pem,
1857 "-----BEGIN RSA PRIVATE KEY-----",
1858 "-----END RSA PRIVATE KEY-----",
1859 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Paul Bakker03a85bc2013-06-19 12:06:00 +02001861 if( ret == POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkered56b222011-07-13 11:26:43 +00001862 {
1863 ret = pem_read_buffer( &pem,
1864 "-----BEGIN PRIVATE KEY-----",
1865 "-----END PRIVATE KEY-----",
1866 key, pwd, pwdlen, &len );
1867 }
1868
Paul Bakker96743fc2011-02-12 14:30:57 +00001869 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001871 /*
1872 * Was PEM encoded
1873 */
1874 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001876 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001877 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001878 pem_free( &pem );
1879 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
Paul Bakker96743fc2011-02-12 14:30:57 +00001882 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1883#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001884 ((void) pwd);
1885 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001886 p = (unsigned char *) key;
1887#endif
1888 end = p + keylen;
1889
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001891 * Note: Depending on the type of private key file one can expect either a
1892 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1893 *
1894 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001895 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001896 * algorithm AlgorithmIdentifier,
1897 * PrivateKey BIT STRING
1898 * }
1899 *
1900 * AlgorithmIdentifier ::= SEQUENCE {
1901 * algorithm OBJECT IDENTIFIER,
1902 * parameters ANY DEFINED BY algorithm OPTIONAL
1903 * }
1904 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 * RSAPrivateKey ::= SEQUENCE {
1906 * version Version,
1907 * modulus INTEGER, -- n
1908 * publicExponent INTEGER, -- e
1909 * privateExponent INTEGER, -- d
1910 * prime1 INTEGER, -- p
1911 * prime2 INTEGER, -- q
1912 * exponent1 INTEGER, -- d mod (p-1)
1913 * exponent2 INTEGER, -- d mod (q-1)
1914 * coefficient INTEGER, -- (inverse of q) mod p
1915 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1916 * }
1917 */
1918 if( ( ret = asn1_get_tag( &p, end, &len,
1919 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1920 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001921#if defined(POLARSSL_PEM_C)
1922 pem_free( &pem );
1923#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001925 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 }
1927
1928 end = p + len;
1929
1930 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 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 if( rsa->ver != 0 )
1940 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001941#if defined(POLARSSL_PEM_C)
1942 pem_free( &pem );
1943#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001945 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 }
1947
Paul Bakkered56b222011-07-13 11:26:43 +00001948 p_alt = p;
1949
1950 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1951 {
1952 // Assume that we have the PKCS#1 format if wrong
1953 // tag was encountered
1954 //
1955 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1956 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1957 {
1958#if defined(POLARSSL_PEM_C)
1959 pem_free( &pem );
1960#endif
1961 rsa_free( rsa );
1962 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1963 }
1964 }
1965 else
1966 {
1967 int can_handle;
1968
1969 /*
1970 * only RSA keys handled at this time
1971 */
1972 can_handle = 0;
1973
1974 if( pk_alg_oid.len == 9 &&
1975 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1976 can_handle = 1;
1977
1978 if( pk_alg_oid.len == 9 &&
1979 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1980 {
1981 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1982 can_handle = 1;
1983
1984 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1985 can_handle = 1;
1986 }
1987
1988 if( pk_alg_oid.len == 5 &&
1989 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
1990 can_handle = 1;
1991
1992 if( can_handle == 0 )
1993 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
1994
1995 /*
1996 * Parse the PKCS#8 format
1997 */
1998
1999 p = p_alt;
2000 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2001 {
2002#if defined(POLARSSL_PEM_C)
2003 pem_free( &pem );
2004#endif
2005 rsa_free( rsa );
2006 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2007 }
2008
2009 if( ( end - p ) < 1 )
2010 {
2011#if defined(POLARSSL_PEM_C)
2012 pem_free( &pem );
2013#endif
2014 rsa_free( rsa );
2015 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2016 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2017 }
2018
2019 end = p + len;
2020
2021 if( ( ret = asn1_get_tag( &p, end, &len,
2022 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2023 {
2024#if defined(POLARSSL_PEM_C)
2025 pem_free( &pem );
2026#endif
2027 rsa_free( rsa );
2028 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2029 }
2030
2031 end = p + len;
2032
2033 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 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 if( rsa->ver != 0 )
2043 {
2044#if defined(POLARSSL_PEM_C)
2045 pem_free( &pem );
2046#endif
2047 rsa_free( rsa );
2048 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2049 }
2050 }
2051
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2053 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2054 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2055 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2056 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2057 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2058 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2059 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2060 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002061#if defined(POLARSSL_PEM_C)
2062 pem_free( &pem );
2063#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002065 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 }
2067
2068 rsa->len = mpi_size( &rsa->N );
2069
2070 if( p != end )
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 +
Paul Bakker40e46942009-01-03 21:51:57 +00002077 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 }
2079
2080 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2081 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002082#if defined(POLARSSL_PEM_C)
2083 pem_free( &pem );
2084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 rsa_free( rsa );
2086 return( ret );
2087 }
2088
Paul Bakker96743fc2011-02-12 14:30:57 +00002089#if defined(POLARSSL_PEM_C)
2090 pem_free( &pem );
2091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002092
2093 return( 0 );
2094}
2095
2096/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002097 * Parse a public RSA key
2098 */
Paul Bakker23986e52011-04-24 08:57:21 +00002099int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002100{
Paul Bakker23986e52011-04-24 08:57:21 +00002101 int ret;
2102 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002103 unsigned char *p, *end;
2104 x509_buf alg_oid;
2105#if defined(POLARSSL_PEM_C)
2106 pem_context pem;
2107
2108 pem_init( &pem );
2109 ret = pem_read_buffer( &pem,
2110 "-----BEGIN PUBLIC KEY-----",
2111 "-----END PUBLIC KEY-----",
2112 key, NULL, 0, &len );
2113
2114 if( ret == 0 )
2115 {
2116 /*
2117 * Was PEM encoded
2118 */
2119 keylen = pem.buflen;
2120 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02002121 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002122 {
2123 pem_free( &pem );
2124 return( ret );
2125 }
2126
2127 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2128#else
2129 p = (unsigned char *) key;
2130#endif
2131 end = p + keylen;
2132
2133 /*
2134 * PublicKeyInfo ::= SEQUENCE {
2135 * algorithm AlgorithmIdentifier,
2136 * PublicKey BIT STRING
2137 * }
2138 *
2139 * AlgorithmIdentifier ::= SEQUENCE {
2140 * algorithm OBJECT IDENTIFIER,
2141 * parameters ANY DEFINED BY algorithm OPTIONAL
2142 * }
2143 *
2144 * RSAPublicKey ::= SEQUENCE {
2145 * modulus INTEGER, -- n
2146 * publicExponent INTEGER -- e
2147 * }
2148 */
2149
2150 if( ( ret = asn1_get_tag( &p, end, &len,
2151 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2152 {
2153#if defined(POLARSSL_PEM_C)
2154 pem_free( &pem );
2155#endif
2156 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002157 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002158 }
2159
2160 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2161 {
2162#if defined(POLARSSL_PEM_C)
2163 pem_free( &pem );
2164#endif
2165 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002166 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002167 }
2168
2169 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2170 {
2171#if defined(POLARSSL_PEM_C)
2172 pem_free( &pem );
2173#endif
2174 rsa_free( rsa );
2175 return( ret );
2176 }
2177
2178 rsa->len = mpi_size( &rsa->N );
2179
2180#if defined(POLARSSL_PEM_C)
2181 pem_free( &pem );
2182#endif
2183
2184 return( 0 );
2185}
2186
Paul Bakkereaa89f82011-04-04 21:36:15 +00002187#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002188/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002189 * Parse DHM parameters
2190 */
Paul Bakker23986e52011-04-24 08:57:21 +00002191int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002192{
Paul Bakker23986e52011-04-24 08:57:21 +00002193 int ret;
2194 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002195 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002196#if defined(POLARSSL_PEM_C)
2197 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002198
Paul Bakker96743fc2011-02-12 14:30:57 +00002199 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002200
Paul Bakker96743fc2011-02-12 14:30:57 +00002201 ret = pem_read_buffer( &pem,
2202 "-----BEGIN DH PARAMETERS-----",
2203 "-----END DH PARAMETERS-----",
2204 dhmin, NULL, 0, &dhminlen );
2205
2206 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002207 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002208 /*
2209 * Was PEM encoded
2210 */
2211 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002212 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02002213 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002214 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002215 pem_free( &pem );
2216 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002217 }
2218
Paul Bakker96743fc2011-02-12 14:30:57 +00002219 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2220#else
2221 p = (unsigned char *) dhmin;
2222#endif
2223 end = p + dhminlen;
2224
Paul Bakker1b57b062011-01-06 15:48:19 +00002225 memset( dhm, 0, sizeof( dhm_context ) );
2226
Paul Bakker1b57b062011-01-06 15:48:19 +00002227 /*
2228 * DHParams ::= SEQUENCE {
2229 * prime INTEGER, -- P
2230 * generator INTEGER, -- g
2231 * }
2232 */
2233 if( ( ret = asn1_get_tag( &p, end, &len,
2234 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2235 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002236#if defined(POLARSSL_PEM_C)
2237 pem_free( &pem );
2238#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002239 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002240 }
2241
2242 end = p + len;
2243
2244 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2245 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2246 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002247#if defined(POLARSSL_PEM_C)
2248 pem_free( &pem );
2249#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002250 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002251 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002252 }
2253
2254 if( p != end )
2255 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002256#if defined(POLARSSL_PEM_C)
2257 pem_free( &pem );
2258#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002259 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002260 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002261 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2262 }
2263
Paul Bakker96743fc2011-02-12 14:30:57 +00002264#if defined(POLARSSL_PEM_C)
2265 pem_free( &pem );
2266#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002267
2268 return( 0 );
2269}
2270
Paul Bakker335db3f2011-04-25 15:28:35 +00002271#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002272/*
2273 * Load and parse a private RSA key
2274 */
2275int x509parse_dhmfile( dhm_context *dhm, const char *path )
2276{
2277 int ret;
2278 size_t n;
2279 unsigned char *buf;
2280
Paul Bakker732e1a82011-12-11 16:35:09 +00002281 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2282 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002283
Paul Bakker27fdf462011-06-09 13:55:13 +00002284 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002285
2286 memset( buf, 0, n + 1 );
2287 free( buf );
2288
2289 return( ret );
2290}
Paul Bakker335db3f2011-04-25 15:28:35 +00002291#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002292#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002293
Paul Bakker5121ce52009-01-03 21:22:43 +00002294#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002295#include <stdarg.h>
2296
2297#if !defined vsnprintf
2298#define vsnprintf _vsnprintf
2299#endif // vsnprintf
2300
2301/*
2302 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2303 * Result value is not size of buffer needed, but -1 if no fit is possible.
2304 *
2305 * This fuction tries to 'fix' this by at least suggesting enlarging the
2306 * size by 20.
2307 */
2308int compat_snprintf(char *str, size_t size, const char *format, ...)
2309{
2310 va_list ap;
2311 int res = -1;
2312
2313 va_start( ap, format );
2314
2315 res = vsnprintf( str, size, format, ap );
2316
2317 va_end( ap );
2318
2319 // No quick fix possible
2320 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002321 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002322
2323 return res;
2324}
2325
2326#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002327#endif
2328
Paul Bakkerd98030e2009-05-02 15:13:40 +00002329#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2330
2331#define SAFE_SNPRINTF() \
2332{ \
2333 if( ret == -1 ) \
2334 return( -1 ); \
2335 \
Paul Bakker23986e52011-04-24 08:57:21 +00002336 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002337 p[n - 1] = '\0'; \
2338 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2339 } \
2340 \
Paul Bakker23986e52011-04-24 08:57:21 +00002341 n -= (unsigned int) ret; \
2342 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002343}
2344
Paul Bakker5121ce52009-01-03 21:22:43 +00002345/*
2346 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002347 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002349int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002350{
Paul Bakker23986e52011-04-24 08:57:21 +00002351 int ret;
2352 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002354 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 char s[128], *p;
2356
2357 memset( s, 0, sizeof( s ) );
2358
2359 name = dn;
2360 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002361 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002362
2363 while( name != NULL )
2364 {
Paul Bakker74111d32011-01-15 16:57:55 +00002365 if( name != dn )
2366 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002367 ret = snprintf( p, n, ", " );
2368 SAFE_SNPRINTF();
2369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
Paul Bakker7261cba2013-01-16 12:39:54 +01002371 if( name->oid.len == 3 &&
2372 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 {
2374 switch( name->oid.p[2] )
2375 {
2376 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002377 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
2379 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002380 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
2382 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002383 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
2385 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002386 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
2388 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002389 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
2391 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002392 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
2394 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002395 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 name->oid.p[2] );
2397 break;
2398 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002399 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
Paul Bakker7261cba2013-01-16 12:39:54 +01002401 else if( name->oid.len == 9 &&
2402 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 {
2404 switch( name->oid.p[8] )
2405 {
2406 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002407 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
2409 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002410 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 name->oid.p[8] );
2412 break;
2413 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002414 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 }
2416 else
Paul Bakker74111d32011-01-15 16:57:55 +00002417 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002418 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002419 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
2422 for( i = 0; i < name->val.len; i++ )
2423 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002424 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 break;
2426
2427 c = name->val.p[i];
2428 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2429 s[i] = '?';
2430 else s[i] = c;
2431 }
2432 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002433 ret = snprintf( p, n, "%s", s );
2434 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 name = name->next;
2436 }
2437
Paul Bakker23986e52011-04-24 08:57:21 +00002438 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002439}
2440
2441/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002442 * Store the serial in printable form into buf; no more
2443 * than size characters will be written
2444 */
2445int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2446{
Paul Bakker23986e52011-04-24 08:57:21 +00002447 int ret;
2448 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002449 char *p;
2450
2451 p = buf;
2452 n = size;
2453
2454 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002455 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002456
2457 for( i = 0; i < nr; i++ )
2458 {
Paul Bakker93048802011-12-05 14:38:06 +00002459 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002460 continue;
2461
Paul Bakkerdd476992011-01-16 21:34:59 +00002462 ret = snprintf( p, n, "%02X%s",
2463 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2464 SAFE_SNPRINTF();
2465 }
2466
Paul Bakker03c7c252011-11-25 12:37:37 +00002467 if( nr != serial->len )
2468 {
2469 ret = snprintf( p, n, "...." );
2470 SAFE_SNPRINTF();
2471 }
2472
Paul Bakker23986e52011-04-24 08:57:21 +00002473 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002474}
2475
2476/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002477 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002479int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2480 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002481{
Paul Bakker23986e52011-04-24 08:57:21 +00002482 int ret;
2483 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002484 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
2486 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002487 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
Paul Bakkerd98030e2009-05-02 15:13:40 +00002489 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002491 SAFE_SNPRINTF();
2492 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002494 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Paul Bakkerdd476992011-01-16 21:34:59 +00002496 ret = x509parse_serial_gets( p, n, &crt->serial);
2497 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
Paul Bakkerd98030e2009-05-02 15:13:40 +00002499 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2500 SAFE_SNPRINTF();
2501 ret = x509parse_dn_gets( p, n, &crt->issuer );
2502 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Paul Bakkerd98030e2009-05-02 15:13:40 +00002504 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2505 SAFE_SNPRINTF();
2506 ret = x509parse_dn_gets( p, n, &crt->subject );
2507 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Paul Bakkerd98030e2009-05-02 15:13:40 +00002509 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2511 crt->valid_from.year, crt->valid_from.mon,
2512 crt->valid_from.day, crt->valid_from.hour,
2513 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002514 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Paul Bakkerd98030e2009-05-02 15:13:40 +00002516 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2518 crt->valid_to.year, crt->valid_to.mon,
2519 crt->valid_to.day, crt->valid_to.hour,
2520 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002521 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Paul Bakkerd98030e2009-05-02 15:13:40 +00002523 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2524 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Paul Bakker27d66162010-03-17 06:56:01 +00002526 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002528 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2529 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2530 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2531 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2532 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2533 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2534 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2535 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2536 default: ret = snprintf( p, n, "???" ); break;
2537 }
2538 SAFE_SNPRINTF();
2539
2540 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002541 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002542 SAFE_SNPRINTF();
2543
Paul Bakker23986e52011-04-24 08:57:21 +00002544 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002545}
2546
Paul Bakker74111d32011-01-15 16:57:55 +00002547/* Compare a given OID string with an OID x509_buf * */
2548#define OID_CMP(oid_str, oid_buf) \
2549 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2550 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2551
2552/*
2553 * Return an informational string describing the given OID
2554 */
2555const char *x509_oid_get_description( x509_buf *oid )
2556{
2557 if ( oid == NULL )
2558 return ( NULL );
2559
2560 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2561 return( STRING_SERVER_AUTH );
2562
2563 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2564 return( STRING_CLIENT_AUTH );
2565
2566 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2567 return( STRING_CODE_SIGNING );
2568
2569 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2570 return( STRING_EMAIL_PROTECTION );
2571
2572 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2573 return( STRING_TIME_STAMPING );
2574
2575 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2576 return( STRING_OCSP_SIGNING );
2577
2578 return( NULL );
2579}
2580
2581/* Return the x.y.z.... style numeric string for the given OID */
2582int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2583{
Paul Bakker23986e52011-04-24 08:57:21 +00002584 int ret;
2585 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002586 unsigned int value;
2587 char *p;
2588
2589 p = buf;
2590 n = size;
2591
2592 /* First byte contains first two dots */
2593 if( oid->len > 0 )
2594 {
2595 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2596 SAFE_SNPRINTF();
2597 }
2598
2599 /* TODO: value can overflow in value. */
2600 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002601 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002602 {
2603 value <<= 7;
2604 value += oid->p[i] & 0x7F;
2605
2606 if( !( oid->p[i] & 0x80 ) )
2607 {
2608 /* Last byte */
2609 ret = snprintf( p, n, ".%d", value );
2610 SAFE_SNPRINTF();
2611 value = 0;
2612 }
2613 }
2614
Paul Bakker23986e52011-04-24 08:57:21 +00002615 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002616}
2617
Paul Bakkerd98030e2009-05-02 15:13:40 +00002618/*
2619 * Return an informational string about the CRL.
2620 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002621int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2622 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002623{
Paul Bakker23986e52011-04-24 08:57:21 +00002624 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002625 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002626 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002627 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002628
2629 p = buf;
2630 n = size;
2631
2632 ret = snprintf( p, n, "%sCRL version : %d",
2633 prefix, crl->version );
2634 SAFE_SNPRINTF();
2635
2636 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2637 SAFE_SNPRINTF();
2638 ret = x509parse_dn_gets( p, n, &crl->issuer );
2639 SAFE_SNPRINTF();
2640
2641 ret = snprintf( p, n, "\n%sthis update : " \
2642 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2643 crl->this_update.year, crl->this_update.mon,
2644 crl->this_update.day, crl->this_update.hour,
2645 crl->this_update.min, crl->this_update.sec );
2646 SAFE_SNPRINTF();
2647
2648 ret = snprintf( p, n, "\n%snext update : " \
2649 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2650 crl->next_update.year, crl->next_update.mon,
2651 crl->next_update.day, crl->next_update.hour,
2652 crl->next_update.min, crl->next_update.sec );
2653 SAFE_SNPRINTF();
2654
2655 entry = &crl->entry;
2656
2657 ret = snprintf( p, n, "\n%sRevoked certificates:",
2658 prefix );
2659 SAFE_SNPRINTF();
2660
Paul Bakker9be19372009-07-27 20:21:53 +00002661 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002662 {
2663 ret = snprintf( p, n, "\n%sserial number: ",
2664 prefix );
2665 SAFE_SNPRINTF();
2666
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002667 ret = x509parse_serial_gets( p, n, &entry->serial);
2668 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002669
Paul Bakkerd98030e2009-05-02 15:13:40 +00002670 ret = snprintf( p, n, " revocation date: " \
2671 "%04d-%02d-%02d %02d:%02d:%02d",
2672 entry->revocation_date.year, entry->revocation_date.mon,
2673 entry->revocation_date.day, entry->revocation_date.hour,
2674 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002675 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002676
2677 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 }
2679
Paul Bakkerd98030e2009-05-02 15:13:40 +00002680 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2681 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Paul Bakker27d66162010-03-17 06:56:01 +00002683 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002684 {
2685 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2686 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2687 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2688 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2689 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2690 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2691 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2692 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2693 default: ret = snprintf( p, n, "???" ); break;
2694 }
2695 SAFE_SNPRINTF();
2696
Paul Bakker1e27bb22009-07-19 20:25:25 +00002697 ret = snprintf( p, n, "\n" );
2698 SAFE_SNPRINTF();
2699
Paul Bakker23986e52011-04-24 08:57:21 +00002700 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701}
2702
2703/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002704 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002706int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707{
Paul Bakkercce9d772011-11-18 14:26:47 +00002708 int year, mon, day;
2709 int hour, min, sec;
2710
2711#if defined(_WIN32)
2712 SYSTEMTIME st;
2713
2714 GetLocalTime(&st);
2715
2716 year = st.wYear;
2717 mon = st.wMonth;
2718 day = st.wDay;
2719 hour = st.wHour;
2720 min = st.wMinute;
2721 sec = st.wSecond;
2722#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 struct tm *lt;
2724 time_t tt;
2725
2726 tt = time( NULL );
2727 lt = localtime( &tt );
2728
Paul Bakkercce9d772011-11-18 14:26:47 +00002729 year = lt->tm_year + 1900;
2730 mon = lt->tm_mon + 1;
2731 day = lt->tm_mday;
2732 hour = lt->tm_hour;
2733 min = lt->tm_min;
2734 sec = lt->tm_sec;
2735#endif
2736
2737 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002738 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Paul Bakkercce9d772011-11-18 14:26:47 +00002740 if( year == to->year &&
2741 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002742 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
Paul Bakkercce9d772011-11-18 14:26:47 +00002744 if( year == to->year &&
2745 mon == to->mon &&
2746 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002747 return( 1 );
2748
Paul Bakkercce9d772011-11-18 14:26:47 +00002749 if( year == to->year &&
2750 mon == to->mon &&
2751 day == to->day &&
2752 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002753 return( 1 );
2754
Paul Bakkercce9d772011-11-18 14:26:47 +00002755 if( year == to->year &&
2756 mon == to->mon &&
2757 day == to->day &&
2758 hour == to->hour &&
2759 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002760 return( 1 );
2761
Paul Bakkercce9d772011-11-18 14:26:47 +00002762 if( year == to->year &&
2763 mon == to->mon &&
2764 day == to->day &&
2765 hour == to->hour &&
2766 min == to->min &&
2767 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002768 return( 1 );
2769
Paul Bakker40ea7de2009-05-03 10:18:48 +00002770 return( 0 );
2771}
2772
2773/*
2774 * Return 1 if the certificate is revoked, or 0 otherwise.
2775 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002776int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002777{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002778 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002779
2780 while( cur != NULL && cur->serial.len != 0 )
2781 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002782 if( crt->serial.len == cur->serial.len &&
2783 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002784 {
2785 if( x509parse_time_expired( &cur->revocation_date ) )
2786 return( 1 );
2787 }
2788
2789 cur = cur->next;
2790 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
2792 return( 0 );
2793}
2794
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002795/*
2796 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002797 */
Paul Bakker23986e52011-04-24 08:57:21 +00002798static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 unsigned char *out )
2800{
2801 switch( alg )
2802 {
Paul Bakker40e46942009-01-03 21:51:57 +00002803#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002804 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002805#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002806#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002807 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002809#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002810 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002811#endif
2812#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002813 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002814#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002815#if defined(POLARSSL_SHA2_C)
2816 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2817 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2818#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002819#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002820 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2821 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2822#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002824 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 break;
2826 }
2827}
2828
2829/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002830 * Check that the given certificate is valid accoring to the CRL.
2831 */
2832static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2833 x509_crl *crl_list)
2834{
2835 int flags = 0;
2836 int hash_id;
2837 unsigned char hash[64];
2838
2839 /*
2840 * TODO: What happens if no CRL is present?
2841 * Suggestion: Revocation state should be unknown if no CRL is present.
2842 * For backwards compatibility this is not yet implemented.
2843 */
2844
2845 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2846 {
2847 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2848 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2849 crl_list->issuer_raw.len ) != 0 )
2850 {
2851 crl_list = crl_list->next;
2852 continue;
2853 }
2854
2855 /*
2856 * Check if CRL is correctly signed by the trusted CA
2857 */
2858 hash_id = crl_list->sig_alg;
2859
2860 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2861
2862 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2863 0, hash, crl_list->sig.p ) == 0 )
2864 {
2865 /*
2866 * CRL is not trusted
2867 */
2868 flags |= BADCRL_NOT_TRUSTED;
2869 break;
2870 }
2871
2872 /*
2873 * Check for validity of CRL (Do not drop out)
2874 */
2875 if( x509parse_time_expired( &crl_list->next_update ) )
2876 flags |= BADCRL_EXPIRED;
2877
2878 /*
2879 * Check if certificate is revoked
2880 */
2881 if( x509parse_revoked(crt, crl_list) )
2882 {
2883 flags |= BADCERT_REVOKED;
2884 break;
2885 }
2886
2887 crl_list = crl_list->next;
2888 }
2889 return flags;
2890}
2891
2892/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002893 * Verify the certificate validity
2894 */
2895int x509parse_verify( x509_cert *crt,
2896 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002897 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002898 const char *cn, int *flags,
2899 int (*f_vrfy)(void *, x509_cert *, int, int),
2900 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002901{
Paul Bakker23986e52011-04-24 08:57:21 +00002902 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 int hash_id;
2904 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002905 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002907 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Paul Bakker40ea7de2009-05-03 10:18:48 +00002909 *flags = 0;
2910
2911 if( x509parse_time_expired( &crt->valid_to ) )
2912 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
2914 if( cn != NULL )
2915 {
2916 name = &crt->subject;
2917 cn_len = strlen( cn );
2918
2919 while( name != NULL )
2920 {
Paul Bakker7261cba2013-01-16 12:39:54 +01002921 if( name->oid.len == 3 &&
2922 memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2923 name->val.len == cn_len &&
2924 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 break;
2926
2927 name = name->next;
2928 }
2929
2930 if( name == NULL )
2931 *flags |= BADCERT_CN_MISMATCH;
2932 }
2933
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 /*
2935 * Iterate upwards in the given cert chain,
2936 * ignoring any upper cert with CA != TRUE.
2937 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002938 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
2940 pathlen = 1;
2941
Paul Bakker76fd75a2011-01-16 21:12:10 +00002942 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002943 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002944 if( parent->ca_istrue == 0 ||
2945 crt->issuer_raw.len != parent->subject_raw.len ||
2946 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002947 crt->issuer_raw.len ) != 0 )
2948 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002949 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 continue;
2951 }
2952
Paul Bakker27d66162010-03-17 06:56:01 +00002953 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
2955 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2956
Paul Bakker76fd75a2011-01-16 21:12:10 +00002957 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2958 crt->sig.p ) != 0 )
2959 *flags |= BADCERT_NOT_TRUSTED;
2960
2961 /* Check trusted CA's CRL for the given crt */
2962 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002963
2964 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002965 if( NULL != f_vrfy )
2966 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002967 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002968 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002969 else
2970 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002971 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002972 else if( *flags != 0 )
2973 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
2975 pathlen++;
2976
Paul Bakker76fd75a2011-01-16 21:12:10 +00002977 crt = parent;
2978 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 }
2980
2981 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002982 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002984 *flags |= BADCERT_NOT_TRUSTED;
2985
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002986 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 {
2988 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2989 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2990 crt->issuer_raw.len ) != 0 )
2991 {
2992 trust_ca = trust_ca->next;
2993 continue;
2994 }
2995
2996 if( trust_ca->max_pathlen > 0 &&
2997 trust_ca->max_pathlen < pathlen )
2998 break;
2999
Paul Bakker27d66162010-03-17 06:56:01 +00003000 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
3002 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3003
3004 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3005 0, hash, crt->sig.p ) == 0 )
3006 {
3007 /*
3008 * cert. is signed by a trusted CA
3009 */
3010 *flags &= ~BADCERT_NOT_TRUSTED;
3011 break;
3012 }
3013
3014 trust_ca = trust_ca->next;
3015 }
3016
Paul Bakker76fd75a2011-01-16 21:12:10 +00003017 /* Check trusted CA's CRL for the given crt */
3018 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003019
3020 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003021 if( NULL != f_vrfy )
3022 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003023 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003024 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003025 else
3026 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003027 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003028 else if( *flags != 0 )
3029 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003030
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 return( 0 );
3032}
3033
3034/*
3035 * Unallocate all certificate data
3036 */
3037void x509_free( x509_cert *crt )
3038{
3039 x509_cert *cert_cur = crt;
3040 x509_cert *cert_prv;
3041 x509_name *name_cur;
3042 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003043 x509_sequence *seq_cur;
3044 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
3046 if( crt == NULL )
3047 return;
3048
3049 do
3050 {
3051 rsa_free( &cert_cur->rsa );
3052
3053 name_cur = cert_cur->issuer.next;
3054 while( name_cur != NULL )
3055 {
3056 name_prv = name_cur;
3057 name_cur = name_cur->next;
3058 memset( name_prv, 0, sizeof( x509_name ) );
3059 free( name_prv );
3060 }
3061
3062 name_cur = cert_cur->subject.next;
3063 while( name_cur != NULL )
3064 {
3065 name_prv = name_cur;
3066 name_cur = name_cur->next;
3067 memset( name_prv, 0, sizeof( x509_name ) );
3068 free( name_prv );
3069 }
3070
Paul Bakker74111d32011-01-15 16:57:55 +00003071 seq_cur = cert_cur->ext_key_usage.next;
3072 while( seq_cur != NULL )
3073 {
3074 seq_prv = seq_cur;
3075 seq_cur = seq_cur->next;
3076 memset( seq_prv, 0, sizeof( x509_sequence ) );
3077 free( seq_prv );
3078 }
3079
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 if( cert_cur->raw.p != NULL )
3081 {
3082 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3083 free( cert_cur->raw.p );
3084 }
3085
3086 cert_cur = cert_cur->next;
3087 }
3088 while( cert_cur != NULL );
3089
3090 cert_cur = crt;
3091 do
3092 {
3093 cert_prv = cert_cur;
3094 cert_cur = cert_cur->next;
3095
3096 memset( cert_prv, 0, sizeof( x509_cert ) );
3097 if( cert_prv != crt )
3098 free( cert_prv );
3099 }
3100 while( cert_cur != NULL );
3101}
3102
Paul Bakkerd98030e2009-05-02 15:13:40 +00003103/*
3104 * Unallocate all CRL data
3105 */
3106void x509_crl_free( x509_crl *crl )
3107{
3108 x509_crl *crl_cur = crl;
3109 x509_crl *crl_prv;
3110 x509_name *name_cur;
3111 x509_name *name_prv;
3112 x509_crl_entry *entry_cur;
3113 x509_crl_entry *entry_prv;
3114
3115 if( crl == NULL )
3116 return;
3117
3118 do
3119 {
3120 name_cur = crl_cur->issuer.next;
3121 while( name_cur != NULL )
3122 {
3123 name_prv = name_cur;
3124 name_cur = name_cur->next;
3125 memset( name_prv, 0, sizeof( x509_name ) );
3126 free( name_prv );
3127 }
3128
3129 entry_cur = crl_cur->entry.next;
3130 while( entry_cur != NULL )
3131 {
3132 entry_prv = entry_cur;
3133 entry_cur = entry_cur->next;
3134 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3135 free( entry_prv );
3136 }
3137
3138 if( crl_cur->raw.p != NULL )
3139 {
3140 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3141 free( crl_cur->raw.p );
3142 }
3143
3144 crl_cur = crl_cur->next;
3145 }
3146 while( crl_cur != NULL );
3147
3148 crl_cur = crl;
3149 do
3150 {
3151 crl_prv = crl_cur;
3152 crl_cur = crl_cur->next;
3153
3154 memset( crl_prv, 0, sizeof( x509_crl ) );
3155 if( crl_prv != crl )
3156 free( crl_prv );
3157 }
3158 while( crl_cur != NULL );
3159}
3160
Paul Bakker40e46942009-01-03 21:51:57 +00003161#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
Paul Bakker40e46942009-01-03 21:51:57 +00003163#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
3165/*
3166 * Checkup routine
3167 */
3168int x509_self_test( int verbose )
3169{
Paul Bakker5690efc2011-05-26 13:16:06 +00003170#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003171 int ret;
3172 int flags;
3173 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 x509_cert cacert;
3175 x509_cert clicert;
3176 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003177#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003178 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003180
3181 if( verbose != 0 )
3182 printf( " X.509 certificate load: " );
3183
3184 memset( &clicert, 0, sizeof( x509_cert ) );
3185
3186 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003187 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 if( ret != 0 )
3189 {
3190 if( verbose != 0 )
3191 printf( "failed\n" );
3192
3193 return( ret );
3194 }
3195
3196 memset( &cacert, 0, sizeof( x509_cert ) );
3197
3198 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003199 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 if( ret != 0 )
3201 {
3202 if( verbose != 0 )
3203 printf( "failed\n" );
3204
3205 return( ret );
3206 }
3207
3208 if( verbose != 0 )
3209 printf( "passed\n X.509 private key load: " );
3210
3211 i = strlen( test_ca_key );
3212 j = strlen( test_ca_pwd );
3213
Paul Bakker66b78b22011-03-25 14:22:50 +00003214 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3215
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 if( ( ret = x509parse_key( &rsa,
3217 (unsigned char *) test_ca_key, i,
3218 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3219 {
3220 if( verbose != 0 )
3221 printf( "failed\n" );
3222
3223 return( ret );
3224 }
3225
3226 if( verbose != 0 )
3227 printf( "passed\n X.509 signature verify: ");
3228
Paul Bakker23986e52011-04-24 08:57:21 +00003229 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230 if( ret != 0 )
3231 {
Paul Bakker23986e52011-04-24 08:57:21 +00003232 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 if( verbose != 0 )
3234 printf( "failed\n" );
3235
3236 return( ret );
3237 }
3238
Paul Bakker5690efc2011-05-26 13:16:06 +00003239#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003240 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003241 printf( "passed\n X.509 DHM parameter load: " );
3242
3243 i = strlen( test_dhm_params );
3244 j = strlen( test_ca_pwd );
3245
3246 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3247 {
3248 if( verbose != 0 )
3249 printf( "failed\n" );
3250
3251 return( ret );
3252 }
3253
3254 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003255 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
3258 x509_free( &cacert );
3259 x509_free( &clicert );
3260 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003261#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003262 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003263#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003264
3265 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003266#else
3267 ((void) verbose);
3268 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3269#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003270}
3271
3272#endif
3273
3274#endif