blob: d2bfddc74ad07c65f8fbc6b242151ce7ded583a3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerefc30292011-11-10 14:43:23 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000043#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/des.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010045#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000046#include "polarssl/md2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010047#endif
48#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/md4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010050#endif
51#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/md5.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010053#endif
54#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000055#include "polarssl/sha1.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010056#endif
57#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000058#include "polarssl/sha2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010059#endif
60#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000061#include "polarssl/sha4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010062#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000063#include "polarssl/dhm.h"
Paul Bakkercf6e95d2013-06-12 13:18:15 +020064#include "polarssl/pkcs12.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66#include <string.h>
67#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000068#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000069#include <windows.h>
70#else
Paul Bakker5121ce52009-01-03 21:22:43 +000071#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000072#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Paul Bakker335db3f2011-04-25 15:28:35 +000074#if defined(POLARSSL_FS_IO)
75#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000076#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000077#include <sys/types.h>
Paul Bakkercbfcaa92013-06-13 09:20:25 +020078#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000079#include <dirent.h>
80#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000081#endif
82
Paul Bakkercf6e95d2013-06-12 13:18:15 +020083/* Compare a given OID string with an OID x509_buf * */
84#define OID_CMP(oid_str, oid_buf) \
85 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
86 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
87
Paul Bakker5121ce52009-01-03 21:22:43 +000088/*
Paul Bakker5121ce52009-01-03 21:22:43 +000089 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
90 */
91static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000092 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000093 int *ver )
94{
Paul Bakker23986e52011-04-24 08:57:21 +000095 int ret;
96 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 if( ( ret = asn1_get_tag( p, end, &len,
99 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
100 {
Paul Bakker40e46942009-01-03 21:51:57 +0000101 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000102 {
103 *ver = 0;
104 return( 0 );
105 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107 return( ret );
108 }
109
110 end = *p + len;
111
112 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000113 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000116 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000117 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119 return( 0 );
120}
121
122/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000123 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000124 */
125static int x509_crl_get_version( unsigned char **p,
126 const unsigned char *end,
127 int *ver )
128{
129 int ret;
130
131 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
132 {
133 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000134 {
135 *ver = 0;
136 return( 0 );
137 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000138
139 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
140 }
141
142 return( 0 );
143}
144
145/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 * CertificateSerialNumber ::= INTEGER
147 */
148static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000149 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 x509_buf *serial )
151{
152 int ret;
153
154 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000156 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
159 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000160 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000161 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 serial->tag = *(*p)++;
164
165 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000166 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 serial->p = *p;
169 *p += serial->len;
170
171 return( 0 );
172}
173
174/*
175 * AlgorithmIdentifier ::= SEQUENCE {
176 * algorithm OBJECT IDENTIFIER,
177 * parameters ANY DEFINED BY algorithm OPTIONAL }
178 */
179static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000180 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 x509_buf *alg )
182{
Paul Bakker23986e52011-04-24 08:57:21 +0000183 int ret;
184 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186 if( ( ret = asn1_get_tag( p, end, &len,
187 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000188 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190 end = *p + len;
191 alg->tag = **p;
192
193 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000194 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196 alg->p = *p;
197 *p += alg->len;
198
199 if( *p == end )
200 return( 0 );
201
202 /*
203 * assume the algorithm parameters must be NULL
204 */
205 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000206 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000209 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000210 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212 return( 0 );
213}
214
215/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 * AttributeTypeAndValue ::= SEQUENCE {
217 * type AttributeType,
218 * value AttributeValue }
219 *
220 * AttributeType ::= OBJECT IDENTIFIER
221 *
222 * AttributeValue ::= ANY DEFINED BY AttributeType
223 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000224static int x509_get_attr_type_value( unsigned char **p,
225 const unsigned char *end,
226 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000227{
Paul Bakker23986e52011-04-24 08:57:21 +0000228 int ret;
229 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 x509_buf *oid;
231 x509_buf *val;
232
233 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000235 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 oid = &cur->oid;
238 oid->tag = **p;
239
240 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000241 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
243 oid->p = *p;
244 *p += oid->len;
245
246 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000247 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000248 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
251 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
252 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000253 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000254 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
256 val = &cur->val;
257 val->tag = *(*p)++;
258
259 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000260 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262 val->p = *p;
263 *p += val->len;
264
265 cur->next = NULL;
266
Paul Bakker400ff6f2011-02-20 10:40:16 +0000267 return( 0 );
268}
269
270/*
271 * RelativeDistinguishedName ::=
272 * SET OF AttributeTypeAndValue
273 *
274 * AttributeTypeAndValue ::= SEQUENCE {
275 * type AttributeType,
276 * value AttributeValue }
277 *
278 * AttributeType ::= OBJECT IDENTIFIER
279 *
280 * AttributeValue ::= ANY DEFINED BY AttributeType
281 */
282static int x509_get_name( unsigned char **p,
283 const unsigned char *end,
284 x509_name *cur )
285{
Paul Bakker23986e52011-04-24 08:57:21 +0000286 int ret;
287 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000288 const unsigned char *end2;
289 x509_name *use;
290
291 if( ( ret = asn1_get_tag( p, end, &len,
292 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000293 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000294
295 end2 = end;
296 end = *p + len;
297 use = cur;
298
299 do
300 {
301 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
302 return( ret );
303
304 if( *p != end )
305 {
306 use->next = (x509_name *) malloc(
307 sizeof( x509_name ) );
308
309 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000310 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000311
312 memset( use->next, 0, sizeof( x509_name ) );
313
314 use = use->next;
315 }
316 }
317 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319 /*
320 * recurse until end of SEQUENCE is reached
321 */
322 if( *p == end2 )
323 return( 0 );
324
325 cur->next = (x509_name *) malloc(
326 sizeof( x509_name ) );
327
328 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000329 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Paul Bakker430ffbe2012-05-01 08:14:20 +0000331 memset( cur->next, 0, sizeof( x509_name ) );
332
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 return( x509_get_name( p, end2, cur->next ) );
334}
335
336/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 * Time ::= CHOICE {
338 * utcTime UTCTime,
339 * generalTime GeneralizedTime }
340 */
Paul Bakker91200182010-02-18 21:26:15 +0000341static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000342 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000343 x509_time *time )
344{
Paul Bakker23986e52011-04-24 08:57:21 +0000345 int ret;
346 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000347 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000348 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000349
Paul Bakker91200182010-02-18 21:26:15 +0000350 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000351 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
352 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000353
Paul Bakker91200182010-02-18 21:26:15 +0000354 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000355
Paul Bakker91200182010-02-18 21:26:15 +0000356 if ( tag == ASN1_UTC_TIME )
357 {
358 (*p)++;
359 ret = asn1_get_len( p, end, &len );
360
361 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000362 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000363
Paul Bakker91200182010-02-18 21:26:15 +0000364 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000365 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
366 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000367
Paul Bakker91200182010-02-18 21:26:15 +0000368 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
369 &time->year, &time->mon, &time->day,
370 &time->hour, &time->min, &time->sec ) < 5 )
371 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000372
Paul Bakker400ff6f2011-02-20 10:40:16 +0000373 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000374 time->year += 1900;
375
376 *p += len;
377
378 return( 0 );
379 }
380 else if ( tag == ASN1_GENERALIZED_TIME )
381 {
382 (*p)++;
383 ret = asn1_get_len( p, end, &len );
384
385 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000386 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000387
388 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000389 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
390 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000391
392 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
393 &time->year, &time->mon, &time->day,
394 &time->hour, &time->min, &time->sec ) < 5 )
395 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
396
397 *p += len;
398
399 return( 0 );
400 }
401 else
Paul Bakker9d781402011-05-09 16:17:09 +0000402 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000403}
404
405
406/*
407 * Validity ::= SEQUENCE {
408 * notBefore Time,
409 * notAfter Time }
410 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000411static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000412 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 x509_time *from,
414 x509_time *to )
415{
Paul Bakker23986e52011-04-24 08:57:21 +0000416 int ret;
417 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419 if( ( ret = asn1_get_tag( p, end, &len,
420 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000421 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423 end = *p + len;
424
Paul Bakker91200182010-02-18 21:26:15 +0000425 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000426 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
Paul Bakker91200182010-02-18 21:26:15 +0000428 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000429 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000432 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000433 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435 return( 0 );
436}
437
438/*
439 * SubjectPublicKeyInfo ::= SEQUENCE {
440 * algorithm AlgorithmIdentifier,
441 * subjectPublicKey BIT STRING }
442 */
443static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000444 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 x509_buf *pk_alg_oid,
446 mpi *N, mpi *E )
447{
Paul Bakker65a19092013-06-06 21:14:58 +0200448 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000449 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 unsigned char *end2;
451
452 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
453 return( ret );
454
455 /*
456 * only RSA public keys handled at this time
457 */
Paul Bakker65a19092013-06-06 21:14:58 +0200458 if( pk_alg_oid->len != 9 ||
459 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) != 0 )
Paul Bakker400ff6f2011-02-20 10:40:16 +0000460 {
Paul Bakkered56b222011-07-13 11:26:43 +0000461 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker65a19092013-06-06 21:14:58 +0200462 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000465 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000468 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000469 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 end2 = *p + len;
472
473 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000474 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 /*
477 * RSAPublicKey ::= SEQUENCE {
478 * modulus INTEGER, -- n
479 * publicExponent INTEGER -- e
480 * }
481 */
482 if( ( ret = asn1_get_tag( p, end2, &len,
483 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
491 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000492 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000495 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000496 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 return( 0 );
499}
500
501static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000502 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 x509_buf *sig )
504{
Paul Bakker23986e52011-04-24 08:57:21 +0000505 int ret;
506 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakker8afa70d2012-02-11 18:42:45 +0000508 if( ( end - *p ) < 1 )
509 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
510 POLARSSL_ERR_ASN1_OUT_OF_DATA );
511
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 sig->tag = **p;
513
514 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000515 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker74111d32011-01-15 16:57:55 +0000517
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000519 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521 sig->len = len;
522 sig->p = *p;
523
524 *p += len;
525
526 return( 0 );
527}
528
529/*
530 * X.509 v2/v3 unique identifier (not parsed)
531 */
532static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000533 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 x509_buf *uid, int n )
535{
536 int ret;
537
538 if( *p == end )
539 return( 0 );
540
541 uid->tag = **p;
542
543 if( ( ret = asn1_get_tag( p, end, &uid->len,
544 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
545 {
Paul Bakker40e46942009-01-03 21:51:57 +0000546 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 return( 0 );
548
549 return( ret );
550 }
551
552 uid->p = *p;
553 *p += uid->len;
554
555 return( 0 );
556}
557
558/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000559 * X.509 Extensions (No parsing of extensions, pointer should
560 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
562static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000563 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000564 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565{
Paul Bakker23986e52011-04-24 08:57:21 +0000566 int ret;
567 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 if( *p == end )
570 return( 0 );
571
572 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000573
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000575 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 ext->p = *p;
579 end = *p + ext->len;
580
581 /*
582 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
583 *
584 * Extension ::= SEQUENCE {
585 * extnID OBJECT IDENTIFIER,
586 * critical BOOLEAN DEFAULT FALSE,
587 * extnValue OCTET STRING }
588 */
589 if( ( ret = asn1_get_tag( p, end, &len,
590 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000591 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000594 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000595 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Paul Bakkerd98030e2009-05-02 15:13:40 +0000597 return( 0 );
598}
599
600/*
601 * X.509 CRL v2 extensions (no extensions parsed yet.)
602 */
603static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000604 const unsigned char *end,
605 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000606{
Paul Bakker23986e52011-04-24 08:57:21 +0000607 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000608 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000609
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000610 /* Get explicit tag */
611 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000612 {
613 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
614 return( 0 );
615
616 return( ret );
617 }
618
619 while( *p < end )
620 {
621 if( ( ret = asn1_get_tag( p, end, &len,
622 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000623 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000624
625 *p += len;
626 }
627
628 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000629 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000630 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
631
632 return( 0 );
633}
634
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000635/*
636 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
637 */
638static int x509_get_crl_entry_ext( unsigned char **p,
639 const unsigned char *end,
640 x509_buf *ext )
641{
642 int ret;
643 size_t len = 0;
644
645 /* OPTIONAL */
646 if (end <= *p)
647 return( 0 );
648
649 ext->tag = **p;
650 ext->p = *p;
651
652 /*
653 * Get CRL-entry extension sequence header
654 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
655 */
656 if( ( ret = asn1_get_tag( p, end, &ext->len,
657 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
658 {
659 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
660 {
661 ext->p = NULL;
662 return( 0 );
663 }
664 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
665 }
666
667 end = *p + ext->len;
668
669 if( end != *p + ext->len )
670 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
671 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
672
673 while( *p < end )
674 {
675 if( ( ret = asn1_get_tag( p, end, &len,
676 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
677 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
678
679 *p += len;
680 }
681
682 if( *p != end )
683 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
684 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
685
686 return( 0 );
687}
688
Paul Bakker74111d32011-01-15 16:57:55 +0000689static int x509_get_basic_constraints( unsigned char **p,
690 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000691 int *ca_istrue,
692 int *max_pathlen )
693{
Paul Bakker23986e52011-04-24 08:57:21 +0000694 int ret;
695 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000696
697 /*
698 * BasicConstraints ::= SEQUENCE {
699 * cA BOOLEAN DEFAULT FALSE,
700 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
701 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000702 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000703 *max_pathlen = 0; /* endless */
704
705 if( ( ret = asn1_get_tag( p, end, &len,
706 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000707 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000708
709 if( *p == end )
710 return 0;
711
Paul Bakker3cccddb2011-01-16 21:46:31 +0000712 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000713 {
714 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000715 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000716
717 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000718 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000719
Paul Bakker3cccddb2011-01-16 21:46:31 +0000720 if( *ca_istrue != 0 )
721 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000722 }
723
724 if( *p == end )
725 return 0;
726
727 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000728 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000729
730 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000731 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000732 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
733
734 (*max_pathlen)++;
735
Paul Bakker74111d32011-01-15 16:57:55 +0000736 return 0;
737}
738
739static int x509_get_ns_cert_type( unsigned char **p,
740 const unsigned char *end,
741 unsigned char *ns_cert_type)
742{
743 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000744 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000745
746 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000747 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000748
749 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000750 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000751 POLARSSL_ERR_ASN1_INVALID_LENGTH );
752
753 /* Get actual bitstring */
754 *ns_cert_type = *bs.p;
755 return 0;
756}
757
758static int x509_get_key_usage( unsigned char **p,
759 const unsigned char *end,
760 unsigned char *key_usage)
761{
762 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000763 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000764
765 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000766 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000767
Paul Bakker94a67962012-08-23 13:03:52 +0000768 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000769 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000770 POLARSSL_ERR_ASN1_INVALID_LENGTH );
771
772 /* Get actual bitstring */
773 *key_usage = *bs.p;
774 return 0;
775}
776
Paul Bakkerd98030e2009-05-02 15:13:40 +0000777/*
Paul Bakker74111d32011-01-15 16:57:55 +0000778 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
779 *
780 * KeyPurposeId ::= OBJECT IDENTIFIER
781 */
782static int x509_get_ext_key_usage( unsigned char **p,
783 const unsigned char *end,
784 x509_sequence *ext_key_usage)
785{
786 int ret;
787
788 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000789 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000790
791 /* Sequence length must be >= 1 */
792 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000793 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000794 POLARSSL_ERR_ASN1_INVALID_LENGTH );
795
796 return 0;
797}
798
799/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000800 * SubjectAltName ::= GeneralNames
801 *
802 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
803 *
804 * GeneralName ::= CHOICE {
805 * otherName [0] OtherName,
806 * rfc822Name [1] IA5String,
807 * dNSName [2] IA5String,
808 * x400Address [3] ORAddress,
809 * directoryName [4] Name,
810 * ediPartyName [5] EDIPartyName,
811 * uniformResourceIdentifier [6] IA5String,
812 * iPAddress [7] OCTET STRING,
813 * registeredID [8] OBJECT IDENTIFIER }
814 *
815 * OtherName ::= SEQUENCE {
816 * type-id OBJECT IDENTIFIER,
817 * value [0] EXPLICIT ANY DEFINED BY type-id }
818 *
819 * EDIPartyName ::= SEQUENCE {
820 * nameAssigner [0] DirectoryString OPTIONAL,
821 * partyName [1] DirectoryString }
822 *
823 * NOTE: PolarSSL only parses and uses dNSName at this point.
824 */
825static int x509_get_subject_alt_name( unsigned char **p,
826 const unsigned char *end,
827 x509_sequence *subject_alt_name )
828{
829 int ret;
830 size_t len, tag_len;
831 asn1_buf *buf;
832 unsigned char tag;
833 asn1_sequence *cur = subject_alt_name;
834
835 /* Get main sequence tag */
836 if( ( ret = asn1_get_tag( p, end, &len,
837 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
838 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
839
840 if( *p + len != end )
841 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
842 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
843
844 while( *p < end )
845 {
846 if( ( end - *p ) < 1 )
847 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
848 POLARSSL_ERR_ASN1_OUT_OF_DATA );
849
850 tag = **p;
851 (*p)++;
852 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
853 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
854
855 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
856 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
857 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
858
859 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
860 {
861 *p += tag_len;
862 continue;
863 }
864
865 buf = &(cur->buf);
866 buf->tag = tag;
867 buf->p = *p;
868 buf->len = tag_len;
869 *p += buf->len;
870
871 /* Allocate and assign next pointer */
872 if (*p < end)
873 {
874 cur->next = (asn1_sequence *) malloc(
875 sizeof( asn1_sequence ) );
876
877 if( cur->next == NULL )
878 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
879 POLARSSL_ERR_ASN1_MALLOC_FAILED );
880
Paul Bakker535e97d2012-08-23 10:49:55 +0000881 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000882 cur = cur->next;
883 }
884 }
885
886 /* Set final sequence entry's next pointer to NULL */
887 cur->next = NULL;
888
889 if( *p != end )
890 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
891 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
892
893 return( 0 );
894}
895
896/*
Paul Bakker74111d32011-01-15 16:57:55 +0000897 * X.509 v3 extensions
898 *
899 * TODO: Perform all of the basic constraints tests required by the RFC
900 * TODO: Set values for undetected extensions to a sane default?
901 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000902 */
903static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000904 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000905 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000906{
Paul Bakker23986e52011-04-24 08:57:21 +0000907 int ret;
908 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000909 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000910
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000911 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000912 {
913 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
914 return( 0 );
915
916 return( ret );
917 }
918
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 while( *p < end )
920 {
Paul Bakker74111d32011-01-15 16:57:55 +0000921 /*
922 * Extension ::= SEQUENCE {
923 * extnID OBJECT IDENTIFIER,
924 * critical BOOLEAN DEFAULT FALSE,
925 * extnValue OCTET STRING }
926 */
927 x509_buf extn_oid = {0, 0, NULL};
928 int is_critical = 0; /* DEFAULT FALSE */
929
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 if( ( ret = asn1_get_tag( p, end, &len,
931 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000932 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000934 end_ext_data = *p + len;
935
Paul Bakker74111d32011-01-15 16:57:55 +0000936 /* Get extension ID */
937 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Paul Bakker74111d32011-01-15 16:57:55 +0000939 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000940 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Paul Bakker74111d32011-01-15 16:57:55 +0000942 extn_oid.p = *p;
943 *p += extn_oid.len;
944
945 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000946 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000947 POLARSSL_ERR_ASN1_OUT_OF_DATA );
948
949 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000950 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000951 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000952 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Paul Bakker74111d32011-01-15 16:57:55 +0000954 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000955 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000957 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000959 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000960
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000961 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000962 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000963 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Paul Bakker74111d32011-01-15 16:57:55 +0000965 /*
966 * Detect supported extensions
967 */
968 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
969 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 {
Paul Bakker74111d32011-01-15 16:57:55 +0000971 /* Parse basic constraints */
972 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000973 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000974 return ( ret );
975 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 }
Paul Bakker74111d32011-01-15 16:57:55 +0000977 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
978 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
979 {
980 /* Parse netscape certificate type */
981 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
982 &crt->ns_cert_type ) ) != 0 )
983 return ( ret );
984 crt->ext_types |= EXT_NS_CERT_TYPE;
985 }
986 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
987 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
988 {
989 /* Parse key usage */
990 if( ( ret = x509_get_key_usage( p, end_ext_octet,
991 &crt->key_usage ) ) != 0 )
992 return ( ret );
993 crt->ext_types |= EXT_KEY_USAGE;
994 }
995 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
996 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
997 {
998 /* Parse extended key usage */
999 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1000 &crt->ext_key_usage ) ) != 0 )
1001 return ( ret );
1002 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1003 }
Paul Bakkera8cd2392012-02-11 16:09:32 +00001004 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == extn_oid.len ) &&
1005 memcmp( extn_oid.p, OID_SUBJECT_ALT_NAME, extn_oid.len ) == 0 )
1006 {
1007 /* Parse extended key usage */
1008 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1009 &crt->subject_alt_names ) ) != 0 )
1010 return ( ret );
1011 crt->ext_types |= EXT_SUBJECT_ALT_NAME;
1012 }
Paul Bakker74111d32011-01-15 16:57:55 +00001013 else
1014 {
1015 /* No parser found, skip extension */
1016 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Paul Bakker5c721f92011-07-27 16:51:09 +00001018#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001019 if( is_critical )
1020 {
1021 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001022 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001023 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1024 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001025#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001026 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 }
1028
1029 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001030 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001031 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 return( 0 );
1034}
1035
1036/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001037 * X.509 CRL Entries
1038 */
1039static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001040 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001041 x509_crl_entry *entry )
1042{
Paul Bakker23986e52011-04-24 08:57:21 +00001043 int ret;
1044 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001045 x509_crl_entry *cur_entry = entry;
1046
1047 if( *p == end )
1048 return( 0 );
1049
Paul Bakker9be19372009-07-27 20:21:53 +00001050 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001051 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1052 {
1053 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1054 return( 0 );
1055
1056 return( ret );
1057 }
1058
Paul Bakker9be19372009-07-27 20:21:53 +00001059 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001060
1061 while( *p < end )
1062 {
Paul Bakker23986e52011-04-24 08:57:21 +00001063 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001064 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001065
1066 if( ( ret = asn1_get_tag( p, end, &len2,
1067 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1068 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001069 return( ret );
1070 }
1071
Paul Bakker9be19372009-07-27 20:21:53 +00001072 cur_entry->raw.tag = **p;
1073 cur_entry->raw.p = *p;
1074 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001075 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001076
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001077 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001078 return( ret );
1079
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001080 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001081 return( ret );
1082
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001083 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001084 return( ret );
1085
Paul Bakker74111d32011-01-15 16:57:55 +00001086 if ( *p < end )
1087 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001088 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001089
1090 if( cur_entry->next == NULL )
1091 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1092
Paul Bakkerd98030e2009-05-02 15:13:40 +00001093 cur_entry = cur_entry->next;
1094 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1095 }
1096 }
1097
1098 return( 0 );
1099}
1100
Paul Bakker27d66162010-03-17 06:56:01 +00001101static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1102{
1103 if( sig_oid->len == 9 &&
1104 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1105 {
1106 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1107 {
1108 *sig_alg = sig_oid->p[8];
1109 return( 0 );
1110 }
1111
1112 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1113 {
1114 *sig_alg = sig_oid->p[8];
1115 return( 0 );
1116 }
1117
1118 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1119 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001120 if( sig_oid->len == 5 &&
1121 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1122 {
1123 *sig_alg = SIG_RSA_SHA1;
1124 return( 0 );
1125 }
Paul Bakker27d66162010-03-17 06:56:01 +00001126
1127 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1128}
1129
Paul Bakkerd98030e2009-05-02 15:13:40 +00001130/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001131 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001133int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1134 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135{
Paul Bakker23986e52011-04-24 08:57:21 +00001136 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001137 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001138 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Paul Bakker320a4b52009-03-28 18:52:39 +00001140 /*
1141 * Check for valid input
1142 */
1143 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001144 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001145
Paul Bakker96743fc2011-02-12 14:30:57 +00001146 p = (unsigned char *) malloc( len = buflen );
1147
1148 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001149 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001150
1151 memcpy( p, buf, buflen );
1152
1153 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
1155 crt->raw.p = p;
1156 crt->raw.len = len;
1157 end = p + len;
1158
1159 /*
1160 * Certificate ::= SEQUENCE {
1161 * tbsCertificate TBSCertificate,
1162 * signatureAlgorithm AlgorithmIdentifier,
1163 * signatureValue BIT STRING }
1164 */
1165 if( ( ret = asn1_get_tag( &p, end, &len,
1166 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1167 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001168 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001169 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 }
1171
Paul Bakkerb00ca422012-09-25 12:10:00 +00001172 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001174 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001175 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001176 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001178 crt_end = p + len;
Paul Bakkerd6d41092013-06-13 09:00:25 +02001179
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 /*
1181 * TBSCertificate ::= SEQUENCE {
1182 */
1183 crt->tbs.p = p;
1184
1185 if( ( ret = asn1_get_tag( &p, end, &len,
1186 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1187 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001188 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001189 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 }
1191
1192 end = p + len;
1193 crt->tbs.len = end - crt->tbs.p;
1194
1195 /*
1196 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1197 *
1198 * CertificateSerialNumber ::= INTEGER
1199 *
1200 * signature AlgorithmIdentifier
1201 */
1202 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1203 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1204 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1205 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001206 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 return( ret );
1208 }
1209
1210 crt->version++;
1211
1212 if( crt->version > 3 )
1213 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001214 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001215 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 }
1217
Paul Bakker27d66162010-03-17 06:56:01 +00001218 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001220 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001221 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 }
1223
1224 /*
1225 * issuer Name
1226 */
1227 crt->issuer_raw.p = p;
1228
1229 if( ( ret = asn1_get_tag( &p, end, &len,
1230 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1231 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001232 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001233 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 }
1235
1236 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1237 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001238 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 return( ret );
1240 }
1241
1242 crt->issuer_raw.len = p - crt->issuer_raw.p;
1243
1244 /*
1245 * Validity ::= SEQUENCE {
1246 * notBefore Time,
1247 * notAfter Time }
1248 *
1249 */
1250 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1251 &crt->valid_to ) ) != 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
1257 /*
1258 * subject Name
1259 */
1260 crt->subject_raw.p = p;
1261
1262 if( ( ret = asn1_get_tag( &p, end, &len,
1263 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001266 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 }
1268
Paul Bakkercefb3962012-06-27 11:51:09 +00001269 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001271 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 return( ret );
1273 }
1274
1275 crt->subject_raw.len = p - crt->subject_raw.p;
1276
1277 /*
1278 * SubjectPublicKeyInfo ::= SEQUENCE
1279 * algorithm AlgorithmIdentifier,
1280 * subjectPublicKey BIT STRING }
1281 */
1282 if( ( ret = asn1_get_tag( &p, end, &len,
1283 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1284 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001285 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001286 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 }
1288
1289 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1290 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1291 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001292 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 return( ret );
1294 }
1295
1296 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1297 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001298 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 return( ret );
1300 }
1301
1302 crt->rsa.len = mpi_size( &crt->rsa.N );
1303
1304 /*
1305 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1306 * -- If present, version shall be v2 or v3
1307 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1308 * -- If present, version shall be v2 or v3
1309 * extensions [3] EXPLICIT Extensions OPTIONAL
1310 * -- If present, version shall be v3
1311 */
1312 if( crt->version == 2 || crt->version == 3 )
1313 {
1314 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1315 if( ret != 0 )
1316 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001317 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 return( ret );
1319 }
1320 }
1321
1322 if( crt->version == 2 || crt->version == 3 )
1323 {
1324 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1325 if( ret != 0 )
1326 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001327 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 return( ret );
1329 }
1330 }
1331
1332 if( crt->version == 3 )
1333 {
Paul Bakker74111d32011-01-15 16:57:55 +00001334 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 if( ret != 0 )
1336 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001337 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 return( ret );
1339 }
1340 }
1341
1342 if( p != end )
1343 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001344 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001345 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001346 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 }
1348
Paul Bakkerb00ca422012-09-25 12:10:00 +00001349 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
1351 /*
1352 * signatureAlgorithm AlgorithmIdentifier,
1353 * signatureValue BIT STRING
1354 */
1355 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1356 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001357 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 return( ret );
1359 }
1360
Paul Bakker535e97d2012-08-23 10:49:55 +00001361 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1362 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001364 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001365 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 }
1367
1368 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1369 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001370 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 return( ret );
1372 }
1373
1374 if( p != end )
1375 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001376 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001377 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001378 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 }
1380
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001381 return( 0 );
1382}
1383
1384/*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001385 * Parse one X.509 certificate in DER format from a buffer and add them to a
1386 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001387 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001388int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001389{
Paul Bakkerd6d41092013-06-13 09:00:25 +02001390 int ret;
1391 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001392
1393 /*
1394 * Check for valid input
1395 */
1396 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001397 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001398
1399 while( crt->version != 0 && crt->next != NULL )
1400 {
1401 prev = crt;
1402 crt = crt->next;
1403 }
1404
1405 /*
1406 * Add new certificate on the end of the chain if needed.
1407 */
1408 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001409 {
1410 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1411
Paul Bakker7d06ad22009-05-02 15:53:56 +00001412 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001413 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001414
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001415 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001416 crt = crt->next;
1417 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Paul Bakkerd6d41092013-06-13 09:00:25 +02001420 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1421 {
1422 if( prev )
1423 prev->next = NULL;
1424
1425 if( crt != chain )
1426 free( crt );
1427
1428 return( ret );
1429 }
1430
1431 return( 0 );
1432}
1433
1434/*
1435 * Parse one or more PEM certificates from a buffer and add them to the chained list
1436 */
1437int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1438{
1439 int ret, success = 0, first_error = 0, total_failed = 0;
1440 int buf_format = X509_FORMAT_DER;
1441
1442 /*
1443 * Check for valid input
1444 */
1445 if( chain == NULL || buf == NULL )
1446 return( POLARSSL_ERR_X509_INVALID_INPUT );
1447
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001448 /*
1449 * Determine buffer content. Buffer contains either one DER certificate or
1450 * one or more PEM certificates.
1451 */
1452#if defined(POLARSSL_PEM_C)
Paul Bakkereae09db2013-06-06 12:35:54 +02001453 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001454 buf_format = X509_FORMAT_PEM;
1455#endif
1456
1457 if( buf_format == X509_FORMAT_DER )
Paul Bakkerd6d41092013-06-13 09:00:25 +02001458 return x509parse_crt_der( chain, buf, buflen );
1459
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001460#if defined(POLARSSL_PEM_C)
1461 if( buf_format == X509_FORMAT_PEM )
1462 {
1463 pem_context pem;
1464
1465 while( buflen > 0 )
1466 {
1467 size_t use_len;
1468 pem_init( &pem );
1469
1470 ret = pem_read_buffer( &pem,
1471 "-----BEGIN CERTIFICATE-----",
1472 "-----END CERTIFICATE-----",
1473 buf, NULL, 0, &use_len );
1474
1475 if( ret == 0 )
1476 {
1477 /*
1478 * Was PEM encoded
1479 */
1480 buflen -= use_len;
1481 buf += use_len;
1482 }
Paul Bakker64171862013-06-06 15:01:18 +02001483 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1484 {
1485 return( ret );
1486 }
Paul Bakker9255e832013-06-06 14:58:28 +02001487 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001488 {
1489 pem_free( &pem );
1490
Paul Bakker64171862013-06-06 15:01:18 +02001491 /*
1492 * PEM header and footer were found
1493 */
1494 buflen -= use_len;
1495 buf += use_len;
1496
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001497 if( first_error == 0 )
1498 first_error = ret;
1499
1500 continue;
1501 }
1502 else
1503 break;
1504
Paul Bakkerd6d41092013-06-13 09:00:25 +02001505 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001506
1507 pem_free( &pem );
1508
1509 if( ret != 0 )
1510 {
1511 /*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001512 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001513 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001514 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001515 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001516
1517 if( first_error == 0 )
1518 first_error = ret;
1519
Paul Bakkerd6d41092013-06-13 09:00:25 +02001520 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001521 continue;
1522 }
1523
1524 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001525 }
1526 }
1527#endif
1528
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001529 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001530 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001531 else if( first_error )
1532 return( first_error );
1533 else
1534 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001535}
1536
1537/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001538 * Parse one or more CRLs and add them to the chained list
1539 */
Paul Bakker23986e52011-04-24 08:57:21 +00001540int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001541{
Paul Bakker23986e52011-04-24 08:57:21 +00001542 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001543 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001544 unsigned char *p, *end;
1545 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001546#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001547 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001548 pem_context pem;
1549#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001550
1551 crl = chain;
1552
1553 /*
1554 * Check for valid input
1555 */
1556 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001557 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001558
1559 while( crl->version != 0 && crl->next != NULL )
1560 crl = crl->next;
1561
1562 /*
1563 * Add new CRL on the end of the chain if needed.
1564 */
1565 if ( crl->version != 0 && crl->next == NULL)
1566 {
1567 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1568
Paul Bakker7d06ad22009-05-02 15:53:56 +00001569 if( crl->next == NULL )
1570 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001571 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001572 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001573 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001574
Paul Bakker7d06ad22009-05-02 15:53:56 +00001575 crl = crl->next;
1576 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001577 }
1578
Paul Bakker96743fc2011-02-12 14:30:57 +00001579#if defined(POLARSSL_PEM_C)
1580 pem_init( &pem );
1581 ret = pem_read_buffer( &pem,
1582 "-----BEGIN X509 CRL-----",
1583 "-----END X509 CRL-----",
1584 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585
Paul Bakker96743fc2011-02-12 14:30:57 +00001586 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001588 /*
1589 * Was PEM encoded
1590 */
1591 buflen -= use_len;
1592 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001593
1594 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001595 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001596 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001597 p = pem.buf;
1598 pem.buf = NULL;
1599 len = pem.buflen;
1600 pem_free( &pem );
1601 }
Paul Bakker9255e832013-06-06 14:58:28 +02001602 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001603 {
1604 pem_free( &pem );
1605 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001606 }
1607 else
1608 {
1609 /*
1610 * nope, copy the raw DER data
1611 */
1612 p = (unsigned char *) malloc( len = buflen );
1613
1614 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001615 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001616
1617 memcpy( p, buf, buflen );
1618
1619 buflen = 0;
1620 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001621#else
1622 p = (unsigned char *) malloc( len = buflen );
1623
1624 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001625 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001626
1627 memcpy( p, buf, buflen );
1628
1629 buflen = 0;
1630#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631
1632 crl->raw.p = p;
1633 crl->raw.len = len;
1634 end = p + len;
1635
1636 /*
1637 * CertificateList ::= SEQUENCE {
1638 * tbsCertList TBSCertList,
1639 * signatureAlgorithm AlgorithmIdentifier,
1640 * signatureValue BIT STRING }
1641 */
1642 if( ( ret = asn1_get_tag( &p, end, &len,
1643 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1644 {
1645 x509_crl_free( crl );
1646 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1647 }
1648
Paul Bakker23986e52011-04-24 08:57:21 +00001649 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001650 {
1651 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001652 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1654 }
1655
1656 /*
1657 * TBSCertList ::= SEQUENCE {
1658 */
1659 crl->tbs.p = p;
1660
1661 if( ( ret = asn1_get_tag( &p, end, &len,
1662 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1663 {
1664 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001665 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001666 }
1667
1668 end = p + len;
1669 crl->tbs.len = end - crl->tbs.p;
1670
1671 /*
1672 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1673 * -- if present, MUST be v2
1674 *
1675 * signature AlgorithmIdentifier
1676 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001677 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1679 {
1680 x509_crl_free( crl );
1681 return( ret );
1682 }
1683
1684 crl->version++;
1685
1686 if( crl->version > 2 )
1687 {
1688 x509_crl_free( crl );
1689 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1690 }
1691
Paul Bakker27d66162010-03-17 06:56:01 +00001692 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001693 {
1694 x509_crl_free( crl );
1695 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1696 }
1697
1698 /*
1699 * issuer Name
1700 */
1701 crl->issuer_raw.p = p;
1702
1703 if( ( ret = asn1_get_tag( &p, end, &len,
1704 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1705 {
1706 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001707 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001708 }
1709
1710 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1711 {
1712 x509_crl_free( crl );
1713 return( ret );
1714 }
1715
1716 crl->issuer_raw.len = p - crl->issuer_raw.p;
1717
1718 /*
1719 * thisUpdate Time
1720 * nextUpdate Time OPTIONAL
1721 */
Paul Bakker91200182010-02-18 21:26:15 +00001722 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001723 {
1724 x509_crl_free( crl );
1725 return( ret );
1726 }
1727
Paul Bakker91200182010-02-18 21:26:15 +00001728 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001729 {
Paul Bakker9d781402011-05-09 16:17:09 +00001730 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001731 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001732 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001733 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001734 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 x509_crl_free( crl );
1736 return( ret );
1737 }
1738 }
1739
1740 /*
1741 * revokedCertificates SEQUENCE OF SEQUENCE {
1742 * userCertificate CertificateSerialNumber,
1743 * revocationDate Time,
1744 * crlEntryExtensions Extensions OPTIONAL
1745 * -- if present, MUST be v2
1746 * } OPTIONAL
1747 */
1748 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1749 {
1750 x509_crl_free( crl );
1751 return( ret );
1752 }
1753
1754 /*
1755 * crlExtensions EXPLICIT Extensions OPTIONAL
1756 * -- if present, MUST be v2
1757 */
1758 if( crl->version == 2 )
1759 {
1760 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1761
1762 if( ret != 0 )
1763 {
1764 x509_crl_free( crl );
1765 return( ret );
1766 }
1767 }
1768
1769 if( p != end )
1770 {
1771 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001772 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001773 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1774 }
1775
1776 end = crl->raw.p + crl->raw.len;
1777
1778 /*
1779 * signatureAlgorithm AlgorithmIdentifier,
1780 * signatureValue BIT STRING
1781 */
1782 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1783 {
1784 x509_crl_free( crl );
1785 return( ret );
1786 }
1787
Paul Bakker535e97d2012-08-23 10:49:55 +00001788 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1789 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790 {
1791 x509_crl_free( crl );
1792 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1793 }
1794
1795 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1796 {
1797 x509_crl_free( crl );
1798 return( ret );
1799 }
1800
1801 if( p != end )
1802 {
1803 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001804 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001805 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1806 }
1807
1808 if( buflen > 0 )
1809 {
1810 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1811
Paul Bakker7d06ad22009-05-02 15:53:56 +00001812 if( crl->next == NULL )
1813 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001815 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001816 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001817
Paul Bakker7d06ad22009-05-02 15:53:56 +00001818 crl = crl->next;
1819 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820
1821 return( x509parse_crl( crl, buf, buflen ) );
1822 }
1823
1824 return( 0 );
1825}
1826
Paul Bakker335db3f2011-04-25 15:28:35 +00001827#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001828/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001829 * Load all data from a file into a given buffer.
1830 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001831int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001832{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001833 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001834
Paul Bakkerd98030e2009-05-02 15:13:40 +00001835 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001836 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001837
Paul Bakkerd98030e2009-05-02 15:13:40 +00001838 fseek( f, 0, SEEK_END );
1839 *n = (size_t) ftell( f );
1840 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001841
Paul Bakkerd98030e2009-05-02 15:13:40 +00001842 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001843 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001844
Paul Bakkerd98030e2009-05-02 15:13:40 +00001845 if( fread( *buf, 1, *n, f ) != *n )
1846 {
1847 fclose( f );
1848 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001849 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001851
Paul Bakkerd98030e2009-05-02 15:13:40 +00001852 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001853
Paul Bakkerd98030e2009-05-02 15:13:40 +00001854 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001855
Paul Bakkerd98030e2009-05-02 15:13:40 +00001856 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001857}
1858
1859/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 * Load one or more certificates and add them to the chained list
1861 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001862int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001863{
1864 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 size_t n;
1866 unsigned char *buf;
1867
Paul Bakker69e095c2011-12-10 21:55:01 +00001868 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1869 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Paul Bakker69e095c2011-12-10 21:55:01 +00001871 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001872
1873 memset( buf, 0, n + 1 );
1874 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
1876 return( ret );
1877}
1878
Paul Bakker8d914582012-06-04 12:46:42 +00001879int x509parse_crtpath( x509_cert *chain, const char *path )
1880{
1881 int ret = 0;
1882#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001883 int w_ret;
1884 WCHAR szDir[MAX_PATH];
1885 char filename[MAX_PATH];
1886 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001887 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001888
Paul Bakker97872ac2012-11-02 12:53:26 +00001889 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001890 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001891
1892 if( len > MAX_PATH - 3 )
1893 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001894
Paul Bakker3338b792012-10-01 21:13:10 +00001895 memset( szDir, 0, sizeof(szDir) );
1896 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001897 memcpy( filename, path, len );
1898 filename[len++] = '\\';
1899 p = filename + len;
1900 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001901
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001902 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001903
Paul Bakker97872ac2012-11-02 12:53:26 +00001904 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001905 if (hFind == INVALID_HANDLE_VALUE)
1906 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1907
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001908 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001909 do
1910 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001911 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001912
Paul Bakkere4791f32012-06-04 21:29:15 +00001913 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001914 continue;
1915
Paul Bakker3338b792012-10-01 21:13:10 +00001916 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1917 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001918 p, len - 1,
1919 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001920
Paul Bakker3338b792012-10-01 21:13:10 +00001921 w_ret = x509parse_crtfile( chain, filename );
1922 if( w_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001923 ret++;
1924 else
1925 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001926 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001927 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001928
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001929 if (GetLastError() != ERROR_NO_MORE_FILES)
1930 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001931
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001932cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001933 FindClose( hFind );
1934#else
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001935 int t_ret, i;
1936 struct stat sb;
1937 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001938 char entry_name[255];
1939 DIR *dir = opendir( path );
1940
1941 if( dir == NULL)
1942 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1943
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001944 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001945 {
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001946 if( result == NULL )
1947 break;
1948
1949 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1950
1951 i = stat( entry_name, &sb );
1952
1953 if( i == -1 )
1954 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1955
1956 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00001957 continue;
1958
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001959 // Ignore parse errors
1960 //
Paul Bakker8d914582012-06-04 12:46:42 +00001961 t_ret = x509parse_crtfile( chain, entry_name );
1962 if( t_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001963 ret++;
1964 else
1965 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001966 }
1967 closedir( dir );
1968#endif
1969
1970 return( ret );
1971}
1972
Paul Bakkerd98030e2009-05-02 15:13:40 +00001973/*
1974 * Load one or more CRLs and add them to the chained list
1975 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001976int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001977{
1978 int ret;
1979 size_t n;
1980 unsigned char *buf;
1981
Paul Bakker69e095c2011-12-10 21:55:01 +00001982 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1983 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001984
Paul Bakker27fdf462011-06-09 13:55:13 +00001985 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001986
1987 memset( buf, 0, n + 1 );
1988 free( buf );
1989
1990 return( ret );
1991}
1992
Paul Bakker5121ce52009-01-03 21:22:43 +00001993/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001994 * Load and parse a private RSA key
1995 */
1996int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1997{
1998 int ret;
1999 size_t n;
2000 unsigned char *buf;
2001
Paul Bakker69e095c2011-12-10 21:55:01 +00002002 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2003 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002004
2005 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002006 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002007 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002008 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002009 (unsigned char *) pwd, strlen( pwd ) );
2010
2011 memset( buf, 0, n + 1 );
2012 free( buf );
2013
2014 return( ret );
2015}
2016
2017/*
2018 * Load and parse a public RSA key
2019 */
2020int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2021{
2022 int ret;
2023 size_t n;
2024 unsigned char *buf;
2025
Paul Bakker69e095c2011-12-10 21:55:01 +00002026 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2027 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002028
Paul Bakker27fdf462011-06-09 13:55:13 +00002029 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002030
2031 memset( buf, 0, n + 1 );
2032 free( buf );
2033
2034 return( ret );
2035}
2036#endif /* POLARSSL_FS_IO */
2037
2038/*
Paul Bakker65a19092013-06-06 21:14:58 +02002039 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 */
Paul Bakker65a19092013-06-06 21:14:58 +02002041static int x509parse_key_pkcs1_der( rsa_context *rsa,
2042 const unsigned char *key,
2043 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002044{
Paul Bakker23986e52011-04-24 08:57:21 +00002045 int ret;
2046 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002048
Paul Bakker96743fc2011-02-12 14:30:57 +00002049 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002050 end = p + keylen;
2051
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 /*
Paul Bakker65a19092013-06-06 21:14:58 +02002053 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002054 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 * RSAPrivateKey ::= SEQUENCE {
2056 * version Version,
2057 * modulus INTEGER, -- n
2058 * publicExponent INTEGER, -- e
2059 * privateExponent INTEGER, -- d
2060 * prime1 INTEGER, -- p
2061 * prime2 INTEGER, -- q
2062 * exponent1 INTEGER, -- d mod (p-1)
2063 * exponent2 INTEGER, -- d mod (q-1)
2064 * coefficient INTEGER, -- (inverse of q) mod p
2065 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2066 * }
2067 */
2068 if( ( ret = asn1_get_tag( &p, end, &len,
2069 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2070 {
Paul Bakker9d781402011-05-09 16:17:09 +00002071 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
2073
2074 end = p + len;
2075
2076 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2077 {
Paul Bakker9d781402011-05-09 16:17:09 +00002078 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 }
2080
2081 if( rsa->ver != 0 )
2082 {
Paul Bakker9d781402011-05-09 16:17:09 +00002083 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 }
2085
2086 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2087 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2088 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2089 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2090 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2091 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2092 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2093 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2094 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002096 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098
2099 rsa->len = mpi_size( &rsa->N );
2100
2101 if( p != end )
2102 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002104 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002105 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 }
2107
2108 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2109 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 rsa_free( rsa );
2111 return( ret );
2112 }
2113
Paul Bakker65a19092013-06-06 21:14:58 +02002114 return( 0 );
2115}
2116
2117/*
2118 * Parse an unencrypted PKCS#8 encoded private RSA key
2119 */
2120static int x509parse_key_pkcs8_unencrypted_der(
2121 rsa_context *rsa,
2122 const unsigned char *key,
2123 size_t keylen )
2124{
2125 int ret;
2126 size_t len;
2127 unsigned char *p, *end;
2128 x509_buf pk_alg_oid;
2129
2130 p = (unsigned char *) key;
2131 end = p + keylen;
2132
2133 /*
2134 * This function parses the PrivatKeyInfo object (PKCS#8)
2135 *
2136 * PrivateKeyInfo ::= SEQUENCE {
2137 * version Version,
2138 * algorithm AlgorithmIdentifier,
2139 * PrivateKey BIT STRING
2140 * }
2141 *
2142 * AlgorithmIdentifier ::= SEQUENCE {
2143 * algorithm OBJECT IDENTIFIER,
2144 * parameters ANY DEFINED BY algorithm OPTIONAL
2145 * }
2146 *
2147 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2148 */
2149 if( ( ret = asn1_get_tag( &p, end, &len,
2150 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2151 {
2152 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2153 }
2154
2155 end = p + len;
2156
2157 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2158 {
2159 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2160 }
2161
2162 if( rsa->ver != 0 )
2163 {
2164 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2165 }
2166
2167 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2168 {
2169 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2170 }
2171
2172 /*
2173 * only RSA keys handled at this time
2174 */
2175 if( pk_alg_oid.len != 9 ||
2176 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) != 0 )
2177 {
2178 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2179 }
2180
2181 /*
2182 * Get the OCTET STRING and parse the PKCS#1 format inside
2183 */
2184 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2185 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2186
2187 if( ( end - p ) < 1 )
2188 {
2189 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2190 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2191 }
2192
2193 end = p + len;
2194
2195 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2196 return( ret );
2197
2198 return( 0 );
2199}
2200
2201/*
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002202 * Parse an unencrypted PKCS#8 encoded private RSA key
2203 */
2204static int x509parse_key_pkcs8_encrypted_der(
2205 rsa_context *rsa,
2206 const unsigned char *key,
2207 size_t keylen,
2208 const unsigned char *pwd,
2209 size_t pwdlen )
2210{
2211 int ret;
2212 size_t len;
2213 unsigned char *p, *end, *end2;
2214 x509_buf pbe_alg_oid, pbe_params;
2215 unsigned char buf[2048];
2216
2217 memset(buf, 0, 2048);
2218
2219 p = (unsigned char *) key;
2220 end = p + keylen;
2221
2222 /*
2223 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2224 *
2225 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2226 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2227 * encryptedData EncryptedData
2228 * }
2229 *
2230 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2231 *
2232 * EncryptedData ::= OCTET STRING
2233 *
2234 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2235 */
2236 if( ( ret = asn1_get_tag( &p, end, &len,
2237 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2238 {
2239 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2240 }
2241
2242 end = p + len;
2243
2244 if( ( ret = asn1_get_tag( &p, end, &len,
2245 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2246 {
2247 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2248 }
2249
2250 end2 = p + len;
2251
2252 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2253 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2254
2255 pbe_alg_oid.p = p;
2256 p += pbe_alg_oid.len;
2257
2258 /*
2259 * Store the algorithm parameters
2260 */
2261 pbe_params.p = p;
2262 pbe_params.len = end2 - p;
2263 p += pbe_params.len;
2264
2265 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2266 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2267
2268 // buf has been sized to 2048 bytes
2269 if( len > 2048 )
2270 return( POLARSSL_ERR_X509_INVALID_INPUT );
2271
2272 /*
2273 * Decrypt EncryptedData with appropriate PDE
2274 */
2275 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2276 {
2277 if( ( ret = pkcs12_pbe_sha1_des3_ede_cbc( &pbe_params,
2278 PKCS12_PBE_DECRYPT,
2279 pwd, pwdlen,
2280 p, len, buf ) ) != 0 )
2281 {
2282 return( ret );
2283 }
2284 }
2285 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2286 {
2287 if( ( ret = pkcs12_pbe_sha1_des2_ede_cbc( &pbe_params,
2288 PKCS12_PBE_DECRYPT,
2289 pwd, pwdlen,
2290 p, len, buf ) ) != 0 )
2291 {
2292 return( ret );
2293 }
2294 }
2295 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2296 {
2297 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2298 PKCS12_PBE_DECRYPT,
2299 pwd, pwdlen,
2300 p, len, buf ) ) != 0 )
2301 {
2302 return( ret );
2303 }
2304 }
2305 else
2306 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2307
2308 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2309}
2310
2311/*
Paul Bakker65a19092013-06-06 21:14:58 +02002312 * Parse a private RSA key
2313 */
2314int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2315 const unsigned char *pwd, size_t pwdlen )
2316{
2317 int ret;
2318
Paul Bakker96743fc2011-02-12 14:30:57 +00002319#if defined(POLARSSL_PEM_C)
Paul Bakker65a19092013-06-06 21:14:58 +02002320 size_t len;
2321 pem_context pem;
2322
2323 pem_init( &pem );
2324 ret = pem_read_buffer( &pem,
2325 "-----BEGIN RSA PRIVATE KEY-----",
2326 "-----END RSA PRIVATE KEY-----",
2327 key, pwd, pwdlen, &len );
2328 if( ret == 0 )
2329 {
2330 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2331 {
2332 rsa_free( rsa );
2333 }
2334
2335 pem_free( &pem );
2336 return( ret );
2337 }
2338 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2339 {
2340 pem_free( &pem );
2341 return( ret );
2342 }
2343
2344 ret = pem_read_buffer( &pem,
2345 "-----BEGIN PRIVATE KEY-----",
2346 "-----END PRIVATE KEY-----",
2347 key, NULL, 0, &len );
2348 if( ret == 0 )
2349 {
2350 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2351 pem.buf, pem.buflen ) ) != 0 )
2352 {
2353 rsa_free( rsa );
2354 }
2355
2356 pem_free( &pem );
2357 return( ret );
2358 }
2359 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2360 {
2361 pem_free( &pem );
2362 return( ret );
2363 }
2364
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002365 ret = pem_read_buffer( &pem,
2366 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2367 "-----END ENCRYPTED PRIVATE KEY-----",
2368 key, NULL, 0, &len );
2369 if( ret == 0 )
2370 {
2371 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2372 pem.buf, pem.buflen,
2373 pwd, pwdlen ) ) != 0 )
2374 {
2375 rsa_free( rsa );
2376 }
2377
2378 pem_free( &pem );
2379 return( ret );
2380 }
2381 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2382 {
2383 pem_free( &pem );
2384 return( ret );
2385 }
Paul Bakker65a19092013-06-06 21:14:58 +02002386#else
2387 ((void) pwd);
2388 ((void) pwdlen);
2389#endif /* POLARSSL_PEM_C */
2390
2391 // At this point we only know it's not a PEM formatted key. Could be any
2392 // of the known DER encoded private key formats
2393 //
2394 // We try the different DER format parsers to see if one passes without
2395 // error
2396 //
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002397 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2398 pwd, pwdlen ) ) == 0 )
Paul Bakker65a19092013-06-06 21:14:58 +02002399 {
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002400 return( 0 );
Paul Bakker65a19092013-06-06 21:14:58 +02002401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002403 rsa_free( rsa );
2404 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2405 return( 0 );
2406
2407 rsa_free( rsa );
2408 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2409 return( 0 );
2410
2411 rsa_free( rsa );
2412 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413}
2414
2415/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002416 * Parse a public RSA key
2417 */
Paul Bakker23986e52011-04-24 08:57:21 +00002418int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002419{
Paul Bakker23986e52011-04-24 08:57:21 +00002420 int ret;
2421 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002422 unsigned char *p, *end;
2423 x509_buf alg_oid;
2424#if defined(POLARSSL_PEM_C)
2425 pem_context pem;
2426
2427 pem_init( &pem );
2428 ret = pem_read_buffer( &pem,
2429 "-----BEGIN PUBLIC KEY-----",
2430 "-----END PUBLIC KEY-----",
2431 key, NULL, 0, &len );
2432
2433 if( ret == 0 )
2434 {
2435 /*
2436 * Was PEM encoded
2437 */
2438 keylen = pem.buflen;
2439 }
Paul Bakker9255e832013-06-06 14:58:28 +02002440 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002441 {
2442 pem_free( &pem );
2443 return( ret );
2444 }
2445
2446 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2447#else
2448 p = (unsigned char *) key;
2449#endif
2450 end = p + keylen;
2451
2452 /*
2453 * PublicKeyInfo ::= SEQUENCE {
2454 * algorithm AlgorithmIdentifier,
2455 * PublicKey BIT STRING
2456 * }
2457 *
2458 * AlgorithmIdentifier ::= SEQUENCE {
2459 * algorithm OBJECT IDENTIFIER,
2460 * parameters ANY DEFINED BY algorithm OPTIONAL
2461 * }
2462 *
2463 * RSAPublicKey ::= SEQUENCE {
2464 * modulus INTEGER, -- n
2465 * publicExponent INTEGER -- e
2466 * }
2467 */
2468
2469 if( ( ret = asn1_get_tag( &p, end, &len,
2470 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2471 {
2472#if defined(POLARSSL_PEM_C)
2473 pem_free( &pem );
2474#endif
2475 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002476 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002477 }
2478
2479 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2480 {
2481#if defined(POLARSSL_PEM_C)
2482 pem_free( &pem );
2483#endif
2484 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002485 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002486 }
2487
2488 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2489 {
2490#if defined(POLARSSL_PEM_C)
2491 pem_free( &pem );
2492#endif
2493 rsa_free( rsa );
2494 return( ret );
2495 }
2496
2497 rsa->len = mpi_size( &rsa->N );
2498
2499#if defined(POLARSSL_PEM_C)
2500 pem_free( &pem );
2501#endif
2502
2503 return( 0 );
2504}
2505
Paul Bakkereaa89f82011-04-04 21:36:15 +00002506#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002507/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002508 * Parse DHM parameters
2509 */
Paul Bakker23986e52011-04-24 08:57:21 +00002510int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002511{
Paul Bakker23986e52011-04-24 08:57:21 +00002512 int ret;
2513 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002514 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002515#if defined(POLARSSL_PEM_C)
2516 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002517
Paul Bakker96743fc2011-02-12 14:30:57 +00002518 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002519
Paul Bakker96743fc2011-02-12 14:30:57 +00002520 ret = pem_read_buffer( &pem,
2521 "-----BEGIN DH PARAMETERS-----",
2522 "-----END DH PARAMETERS-----",
2523 dhmin, NULL, 0, &dhminlen );
2524
2525 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002526 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002527 /*
2528 * Was PEM encoded
2529 */
2530 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002531 }
Paul Bakker9255e832013-06-06 14:58:28 +02002532 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002533 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002534 pem_free( &pem );
2535 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002536 }
2537
Paul Bakker96743fc2011-02-12 14:30:57 +00002538 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2539#else
2540 p = (unsigned char *) dhmin;
2541#endif
2542 end = p + dhminlen;
2543
Paul Bakker1b57b062011-01-06 15:48:19 +00002544 memset( dhm, 0, sizeof( dhm_context ) );
2545
Paul Bakker1b57b062011-01-06 15:48:19 +00002546 /*
2547 * DHParams ::= SEQUENCE {
2548 * prime INTEGER, -- P
2549 * generator INTEGER, -- g
2550 * }
2551 */
2552 if( ( ret = asn1_get_tag( &p, end, &len,
2553 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2554 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002555#if defined(POLARSSL_PEM_C)
2556 pem_free( &pem );
2557#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002558 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002559 }
2560
2561 end = p + len;
2562
2563 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2564 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2565 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002566#if defined(POLARSSL_PEM_C)
2567 pem_free( &pem );
2568#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002569 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002570 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002571 }
2572
2573 if( p != end )
2574 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002575#if defined(POLARSSL_PEM_C)
2576 pem_free( &pem );
2577#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002578 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002579 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002580 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2581 }
2582
Paul Bakker96743fc2011-02-12 14:30:57 +00002583#if defined(POLARSSL_PEM_C)
2584 pem_free( &pem );
2585#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002586
2587 return( 0 );
2588}
2589
Paul Bakker335db3f2011-04-25 15:28:35 +00002590#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002591/*
2592 * Load and parse a private RSA key
2593 */
2594int x509parse_dhmfile( dhm_context *dhm, const char *path )
2595{
2596 int ret;
2597 size_t n;
2598 unsigned char *buf;
2599
Paul Bakker69e095c2011-12-10 21:55:01 +00002600 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2601 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002602
Paul Bakker27fdf462011-06-09 13:55:13 +00002603 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002604
2605 memset( buf, 0, n + 1 );
2606 free( buf );
2607
2608 return( ret );
2609}
Paul Bakker335db3f2011-04-25 15:28:35 +00002610#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002611#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002612
Paul Bakker5121ce52009-01-03 21:22:43 +00002613#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002614#include <stdarg.h>
2615
2616#if !defined vsnprintf
2617#define vsnprintf _vsnprintf
2618#endif // vsnprintf
2619
2620/*
2621 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2622 * Result value is not size of buffer needed, but -1 if no fit is possible.
2623 *
2624 * This fuction tries to 'fix' this by at least suggesting enlarging the
2625 * size by 20.
2626 */
2627int compat_snprintf(char *str, size_t size, const char *format, ...)
2628{
2629 va_list ap;
2630 int res = -1;
2631
2632 va_start( ap, format );
2633
2634 res = vsnprintf( str, size, format, ap );
2635
2636 va_end( ap );
2637
2638 // No quick fix possible
2639 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002640 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002641
2642 return res;
2643}
2644
2645#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002646#endif
2647
Paul Bakkerd98030e2009-05-02 15:13:40 +00002648#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2649
2650#define SAFE_SNPRINTF() \
2651{ \
2652 if( ret == -1 ) \
2653 return( -1 ); \
2654 \
Paul Bakker23986e52011-04-24 08:57:21 +00002655 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002656 p[n - 1] = '\0'; \
2657 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2658 } \
2659 \
Paul Bakker23986e52011-04-24 08:57:21 +00002660 n -= (unsigned int) ret; \
2661 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002662}
2663
Paul Bakker5121ce52009-01-03 21:22:43 +00002664/*
2665 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002666 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002668int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002669{
Paul Bakker23986e52011-04-24 08:57:21 +00002670 int ret;
2671 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002673 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 char s[128], *p;
2675
2676 memset( s, 0, sizeof( s ) );
2677
2678 name = dn;
2679 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002680 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
2682 while( name != NULL )
2683 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002684 if( !name->oid.p )
2685 {
2686 name = name->next;
2687 continue;
2688 }
2689
Paul Bakker74111d32011-01-15 16:57:55 +00002690 if( name != dn )
2691 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002692 ret = snprintf( p, n, ", " );
2693 SAFE_SNPRINTF();
2694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Paul Bakker535e97d2012-08-23 10:49:55 +00002696 if( name->oid.len == 3 &&
2697 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 {
2699 switch( name->oid.p[2] )
2700 {
2701 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002702 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
2704 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002705 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
2707 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002708 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
2710 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002711 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
2713 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002714 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
2716 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002717 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
2719 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002720 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 name->oid.p[2] );
2722 break;
2723 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002724 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002726 else if( name->oid.len == 9 &&
2727 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 {
2729 switch( name->oid.p[8] )
2730 {
2731 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002732 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
2734 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002735 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 name->oid.p[8] );
2737 break;
2738 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002739 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 }
2741 else
Paul Bakker74111d32011-01-15 16:57:55 +00002742 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002743 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002744 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002745 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
2747 for( i = 0; i < name->val.len; i++ )
2748 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002749 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002750 break;
2751
2752 c = name->val.p[i];
2753 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2754 s[i] = '?';
2755 else s[i] = c;
2756 }
2757 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002758 ret = snprintf( p, n, "%s", s );
2759 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 name = name->next;
2761 }
2762
Paul Bakker23986e52011-04-24 08:57:21 +00002763 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002764}
2765
2766/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002767 * Store the serial in printable form into buf; no more
2768 * than size characters will be written
2769 */
2770int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2771{
Paul Bakker23986e52011-04-24 08:57:21 +00002772 int ret;
2773 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002774 char *p;
2775
2776 p = buf;
2777 n = size;
2778
2779 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002780 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002781
2782 for( i = 0; i < nr; i++ )
2783 {
Paul Bakker93048802011-12-05 14:38:06 +00002784 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002785 continue;
2786
Paul Bakkerdd476992011-01-16 21:34:59 +00002787 ret = snprintf( p, n, "%02X%s",
2788 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2789 SAFE_SNPRINTF();
2790 }
2791
Paul Bakker03c7c252011-11-25 12:37:37 +00002792 if( nr != serial->len )
2793 {
2794 ret = snprintf( p, n, "...." );
2795 SAFE_SNPRINTF();
2796 }
2797
Paul Bakker23986e52011-04-24 08:57:21 +00002798 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002799}
2800
2801/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002802 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002804int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2805 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002806{
Paul Bakker23986e52011-04-24 08:57:21 +00002807 int ret;
2808 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002809 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
2811 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002812 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Paul Bakkerd98030e2009-05-02 15:13:40 +00002814 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002816 SAFE_SNPRINTF();
2817 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002819 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Paul Bakkerdd476992011-01-16 21:34:59 +00002821 ret = x509parse_serial_gets( p, n, &crt->serial);
2822 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
Paul Bakkerd98030e2009-05-02 15:13:40 +00002824 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2825 SAFE_SNPRINTF();
2826 ret = x509parse_dn_gets( p, n, &crt->issuer );
2827 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
Paul Bakkerd98030e2009-05-02 15:13:40 +00002829 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2830 SAFE_SNPRINTF();
2831 ret = x509parse_dn_gets( p, n, &crt->subject );
2832 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Paul Bakkerd98030e2009-05-02 15:13:40 +00002834 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2836 crt->valid_from.year, crt->valid_from.mon,
2837 crt->valid_from.day, crt->valid_from.hour,
2838 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002839 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Paul Bakkerd98030e2009-05-02 15:13:40 +00002841 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2843 crt->valid_to.year, crt->valid_to.mon,
2844 crt->valid_to.day, crt->valid_to.hour,
2845 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002846 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakkerd98030e2009-05-02 15:13:40 +00002848 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2849 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Paul Bakker27d66162010-03-17 06:56:01 +00002851 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002853 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2854 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2855 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2856 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2857 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2858 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2859 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2860 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2861 default: ret = snprintf( p, n, "???" ); break;
2862 }
2863 SAFE_SNPRINTF();
2864
2865 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002866 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002867 SAFE_SNPRINTF();
2868
Paul Bakker23986e52011-04-24 08:57:21 +00002869 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002870}
2871
Paul Bakker74111d32011-01-15 16:57:55 +00002872/*
2873 * Return an informational string describing the given OID
2874 */
2875const char *x509_oid_get_description( x509_buf *oid )
2876{
2877 if ( oid == NULL )
2878 return ( NULL );
2879
2880 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2881 return( STRING_SERVER_AUTH );
2882
2883 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2884 return( STRING_CLIENT_AUTH );
2885
2886 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2887 return( STRING_CODE_SIGNING );
2888
2889 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2890 return( STRING_EMAIL_PROTECTION );
2891
2892 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2893 return( STRING_TIME_STAMPING );
2894
2895 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2896 return( STRING_OCSP_SIGNING );
2897
2898 return( NULL );
2899}
2900
2901/* Return the x.y.z.... style numeric string for the given OID */
2902int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2903{
Paul Bakker23986e52011-04-24 08:57:21 +00002904 int ret;
2905 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002906 unsigned int value;
2907 char *p;
2908
2909 p = buf;
2910 n = size;
2911
2912 /* First byte contains first two dots */
2913 if( oid->len > 0 )
2914 {
2915 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2916 SAFE_SNPRINTF();
2917 }
2918
2919 /* TODO: value can overflow in value. */
2920 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002921 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002922 {
2923 value <<= 7;
2924 value += oid->p[i] & 0x7F;
2925
2926 if( !( oid->p[i] & 0x80 ) )
2927 {
2928 /* Last byte */
2929 ret = snprintf( p, n, ".%d", value );
2930 SAFE_SNPRINTF();
2931 value = 0;
2932 }
2933 }
2934
Paul Bakker23986e52011-04-24 08:57:21 +00002935 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002936}
2937
Paul Bakkerd98030e2009-05-02 15:13:40 +00002938/*
2939 * Return an informational string about the CRL.
2940 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002941int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2942 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002943{
Paul Bakker23986e52011-04-24 08:57:21 +00002944 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002945 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002946 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002947 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002948
2949 p = buf;
2950 n = size;
2951
2952 ret = snprintf( p, n, "%sCRL version : %d",
2953 prefix, crl->version );
2954 SAFE_SNPRINTF();
2955
2956 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2957 SAFE_SNPRINTF();
2958 ret = x509parse_dn_gets( p, n, &crl->issuer );
2959 SAFE_SNPRINTF();
2960
2961 ret = snprintf( p, n, "\n%sthis update : " \
2962 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2963 crl->this_update.year, crl->this_update.mon,
2964 crl->this_update.day, crl->this_update.hour,
2965 crl->this_update.min, crl->this_update.sec );
2966 SAFE_SNPRINTF();
2967
2968 ret = snprintf( p, n, "\n%snext update : " \
2969 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2970 crl->next_update.year, crl->next_update.mon,
2971 crl->next_update.day, crl->next_update.hour,
2972 crl->next_update.min, crl->next_update.sec );
2973 SAFE_SNPRINTF();
2974
2975 entry = &crl->entry;
2976
2977 ret = snprintf( p, n, "\n%sRevoked certificates:",
2978 prefix );
2979 SAFE_SNPRINTF();
2980
Paul Bakker9be19372009-07-27 20:21:53 +00002981 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002982 {
2983 ret = snprintf( p, n, "\n%sserial number: ",
2984 prefix );
2985 SAFE_SNPRINTF();
2986
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002987 ret = x509parse_serial_gets( p, n, &entry->serial);
2988 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002989
Paul Bakkerd98030e2009-05-02 15:13:40 +00002990 ret = snprintf( p, n, " revocation date: " \
2991 "%04d-%02d-%02d %02d:%02d:%02d",
2992 entry->revocation_date.year, entry->revocation_date.mon,
2993 entry->revocation_date.day, entry->revocation_date.hour,
2994 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002995 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002996
2997 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002998 }
2999
Paul Bakkerd98030e2009-05-02 15:13:40 +00003000 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
3001 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003002
Paul Bakker27d66162010-03-17 06:56:01 +00003003 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003004 {
3005 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
3006 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
3007 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
3008 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
3009 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
3010 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
3011 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
3012 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
3013 default: ret = snprintf( p, n, "???" ); break;
3014 }
3015 SAFE_SNPRINTF();
3016
Paul Bakker1e27bb22009-07-19 20:25:25 +00003017 ret = snprintf( p, n, "\n" );
3018 SAFE_SNPRINTF();
3019
Paul Bakker23986e52011-04-24 08:57:21 +00003020 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003021}
3022
3023/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003024 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003026int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027{
Paul Bakkercce9d772011-11-18 14:26:47 +00003028 int year, mon, day;
3029 int hour, min, sec;
3030
3031#if defined(_WIN32)
3032 SYSTEMTIME st;
3033
3034 GetLocalTime(&st);
3035
3036 year = st.wYear;
3037 mon = st.wMonth;
3038 day = st.wDay;
3039 hour = st.wHour;
3040 min = st.wMinute;
3041 sec = st.wSecond;
3042#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 struct tm *lt;
3044 time_t tt;
3045
3046 tt = time( NULL );
3047 lt = localtime( &tt );
3048
Paul Bakkercce9d772011-11-18 14:26:47 +00003049 year = lt->tm_year + 1900;
3050 mon = lt->tm_mon + 1;
3051 day = lt->tm_mday;
3052 hour = lt->tm_hour;
3053 min = lt->tm_min;
3054 sec = lt->tm_sec;
3055#endif
3056
3057 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003058 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Paul Bakkercce9d772011-11-18 14:26:47 +00003060 if( year == to->year &&
3061 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003062 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Paul Bakkercce9d772011-11-18 14:26:47 +00003064 if( year == to->year &&
3065 mon == to->mon &&
3066 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003067 return( 1 );
3068
Paul Bakkercce9d772011-11-18 14:26:47 +00003069 if( year == to->year &&
3070 mon == to->mon &&
3071 day == to->day &&
3072 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003073 return( 1 );
3074
Paul Bakkercce9d772011-11-18 14:26:47 +00003075 if( year == to->year &&
3076 mon == to->mon &&
3077 day == to->day &&
3078 hour == to->hour &&
3079 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003080 return( 1 );
3081
Paul Bakkercce9d772011-11-18 14:26:47 +00003082 if( year == to->year &&
3083 mon == to->mon &&
3084 day == to->day &&
3085 hour == to->hour &&
3086 min == to->min &&
3087 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003088 return( 1 );
3089
Paul Bakker40ea7de2009-05-03 10:18:48 +00003090 return( 0 );
3091}
3092
3093/*
3094 * Return 1 if the certificate is revoked, or 0 otherwise.
3095 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003096int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003097{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003098 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003099
3100 while( cur != NULL && cur->serial.len != 0 )
3101 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003102 if( crt->serial.len == cur->serial.len &&
3103 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003104 {
3105 if( x509parse_time_expired( &cur->revocation_date ) )
3106 return( 1 );
3107 }
3108
3109 cur = cur->next;
3110 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
3112 return( 0 );
3113}
3114
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003115/*
3116 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003117 */
Paul Bakker23986e52011-04-24 08:57:21 +00003118static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003119 unsigned char *out )
3120{
3121 switch( alg )
3122 {
Paul Bakker40e46942009-01-03 21:51:57 +00003123#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003124 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003125#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003126#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003127 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003128#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003129#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003130 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003131#endif
3132#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003133 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003134#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003135#if defined(POLARSSL_SHA2_C)
3136 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3137 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3138#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003139#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003140 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3141 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3142#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003143 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003144 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 break;
3146 }
3147}
3148
3149/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003150 * Check that the given certificate is valid accoring to the CRL.
3151 */
3152static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3153 x509_crl *crl_list)
3154{
3155 int flags = 0;
3156 int hash_id;
3157 unsigned char hash[64];
3158
Paul Bakker915275b2012-09-28 07:10:55 +00003159 if( ca == NULL )
3160 return( flags );
3161
Paul Bakker76fd75a2011-01-16 21:12:10 +00003162 /*
3163 * TODO: What happens if no CRL is present?
3164 * Suggestion: Revocation state should be unknown if no CRL is present.
3165 * For backwards compatibility this is not yet implemented.
3166 */
3167
Paul Bakker915275b2012-09-28 07:10:55 +00003168 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003169 {
Paul Bakker915275b2012-09-28 07:10:55 +00003170 if( crl_list->version == 0 ||
3171 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003172 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3173 crl_list->issuer_raw.len ) != 0 )
3174 {
3175 crl_list = crl_list->next;
3176 continue;
3177 }
3178
3179 /*
3180 * Check if CRL is correctly signed by the trusted CA
3181 */
3182 hash_id = crl_list->sig_alg;
3183
3184 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3185
3186 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
3187 0, hash, crl_list->sig.p ) == 0 )
3188 {
3189 /*
3190 * CRL is not trusted
3191 */
3192 flags |= BADCRL_NOT_TRUSTED;
3193 break;
3194 }
3195
3196 /*
3197 * Check for validity of CRL (Do not drop out)
3198 */
3199 if( x509parse_time_expired( &crl_list->next_update ) )
3200 flags |= BADCRL_EXPIRED;
3201
3202 /*
3203 * Check if certificate is revoked
3204 */
3205 if( x509parse_revoked(crt, crl_list) )
3206 {
3207 flags |= BADCERT_REVOKED;
3208 break;
3209 }
3210
3211 crl_list = crl_list->next;
3212 }
3213 return flags;
3214}
3215
Paul Bakker57b12982012-02-11 17:38:38 +00003216int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003217{
3218 size_t i;
3219 size_t cn_idx = 0;
3220
Paul Bakker57b12982012-02-11 17:38:38 +00003221 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003222 return( 0 );
3223
3224 for( i = 0; i < strlen( cn ); ++i )
3225 {
3226 if( cn[i] == '.' )
3227 {
3228 cn_idx = i;
3229 break;
3230 }
3231 }
3232
3233 if( cn_idx == 0 )
3234 return( 0 );
3235
Paul Bakker535e97d2012-08-23 10:49:55 +00003236 if( strlen( cn ) - cn_idx == name->len - 1 &&
3237 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003238 {
3239 return( 1 );
3240 }
3241
3242 return( 0 );
3243}
3244
Paul Bakker915275b2012-09-28 07:10:55 +00003245static int x509parse_verify_top(
3246 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003247 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003248 int (*f_vrfy)(void *, x509_cert *, int, int *),
3249 void *p_vrfy )
3250{
3251 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003252 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003253 unsigned char hash[64];
3254
3255 if( x509parse_time_expired( &child->valid_to ) )
3256 *flags |= BADCERT_EXPIRED;
3257
3258 /*
3259 * Child is the top of the chain. Check against the trust_ca list.
3260 */
3261 *flags |= BADCERT_NOT_TRUSTED;
3262
3263 while( trust_ca != NULL )
3264 {
3265 if( trust_ca->version == 0 ||
3266 child->issuer_raw.len != trust_ca->subject_raw.len ||
3267 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3268 child->issuer_raw.len ) != 0 )
3269 {
3270 trust_ca = trust_ca->next;
3271 continue;
3272 }
3273
Paul Bakker9a736322012-11-14 12:39:52 +00003274 /*
3275 * Reduce path_len to check against if top of the chain is
3276 * the same as the trusted CA
3277 */
3278 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3279 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3280 child->issuer_raw.len ) == 0 )
3281 {
3282 check_path_cnt--;
3283 }
3284
Paul Bakker915275b2012-09-28 07:10:55 +00003285 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003286 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003287 {
3288 trust_ca = trust_ca->next;
3289 continue;
3290 }
3291
3292 hash_id = child->sig_alg;
3293
3294 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3295
3296 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3297 0, hash, child->sig.p ) != 0 )
3298 {
3299 trust_ca = trust_ca->next;
3300 continue;
3301 }
3302
3303 /*
3304 * Top of chain is signed by a trusted CA
3305 */
3306 *flags &= ~BADCERT_NOT_TRUSTED;
3307 break;
3308 }
3309
Paul Bakker9a736322012-11-14 12:39:52 +00003310 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003311 * If top of chain is not the same as the trusted CA send a verify request
3312 * to the callback for any issues with validity and CRL presence for the
3313 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003314 */
3315 if( trust_ca != NULL &&
3316 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3317 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3318 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003319 {
3320 /* Check trusted CA's CRL for then chain's top crt */
3321 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3322
3323 if( x509parse_time_expired( &trust_ca->valid_to ) )
3324 ca_flags |= BADCERT_EXPIRED;
3325
Paul Bakker915275b2012-09-28 07:10:55 +00003326 if( NULL != f_vrfy )
3327 {
Paul Bakker9a736322012-11-14 12:39:52 +00003328 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003329 return( ret );
3330 }
3331 }
3332
3333 /* Call callback on top cert */
3334 if( NULL != f_vrfy )
3335 {
Paul Bakker9a736322012-11-14 12:39:52 +00003336 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003337 return( ret );
3338 }
3339
Paul Bakker915275b2012-09-28 07:10:55 +00003340 *flags |= ca_flags;
3341
3342 return( 0 );
3343}
3344
3345static int x509parse_verify_child(
3346 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003347 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003348 int (*f_vrfy)(void *, x509_cert *, int, int *),
3349 void *p_vrfy )
3350{
3351 int hash_id, ret;
3352 int parent_flags = 0;
3353 unsigned char hash[64];
3354 x509_cert *grandparent;
3355
3356 if( x509parse_time_expired( &child->valid_to ) )
3357 *flags |= BADCERT_EXPIRED;
3358
3359 hash_id = child->sig_alg;
3360
3361 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3362
3363 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3364 child->sig.p ) != 0 )
3365 *flags |= BADCERT_NOT_TRUSTED;
3366
3367 /* Check trusted CA's CRL for the given crt */
3368 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3369
3370 grandparent = parent->next;
3371
3372 while( grandparent != NULL )
3373 {
3374 if( grandparent->version == 0 ||
3375 grandparent->ca_istrue == 0 ||
3376 parent->issuer_raw.len != grandparent->subject_raw.len ||
3377 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3378 parent->issuer_raw.len ) != 0 )
3379 {
3380 grandparent = grandparent->next;
3381 continue;
3382 }
3383 break;
3384 }
3385
Paul Bakker915275b2012-09-28 07:10:55 +00003386 if( grandparent != NULL )
3387 {
3388 /*
3389 * Part of the chain
3390 */
Paul Bakker9a736322012-11-14 12:39:52 +00003391 ret = x509parse_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003392 if( ret != 0 )
3393 return( ret );
3394 }
3395 else
3396 {
Paul Bakker9a736322012-11-14 12:39:52 +00003397 ret = x509parse_verify_top( parent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003398 if( ret != 0 )
3399 return( ret );
3400 }
3401
3402 /* child is verified to be a child of the parent, call verify callback */
3403 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003404 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003405 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003406
3407 *flags |= parent_flags;
3408
3409 return( 0 );
3410}
3411
Paul Bakker76fd75a2011-01-16 21:12:10 +00003412/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003413 * Verify the certificate validity
3414 */
3415int x509parse_verify( x509_cert *crt,
3416 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003417 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003418 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003419 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003420 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003421{
Paul Bakker23986e52011-04-24 08:57:21 +00003422 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003423 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003424 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003425 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003427 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003428
Paul Bakker40ea7de2009-05-03 10:18:48 +00003429 *flags = 0;
3430
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 if( cn != NULL )
3432 {
3433 name = &crt->subject;
3434 cn_len = strlen( cn );
3435
Paul Bakker4d2c1242012-05-10 14:12:46 +00003436 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003437 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003438 cur = &crt->subject_alt_names;
3439
3440 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003441 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003442 if( cur->buf.len == cn_len &&
3443 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003444 break;
3445
Paul Bakker535e97d2012-08-23 10:49:55 +00003446 if( cur->buf.len > 2 &&
3447 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003448 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003449 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003450
Paul Bakker4d2c1242012-05-10 14:12:46 +00003451 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003452 }
3453
3454 if( cur == NULL )
3455 *flags |= BADCERT_CN_MISMATCH;
3456 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003457 else
3458 {
3459 while( name != NULL )
3460 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003461 if( name->oid.len == 3 &&
3462 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003463 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003464 if( name->val.len == cn_len &&
3465 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003466 break;
3467
Paul Bakker535e97d2012-08-23 10:49:55 +00003468 if( name->val.len > 2 &&
3469 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003470 x509_wildcard_verify( cn, &name->val ) )
3471 break;
3472 }
3473
3474 name = name->next;
3475 }
3476
3477 if( name == NULL )
3478 *flags |= BADCERT_CN_MISMATCH;
3479 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003480 }
3481
Paul Bakker5121ce52009-01-03 21:22:43 +00003482 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003483 * Iterate upwards in the given cert chain, to find our crt parent.
3484 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003485 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003486 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003487
Paul Bakker76fd75a2011-01-16 21:12:10 +00003488 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003490 if( parent->ca_istrue == 0 ||
3491 crt->issuer_raw.len != parent->subject_raw.len ||
3492 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 crt->issuer_raw.len ) != 0 )
3494 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003495 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003496 continue;
3497 }
Paul Bakker915275b2012-09-28 07:10:55 +00003498 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 }
3500
Paul Bakker915275b2012-09-28 07:10:55 +00003501 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 {
Paul Bakker915275b2012-09-28 07:10:55 +00003503 /*
3504 * Part of the chain
3505 */
Paul Bakker9a736322012-11-14 12:39:52 +00003506 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003507 if( ret != 0 )
3508 return( ret );
3509 }
3510 else
Paul Bakker74111d32011-01-15 16:57:55 +00003511 {
Paul Bakker9a736322012-11-14 12:39:52 +00003512 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003513 if( ret != 0 )
3514 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003515 }
Paul Bakker915275b2012-09-28 07:10:55 +00003516
3517 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003518 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003519
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 return( 0 );
3521}
3522
3523/*
3524 * Unallocate all certificate data
3525 */
3526void x509_free( x509_cert *crt )
3527{
3528 x509_cert *cert_cur = crt;
3529 x509_cert *cert_prv;
3530 x509_name *name_cur;
3531 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003532 x509_sequence *seq_cur;
3533 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
3535 if( crt == NULL )
3536 return;
3537
3538 do
3539 {
3540 rsa_free( &cert_cur->rsa );
3541
3542 name_cur = cert_cur->issuer.next;
3543 while( name_cur != NULL )
3544 {
3545 name_prv = name_cur;
3546 name_cur = name_cur->next;
3547 memset( name_prv, 0, sizeof( x509_name ) );
3548 free( name_prv );
3549 }
3550
3551 name_cur = cert_cur->subject.next;
3552 while( name_cur != NULL )
3553 {
3554 name_prv = name_cur;
3555 name_cur = name_cur->next;
3556 memset( name_prv, 0, sizeof( x509_name ) );
3557 free( name_prv );
3558 }
3559
Paul Bakker74111d32011-01-15 16:57:55 +00003560 seq_cur = cert_cur->ext_key_usage.next;
3561 while( seq_cur != NULL )
3562 {
3563 seq_prv = seq_cur;
3564 seq_cur = seq_cur->next;
3565 memset( seq_prv, 0, sizeof( x509_sequence ) );
3566 free( seq_prv );
3567 }
3568
Paul Bakker8afa70d2012-02-11 18:42:45 +00003569 seq_cur = cert_cur->subject_alt_names.next;
3570 while( seq_cur != NULL )
3571 {
3572 seq_prv = seq_cur;
3573 seq_cur = seq_cur->next;
3574 memset( seq_prv, 0, sizeof( x509_sequence ) );
3575 free( seq_prv );
3576 }
3577
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 if( cert_cur->raw.p != NULL )
3579 {
3580 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3581 free( cert_cur->raw.p );
3582 }
3583
3584 cert_cur = cert_cur->next;
3585 }
3586 while( cert_cur != NULL );
3587
3588 cert_cur = crt;
3589 do
3590 {
3591 cert_prv = cert_cur;
3592 cert_cur = cert_cur->next;
3593
3594 memset( cert_prv, 0, sizeof( x509_cert ) );
3595 if( cert_prv != crt )
3596 free( cert_prv );
3597 }
3598 while( cert_cur != NULL );
3599}
3600
Paul Bakkerd98030e2009-05-02 15:13:40 +00003601/*
3602 * Unallocate all CRL data
3603 */
3604void x509_crl_free( x509_crl *crl )
3605{
3606 x509_crl *crl_cur = crl;
3607 x509_crl *crl_prv;
3608 x509_name *name_cur;
3609 x509_name *name_prv;
3610 x509_crl_entry *entry_cur;
3611 x509_crl_entry *entry_prv;
3612
3613 if( crl == NULL )
3614 return;
3615
3616 do
3617 {
3618 name_cur = crl_cur->issuer.next;
3619 while( name_cur != NULL )
3620 {
3621 name_prv = name_cur;
3622 name_cur = name_cur->next;
3623 memset( name_prv, 0, sizeof( x509_name ) );
3624 free( name_prv );
3625 }
3626
3627 entry_cur = crl_cur->entry.next;
3628 while( entry_cur != NULL )
3629 {
3630 entry_prv = entry_cur;
3631 entry_cur = entry_cur->next;
3632 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3633 free( entry_prv );
3634 }
3635
3636 if( crl_cur->raw.p != NULL )
3637 {
3638 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3639 free( crl_cur->raw.p );
3640 }
3641
3642 crl_cur = crl_cur->next;
3643 }
3644 while( crl_cur != NULL );
3645
3646 crl_cur = crl;
3647 do
3648 {
3649 crl_prv = crl_cur;
3650 crl_cur = crl_cur->next;
3651
3652 memset( crl_prv, 0, sizeof( x509_crl ) );
3653 if( crl_prv != crl )
3654 free( crl_prv );
3655 }
3656 while( crl_cur != NULL );
3657}
3658
Paul Bakker40e46942009-01-03 21:51:57 +00003659#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003660
Paul Bakker40e46942009-01-03 21:51:57 +00003661#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003662
3663/*
3664 * Checkup routine
3665 */
3666int x509_self_test( int verbose )
3667{
Paul Bakker5690efc2011-05-26 13:16:06 +00003668#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003669 int ret;
3670 int flags;
3671 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003672 x509_cert cacert;
3673 x509_cert clicert;
3674 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003675#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003676 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003677#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003678
3679 if( verbose != 0 )
3680 printf( " X.509 certificate load: " );
3681
3682 memset( &clicert, 0, sizeof( x509_cert ) );
3683
Paul Bakkereae09db2013-06-06 12:35:54 +02003684 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003685 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 if( ret != 0 )
3687 {
3688 if( verbose != 0 )
3689 printf( "failed\n" );
3690
3691 return( ret );
3692 }
3693
3694 memset( &cacert, 0, sizeof( x509_cert ) );
3695
Paul Bakkereae09db2013-06-06 12:35:54 +02003696 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003697 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 if( ret != 0 )
3699 {
3700 if( verbose != 0 )
3701 printf( "failed\n" );
3702
3703 return( ret );
3704 }
3705
3706 if( verbose != 0 )
3707 printf( "passed\n X.509 private key load: " );
3708
3709 i = strlen( test_ca_key );
3710 j = strlen( test_ca_pwd );
3711
Paul Bakker66b78b22011-03-25 14:22:50 +00003712 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3713
Paul Bakker5121ce52009-01-03 21:22:43 +00003714 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003715 (const unsigned char *) test_ca_key, i,
3716 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 {
3718 if( verbose != 0 )
3719 printf( "failed\n" );
3720
3721 return( ret );
3722 }
3723
3724 if( verbose != 0 )
3725 printf( "passed\n X.509 signature verify: ");
3726
Paul Bakker23986e52011-04-24 08:57:21 +00003727 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 if( ret != 0 )
3729 {
Paul Bakker23986e52011-04-24 08:57:21 +00003730 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 if( verbose != 0 )
3732 printf( "failed\n" );
3733
3734 return( ret );
3735 }
3736
Paul Bakker5690efc2011-05-26 13:16:06 +00003737#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003739 printf( "passed\n X.509 DHM parameter load: " );
3740
3741 i = strlen( test_dhm_params );
3742 j = strlen( test_ca_pwd );
3743
Paul Bakkereae09db2013-06-06 12:35:54 +02003744 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003745 {
3746 if( verbose != 0 )
3747 printf( "failed\n" );
3748
3749 return( ret );
3750 }
3751
3752 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003753 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003754#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003755
3756 x509_free( &cacert );
3757 x509_free( &clicert );
3758 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003759#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003760 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003761#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003762
3763 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003764#else
3765 ((void) verbose);
3766 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3767#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003768}
3769
3770#endif
3771
3772#endif