blob: 26d655f1f7f517bbfd471523db194ac7bc362d10 [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 Bakker1fd43212013-06-17 15:14:42 +020064#if defined(POLARSSL_PKCS5_C)
65#include "polarssl/pkcs5.h"
66#endif
Paul Bakker14a222c2013-06-18 16:35:48 +020067#if defined(POLARSSL_PKCS12_C)
68#include "polarssl/pkcs12.h"
69#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000070
71#include <string.h>
72#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000073#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000074#include <windows.h>
75#else
Paul Bakker5121ce52009-01-03 21:22:43 +000076#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Paul Bakker335db3f2011-04-25 15:28:35 +000079#if defined(POLARSSL_FS_IO)
80#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000081#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000082#include <sys/types.h>
Paul Bakkercbfcaa92013-06-13 09:20:25 +020083#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000084#include <dirent.h>
85#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000086#endif
87
Paul Bakkercf6e95d2013-06-12 13:18:15 +020088/* Compare a given OID string with an OID x509_buf * */
89#define OID_CMP(oid_str, oid_buf) \
90 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
91 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093/*
Paul Bakker5121ce52009-01-03 21:22:43 +000094 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
95 */
96static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000097 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000098 int *ver )
99{
Paul Bakker23986e52011-04-24 08:57:21 +0000100 int ret;
101 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
103 if( ( ret = asn1_get_tag( p, end, &len,
104 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
105 {
Paul Bakker40e46942009-01-03 21:51:57 +0000106 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000107 {
108 *ver = 0;
109 return( 0 );
110 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
112 return( ret );
113 }
114
115 end = *p + len;
116
117 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000118 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000121 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000122 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124 return( 0 );
125}
126
127/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000128 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000129 */
130static int x509_crl_get_version( unsigned char **p,
131 const unsigned char *end,
132 int *ver )
133{
134 int ret;
135
136 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
137 {
138 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000139 {
140 *ver = 0;
141 return( 0 );
142 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000143
144 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
145 }
146
147 return( 0 );
148}
149
150/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 * CertificateSerialNumber ::= INTEGER
152 */
153static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000154 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 x509_buf *serial )
156{
157 int ret;
158
159 if( ( end - *p ) < 1 )
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_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
164 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000165 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000166 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 serial->tag = *(*p)++;
169
170 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000171 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 serial->p = *p;
174 *p += serial->len;
175
176 return( 0 );
177}
178
179/*
180 * AlgorithmIdentifier ::= SEQUENCE {
181 * algorithm OBJECT IDENTIFIER,
182 * parameters ANY DEFINED BY algorithm OPTIONAL }
183 */
184static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000185 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 x509_buf *alg )
187{
Paul Bakker23986e52011-04-24 08:57:21 +0000188 int ret;
189 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 if( ( ret = asn1_get_tag( p, end, &len,
192 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000193 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 end = *p + len;
196 alg->tag = **p;
197
198 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000199 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201 alg->p = *p;
202 *p += alg->len;
203
204 if( *p == end )
205 return( 0 );
206
207 /*
208 * assume the algorithm parameters must be NULL
209 */
210 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000211 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000214 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000215 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217 return( 0 );
218}
219
220/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 * AttributeTypeAndValue ::= SEQUENCE {
222 * type AttributeType,
223 * value AttributeValue }
224 *
225 * AttributeType ::= OBJECT IDENTIFIER
226 *
227 * AttributeValue ::= ANY DEFINED BY AttributeType
228 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000229static int x509_get_attr_type_value( unsigned char **p,
230 const unsigned char *end,
231 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000232{
Paul Bakker23986e52011-04-24 08:57:21 +0000233 int ret;
234 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 x509_buf *oid;
236 x509_buf *val;
237
238 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000240 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 oid = &cur->oid;
243 oid->tag = **p;
244
245 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000246 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248 oid->p = *p;
249 *p += oid->len;
250
251 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000252 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000253 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
256 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
257 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000258 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000259 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261 val = &cur->val;
262 val->tag = *(*p)++;
263
264 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000265 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267 val->p = *p;
268 *p += val->len;
269
270 cur->next = NULL;
271
Paul Bakker400ff6f2011-02-20 10:40:16 +0000272 return( 0 );
273}
274
275/*
276 * RelativeDistinguishedName ::=
277 * SET OF AttributeTypeAndValue
278 *
279 * AttributeTypeAndValue ::= SEQUENCE {
280 * type AttributeType,
281 * value AttributeValue }
282 *
283 * AttributeType ::= OBJECT IDENTIFIER
284 *
285 * AttributeValue ::= ANY DEFINED BY AttributeType
286 */
287static int x509_get_name( unsigned char **p,
288 const unsigned char *end,
289 x509_name *cur )
290{
Paul Bakker23986e52011-04-24 08:57:21 +0000291 int ret;
292 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000293 const unsigned char *end2;
294 x509_name *use;
295
296 if( ( ret = asn1_get_tag( p, end, &len,
297 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000298 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000299
300 end2 = end;
301 end = *p + len;
302 use = cur;
303
304 do
305 {
306 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
307 return( ret );
308
309 if( *p != end )
310 {
311 use->next = (x509_name *) malloc(
312 sizeof( x509_name ) );
313
314 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000315 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000316
317 memset( use->next, 0, sizeof( x509_name ) );
318
319 use = use->next;
320 }
321 }
322 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324 /*
325 * recurse until end of SEQUENCE is reached
326 */
327 if( *p == end2 )
328 return( 0 );
329
330 cur->next = (x509_name *) malloc(
331 sizeof( x509_name ) );
332
333 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000334 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
Paul Bakker430ffbe2012-05-01 08:14:20 +0000336 memset( cur->next, 0, sizeof( x509_name ) );
337
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 return( x509_get_name( p, end2, cur->next ) );
339}
340
341/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 * Time ::= CHOICE {
343 * utcTime UTCTime,
344 * generalTime GeneralizedTime }
345 */
Paul Bakker91200182010-02-18 21:26:15 +0000346static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000347 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000348 x509_time *time )
349{
Paul Bakker23986e52011-04-24 08:57:21 +0000350 int ret;
351 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000352 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000353 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000354
Paul Bakker91200182010-02-18 21:26:15 +0000355 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000356 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
357 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000358
Paul Bakker91200182010-02-18 21:26:15 +0000359 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000360
Paul Bakker91200182010-02-18 21:26:15 +0000361 if ( tag == ASN1_UTC_TIME )
362 {
363 (*p)++;
364 ret = asn1_get_len( p, end, &len );
365
366 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000367 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000368
Paul Bakker91200182010-02-18 21:26:15 +0000369 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000370 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
371 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000372
Paul Bakker91200182010-02-18 21:26:15 +0000373 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
374 &time->year, &time->mon, &time->day,
375 &time->hour, &time->min, &time->sec ) < 5 )
376 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000377
Paul Bakker400ff6f2011-02-20 10:40:16 +0000378 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000379 time->year += 1900;
380
381 *p += len;
382
383 return( 0 );
384 }
385 else if ( tag == ASN1_GENERALIZED_TIME )
386 {
387 (*p)++;
388 ret = asn1_get_len( p, end, &len );
389
390 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000391 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000392
393 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000394 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
395 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000396
397 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
398 &time->year, &time->mon, &time->day,
399 &time->hour, &time->min, &time->sec ) < 5 )
400 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
401
402 *p += len;
403
404 return( 0 );
405 }
406 else
Paul Bakker9d781402011-05-09 16:17:09 +0000407 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000408}
409
410
411/*
412 * Validity ::= SEQUENCE {
413 * notBefore Time,
414 * notAfter Time }
415 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000416static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000417 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 x509_time *from,
419 x509_time *to )
420{
Paul Bakker23986e52011-04-24 08:57:21 +0000421 int ret;
422 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 if( ( ret = asn1_get_tag( p, end, &len,
425 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000426 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
428 end = *p + len;
429
Paul Bakker91200182010-02-18 21:26:15 +0000430 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000431 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
Paul Bakker91200182010-02-18 21:26:15 +0000433 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000434 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000437 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000438 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 return( 0 );
441}
442
443/*
444 * SubjectPublicKeyInfo ::= SEQUENCE {
445 * algorithm AlgorithmIdentifier,
446 * subjectPublicKey BIT STRING }
447 */
448static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000449 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 x509_buf *pk_alg_oid,
451 mpi *N, mpi *E )
452{
Paul Bakker65a19092013-06-06 21:14:58 +0200453 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000454 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 unsigned char *end2;
456
457 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
458 return( ret );
459
460 /*
461 * only RSA public keys handled at this time
462 */
Paul Bakker65a19092013-06-06 21:14:58 +0200463 if( pk_alg_oid->len != 9 ||
464 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) != 0 )
Paul Bakker400ff6f2011-02-20 10:40:16 +0000465 {
Paul Bakkered56b222011-07-13 11:26:43 +0000466 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker65a19092013-06-06 21:14:58 +0200467 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000470 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000473 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000474 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 end2 = *p + len;
477
478 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000479 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481 /*
482 * RSAPublicKey ::= SEQUENCE {
483 * modulus INTEGER, -- n
484 * publicExponent INTEGER -- e
485 * }
486 */
487 if( ( ret = asn1_get_tag( p, end2, &len,
488 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
491 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000492 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000493 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
496 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000497 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000500 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000501 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
503 return( 0 );
504}
505
506static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000507 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 x509_buf *sig )
509{
Paul Bakker23986e52011-04-24 08:57:21 +0000510 int ret;
511 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Paul Bakker8afa70d2012-02-11 18:42:45 +0000513 if( ( end - *p ) < 1 )
514 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
515 POLARSSL_ERR_ASN1_OUT_OF_DATA );
516
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 sig->tag = **p;
518
519 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000520 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Paul Bakker74111d32011-01-15 16:57:55 +0000522
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000524 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 sig->len = len;
527 sig->p = *p;
528
529 *p += len;
530
531 return( 0 );
532}
533
534/*
535 * X.509 v2/v3 unique identifier (not parsed)
536 */
537static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000538 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 x509_buf *uid, int n )
540{
541 int ret;
542
543 if( *p == end )
544 return( 0 );
545
546 uid->tag = **p;
547
548 if( ( ret = asn1_get_tag( p, end, &uid->len,
549 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
550 {
Paul Bakker40e46942009-01-03 21:51:57 +0000551 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 return( 0 );
553
554 return( ret );
555 }
556
557 uid->p = *p;
558 *p += uid->len;
559
560 return( 0 );
561}
562
563/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000564 * X.509 Extensions (No parsing of extensions, pointer should
565 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
567static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000569 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570{
Paul Bakker23986e52011-04-24 08:57:21 +0000571 int ret;
572 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 if( *p == end )
575 return( 0 );
576
577 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000578
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000580 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583 ext->p = *p;
584 end = *p + ext->len;
585
586 /*
587 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
588 *
589 * Extension ::= SEQUENCE {
590 * extnID OBJECT IDENTIFIER,
591 * critical BOOLEAN DEFAULT FALSE,
592 * extnValue OCTET STRING }
593 */
594 if( ( ret = asn1_get_tag( p, end, &len,
595 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000596 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
598 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000599 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000600 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Paul Bakkerd98030e2009-05-02 15:13:40 +0000602 return( 0 );
603}
604
605/*
606 * X.509 CRL v2 extensions (no extensions parsed yet.)
607 */
608static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000609 const unsigned char *end,
610 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000611{
Paul Bakker23986e52011-04-24 08:57:21 +0000612 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000613 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000615 /* Get explicit tag */
616 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000617 {
618 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
619 return( 0 );
620
621 return( ret );
622 }
623
624 while( *p < end )
625 {
626 if( ( ret = asn1_get_tag( p, end, &len,
627 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000628 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000629
630 *p += len;
631 }
632
633 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000634 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000635 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
636
637 return( 0 );
638}
639
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000640/*
641 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
642 */
643static int x509_get_crl_entry_ext( unsigned char **p,
644 const unsigned char *end,
645 x509_buf *ext )
646{
647 int ret;
648 size_t len = 0;
649
650 /* OPTIONAL */
651 if (end <= *p)
652 return( 0 );
653
654 ext->tag = **p;
655 ext->p = *p;
656
657 /*
658 * Get CRL-entry extension sequence header
659 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
660 */
661 if( ( ret = asn1_get_tag( p, end, &ext->len,
662 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
663 {
664 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
665 {
666 ext->p = NULL;
667 return( 0 );
668 }
669 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
670 }
671
672 end = *p + ext->len;
673
674 if( end != *p + ext->len )
675 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
676 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
677
678 while( *p < end )
679 {
680 if( ( ret = asn1_get_tag( p, end, &len,
681 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
682 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
683
684 *p += len;
685 }
686
687 if( *p != end )
688 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
689 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
690
691 return( 0 );
692}
693
Paul Bakker74111d32011-01-15 16:57:55 +0000694static int x509_get_basic_constraints( unsigned char **p,
695 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000696 int *ca_istrue,
697 int *max_pathlen )
698{
Paul Bakker23986e52011-04-24 08:57:21 +0000699 int ret;
700 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000701
702 /*
703 * BasicConstraints ::= SEQUENCE {
704 * cA BOOLEAN DEFAULT FALSE,
705 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
706 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000707 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000708 *max_pathlen = 0; /* endless */
709
710 if( ( ret = asn1_get_tag( p, end, &len,
711 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000712 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000713
714 if( *p == end )
715 return 0;
716
Paul Bakker3cccddb2011-01-16 21:46:31 +0000717 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000718 {
719 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000720 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000721
722 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000723 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000724
Paul Bakker3cccddb2011-01-16 21:46:31 +0000725 if( *ca_istrue != 0 )
726 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000727 }
728
729 if( *p == end )
730 return 0;
731
732 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000733 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000734
735 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000736 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000737 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
738
739 (*max_pathlen)++;
740
Paul Bakker74111d32011-01-15 16:57:55 +0000741 return 0;
742}
743
744static int x509_get_ns_cert_type( unsigned char **p,
745 const unsigned char *end,
746 unsigned char *ns_cert_type)
747{
748 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000749 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000750
751 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000752 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000753
754 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000755 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000756 POLARSSL_ERR_ASN1_INVALID_LENGTH );
757
758 /* Get actual bitstring */
759 *ns_cert_type = *bs.p;
760 return 0;
761}
762
763static int x509_get_key_usage( unsigned char **p,
764 const unsigned char *end,
765 unsigned char *key_usage)
766{
767 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000768 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000769
770 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000771 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000772
Paul Bakker94a67962012-08-23 13:03:52 +0000773 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000774 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000775 POLARSSL_ERR_ASN1_INVALID_LENGTH );
776
777 /* Get actual bitstring */
778 *key_usage = *bs.p;
779 return 0;
780}
781
Paul Bakkerd98030e2009-05-02 15:13:40 +0000782/*
Paul Bakker74111d32011-01-15 16:57:55 +0000783 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
784 *
785 * KeyPurposeId ::= OBJECT IDENTIFIER
786 */
787static int x509_get_ext_key_usage( unsigned char **p,
788 const unsigned char *end,
789 x509_sequence *ext_key_usage)
790{
791 int ret;
792
793 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000794 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000795
796 /* Sequence length must be >= 1 */
797 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000798 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000799 POLARSSL_ERR_ASN1_INVALID_LENGTH );
800
801 return 0;
802}
803
804/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000805 * SubjectAltName ::= GeneralNames
806 *
807 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
808 *
809 * GeneralName ::= CHOICE {
810 * otherName [0] OtherName,
811 * rfc822Name [1] IA5String,
812 * dNSName [2] IA5String,
813 * x400Address [3] ORAddress,
814 * directoryName [4] Name,
815 * ediPartyName [5] EDIPartyName,
816 * uniformResourceIdentifier [6] IA5String,
817 * iPAddress [7] OCTET STRING,
818 * registeredID [8] OBJECT IDENTIFIER }
819 *
820 * OtherName ::= SEQUENCE {
821 * type-id OBJECT IDENTIFIER,
822 * value [0] EXPLICIT ANY DEFINED BY type-id }
823 *
824 * EDIPartyName ::= SEQUENCE {
825 * nameAssigner [0] DirectoryString OPTIONAL,
826 * partyName [1] DirectoryString }
827 *
828 * NOTE: PolarSSL only parses and uses dNSName at this point.
829 */
830static int x509_get_subject_alt_name( unsigned char **p,
831 const unsigned char *end,
832 x509_sequence *subject_alt_name )
833{
834 int ret;
835 size_t len, tag_len;
836 asn1_buf *buf;
837 unsigned char tag;
838 asn1_sequence *cur = subject_alt_name;
839
840 /* Get main sequence tag */
841 if( ( ret = asn1_get_tag( p, end, &len,
842 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
843 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
844
845 if( *p + len != end )
846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
847 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
848
849 while( *p < end )
850 {
851 if( ( end - *p ) < 1 )
852 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
853 POLARSSL_ERR_ASN1_OUT_OF_DATA );
854
855 tag = **p;
856 (*p)++;
857 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
858 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
859
860 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
861 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
862 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
863
864 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
865 {
866 *p += tag_len;
867 continue;
868 }
869
870 buf = &(cur->buf);
871 buf->tag = tag;
872 buf->p = *p;
873 buf->len = tag_len;
874 *p += buf->len;
875
876 /* Allocate and assign next pointer */
877 if (*p < end)
878 {
879 cur->next = (asn1_sequence *) malloc(
880 sizeof( asn1_sequence ) );
881
882 if( cur->next == NULL )
883 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
884 POLARSSL_ERR_ASN1_MALLOC_FAILED );
885
Paul Bakker535e97d2012-08-23 10:49:55 +0000886 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000887 cur = cur->next;
888 }
889 }
890
891 /* Set final sequence entry's next pointer to NULL */
892 cur->next = NULL;
893
894 if( *p != end )
895 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
896 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
897
898 return( 0 );
899}
900
901/*
Paul Bakker74111d32011-01-15 16:57:55 +0000902 * X.509 v3 extensions
903 *
904 * TODO: Perform all of the basic constraints tests required by the RFC
905 * TODO: Set values for undetected extensions to a sane default?
906 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000907 */
908static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000909 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000910 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000911{
Paul Bakker23986e52011-04-24 08:57:21 +0000912 int ret;
913 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000914 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000915
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000916 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000917 {
918 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
919 return( 0 );
920
921 return( ret );
922 }
923
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 while( *p < end )
925 {
Paul Bakker74111d32011-01-15 16:57:55 +0000926 /*
927 * Extension ::= SEQUENCE {
928 * extnID OBJECT IDENTIFIER,
929 * critical BOOLEAN DEFAULT FALSE,
930 * extnValue OCTET STRING }
931 */
932 x509_buf extn_oid = {0, 0, NULL};
933 int is_critical = 0; /* DEFAULT FALSE */
934
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 if( ( ret = asn1_get_tag( p, end, &len,
936 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000937 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000939 end_ext_data = *p + len;
940
Paul Bakker74111d32011-01-15 16:57:55 +0000941 /* Get extension ID */
942 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000943
Paul Bakker74111d32011-01-15 16:57:55 +0000944 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000945 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Paul Bakker74111d32011-01-15 16:57:55 +0000947 extn_oid.p = *p;
948 *p += extn_oid.len;
949
950 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000951 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000952 POLARSSL_ERR_ASN1_OUT_OF_DATA );
953
954 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000955 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000956 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
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 Bakker74111d32011-01-15 16:57:55 +0000959 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000960 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000962 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000964 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000965
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000966 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000967 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000968 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000969
Paul Bakker74111d32011-01-15 16:57:55 +0000970 /*
971 * Detect supported extensions
972 */
973 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
974 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 {
Paul Bakker74111d32011-01-15 16:57:55 +0000976 /* Parse basic constraints */
977 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000978 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000979 return ( ret );
980 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 }
Paul Bakker74111d32011-01-15 16:57:55 +0000982 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
983 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
984 {
985 /* Parse netscape certificate type */
986 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
987 &crt->ns_cert_type ) ) != 0 )
988 return ( ret );
989 crt->ext_types |= EXT_NS_CERT_TYPE;
990 }
991 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
992 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
993 {
994 /* Parse key usage */
995 if( ( ret = x509_get_key_usage( p, end_ext_octet,
996 &crt->key_usage ) ) != 0 )
997 return ( ret );
998 crt->ext_types |= EXT_KEY_USAGE;
999 }
1000 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
1001 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
1002 {
1003 /* Parse extended key usage */
1004 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1005 &crt->ext_key_usage ) ) != 0 )
1006 return ( ret );
1007 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1008 }
Paul Bakkera8cd2392012-02-11 16:09:32 +00001009 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == extn_oid.len ) &&
1010 memcmp( extn_oid.p, OID_SUBJECT_ALT_NAME, extn_oid.len ) == 0 )
1011 {
1012 /* Parse extended key usage */
1013 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1014 &crt->subject_alt_names ) ) != 0 )
1015 return ( ret );
1016 crt->ext_types |= EXT_SUBJECT_ALT_NAME;
1017 }
Paul Bakker74111d32011-01-15 16:57:55 +00001018 else
1019 {
1020 /* No parser found, skip extension */
1021 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Paul Bakker5c721f92011-07-27 16:51:09 +00001023#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001024 if( is_critical )
1025 {
1026 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001027 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001028 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1029 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001030#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001031 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 }
1033
1034 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001035 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001036 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 return( 0 );
1039}
1040
1041/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001042 * X.509 CRL Entries
1043 */
1044static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001045 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001046 x509_crl_entry *entry )
1047{
Paul Bakker23986e52011-04-24 08:57:21 +00001048 int ret;
1049 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001050 x509_crl_entry *cur_entry = entry;
1051
1052 if( *p == end )
1053 return( 0 );
1054
Paul Bakker9be19372009-07-27 20:21:53 +00001055 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001056 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1057 {
1058 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1059 return( 0 );
1060
1061 return( ret );
1062 }
1063
Paul Bakker9be19372009-07-27 20:21:53 +00001064 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001065
1066 while( *p < end )
1067 {
Paul Bakker23986e52011-04-24 08:57:21 +00001068 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001069 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001070
1071 if( ( ret = asn1_get_tag( p, end, &len2,
1072 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1073 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001074 return( ret );
1075 }
1076
Paul Bakker9be19372009-07-27 20:21:53 +00001077 cur_entry->raw.tag = **p;
1078 cur_entry->raw.p = *p;
1079 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001080 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001081
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001082 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001083 return( ret );
1084
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001085 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001086 return( ret );
1087
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001088 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001089 return( ret );
1090
Paul Bakker74111d32011-01-15 16:57:55 +00001091 if ( *p < end )
1092 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001093 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001094
1095 if( cur_entry->next == NULL )
1096 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1097
Paul Bakkerd98030e2009-05-02 15:13:40 +00001098 cur_entry = cur_entry->next;
1099 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1100 }
1101 }
1102
1103 return( 0 );
1104}
1105
Paul Bakker27d66162010-03-17 06:56:01 +00001106static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1107{
1108 if( sig_oid->len == 9 &&
1109 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1110 {
1111 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1112 {
1113 *sig_alg = sig_oid->p[8];
1114 return( 0 );
1115 }
1116
1117 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1118 {
1119 *sig_alg = sig_oid->p[8];
1120 return( 0 );
1121 }
1122
1123 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1124 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001125 if( sig_oid->len == 5 &&
1126 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1127 {
1128 *sig_alg = SIG_RSA_SHA1;
1129 return( 0 );
1130 }
Paul Bakker27d66162010-03-17 06:56:01 +00001131
1132 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1133}
1134
Paul Bakkerd98030e2009-05-02 15:13:40 +00001135/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001136 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001138int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1139 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001140{
Paul Bakker23986e52011-04-24 08:57:21 +00001141 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001142 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001143 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Paul Bakker320a4b52009-03-28 18:52:39 +00001145 /*
1146 * Check for valid input
1147 */
1148 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001149 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001150
Paul Bakker96743fc2011-02-12 14:30:57 +00001151 p = (unsigned char *) malloc( len = buflen );
1152
1153 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001154 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001155
1156 memcpy( p, buf, buflen );
1157
1158 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
1160 crt->raw.p = p;
1161 crt->raw.len = len;
1162 end = p + len;
1163
1164 /*
1165 * Certificate ::= SEQUENCE {
1166 * tbsCertificate TBSCertificate,
1167 * signatureAlgorithm AlgorithmIdentifier,
1168 * signatureValue BIT STRING }
1169 */
1170 if( ( ret = asn1_get_tag( &p, end, &len,
1171 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1172 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001173 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001174 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 }
1176
Paul Bakkerb00ca422012-09-25 12:10:00 +00001177 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001179 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001180 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001181 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001183 crt_end = p + len;
Paul Bakkerd6d41092013-06-13 09:00:25 +02001184
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 /*
1186 * TBSCertificate ::= SEQUENCE {
1187 */
1188 crt->tbs.p = p;
1189
1190 if( ( ret = asn1_get_tag( &p, end, &len,
1191 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1192 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001193 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001194 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 }
1196
1197 end = p + len;
1198 crt->tbs.len = end - crt->tbs.p;
1199
1200 /*
1201 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1202 *
1203 * CertificateSerialNumber ::= INTEGER
1204 *
1205 * signature AlgorithmIdentifier
1206 */
1207 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1208 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1209 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1210 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001211 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 return( ret );
1213 }
1214
1215 crt->version++;
1216
1217 if( crt->version > 3 )
1218 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001219 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001220 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 }
1222
Paul Bakker27d66162010-03-17 06:56:01 +00001223 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001225 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001226 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 }
1228
1229 /*
1230 * issuer Name
1231 */
1232 crt->issuer_raw.p = p;
1233
1234 if( ( ret = asn1_get_tag( &p, end, &len,
1235 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1236 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001237 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001238 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 }
1240
1241 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1242 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001243 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 return( ret );
1245 }
1246
1247 crt->issuer_raw.len = p - crt->issuer_raw.p;
1248
1249 /*
1250 * Validity ::= SEQUENCE {
1251 * notBefore Time,
1252 * notAfter Time }
1253 *
1254 */
1255 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1256 &crt->valid_to ) ) != 0 )
1257 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001258 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 return( ret );
1260 }
1261
1262 /*
1263 * subject Name
1264 */
1265 crt->subject_raw.p = p;
1266
1267 if( ( ret = asn1_get_tag( &p, end, &len,
1268 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1269 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001270 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001271 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 }
1273
Paul Bakkercefb3962012-06-27 11:51:09 +00001274 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001276 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 return( ret );
1278 }
1279
1280 crt->subject_raw.len = p - crt->subject_raw.p;
1281
1282 /*
1283 * SubjectPublicKeyInfo ::= SEQUENCE
1284 * algorithm AlgorithmIdentifier,
1285 * subjectPublicKey BIT STRING }
1286 */
1287 if( ( ret = asn1_get_tag( &p, end, &len,
1288 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1289 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001290 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001291 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 }
1293
1294 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1295 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 return( ret );
1299 }
1300
1301 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1302 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001303 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 return( ret );
1305 }
1306
1307 crt->rsa.len = mpi_size( &crt->rsa.N );
1308
1309 /*
1310 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1311 * -- If present, version shall be v2 or v3
1312 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1313 * -- If present, version shall be v2 or v3
1314 * extensions [3] EXPLICIT Extensions OPTIONAL
1315 * -- If present, version shall be v3
1316 */
1317 if( crt->version == 2 || crt->version == 3 )
1318 {
1319 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1320 if( ret != 0 )
1321 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001322 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 return( ret );
1324 }
1325 }
1326
1327 if( crt->version == 2 || crt->version == 3 )
1328 {
1329 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1330 if( ret != 0 )
1331 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001332 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 return( ret );
1334 }
1335 }
1336
1337 if( crt->version == 3 )
1338 {
Paul Bakker74111d32011-01-15 16:57:55 +00001339 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 if( ret != 0 )
1341 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001342 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 return( ret );
1344 }
1345 }
1346
1347 if( p != end )
1348 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001349 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001350 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001351 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 }
1353
Paul Bakkerb00ca422012-09-25 12:10:00 +00001354 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001355
1356 /*
1357 * signatureAlgorithm AlgorithmIdentifier,
1358 * signatureValue BIT STRING
1359 */
1360 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1361 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001362 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 return( ret );
1364 }
1365
Paul Bakker535e97d2012-08-23 10:49:55 +00001366 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1367 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001369 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001370 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 }
1372
1373 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1374 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001375 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 return( ret );
1377 }
1378
1379 if( p != end )
1380 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001381 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001382 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001383 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384 }
1385
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001386 return( 0 );
1387}
1388
1389/*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001390 * Parse one X.509 certificate in DER format from a buffer and add them to a
1391 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001392 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001393int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001394{
Paul Bakkerd6d41092013-06-13 09:00:25 +02001395 int ret;
1396 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001397
1398 /*
1399 * Check for valid input
1400 */
1401 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001402 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001403
1404 while( crt->version != 0 && crt->next != NULL )
1405 {
1406 prev = crt;
1407 crt = crt->next;
1408 }
1409
1410 /*
1411 * Add new certificate on the end of the chain if needed.
1412 */
1413 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001414 {
1415 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1416
Paul Bakker7d06ad22009-05-02 15:53:56 +00001417 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001418 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001419
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001420 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001421 crt = crt->next;
1422 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Paul Bakkerd6d41092013-06-13 09:00:25 +02001425 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1426 {
1427 if( prev )
1428 prev->next = NULL;
1429
1430 if( crt != chain )
1431 free( crt );
1432
1433 return( ret );
1434 }
1435
1436 return( 0 );
1437}
1438
1439/*
1440 * Parse one or more PEM certificates from a buffer and add them to the chained list
1441 */
1442int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1443{
1444 int ret, success = 0, first_error = 0, total_failed = 0;
1445 int buf_format = X509_FORMAT_DER;
1446
1447 /*
1448 * Check for valid input
1449 */
1450 if( chain == NULL || buf == NULL )
1451 return( POLARSSL_ERR_X509_INVALID_INPUT );
1452
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001453 /*
1454 * Determine buffer content. Buffer contains either one DER certificate or
1455 * one or more PEM certificates.
1456 */
1457#if defined(POLARSSL_PEM_C)
Paul Bakkereae09db2013-06-06 12:35:54 +02001458 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001459 buf_format = X509_FORMAT_PEM;
1460#endif
1461
1462 if( buf_format == X509_FORMAT_DER )
Paul Bakkerd6d41092013-06-13 09:00:25 +02001463 return x509parse_crt_der( chain, buf, buflen );
1464
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001465#if defined(POLARSSL_PEM_C)
1466 if( buf_format == X509_FORMAT_PEM )
1467 {
1468 pem_context pem;
1469
1470 while( buflen > 0 )
1471 {
1472 size_t use_len;
1473 pem_init( &pem );
1474
1475 ret = pem_read_buffer( &pem,
1476 "-----BEGIN CERTIFICATE-----",
1477 "-----END CERTIFICATE-----",
1478 buf, NULL, 0, &use_len );
1479
1480 if( ret == 0 )
1481 {
1482 /*
1483 * Was PEM encoded
1484 */
1485 buflen -= use_len;
1486 buf += use_len;
1487 }
Paul Bakker64171862013-06-06 15:01:18 +02001488 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1489 {
1490 return( ret );
1491 }
Paul Bakker9255e832013-06-06 14:58:28 +02001492 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001493 {
1494 pem_free( &pem );
1495
Paul Bakker64171862013-06-06 15:01:18 +02001496 /*
1497 * PEM header and footer were found
1498 */
1499 buflen -= use_len;
1500 buf += use_len;
1501
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001502 if( first_error == 0 )
1503 first_error = ret;
1504
1505 continue;
1506 }
1507 else
1508 break;
1509
Paul Bakkerd6d41092013-06-13 09:00:25 +02001510 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001511
1512 pem_free( &pem );
1513
1514 if( ret != 0 )
1515 {
1516 /*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001517 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001518 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001519 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001520 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001521
1522 if( first_error == 0 )
1523 first_error = ret;
1524
Paul Bakkerd6d41092013-06-13 09:00:25 +02001525 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001526 continue;
1527 }
1528
1529 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001530 }
1531 }
1532#endif
1533
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001534 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001535 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001536 else if( first_error )
1537 return( first_error );
1538 else
1539 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001540}
1541
1542/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001543 * Parse one or more CRLs and add them to the chained list
1544 */
Paul Bakker23986e52011-04-24 08:57:21 +00001545int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001546{
Paul Bakker23986e52011-04-24 08:57:21 +00001547 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001548 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001549 unsigned char *p, *end;
1550 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001551#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001552 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001553 pem_context pem;
1554#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001555
1556 crl = chain;
1557
1558 /*
1559 * Check for valid input
1560 */
1561 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001562 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001563
1564 while( crl->version != 0 && crl->next != NULL )
1565 crl = crl->next;
1566
1567 /*
1568 * Add new CRL on the end of the chain if needed.
1569 */
1570 if ( crl->version != 0 && crl->next == NULL)
1571 {
1572 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1573
Paul Bakker7d06ad22009-05-02 15:53:56 +00001574 if( crl->next == NULL )
1575 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001576 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001577 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001578 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001579
Paul Bakker7d06ad22009-05-02 15:53:56 +00001580 crl = crl->next;
1581 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001582 }
1583
Paul Bakker96743fc2011-02-12 14:30:57 +00001584#if defined(POLARSSL_PEM_C)
1585 pem_init( &pem );
1586 ret = pem_read_buffer( &pem,
1587 "-----BEGIN X509 CRL-----",
1588 "-----END X509 CRL-----",
1589 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001590
Paul Bakker96743fc2011-02-12 14:30:57 +00001591 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001592 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001593 /*
1594 * Was PEM encoded
1595 */
1596 buflen -= use_len;
1597 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001598
1599 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001600 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001601 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001602 p = pem.buf;
1603 pem.buf = NULL;
1604 len = pem.buflen;
1605 pem_free( &pem );
1606 }
Paul Bakker9255e832013-06-06 14:58:28 +02001607 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001608 {
1609 pem_free( &pem );
1610 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001611 }
1612 else
1613 {
1614 /*
1615 * nope, copy the raw DER data
1616 */
1617 p = (unsigned char *) malloc( len = buflen );
1618
1619 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001620 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001621
1622 memcpy( p, buf, buflen );
1623
1624 buflen = 0;
1625 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001626#else
1627 p = (unsigned char *) malloc( len = buflen );
1628
1629 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001630 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001631
1632 memcpy( p, buf, buflen );
1633
1634 buflen = 0;
1635#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001636
1637 crl->raw.p = p;
1638 crl->raw.len = len;
1639 end = p + len;
1640
1641 /*
1642 * CertificateList ::= SEQUENCE {
1643 * tbsCertList TBSCertList,
1644 * signatureAlgorithm AlgorithmIdentifier,
1645 * signatureValue BIT STRING }
1646 */
1647 if( ( ret = asn1_get_tag( &p, end, &len,
1648 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1649 {
1650 x509_crl_free( crl );
1651 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1652 }
1653
Paul Bakker23986e52011-04-24 08:57:21 +00001654 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001655 {
1656 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001657 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001658 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1659 }
1660
1661 /*
1662 * TBSCertList ::= SEQUENCE {
1663 */
1664 crl->tbs.p = p;
1665
1666 if( ( ret = asn1_get_tag( &p, end, &len,
1667 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1668 {
1669 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001670 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001671 }
1672
1673 end = p + len;
1674 crl->tbs.len = end - crl->tbs.p;
1675
1676 /*
1677 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1678 * -- if present, MUST be v2
1679 *
1680 * signature AlgorithmIdentifier
1681 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001682 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001683 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1684 {
1685 x509_crl_free( crl );
1686 return( ret );
1687 }
1688
1689 crl->version++;
1690
1691 if( crl->version > 2 )
1692 {
1693 x509_crl_free( crl );
1694 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1695 }
1696
Paul Bakker27d66162010-03-17 06:56:01 +00001697 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001698 {
1699 x509_crl_free( crl );
1700 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1701 }
1702
1703 /*
1704 * issuer Name
1705 */
1706 crl->issuer_raw.p = p;
1707
1708 if( ( ret = asn1_get_tag( &p, end, &len,
1709 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1710 {
1711 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001712 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001713 }
1714
1715 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1716 {
1717 x509_crl_free( crl );
1718 return( ret );
1719 }
1720
1721 crl->issuer_raw.len = p - crl->issuer_raw.p;
1722
1723 /*
1724 * thisUpdate Time
1725 * nextUpdate Time OPTIONAL
1726 */
Paul Bakker91200182010-02-18 21:26:15 +00001727 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001728 {
1729 x509_crl_free( crl );
1730 return( ret );
1731 }
1732
Paul Bakker91200182010-02-18 21:26:15 +00001733 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001734 {
Paul Bakker9d781402011-05-09 16:17:09 +00001735 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001736 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001737 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001738 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001739 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001740 x509_crl_free( crl );
1741 return( ret );
1742 }
1743 }
1744
1745 /*
1746 * revokedCertificates SEQUENCE OF SEQUENCE {
1747 * userCertificate CertificateSerialNumber,
1748 * revocationDate Time,
1749 * crlEntryExtensions Extensions OPTIONAL
1750 * -- if present, MUST be v2
1751 * } OPTIONAL
1752 */
1753 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1754 {
1755 x509_crl_free( crl );
1756 return( ret );
1757 }
1758
1759 /*
1760 * crlExtensions EXPLICIT Extensions OPTIONAL
1761 * -- if present, MUST be v2
1762 */
1763 if( crl->version == 2 )
1764 {
1765 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1766
1767 if( ret != 0 )
1768 {
1769 x509_crl_free( crl );
1770 return( ret );
1771 }
1772 }
1773
1774 if( p != end )
1775 {
1776 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001777 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001778 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1779 }
1780
1781 end = crl->raw.p + crl->raw.len;
1782
1783 /*
1784 * signatureAlgorithm AlgorithmIdentifier,
1785 * signatureValue BIT STRING
1786 */
1787 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1788 {
1789 x509_crl_free( crl );
1790 return( ret );
1791 }
1792
Paul Bakker535e97d2012-08-23 10:49:55 +00001793 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1794 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001795 {
1796 x509_crl_free( crl );
1797 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1798 }
1799
1800 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1801 {
1802 x509_crl_free( crl );
1803 return( ret );
1804 }
1805
1806 if( p != end )
1807 {
1808 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001809 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1811 }
1812
1813 if( buflen > 0 )
1814 {
1815 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1816
Paul Bakker7d06ad22009-05-02 15:53:56 +00001817 if( crl->next == NULL )
1818 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001819 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001820 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001821 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001822
Paul Bakker7d06ad22009-05-02 15:53:56 +00001823 crl = crl->next;
1824 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001825
1826 return( x509parse_crl( crl, buf, buflen ) );
1827 }
1828
1829 return( 0 );
1830}
1831
Paul Bakker335db3f2011-04-25 15:28:35 +00001832#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001833/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001834 * Load all data from a file into a given buffer.
1835 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001836int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001837{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001838 FILE *f;
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001839 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001840
Paul Bakkerd98030e2009-05-02 15:13:40 +00001841 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001842 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001843
Paul Bakkerd98030e2009-05-02 15:13:40 +00001844 fseek( f, 0, SEEK_END );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001845 if( ( size = ftell( f ) ) == -1 )
1846 {
1847 fclose( f );
1848 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1849 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001851
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001852 *n = (size_t) size;
1853
1854 if( *n + 1 == 0 ||
1855 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1856 {
1857 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001858 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001859 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001860
Paul Bakkerd98030e2009-05-02 15:13:40 +00001861 if( fread( *buf, 1, *n, f ) != *n )
1862 {
1863 fclose( f );
1864 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001865 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001866 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001867
Paul Bakkerd98030e2009-05-02 15:13:40 +00001868 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001869
Paul Bakkerd98030e2009-05-02 15:13:40 +00001870 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001871
Paul Bakkerd98030e2009-05-02 15:13:40 +00001872 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001873}
1874
1875/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 * Load one or more certificates and add them to the chained list
1877 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001878int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001879{
1880 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 size_t n;
1882 unsigned char *buf;
1883
Paul Bakker69e095c2011-12-10 21:55:01 +00001884 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1885 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
Paul Bakker69e095c2011-12-10 21:55:01 +00001887 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001888
1889 memset( buf, 0, n + 1 );
1890 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
1892 return( ret );
1893}
1894
Paul Bakker8d914582012-06-04 12:46:42 +00001895int x509parse_crtpath( x509_cert *chain, const char *path )
1896{
1897 int ret = 0;
1898#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001899 int w_ret;
1900 WCHAR szDir[MAX_PATH];
1901 char filename[MAX_PATH];
1902 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001903 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001904
Paul Bakker97872ac2012-11-02 12:53:26 +00001905 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001906 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001907
1908 if( len > MAX_PATH - 3 )
1909 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001910
Paul Bakker3338b792012-10-01 21:13:10 +00001911 memset( szDir, 0, sizeof(szDir) );
1912 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001913 memcpy( filename, path, len );
1914 filename[len++] = '\\';
1915 p = filename + len;
1916 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001917
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001918 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001919
Paul Bakker97872ac2012-11-02 12:53:26 +00001920 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001921 if (hFind == INVALID_HANDLE_VALUE)
1922 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1923
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001924 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001925 do
1926 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001927 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001928
Paul Bakkere4791f32012-06-04 21:29:15 +00001929 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001930 continue;
1931
Paul Bakker3338b792012-10-01 21:13:10 +00001932 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1933 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001934 p, len - 1,
1935 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001936
Paul Bakker3338b792012-10-01 21:13:10 +00001937 w_ret = x509parse_crtfile( chain, filename );
1938 if( w_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001939 ret++;
1940 else
1941 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001942 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001943 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001944
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001945 if (GetLastError() != ERROR_NO_MORE_FILES)
1946 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001947
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001948cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001949 FindClose( hFind );
1950#else
Paul Bakker9ccb2112014-07-07 13:43:31 +02001951#if defined(POLARSSL_HAVE_READDIR_R)
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001952 int t_ret, i;
1953 struct stat sb;
1954 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001955 char entry_name[255];
1956 DIR *dir = opendir( path );
1957
1958 if( dir == NULL)
1959 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1960
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001961 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001962 {
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001963 if( result == NULL )
1964 break;
1965
1966 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1967
1968 i = stat( entry_name, &sb );
1969
1970 if( i == -1 )
Paul Bakker88a22642013-09-11 12:14:16 +02001971 {
1972 closedir( dir );
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001973 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker88a22642013-09-11 12:14:16 +02001974 }
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001975
1976 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00001977 continue;
1978
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001979 // Ignore parse errors
1980 //
Paul Bakker8d914582012-06-04 12:46:42 +00001981 t_ret = x509parse_crtfile( chain, entry_name );
1982 if( t_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001983 ret++;
1984 else
1985 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001986 }
1987 closedir( dir );
Paul Bakker9ccb2112014-07-07 13:43:31 +02001988#else /* POLARSSL_HAVE_READDIR_R */
1989 ((void) chain);
1990 ((void) path);
1991 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
1992#endif /* POLARSSL_HAVE_READDIR_R */
1993#endif /* _WIN32 */
Paul Bakker8d914582012-06-04 12:46:42 +00001994
1995 return( ret );
1996}
1997
Paul Bakkerd98030e2009-05-02 15:13:40 +00001998/*
1999 * Load one or more CRLs and add them to the chained list
2000 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002001int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002002{
2003 int ret;
2004 size_t n;
2005 unsigned char *buf;
2006
Paul Bakker69e095c2011-12-10 21:55:01 +00002007 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2008 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002009
Paul Bakker27fdf462011-06-09 13:55:13 +00002010 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002011
2012 memset( buf, 0, n + 1 );
2013 free( buf );
2014
2015 return( ret );
2016}
2017
Paul Bakker5121ce52009-01-03 21:22:43 +00002018/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002019 * Load and parse a private RSA key
2020 */
2021int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
2022{
2023 int ret;
2024 size_t n;
2025 unsigned char *buf;
2026
Paul Bakker69e095c2011-12-10 21:55:01 +00002027 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2028 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002029
2030 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002031 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002032 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002033 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002034 (unsigned char *) pwd, strlen( pwd ) );
2035
2036 memset( buf, 0, n + 1 );
2037 free( buf );
2038
2039 return( ret );
2040}
2041
2042/*
2043 * Load and parse a public RSA key
2044 */
2045int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2046{
2047 int ret;
2048 size_t n;
2049 unsigned char *buf;
2050
Paul Bakker69e095c2011-12-10 21:55:01 +00002051 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2052 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002053
Paul Bakker27fdf462011-06-09 13:55:13 +00002054 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002055
2056 memset( buf, 0, n + 1 );
2057 free( buf );
2058
2059 return( ret );
2060}
2061#endif /* POLARSSL_FS_IO */
2062
2063/*
Paul Bakker65a19092013-06-06 21:14:58 +02002064 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 */
Paul Bakker65a19092013-06-06 21:14:58 +02002066static int x509parse_key_pkcs1_der( rsa_context *rsa,
2067 const unsigned char *key,
2068 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002069{
Paul Bakker23986e52011-04-24 08:57:21 +00002070 int ret;
2071 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002073
Paul Bakker96743fc2011-02-12 14:30:57 +00002074 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002075 end = p + keylen;
2076
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 /*
Paul Bakker65a19092013-06-06 21:14:58 +02002078 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002079 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 * RSAPrivateKey ::= SEQUENCE {
2081 * version Version,
2082 * modulus INTEGER, -- n
2083 * publicExponent INTEGER, -- e
2084 * privateExponent INTEGER, -- d
2085 * prime1 INTEGER, -- p
2086 * prime2 INTEGER, -- q
2087 * exponent1 INTEGER, -- d mod (p-1)
2088 * exponent2 INTEGER, -- d mod (q-1)
2089 * coefficient INTEGER, -- (inverse of q) mod p
2090 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2091 * }
2092 */
2093 if( ( ret = asn1_get_tag( &p, end, &len,
2094 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2095 {
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 end = p + len;
2100
2101 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2102 {
Paul Bakker9d781402011-05-09 16:17:09 +00002103 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
2105
2106 if( rsa->ver != 0 )
2107 {
Paul Bakker9d781402011-05-09 16:17:09 +00002108 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 }
2110
2111 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2112 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2113 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2114 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2115 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2116 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2117 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2118 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2119 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002121 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
2123
2124 rsa->len = mpi_size( &rsa->N );
2125
2126 if( p != end )
2127 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002129 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002130 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 }
2132
2133 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2134 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 rsa_free( rsa );
2136 return( ret );
2137 }
2138
Paul Bakker65a19092013-06-06 21:14:58 +02002139 return( 0 );
2140}
2141
2142/*
2143 * Parse an unencrypted PKCS#8 encoded private RSA key
2144 */
2145static int x509parse_key_pkcs8_unencrypted_der(
2146 rsa_context *rsa,
2147 const unsigned char *key,
2148 size_t keylen )
2149{
2150 int ret;
2151 size_t len;
2152 unsigned char *p, *end;
2153 x509_buf pk_alg_oid;
2154
2155 p = (unsigned char *) key;
2156 end = p + keylen;
2157
2158 /*
2159 * This function parses the PrivatKeyInfo object (PKCS#8)
2160 *
2161 * PrivateKeyInfo ::= SEQUENCE {
2162 * version Version,
2163 * algorithm AlgorithmIdentifier,
2164 * PrivateKey BIT STRING
2165 * }
2166 *
2167 * AlgorithmIdentifier ::= SEQUENCE {
2168 * algorithm OBJECT IDENTIFIER,
2169 * parameters ANY DEFINED BY algorithm OPTIONAL
2170 * }
2171 *
2172 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2173 */
2174 if( ( ret = asn1_get_tag( &p, end, &len,
2175 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2176 {
2177 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2178 }
2179
2180 end = p + len;
2181
2182 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2183 {
2184 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2185 }
2186
2187 if( rsa->ver != 0 )
2188 {
2189 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2190 }
2191
2192 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2193 {
2194 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2195 }
2196
2197 /*
2198 * only RSA keys handled at this time
2199 */
2200 if( pk_alg_oid.len != 9 ||
2201 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) != 0 )
2202 {
2203 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2204 }
2205
2206 /*
2207 * Get the OCTET STRING and parse the PKCS#1 format inside
2208 */
2209 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2210 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2211
2212 if( ( end - p ) < 1 )
2213 {
2214 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2215 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2216 }
2217
2218 end = p + len;
2219
2220 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2221 return( ret );
2222
2223 return( 0 );
2224}
2225
2226/*
Paul Bakkerda7fdbd2013-06-19 11:15:43 +02002227 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002228 */
2229static int x509parse_key_pkcs8_encrypted_der(
2230 rsa_context *rsa,
2231 const unsigned char *key,
2232 size_t keylen,
2233 const unsigned char *pwd,
2234 size_t pwdlen )
2235{
2236 int ret;
2237 size_t len;
2238 unsigned char *p, *end, *end2;
2239 x509_buf pbe_alg_oid, pbe_params;
2240 unsigned char buf[2048];
2241
2242 memset(buf, 0, 2048);
2243
2244 p = (unsigned char *) key;
2245 end = p + keylen;
2246
Paul Bakker1fd43212013-06-17 15:14:42 +02002247 if( pwdlen == 0 )
2248 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2249
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002250 /*
2251 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2252 *
2253 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2254 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2255 * encryptedData EncryptedData
2256 * }
2257 *
2258 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2259 *
2260 * EncryptedData ::= OCTET STRING
2261 *
2262 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2263 */
2264 if( ( ret = asn1_get_tag( &p, end, &len,
2265 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2266 {
2267 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2268 }
2269
2270 end = p + len;
2271
2272 if( ( ret = asn1_get_tag( &p, end, &len,
2273 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2274 {
2275 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2276 }
2277
2278 end2 = p + len;
2279
2280 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2281 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2282
2283 pbe_alg_oid.p = p;
2284 p += pbe_alg_oid.len;
2285
2286 /*
2287 * Store the algorithm parameters
2288 */
2289 pbe_params.p = p;
2290 pbe_params.len = end2 - p;
2291 p += pbe_params.len;
2292
2293 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2294 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2295
2296 // buf has been sized to 2048 bytes
2297 if( len > 2048 )
2298 return( POLARSSL_ERR_X509_INVALID_INPUT );
2299
2300 /*
2301 * Decrypt EncryptedData with appropriate PDE
2302 */
Paul Bakker14a222c2013-06-18 16:35:48 +02002303#if defined(POLARSSL_PKCS12_C)
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002304 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2305 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002306 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2307 POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
2308 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002309 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002310 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2311 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2312
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002313 return( ret );
2314 }
2315 }
2316 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2317 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002318 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2319 POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
2320 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002321 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002322 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2323 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2324
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002325 return( ret );
2326 }
2327 }
2328 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2329 {
2330 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2331 PKCS12_PBE_DECRYPT,
2332 pwd, pwdlen,
2333 p, len, buf ) ) != 0 )
2334 {
2335 return( ret );
2336 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002337
2338 // Best guess for password mismatch when using RC4. If first tag is
2339 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2340 //
2341 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2342 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002343 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002344 else
2345#endif /* POLARSSL_PKCS12_C */
Paul Bakker1fd43212013-06-17 15:14:42 +02002346#if defined(POLARSSL_PKCS5_C)
Paul Bakker14a222c2013-06-18 16:35:48 +02002347 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker1fd43212013-06-17 15:14:42 +02002348 {
2349 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2350 p, len, buf ) ) != 0 )
2351 {
2352 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2353 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2354
2355 return( ret );
2356 }
2357 }
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002358 else
Paul Bakker14a222c2013-06-18 16:35:48 +02002359#endif /* POLARSSL_PKCS5_C */
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002360 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2361
2362 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2363}
2364
2365/*
Paul Bakker65a19092013-06-06 21:14:58 +02002366 * Parse a private RSA key
2367 */
2368int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2369 const unsigned char *pwd, size_t pwdlen )
2370{
2371 int ret;
2372
Paul Bakker96743fc2011-02-12 14:30:57 +00002373#if defined(POLARSSL_PEM_C)
Paul Bakker65a19092013-06-06 21:14:58 +02002374 size_t len;
2375 pem_context pem;
2376
2377 pem_init( &pem );
2378 ret = pem_read_buffer( &pem,
2379 "-----BEGIN RSA PRIVATE KEY-----",
2380 "-----END RSA PRIVATE KEY-----",
2381 key, pwd, pwdlen, &len );
2382 if( ret == 0 )
2383 {
2384 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2385 {
2386 rsa_free( rsa );
2387 }
2388
2389 pem_free( &pem );
2390 return( ret );
2391 }
Paul Bakkerb495d3a2013-06-17 15:58:04 +02002392 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2393 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2394 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2395 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakker65a19092013-06-06 21:14:58 +02002396 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002397 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002398
2399 ret = pem_read_buffer( &pem,
2400 "-----BEGIN PRIVATE KEY-----",
2401 "-----END PRIVATE KEY-----",
2402 key, NULL, 0, &len );
2403 if( ret == 0 )
2404 {
2405 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2406 pem.buf, pem.buflen ) ) != 0 )
2407 {
2408 rsa_free( rsa );
2409 }
2410
2411 pem_free( &pem );
2412 return( ret );
2413 }
2414 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002415 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002416
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002417 ret = pem_read_buffer( &pem,
2418 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2419 "-----END ENCRYPTED PRIVATE KEY-----",
2420 key, NULL, 0, &len );
2421 if( ret == 0 )
2422 {
2423 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2424 pem.buf, pem.buflen,
2425 pwd, pwdlen ) ) != 0 )
2426 {
2427 rsa_free( rsa );
2428 }
2429
2430 pem_free( &pem );
2431 return( ret );
2432 }
2433 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002434 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002435#else
2436 ((void) pwd);
2437 ((void) pwdlen);
2438#endif /* POLARSSL_PEM_C */
2439
2440 // At this point we only know it's not a PEM formatted key. Could be any
2441 // of the known DER encoded private key formats
2442 //
2443 // We try the different DER format parsers to see if one passes without
2444 // error
2445 //
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002446 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2447 pwd, pwdlen ) ) == 0 )
Paul Bakker65a19092013-06-06 21:14:58 +02002448 {
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002449 return( 0 );
Paul Bakker65a19092013-06-06 21:14:58 +02002450 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002452 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002453
2454 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2455 {
2456 return( ret );
2457 }
2458
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002459 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2460 return( 0 );
2461
2462 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002463
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002464 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2465 return( 0 );
2466
2467 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002468
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002469 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470}
2471
2472/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002473 * Parse a public RSA key
2474 */
Paul Bakker23986e52011-04-24 08:57:21 +00002475int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002476{
Paul Bakker23986e52011-04-24 08:57:21 +00002477 int ret;
2478 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002479 unsigned char *p, *end;
2480 x509_buf alg_oid;
2481#if defined(POLARSSL_PEM_C)
2482 pem_context pem;
2483
2484 pem_init( &pem );
2485 ret = pem_read_buffer( &pem,
2486 "-----BEGIN PUBLIC KEY-----",
2487 "-----END PUBLIC KEY-----",
2488 key, NULL, 0, &len );
2489
2490 if( ret == 0 )
2491 {
2492 /*
2493 * Was PEM encoded
2494 */
2495 keylen = pem.buflen;
2496 }
Paul Bakker9255e832013-06-06 14:58:28 +02002497 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002498 {
2499 pem_free( &pem );
2500 return( ret );
2501 }
2502
2503 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2504#else
2505 p = (unsigned char *) key;
2506#endif
2507 end = p + keylen;
2508
2509 /*
2510 * PublicKeyInfo ::= SEQUENCE {
2511 * algorithm AlgorithmIdentifier,
2512 * PublicKey BIT STRING
2513 * }
2514 *
2515 * AlgorithmIdentifier ::= SEQUENCE {
2516 * algorithm OBJECT IDENTIFIER,
2517 * parameters ANY DEFINED BY algorithm OPTIONAL
2518 * }
2519 *
2520 * RSAPublicKey ::= SEQUENCE {
2521 * modulus INTEGER, -- n
2522 * publicExponent INTEGER -- e
2523 * }
2524 */
2525
2526 if( ( ret = asn1_get_tag( &p, end, &len,
2527 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2528 {
2529#if defined(POLARSSL_PEM_C)
2530 pem_free( &pem );
2531#endif
2532 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002533 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002534 }
2535
2536 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2537 {
2538#if defined(POLARSSL_PEM_C)
2539 pem_free( &pem );
2540#endif
2541 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002542 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002543 }
2544
2545 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2546 {
2547#if defined(POLARSSL_PEM_C)
2548 pem_free( &pem );
2549#endif
2550 rsa_free( rsa );
2551 return( ret );
2552 }
2553
2554 rsa->len = mpi_size( &rsa->N );
2555
2556#if defined(POLARSSL_PEM_C)
2557 pem_free( &pem );
2558#endif
2559
2560 return( 0 );
2561}
2562
Paul Bakkereaa89f82011-04-04 21:36:15 +00002563#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002564/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002565 * Parse DHM parameters
2566 */
Paul Bakker23986e52011-04-24 08:57:21 +00002567int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002568{
Paul Bakker23986e52011-04-24 08:57:21 +00002569 int ret;
2570 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002571 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002572#if defined(POLARSSL_PEM_C)
2573 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002574
Paul Bakker96743fc2011-02-12 14:30:57 +00002575 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002576
Paul Bakker96743fc2011-02-12 14:30:57 +00002577 ret = pem_read_buffer( &pem,
2578 "-----BEGIN DH PARAMETERS-----",
2579 "-----END DH PARAMETERS-----",
2580 dhmin, NULL, 0, &dhminlen );
2581
2582 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002583 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002584 /*
2585 * Was PEM encoded
2586 */
2587 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002588 }
Paul Bakker9255e832013-06-06 14:58:28 +02002589 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002590 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002591 pem_free( &pem );
2592 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002593 }
2594
Paul Bakker96743fc2011-02-12 14:30:57 +00002595 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2596#else
2597 p = (unsigned char *) dhmin;
2598#endif
2599 end = p + dhminlen;
2600
Paul Bakker1b57b062011-01-06 15:48:19 +00002601 memset( dhm, 0, sizeof( dhm_context ) );
2602
Paul Bakker1b57b062011-01-06 15:48:19 +00002603 /*
2604 * DHParams ::= SEQUENCE {
2605 * prime INTEGER, -- P
2606 * generator INTEGER, -- g
2607 * }
2608 */
2609 if( ( ret = asn1_get_tag( &p, end, &len,
2610 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2611 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002612#if defined(POLARSSL_PEM_C)
2613 pem_free( &pem );
2614#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002615 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002616 }
2617
2618 end = p + len;
2619
2620 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2621 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2622 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002623#if defined(POLARSSL_PEM_C)
2624 pem_free( &pem );
2625#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002626 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002627 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002628 }
2629
2630 if( p != end )
2631 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002632#if defined(POLARSSL_PEM_C)
2633 pem_free( &pem );
2634#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002635 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002636 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002637 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2638 }
2639
Paul Bakker96743fc2011-02-12 14:30:57 +00002640#if defined(POLARSSL_PEM_C)
2641 pem_free( &pem );
2642#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002643
2644 return( 0 );
2645}
2646
Paul Bakker335db3f2011-04-25 15:28:35 +00002647#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002648/*
2649 * Load and parse a private RSA key
2650 */
2651int x509parse_dhmfile( dhm_context *dhm, const char *path )
2652{
2653 int ret;
2654 size_t n;
2655 unsigned char *buf;
2656
Paul Bakker69e095c2011-12-10 21:55:01 +00002657 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2658 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002659
Paul Bakker27fdf462011-06-09 13:55:13 +00002660 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002661
2662 memset( buf, 0, n + 1 );
2663 free( buf );
2664
2665 return( ret );
2666}
Paul Bakker335db3f2011-04-25 15:28:35 +00002667#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002668#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002669
Paul Bakker5121ce52009-01-03 21:22:43 +00002670#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002671#include <stdarg.h>
2672
2673#if !defined vsnprintf
2674#define vsnprintf _vsnprintf
2675#endif // vsnprintf
2676
2677/*
2678 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2679 * Result value is not size of buffer needed, but -1 if no fit is possible.
2680 *
2681 * This fuction tries to 'fix' this by at least suggesting enlarging the
2682 * size by 20.
2683 */
2684int compat_snprintf(char *str, size_t size, const char *format, ...)
2685{
2686 va_list ap;
2687 int res = -1;
2688
2689 va_start( ap, format );
2690
2691 res = vsnprintf( str, size, format, ap );
2692
2693 va_end( ap );
2694
2695 // No quick fix possible
2696 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002697 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002698
2699 return res;
2700}
2701
2702#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002703#endif
2704
Paul Bakkerd98030e2009-05-02 15:13:40 +00002705#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2706
2707#define SAFE_SNPRINTF() \
2708{ \
2709 if( ret == -1 ) \
2710 return( -1 ); \
2711 \
Paul Bakker23986e52011-04-24 08:57:21 +00002712 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002713 p[n - 1] = '\0'; \
2714 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2715 } \
2716 \
Paul Bakker23986e52011-04-24 08:57:21 +00002717 n -= (unsigned int) ret; \
2718 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002719}
2720
Paul Bakker5121ce52009-01-03 21:22:43 +00002721/*
2722 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002723 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002725int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002726{
Paul Bakker23986e52011-04-24 08:57:21 +00002727 int ret;
2728 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002730 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 char s[128], *p;
2732
2733 memset( s, 0, sizeof( s ) );
2734
2735 name = dn;
2736 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002737 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
2739 while( name != NULL )
2740 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002741 if( !name->oid.p )
2742 {
2743 name = name->next;
2744 continue;
2745 }
2746
Paul Bakker74111d32011-01-15 16:57:55 +00002747 if( name != dn )
2748 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002749 ret = snprintf( p, n, ", " );
2750 SAFE_SNPRINTF();
2751 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Paul Bakker535e97d2012-08-23 10:49:55 +00002753 if( name->oid.len == 3 &&
2754 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 {
2756 switch( name->oid.p[2] )
2757 {
2758 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002759 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
2761 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002762 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
2764 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002765 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
2767 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002768 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
2770 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002771 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002772
2773 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002774 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
2776 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002777 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 name->oid.p[2] );
2779 break;
2780 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002781 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002783 else if( name->oid.len == 9 &&
2784 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 {
2786 switch( name->oid.p[8] )
2787 {
2788 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002789 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
2791 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002792 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 name->oid.p[8] );
2794 break;
2795 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002796 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 }
2798 else
Paul Bakker74111d32011-01-15 16:57:55 +00002799 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002800 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002801 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002802 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
2804 for( i = 0; i < name->val.len; i++ )
2805 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002806 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 break;
2808
2809 c = name->val.p[i];
2810 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2811 s[i] = '?';
2812 else s[i] = c;
2813 }
2814 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002815 ret = snprintf( p, n, "%s", s );
2816 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 name = name->next;
2818 }
2819
Paul Bakker23986e52011-04-24 08:57:21 +00002820 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002821}
2822
2823/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002824 * Store the serial in printable form into buf; no more
2825 * than size characters will be written
2826 */
2827int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2828{
Paul Bakker23986e52011-04-24 08:57:21 +00002829 int ret;
2830 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002831 char *p;
2832
2833 p = buf;
2834 n = size;
2835
2836 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002837 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002838
2839 for( i = 0; i < nr; i++ )
2840 {
Paul Bakker93048802011-12-05 14:38:06 +00002841 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002842 continue;
2843
Paul Bakkerdd476992011-01-16 21:34:59 +00002844 ret = snprintf( p, n, "%02X%s",
2845 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2846 SAFE_SNPRINTF();
2847 }
2848
Paul Bakker03c7c252011-11-25 12:37:37 +00002849 if( nr != serial->len )
2850 {
2851 ret = snprintf( p, n, "...." );
2852 SAFE_SNPRINTF();
2853 }
2854
Paul Bakker23986e52011-04-24 08:57:21 +00002855 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002856}
2857
2858/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002859 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002861int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2862 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002863{
Paul Bakker23986e52011-04-24 08:57:21 +00002864 int ret;
2865 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002866 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
2868 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002869 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakkerd98030e2009-05-02 15:13:40 +00002871 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002873 SAFE_SNPRINTF();
2874 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002875 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002876 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Paul Bakkerdd476992011-01-16 21:34:59 +00002878 ret = x509parse_serial_gets( p, n, &crt->serial);
2879 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Paul Bakkerd98030e2009-05-02 15:13:40 +00002881 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2882 SAFE_SNPRINTF();
2883 ret = x509parse_dn_gets( p, n, &crt->issuer );
2884 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Paul Bakkerd98030e2009-05-02 15:13:40 +00002886 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2887 SAFE_SNPRINTF();
2888 ret = x509parse_dn_gets( p, n, &crt->subject );
2889 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
Paul Bakkerd98030e2009-05-02 15:13:40 +00002891 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002892 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2893 crt->valid_from.year, crt->valid_from.mon,
2894 crt->valid_from.day, crt->valid_from.hour,
2895 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002896 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Paul Bakkerd98030e2009-05-02 15:13:40 +00002898 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2900 crt->valid_to.year, crt->valid_to.mon,
2901 crt->valid_to.day, crt->valid_to.hour,
2902 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002903 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Paul Bakkerd98030e2009-05-02 15:13:40 +00002905 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2906 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
Paul Bakker27d66162010-03-17 06:56:01 +00002908 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002909 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002910 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2911 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2912 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2913 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2914 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2915 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2916 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2917 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2918 default: ret = snprintf( p, n, "???" ); break;
2919 }
2920 SAFE_SNPRINTF();
2921
2922 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002923 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002924 SAFE_SNPRINTF();
2925
Paul Bakker23986e52011-04-24 08:57:21 +00002926 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002927}
2928
Paul Bakker74111d32011-01-15 16:57:55 +00002929/*
2930 * Return an informational string describing the given OID
2931 */
2932const char *x509_oid_get_description( x509_buf *oid )
2933{
2934 if ( oid == NULL )
2935 return ( NULL );
2936
2937 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2938 return( STRING_SERVER_AUTH );
2939
2940 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2941 return( STRING_CLIENT_AUTH );
2942
2943 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2944 return( STRING_CODE_SIGNING );
2945
2946 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2947 return( STRING_EMAIL_PROTECTION );
2948
2949 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2950 return( STRING_TIME_STAMPING );
2951
2952 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2953 return( STRING_OCSP_SIGNING );
2954
2955 return( NULL );
2956}
2957
2958/* Return the x.y.z.... style numeric string for the given OID */
2959int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2960{
Paul Bakker23986e52011-04-24 08:57:21 +00002961 int ret;
2962 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002963 unsigned int value;
2964 char *p;
2965
2966 p = buf;
2967 n = size;
2968
2969 /* First byte contains first two dots */
2970 if( oid->len > 0 )
2971 {
2972 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2973 SAFE_SNPRINTF();
2974 }
2975
2976 /* TODO: value can overflow in value. */
2977 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002978 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002979 {
2980 value <<= 7;
2981 value += oid->p[i] & 0x7F;
2982
2983 if( !( oid->p[i] & 0x80 ) )
2984 {
2985 /* Last byte */
2986 ret = snprintf( p, n, ".%d", value );
2987 SAFE_SNPRINTF();
2988 value = 0;
2989 }
2990 }
2991
Paul Bakker23986e52011-04-24 08:57:21 +00002992 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002993}
2994
Paul Bakkerd98030e2009-05-02 15:13:40 +00002995/*
2996 * Return an informational string about the CRL.
2997 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002998int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2999 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003000{
Paul Bakker23986e52011-04-24 08:57:21 +00003001 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003002 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003003 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003004 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003005
3006 p = buf;
3007 n = size;
3008
3009 ret = snprintf( p, n, "%sCRL version : %d",
3010 prefix, crl->version );
3011 SAFE_SNPRINTF();
3012
3013 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3014 SAFE_SNPRINTF();
3015 ret = x509parse_dn_gets( p, n, &crl->issuer );
3016 SAFE_SNPRINTF();
3017
3018 ret = snprintf( p, n, "\n%sthis update : " \
3019 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3020 crl->this_update.year, crl->this_update.mon,
3021 crl->this_update.day, crl->this_update.hour,
3022 crl->this_update.min, crl->this_update.sec );
3023 SAFE_SNPRINTF();
3024
3025 ret = snprintf( p, n, "\n%snext update : " \
3026 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3027 crl->next_update.year, crl->next_update.mon,
3028 crl->next_update.day, crl->next_update.hour,
3029 crl->next_update.min, crl->next_update.sec );
3030 SAFE_SNPRINTF();
3031
3032 entry = &crl->entry;
3033
3034 ret = snprintf( p, n, "\n%sRevoked certificates:",
3035 prefix );
3036 SAFE_SNPRINTF();
3037
Paul Bakker9be19372009-07-27 20:21:53 +00003038 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003039 {
3040 ret = snprintf( p, n, "\n%sserial number: ",
3041 prefix );
3042 SAFE_SNPRINTF();
3043
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003044 ret = x509parse_serial_gets( p, n, &entry->serial);
3045 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003046
Paul Bakkerd98030e2009-05-02 15:13:40 +00003047 ret = snprintf( p, n, " revocation date: " \
3048 "%04d-%02d-%02d %02d:%02d:%02d",
3049 entry->revocation_date.year, entry->revocation_date.mon,
3050 entry->revocation_date.day, entry->revocation_date.hour,
3051 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003052 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003053
3054 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003055 }
3056
Paul Bakkerd98030e2009-05-02 15:13:40 +00003057 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
3058 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Paul Bakker27d66162010-03-17 06:56:01 +00003060 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003061 {
3062 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
3063 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
3064 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
3065 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
3066 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
3067 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
3068 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
3069 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
3070 default: ret = snprintf( p, n, "???" ); break;
3071 }
3072 SAFE_SNPRINTF();
3073
Paul Bakker1e27bb22009-07-19 20:25:25 +00003074 ret = snprintf( p, n, "\n" );
3075 SAFE_SNPRINTF();
3076
Paul Bakker23986e52011-04-24 08:57:21 +00003077 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078}
3079
3080/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003081 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003083int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003084{
Paul Bakkercce9d772011-11-18 14:26:47 +00003085 int year, mon, day;
3086 int hour, min, sec;
3087
3088#if defined(_WIN32)
3089 SYSTEMTIME st;
3090
3091 GetLocalTime(&st);
3092
3093 year = st.wYear;
3094 mon = st.wMonth;
3095 day = st.wDay;
3096 hour = st.wHour;
3097 min = st.wMinute;
3098 sec = st.wSecond;
3099#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 struct tm *lt;
3101 time_t tt;
3102
3103 tt = time( NULL );
3104 lt = localtime( &tt );
3105
Paul Bakkercce9d772011-11-18 14:26:47 +00003106 year = lt->tm_year + 1900;
3107 mon = lt->tm_mon + 1;
3108 day = lt->tm_mday;
3109 hour = lt->tm_hour;
3110 min = lt->tm_min;
3111 sec = lt->tm_sec;
3112#endif
3113
3114 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003115 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003116
Paul Bakkercce9d772011-11-18 14:26:47 +00003117 if( year == to->year &&
3118 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003119 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003120
Paul Bakkercce9d772011-11-18 14:26:47 +00003121 if( year == to->year &&
3122 mon == to->mon &&
3123 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003124 return( 1 );
3125
Paul Bakkercce9d772011-11-18 14:26:47 +00003126 if( year == to->year &&
3127 mon == to->mon &&
3128 day == to->day &&
3129 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003130 return( 1 );
3131
Paul Bakkercce9d772011-11-18 14:26:47 +00003132 if( year == to->year &&
3133 mon == to->mon &&
3134 day == to->day &&
3135 hour == to->hour &&
3136 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003137 return( 1 );
3138
Paul Bakkercce9d772011-11-18 14:26:47 +00003139 if( year == to->year &&
3140 mon == to->mon &&
3141 day == to->day &&
3142 hour == to->hour &&
3143 min == to->min &&
3144 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003145 return( 1 );
3146
Paul Bakker40ea7de2009-05-03 10:18:48 +00003147 return( 0 );
3148}
3149
3150/*
3151 * Return 1 if the certificate is revoked, or 0 otherwise.
3152 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003153int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003154{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003155 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003156
3157 while( cur != NULL && cur->serial.len != 0 )
3158 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003159 if( crt->serial.len == cur->serial.len &&
3160 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003161 {
3162 if( x509parse_time_expired( &cur->revocation_date ) )
3163 return( 1 );
3164 }
3165
3166 cur = cur->next;
3167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
3169 return( 0 );
3170}
3171
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003172/*
3173 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003174 */
Paul Bakker23986e52011-04-24 08:57:21 +00003175static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 unsigned char *out )
3177{
3178 switch( alg )
3179 {
Paul Bakker40e46942009-01-03 21:51:57 +00003180#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003181 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003182#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003183#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003184 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003185#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003186#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003187 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003188#endif
3189#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003190 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003191#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003192#if defined(POLARSSL_SHA2_C)
3193 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3194 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3195#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003196#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003197 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3198 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3199#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003201 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 break;
3203 }
3204}
3205
3206/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003207 * Check that the given certificate is valid accoring to the CRL.
3208 */
3209static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3210 x509_crl *crl_list)
3211{
3212 int flags = 0;
3213 int hash_id;
3214 unsigned char hash[64];
3215
Paul Bakker915275b2012-09-28 07:10:55 +00003216 if( ca == NULL )
3217 return( flags );
3218
Paul Bakker76fd75a2011-01-16 21:12:10 +00003219 /*
3220 * TODO: What happens if no CRL is present?
3221 * Suggestion: Revocation state should be unknown if no CRL is present.
3222 * For backwards compatibility this is not yet implemented.
3223 */
3224
Paul Bakker915275b2012-09-28 07:10:55 +00003225 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003226 {
Paul Bakker915275b2012-09-28 07:10:55 +00003227 if( crl_list->version == 0 ||
3228 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003229 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3230 crl_list->issuer_raw.len ) != 0 )
3231 {
3232 crl_list = crl_list->next;
3233 continue;
3234 }
3235
3236 /*
3237 * Check if CRL is correctly signed by the trusted CA
3238 */
3239 hash_id = crl_list->sig_alg;
3240
3241 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3242
Paul Bakker43f97992013-09-23 11:23:31 +02003243 if( !rsa_pkcs1_verify( &ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003244 0, hash, crl_list->sig.p ) == 0 )
3245 {
3246 /*
3247 * CRL is not trusted
3248 */
3249 flags |= BADCRL_NOT_TRUSTED;
3250 break;
3251 }
3252
3253 /*
3254 * Check for validity of CRL (Do not drop out)
3255 */
3256 if( x509parse_time_expired( &crl_list->next_update ) )
3257 flags |= BADCRL_EXPIRED;
3258
3259 /*
3260 * Check if certificate is revoked
3261 */
3262 if( x509parse_revoked(crt, crl_list) )
3263 {
3264 flags |= BADCERT_REVOKED;
3265 break;
3266 }
3267
3268 crl_list = crl_list->next;
3269 }
3270 return flags;
3271}
3272
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003273// Equal == 0, inequal == 1
3274static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3275{
3276 size_t i;
3277 unsigned char diff;
3278 const unsigned char *n1 = s1, *n2 = s2;
3279
3280 for( i = 0; i < len; i++ )
3281 {
3282 diff = n1[i] ^ n2[i];
3283
Paul Bakkerc941adb2014-07-07 14:17:24 +02003284 if( diff == 0 )
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003285 continue;
3286
Paul Bakkerc941adb2014-07-07 14:17:24 +02003287 if( diff == 32 &&
3288 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
3289 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
3290 {
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003291 continue;
Paul Bakkerc941adb2014-07-07 14:17:24 +02003292 }
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003293
3294 return( 1 );
3295 }
3296
3297 return( 0 );
3298}
3299
Paul Bakker57b12982012-02-11 17:38:38 +00003300int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003301{
3302 size_t i;
3303 size_t cn_idx = 0;
3304
Paul Bakker57b12982012-02-11 17:38:38 +00003305 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003306 return( 0 );
3307
3308 for( i = 0; i < strlen( cn ); ++i )
3309 {
3310 if( cn[i] == '.' )
3311 {
3312 cn_idx = i;
3313 break;
3314 }
3315 }
3316
3317 if( cn_idx == 0 )
3318 return( 0 );
3319
Paul Bakker535e97d2012-08-23 10:49:55 +00003320 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003321 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003322 {
3323 return( 1 );
3324 }
3325
3326 return( 0 );
3327}
3328
Paul Bakker915275b2012-09-28 07:10:55 +00003329static int x509parse_verify_top(
3330 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003331 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003332 int (*f_vrfy)(void *, x509_cert *, int, int *),
3333 void *p_vrfy )
3334{
3335 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003336 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003337 unsigned char hash[64];
3338
3339 if( x509parse_time_expired( &child->valid_to ) )
3340 *flags |= BADCERT_EXPIRED;
3341
3342 /*
3343 * Child is the top of the chain. Check against the trust_ca list.
3344 */
3345 *flags |= BADCERT_NOT_TRUSTED;
3346
3347 while( trust_ca != NULL )
3348 {
3349 if( trust_ca->version == 0 ||
3350 child->issuer_raw.len != trust_ca->subject_raw.len ||
3351 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3352 child->issuer_raw.len ) != 0 )
3353 {
3354 trust_ca = trust_ca->next;
3355 continue;
3356 }
3357
Paul Bakker9a736322012-11-14 12:39:52 +00003358 /*
3359 * Reduce path_len to check against if top of the chain is
3360 * the same as the trusted CA
3361 */
3362 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3363 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3364 child->issuer_raw.len ) == 0 )
3365 {
3366 check_path_cnt--;
3367 }
3368
Paul Bakker915275b2012-09-28 07:10:55 +00003369 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003370 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003371 {
3372 trust_ca = trust_ca->next;
3373 continue;
3374 }
3375
3376 hash_id = child->sig_alg;
3377
3378 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3379
Paul Bakker43f97992013-09-23 11:23:31 +02003380 if( rsa_pkcs1_verify( &trust_ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker915275b2012-09-28 07:10:55 +00003381 0, hash, child->sig.p ) != 0 )
3382 {
3383 trust_ca = trust_ca->next;
3384 continue;
3385 }
3386
3387 /*
3388 * Top of chain is signed by a trusted CA
3389 */
3390 *flags &= ~BADCERT_NOT_TRUSTED;
3391 break;
3392 }
3393
Paul Bakker9a736322012-11-14 12:39:52 +00003394 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003395 * If top of chain is not the same as the trusted CA send a verify request
3396 * to the callback for any issues with validity and CRL presence for the
3397 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003398 */
3399 if( trust_ca != NULL &&
3400 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3401 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3402 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003403 {
3404 /* Check trusted CA's CRL for then chain's top crt */
3405 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3406
3407 if( x509parse_time_expired( &trust_ca->valid_to ) )
3408 ca_flags |= BADCERT_EXPIRED;
3409
Paul Bakker915275b2012-09-28 07:10:55 +00003410 if( NULL != f_vrfy )
3411 {
Paul Bakker9a736322012-11-14 12:39:52 +00003412 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003413 return( ret );
3414 }
3415 }
3416
3417 /* Call callback on top cert */
3418 if( NULL != f_vrfy )
3419 {
Paul Bakker9a736322012-11-14 12:39:52 +00003420 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003421 return( ret );
3422 }
3423
Paul Bakker915275b2012-09-28 07:10:55 +00003424 *flags |= ca_flags;
3425
3426 return( 0 );
3427}
3428
3429static int x509parse_verify_child(
3430 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003431 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003432 int (*f_vrfy)(void *, x509_cert *, int, int *),
3433 void *p_vrfy )
3434{
3435 int hash_id, ret;
3436 int parent_flags = 0;
3437 unsigned char hash[64];
3438 x509_cert *grandparent;
3439
3440 if( x509parse_time_expired( &child->valid_to ) )
3441 *flags |= BADCERT_EXPIRED;
3442
3443 hash_id = child->sig_alg;
3444
3445 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3446
Paul Bakker43f97992013-09-23 11:23:31 +02003447 if( rsa_pkcs1_verify( &parent->rsa, NULL, NULL, RSA_PUBLIC, hash_id, 0,
3448 hash, child->sig.p ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003449 *flags |= BADCERT_NOT_TRUSTED;
3450
3451 /* Check trusted CA's CRL for the given crt */
3452 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3453
3454 grandparent = parent->next;
3455
3456 while( grandparent != NULL )
3457 {
3458 if( grandparent->version == 0 ||
3459 grandparent->ca_istrue == 0 ||
3460 parent->issuer_raw.len != grandparent->subject_raw.len ||
3461 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3462 parent->issuer_raw.len ) != 0 )
3463 {
3464 grandparent = grandparent->next;
3465 continue;
3466 }
3467 break;
3468 }
3469
Paul Bakker915275b2012-09-28 07:10:55 +00003470 if( grandparent != NULL )
3471 {
3472 /*
3473 * Part of the chain
3474 */
Paul Bakker9a736322012-11-14 12:39:52 +00003475 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 +00003476 if( ret != 0 )
3477 return( ret );
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003478 }
Paul Bakker915275b2012-09-28 07:10:55 +00003479 else
3480 {
Paul Bakker9a736322012-11-14 12:39:52 +00003481 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 +00003482 if( ret != 0 )
3483 return( ret );
3484 }
3485
3486 /* child is verified to be a child of the parent, call verify callback */
3487 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003488 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003489 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003490
3491 *flags |= parent_flags;
3492
3493 return( 0 );
3494}
3495
Paul Bakker76fd75a2011-01-16 21:12:10 +00003496/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003497 * Verify the certificate validity
3498 */
3499int x509parse_verify( x509_cert *crt,
3500 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003501 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003502 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003503 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003504 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003505{
Paul Bakker23986e52011-04-24 08:57:21 +00003506 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003507 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003508 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003509 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003510 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003511 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003512
Paul Bakker40ea7de2009-05-03 10:18:48 +00003513 *flags = 0;
3514
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 if( cn != NULL )
3516 {
3517 name = &crt->subject;
3518 cn_len = strlen( cn );
3519
Paul Bakker4d2c1242012-05-10 14:12:46 +00003520 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003521 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003522 cur = &crt->subject_alt_names;
3523
3524 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003525 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003526 if( cur->buf.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003527 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003528 break;
3529
Paul Bakker535e97d2012-08-23 10:49:55 +00003530 if( cur->buf.len > 2 &&
3531 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003532 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003533 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
Paul Bakker4d2c1242012-05-10 14:12:46 +00003535 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003536 }
3537
3538 if( cur == NULL )
3539 *flags |= BADCERT_CN_MISMATCH;
3540 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003541 else
3542 {
3543 while( name != NULL )
3544 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003545 if( name->oid.len == 3 &&
3546 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003547 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003548 if( name->val.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003549 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003550 break;
3551
Paul Bakker535e97d2012-08-23 10:49:55 +00003552 if( name->val.len > 2 &&
3553 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003554 x509_wildcard_verify( cn, &name->val ) )
3555 break;
3556 }
3557
3558 name = name->next;
3559 }
3560
3561 if( name == NULL )
3562 *flags |= BADCERT_CN_MISMATCH;
3563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003564 }
3565
Paul Bakker5121ce52009-01-03 21:22:43 +00003566 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003567 * Iterate upwards in the given cert chain, to find our crt parent.
3568 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003570 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003571
Paul Bakker76fd75a2011-01-16 21:12:10 +00003572 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003574 if( parent->ca_istrue == 0 ||
3575 crt->issuer_raw.len != parent->subject_raw.len ||
3576 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003577 crt->issuer_raw.len ) != 0 )
3578 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003579 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 continue;
3581 }
Paul Bakker915275b2012-09-28 07:10:55 +00003582 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003583 }
3584
Paul Bakker915275b2012-09-28 07:10:55 +00003585 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003586 {
Paul Bakker915275b2012-09-28 07:10:55 +00003587 /*
3588 * Part of the chain
3589 */
Paul Bakker9a736322012-11-14 12:39:52 +00003590 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003591 if( ret != 0 )
3592 return( ret );
3593 }
3594 else
Paul Bakker74111d32011-01-15 16:57:55 +00003595 {
Paul Bakker9a736322012-11-14 12:39:52 +00003596 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003597 if( ret != 0 )
3598 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003599 }
Paul Bakker915275b2012-09-28 07:10:55 +00003600
3601 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003602 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003603
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 return( 0 );
3605}
3606
3607/*
3608 * Unallocate all certificate data
3609 */
3610void x509_free( x509_cert *crt )
3611{
3612 x509_cert *cert_cur = crt;
3613 x509_cert *cert_prv;
3614 x509_name *name_cur;
3615 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003616 x509_sequence *seq_cur;
3617 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003618
3619 if( crt == NULL )
3620 return;
3621
3622 do
3623 {
3624 rsa_free( &cert_cur->rsa );
3625
3626 name_cur = cert_cur->issuer.next;
3627 while( name_cur != NULL )
3628 {
3629 name_prv = name_cur;
3630 name_cur = name_cur->next;
3631 memset( name_prv, 0, sizeof( x509_name ) );
3632 free( name_prv );
3633 }
3634
3635 name_cur = cert_cur->subject.next;
3636 while( name_cur != NULL )
3637 {
3638 name_prv = name_cur;
3639 name_cur = name_cur->next;
3640 memset( name_prv, 0, sizeof( x509_name ) );
3641 free( name_prv );
3642 }
3643
Paul Bakker74111d32011-01-15 16:57:55 +00003644 seq_cur = cert_cur->ext_key_usage.next;
3645 while( seq_cur != NULL )
3646 {
3647 seq_prv = seq_cur;
3648 seq_cur = seq_cur->next;
3649 memset( seq_prv, 0, sizeof( x509_sequence ) );
3650 free( seq_prv );
3651 }
3652
Paul Bakker8afa70d2012-02-11 18:42:45 +00003653 seq_cur = cert_cur->subject_alt_names.next;
3654 while( seq_cur != NULL )
3655 {
3656 seq_prv = seq_cur;
3657 seq_cur = seq_cur->next;
3658 memset( seq_prv, 0, sizeof( x509_sequence ) );
3659 free( seq_prv );
3660 }
3661
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 if( cert_cur->raw.p != NULL )
3663 {
3664 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3665 free( cert_cur->raw.p );
3666 }
3667
3668 cert_cur = cert_cur->next;
3669 }
3670 while( cert_cur != NULL );
3671
3672 cert_cur = crt;
3673 do
3674 {
3675 cert_prv = cert_cur;
3676 cert_cur = cert_cur->next;
3677
3678 memset( cert_prv, 0, sizeof( x509_cert ) );
3679 if( cert_prv != crt )
3680 free( cert_prv );
3681 }
3682 while( cert_cur != NULL );
3683}
3684
Paul Bakkerd98030e2009-05-02 15:13:40 +00003685/*
3686 * Unallocate all CRL data
3687 */
3688void x509_crl_free( x509_crl *crl )
3689{
3690 x509_crl *crl_cur = crl;
3691 x509_crl *crl_prv;
3692 x509_name *name_cur;
3693 x509_name *name_prv;
3694 x509_crl_entry *entry_cur;
3695 x509_crl_entry *entry_prv;
3696
3697 if( crl == NULL )
3698 return;
3699
3700 do
3701 {
3702 name_cur = crl_cur->issuer.next;
3703 while( name_cur != NULL )
3704 {
3705 name_prv = name_cur;
3706 name_cur = name_cur->next;
3707 memset( name_prv, 0, sizeof( x509_name ) );
3708 free( name_prv );
3709 }
3710
3711 entry_cur = crl_cur->entry.next;
3712 while( entry_cur != NULL )
3713 {
3714 entry_prv = entry_cur;
3715 entry_cur = entry_cur->next;
3716 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3717 free( entry_prv );
3718 }
3719
3720 if( crl_cur->raw.p != NULL )
3721 {
3722 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3723 free( crl_cur->raw.p );
3724 }
3725
3726 crl_cur = crl_cur->next;
3727 }
3728 while( crl_cur != NULL );
3729
3730 crl_cur = crl;
3731 do
3732 {
3733 crl_prv = crl_cur;
3734 crl_cur = crl_cur->next;
3735
3736 memset( crl_prv, 0, sizeof( x509_crl ) );
3737 if( crl_prv != crl )
3738 free( crl_prv );
3739 }
3740 while( crl_cur != NULL );
3741}
3742
Paul Bakker40e46942009-01-03 21:51:57 +00003743#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003744
Paul Bakker40e46942009-01-03 21:51:57 +00003745#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003746
3747/*
3748 * Checkup routine
3749 */
3750int x509_self_test( int verbose )
3751{
Paul Bakker5690efc2011-05-26 13:16:06 +00003752#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003753 int ret;
3754 int flags;
3755 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003756 x509_cert cacert;
3757 x509_cert clicert;
3758 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003759#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003760 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003761#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003762
3763 if( verbose != 0 )
3764 printf( " X.509 certificate load: " );
3765
3766 memset( &clicert, 0, sizeof( x509_cert ) );
3767
Paul Bakkereae09db2013-06-06 12:35:54 +02003768 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003769 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 if( ret != 0 )
3771 {
3772 if( verbose != 0 )
3773 printf( "failed\n" );
3774
3775 return( ret );
3776 }
3777
3778 memset( &cacert, 0, sizeof( x509_cert ) );
3779
Paul Bakkereae09db2013-06-06 12:35:54 +02003780 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003781 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003782 if( ret != 0 )
3783 {
3784 if( verbose != 0 )
3785 printf( "failed\n" );
3786
3787 return( ret );
3788 }
3789
3790 if( verbose != 0 )
3791 printf( "passed\n X.509 private key load: " );
3792
3793 i = strlen( test_ca_key );
3794 j = strlen( test_ca_pwd );
3795
Paul Bakker66b78b22011-03-25 14:22:50 +00003796 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3797
Paul Bakker5121ce52009-01-03 21:22:43 +00003798 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003799 (const unsigned char *) test_ca_key, i,
3800 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 {
3802 if( verbose != 0 )
3803 printf( "failed\n" );
3804
3805 return( ret );
3806 }
3807
3808 if( verbose != 0 )
3809 printf( "passed\n X.509 signature verify: ");
3810
Paul Bakker23986e52011-04-24 08:57:21 +00003811 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003812 if( ret != 0 )
3813 {
3814 if( verbose != 0 )
3815 printf( "failed\n" );
3816
3817 return( ret );
3818 }
3819
Paul Bakker5690efc2011-05-26 13:16:06 +00003820#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003821 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003822 printf( "passed\n X.509 DHM parameter load: " );
3823
3824 i = strlen( test_dhm_params );
3825 j = strlen( test_ca_pwd );
3826
Paul Bakkereae09db2013-06-06 12:35:54 +02003827 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003828 {
3829 if( verbose != 0 )
3830 printf( "failed\n" );
3831
3832 return( ret );
3833 }
3834
3835 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003836 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003837#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
3839 x509_free( &cacert );
3840 x509_free( &clicert );
3841 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003842#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003843 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003844#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003845
3846 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003847#else
3848 ((void) verbose);
3849 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3850#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003851}
3852
3853#endif
3854
3855#endif