blob: 7603eca3b14f293be69572ce5991360a6a2a37e8 [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 Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000045#include "polarssl/des.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010046#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000047#include "polarssl/md2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010048#endif
49#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000050#include "polarssl/md4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010051#endif
52#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000053#include "polarssl/md5.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010054#endif
55#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000056#include "polarssl/sha1.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010057#endif
58#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000059#include "polarssl/sha2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010060#endif
61#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000062#include "polarssl/sha4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010063#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000064#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66#include <string.h>
67#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000068#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000069#include <windows.h>
70#else
Paul Bakker5121ce52009-01-03 21:22:43 +000071#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000072#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Paul Bakker335db3f2011-04-25 15:28:35 +000074#if defined(POLARSSL_FS_IO)
75#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000076#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000077#include <sys/types.h>
78#include <dirent.h>
79#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000080#endif
81
Paul Bakker5121ce52009-01-03 21:22:43 +000082/*
Paul Bakker5121ce52009-01-03 21:22:43 +000083 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
84 */
85static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000086 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000087 int *ver )
88{
Paul Bakker23986e52011-04-24 08:57:21 +000089 int ret;
90 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 if( ( ret = asn1_get_tag( p, end, &len,
93 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
94 {
Paul Bakker40e46942009-01-03 21:51:57 +000095 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000096 {
97 *ver = 0;
98 return( 0 );
99 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101 return( ret );
102 }
103
104 end = *p + len;
105
106 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000107 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000110 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000111 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113 return( 0 );
114}
115
116/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000117 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000118 */
119static int x509_crl_get_version( unsigned char **p,
120 const unsigned char *end,
121 int *ver )
122{
123 int ret;
124
125 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
126 {
127 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000128 {
129 *ver = 0;
130 return( 0 );
131 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000132
133 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
134 }
135
136 return( 0 );
137}
138
139/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 * CertificateSerialNumber ::= INTEGER
141 */
142static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000143 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 x509_buf *serial )
145{
146 int ret;
147
148 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
153 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000154 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000155 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->tag = *(*p)++;
158
159 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000160 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
162 serial->p = *p;
163 *p += serial->len;
164
165 return( 0 );
166}
167
168/*
169 * AlgorithmIdentifier ::= SEQUENCE {
170 * algorithm OBJECT IDENTIFIER,
171 * parameters ANY DEFINED BY algorithm OPTIONAL }
172 */
173static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000174 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 x509_buf *alg )
176{
Paul Bakker23986e52011-04-24 08:57:21 +0000177 int ret;
178 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180 if( ( ret = asn1_get_tag( p, end, &len,
181 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000182 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184 end = *p + len;
185 alg->tag = **p;
186
187 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000188 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190 alg->p = *p;
191 *p += alg->len;
192
193 if( *p == end )
194 return( 0 );
195
196 /*
197 * assume the algorithm parameters must be NULL
198 */
199 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000200 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000203 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000204 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
206 return( 0 );
207}
208
209/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 * AttributeTypeAndValue ::= SEQUENCE {
211 * type AttributeType,
212 * value AttributeValue }
213 *
214 * AttributeType ::= OBJECT IDENTIFIER
215 *
216 * AttributeValue ::= ANY DEFINED BY AttributeType
217 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000218static int x509_get_attr_type_value( unsigned char **p,
219 const unsigned char *end,
220 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221{
Paul Bakker23986e52011-04-24 08:57:21 +0000222 int ret;
223 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 x509_buf *oid;
225 x509_buf *val;
226
227 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000229 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 oid = &cur->oid;
232 oid->tag = **p;
233
234 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000235 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237 oid->p = *p;
238 *p += oid->len;
239
240 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000241 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000242 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
245 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
246 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000247 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000248 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250 val = &cur->val;
251 val->tag = *(*p)++;
252
253 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000254 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
256 val->p = *p;
257 *p += val->len;
258
259 cur->next = NULL;
260
Paul Bakker400ff6f2011-02-20 10:40:16 +0000261 return( 0 );
262}
263
264/*
265 * RelativeDistinguishedName ::=
266 * SET OF AttributeTypeAndValue
267 *
268 * AttributeTypeAndValue ::= SEQUENCE {
269 * type AttributeType,
270 * value AttributeValue }
271 *
272 * AttributeType ::= OBJECT IDENTIFIER
273 *
274 * AttributeValue ::= ANY DEFINED BY AttributeType
275 */
276static int x509_get_name( unsigned char **p,
277 const unsigned char *end,
278 x509_name *cur )
279{
Paul Bakker23986e52011-04-24 08:57:21 +0000280 int ret;
281 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000282 const unsigned char *end2;
283 x509_name *use;
284
285 if( ( ret = asn1_get_tag( p, end, &len,
286 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000287 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000288
289 end2 = end;
290 end = *p + len;
291 use = cur;
292
293 do
294 {
295 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
296 return( ret );
297
298 if( *p != end )
299 {
300 use->next = (x509_name *) malloc(
301 sizeof( x509_name ) );
302
303 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000304 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000305
306 memset( use->next, 0, sizeof( x509_name ) );
307
308 use = use->next;
309 }
310 }
311 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313 /*
314 * recurse until end of SEQUENCE is reached
315 */
316 if( *p == end2 )
317 return( 0 );
318
319 cur->next = (x509_name *) malloc(
320 sizeof( x509_name ) );
321
322 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000323 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Paul Bakker430ffbe2012-05-01 08:14:20 +0000325 memset( cur->next, 0, sizeof( x509_name ) );
326
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 return( x509_get_name( p, end2, cur->next ) );
328}
329
330/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 * Time ::= CHOICE {
332 * utcTime UTCTime,
333 * generalTime GeneralizedTime }
334 */
Paul Bakker91200182010-02-18 21:26:15 +0000335static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000336 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000337 x509_time *time )
338{
Paul Bakker23986e52011-04-24 08:57:21 +0000339 int ret;
340 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000341 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000342 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000343
Paul Bakker91200182010-02-18 21:26:15 +0000344 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000345 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
346 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000347
Paul Bakker91200182010-02-18 21:26:15 +0000348 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000349
Paul Bakker91200182010-02-18 21:26:15 +0000350 if ( tag == ASN1_UTC_TIME )
351 {
352 (*p)++;
353 ret = asn1_get_len( p, end, &len );
354
355 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000356 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000357
Paul Bakker91200182010-02-18 21:26:15 +0000358 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000359 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
360 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000361
Paul Bakker91200182010-02-18 21:26:15 +0000362 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
363 &time->year, &time->mon, &time->day,
364 &time->hour, &time->min, &time->sec ) < 5 )
365 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000366
Paul Bakker400ff6f2011-02-20 10:40:16 +0000367 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000368 time->year += 1900;
369
370 *p += len;
371
372 return( 0 );
373 }
374 else if ( tag == ASN1_GENERALIZED_TIME )
375 {
376 (*p)++;
377 ret = asn1_get_len( p, end, &len );
378
379 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000380 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000381
382 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000383 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
384 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000385
386 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
387 &time->year, &time->mon, &time->day,
388 &time->hour, &time->min, &time->sec ) < 5 )
389 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
390
391 *p += len;
392
393 return( 0 );
394 }
395 else
Paul Bakker9d781402011-05-09 16:17:09 +0000396 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000397}
398
399
400/*
401 * Validity ::= SEQUENCE {
402 * notBefore Time,
403 * notAfter Time }
404 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000405static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000406 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 x509_time *from,
408 x509_time *to )
409{
Paul Bakker23986e52011-04-24 08:57:21 +0000410 int ret;
411 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413 if( ( ret = asn1_get_tag( p, end, &len,
414 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000415 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417 end = *p + len;
418
Paul Bakker91200182010-02-18 21:26:15 +0000419 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000420 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
Paul Bakker91200182010-02-18 21:26:15 +0000422 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000423 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000426 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000427 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
429 return( 0 );
430}
431
432/*
433 * SubjectPublicKeyInfo ::= SEQUENCE {
434 * algorithm AlgorithmIdentifier,
435 * subjectPublicKey BIT STRING }
436 */
437static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000438 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 x509_buf *pk_alg_oid,
440 mpi *N, mpi *E )
441{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200442 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000443 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 unsigned char *end2;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200445 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
448 return( ret );
449
450 /*
451 * only RSA public keys handled at this time
452 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200453 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000454 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000457 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000460 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000461 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463 end2 = *p + len;
464
465 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000466 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 /*
469 * RSAPublicKey ::= SEQUENCE {
470 * modulus INTEGER, -- n
471 * publicExponent INTEGER -- e
472 * }
473 */
474 if( ( ret = asn1_get_tag( p, end2, &len,
475 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000476 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000479 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000480 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
483 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 return( 0 );
491}
492
493static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000494 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 x509_buf *sig )
496{
Paul Bakker23986e52011-04-24 08:57:21 +0000497 int ret;
498 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
Paul Bakker8afa70d2012-02-11 18:42:45 +0000500 if( ( end - *p ) < 1 )
501 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
502 POLARSSL_ERR_ASN1_OUT_OF_DATA );
503
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 sig->tag = **p;
505
506 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000507 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
Paul Bakker74111d32011-01-15 16:57:55 +0000509
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000511 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 sig->len = len;
514 sig->p = *p;
515
516 *p += len;
517
518 return( 0 );
519}
520
521/*
522 * X.509 v2/v3 unique identifier (not parsed)
523 */
524static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000525 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 x509_buf *uid, int n )
527{
528 int ret;
529
530 if( *p == end )
531 return( 0 );
532
533 uid->tag = **p;
534
535 if( ( ret = asn1_get_tag( p, end, &uid->len,
536 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
537 {
Paul Bakker40e46942009-01-03 21:51:57 +0000538 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 return( 0 );
540
541 return( ret );
542 }
543
544 uid->p = *p;
545 *p += uid->len;
546
547 return( 0 );
548}
549
550/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000551 * X.509 Extensions (No parsing of extensions, pointer should
552 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 */
554static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000555 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000556 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000557{
Paul Bakker23986e52011-04-24 08:57:21 +0000558 int ret;
559 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
561 if( *p == end )
562 return( 0 );
563
564 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000565
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000567 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 ext->p = *p;
571 end = *p + ext->len;
572
573 /*
574 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
575 *
576 * Extension ::= SEQUENCE {
577 * extnID OBJECT IDENTIFIER,
578 * critical BOOLEAN DEFAULT FALSE,
579 * extnValue OCTET STRING }
580 */
581 if( ( ret = asn1_get_tag( p, end, &len,
582 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000583 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000586 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000587 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Paul Bakkerd98030e2009-05-02 15:13:40 +0000589 return( 0 );
590}
591
592/*
593 * X.509 CRL v2 extensions (no extensions parsed yet.)
594 */
595static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000596 const unsigned char *end,
597 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000598{
Paul Bakker23986e52011-04-24 08:57:21 +0000599 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000600 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000601
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000602 /* Get explicit tag */
603 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000604 {
605 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
606 return( 0 );
607
608 return( ret );
609 }
610
611 while( *p < end )
612 {
613 if( ( ret = asn1_get_tag( p, end, &len,
614 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000615 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000616
617 *p += len;
618 }
619
620 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000621 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000622 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
623
624 return( 0 );
625}
626
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000627/*
628 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
629 */
630static int x509_get_crl_entry_ext( unsigned char **p,
631 const unsigned char *end,
632 x509_buf *ext )
633{
634 int ret;
635 size_t len = 0;
636
637 /* OPTIONAL */
638 if (end <= *p)
639 return( 0 );
640
641 ext->tag = **p;
642 ext->p = *p;
643
644 /*
645 * Get CRL-entry extension sequence header
646 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
647 */
648 if( ( ret = asn1_get_tag( p, end, &ext->len,
649 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
650 {
651 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
652 {
653 ext->p = NULL;
654 return( 0 );
655 }
656 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
657 }
658
659 end = *p + ext->len;
660
661 if( end != *p + ext->len )
662 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
663 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
664
665 while( *p < end )
666 {
667 if( ( ret = asn1_get_tag( p, end, &len,
668 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
669 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
670
671 *p += len;
672 }
673
674 if( *p != end )
675 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
676 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
677
678 return( 0 );
679}
680
Paul Bakker74111d32011-01-15 16:57:55 +0000681static int x509_get_basic_constraints( unsigned char **p,
682 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000683 int *ca_istrue,
684 int *max_pathlen )
685{
Paul Bakker23986e52011-04-24 08:57:21 +0000686 int ret;
687 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000688
689 /*
690 * BasicConstraints ::= SEQUENCE {
691 * cA BOOLEAN DEFAULT FALSE,
692 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
693 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000694 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000695 *max_pathlen = 0; /* endless */
696
697 if( ( ret = asn1_get_tag( p, end, &len,
698 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000699 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000700
701 if( *p == end )
702 return 0;
703
Paul Bakker3cccddb2011-01-16 21:46:31 +0000704 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000705 {
706 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000707 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000708
709 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000710 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000711
Paul Bakker3cccddb2011-01-16 21:46:31 +0000712 if( *ca_istrue != 0 )
713 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000714 }
715
716 if( *p == end )
717 return 0;
718
719 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000720 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000721
722 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000723 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000724 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
725
726 (*max_pathlen)++;
727
Paul Bakker74111d32011-01-15 16:57:55 +0000728 return 0;
729}
730
731static int x509_get_ns_cert_type( unsigned char **p,
732 const unsigned char *end,
733 unsigned char *ns_cert_type)
734{
735 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000736 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000737
738 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000739 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000740
741 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000742 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000743 POLARSSL_ERR_ASN1_INVALID_LENGTH );
744
745 /* Get actual bitstring */
746 *ns_cert_type = *bs.p;
747 return 0;
748}
749
750static int x509_get_key_usage( unsigned char **p,
751 const unsigned char *end,
752 unsigned char *key_usage)
753{
754 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000755 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000756
757 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000758 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000759
Paul Bakker94a67962012-08-23 13:03:52 +0000760 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000761 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000762 POLARSSL_ERR_ASN1_INVALID_LENGTH );
763
764 /* Get actual bitstring */
765 *key_usage = *bs.p;
766 return 0;
767}
768
Paul Bakkerd98030e2009-05-02 15:13:40 +0000769/*
Paul Bakker74111d32011-01-15 16:57:55 +0000770 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
771 *
772 * KeyPurposeId ::= OBJECT IDENTIFIER
773 */
774static int x509_get_ext_key_usage( unsigned char **p,
775 const unsigned char *end,
776 x509_sequence *ext_key_usage)
777{
778 int ret;
779
780 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000782
783 /* Sequence length must be >= 1 */
784 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000785 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000786 POLARSSL_ERR_ASN1_INVALID_LENGTH );
787
788 return 0;
789}
790
791/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000792 * SubjectAltName ::= GeneralNames
793 *
794 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
795 *
796 * GeneralName ::= CHOICE {
797 * otherName [0] OtherName,
798 * rfc822Name [1] IA5String,
799 * dNSName [2] IA5String,
800 * x400Address [3] ORAddress,
801 * directoryName [4] Name,
802 * ediPartyName [5] EDIPartyName,
803 * uniformResourceIdentifier [6] IA5String,
804 * iPAddress [7] OCTET STRING,
805 * registeredID [8] OBJECT IDENTIFIER }
806 *
807 * OtherName ::= SEQUENCE {
808 * type-id OBJECT IDENTIFIER,
809 * value [0] EXPLICIT ANY DEFINED BY type-id }
810 *
811 * EDIPartyName ::= SEQUENCE {
812 * nameAssigner [0] DirectoryString OPTIONAL,
813 * partyName [1] DirectoryString }
814 *
815 * NOTE: PolarSSL only parses and uses dNSName at this point.
816 */
817static int x509_get_subject_alt_name( unsigned char **p,
818 const unsigned char *end,
819 x509_sequence *subject_alt_name )
820{
821 int ret;
822 size_t len, tag_len;
823 asn1_buf *buf;
824 unsigned char tag;
825 asn1_sequence *cur = subject_alt_name;
826
827 /* Get main sequence tag */
828 if( ( ret = asn1_get_tag( p, end, &len,
829 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
830 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
831
832 if( *p + len != end )
833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
834 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
835
836 while( *p < end )
837 {
838 if( ( end - *p ) < 1 )
839 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
840 POLARSSL_ERR_ASN1_OUT_OF_DATA );
841
842 tag = **p;
843 (*p)++;
844 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
846
847 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
848 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
849 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
850
851 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
852 {
853 *p += tag_len;
854 continue;
855 }
856
857 buf = &(cur->buf);
858 buf->tag = tag;
859 buf->p = *p;
860 buf->len = tag_len;
861 *p += buf->len;
862
863 /* Allocate and assign next pointer */
864 if (*p < end)
865 {
866 cur->next = (asn1_sequence *) malloc(
867 sizeof( asn1_sequence ) );
868
869 if( cur->next == NULL )
870 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
871 POLARSSL_ERR_ASN1_MALLOC_FAILED );
872
Paul Bakker535e97d2012-08-23 10:49:55 +0000873 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000874 cur = cur->next;
875 }
876 }
877
878 /* Set final sequence entry's next pointer to NULL */
879 cur->next = NULL;
880
881 if( *p != end )
882 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
883 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
884
885 return( 0 );
886}
887
888/*
Paul Bakker74111d32011-01-15 16:57:55 +0000889 * X.509 v3 extensions
890 *
891 * TODO: Perform all of the basic constraints tests required by the RFC
892 * TODO: Set values for undetected extensions to a sane default?
893 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000894 */
895static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000896 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000897 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000898{
Paul Bakker23986e52011-04-24 08:57:21 +0000899 int ret;
900 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000901 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000902
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000903 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000904 {
905 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
906 return( 0 );
907
908 return( ret );
909 }
910
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 while( *p < end )
912 {
Paul Bakker74111d32011-01-15 16:57:55 +0000913 /*
914 * Extension ::= SEQUENCE {
915 * extnID OBJECT IDENTIFIER,
916 * critical BOOLEAN DEFAULT FALSE,
917 * extnValue OCTET STRING }
918 */
919 x509_buf extn_oid = {0, 0, NULL};
920 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200921 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000922
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 if( ( ret = asn1_get_tag( p, end, &len,
924 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000925 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000927 end_ext_data = *p + len;
928
Paul Bakker74111d32011-01-15 16:57:55 +0000929 /* Get extension ID */
930 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Paul Bakker74111d32011-01-15 16:57:55 +0000932 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000933 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Paul Bakker74111d32011-01-15 16:57:55 +0000935 extn_oid.p = *p;
936 *p += extn_oid.len;
937
938 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000939 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000940 POLARSSL_ERR_ASN1_OUT_OF_DATA );
941
942 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000943 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000944 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
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 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000948 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000950 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000952 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000953
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000954 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000955 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000956 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
Paul Bakker74111d32011-01-15 16:57:55 +0000958 /*
959 * Detect supported extensions
960 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200961 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
962
963 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000964 {
965 /* No parser found, skip extension */
966 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Paul Bakker5c721f92011-07-27 16:51:09 +0000968#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000969 if( is_critical )
970 {
971 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000972 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000973 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
974 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000975#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +0200976 continue;
977 }
978
979 crt->ext_types |= ext_type;
980
981 switch( ext_type )
982 {
983 case EXT_BASIC_CONSTRAINTS:
984 /* Parse basic constraints */
985 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
986 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
987 return ( ret );
988 break;
989
990 case EXT_KEY_USAGE:
991 /* Parse key usage */
992 if( ( ret = x509_get_key_usage( p, end_ext_octet,
993 &crt->key_usage ) ) != 0 )
994 return ( ret );
995 break;
996
997 case EXT_EXTENDED_KEY_USAGE:
998 /* Parse extended key usage */
999 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1000 &crt->ext_key_usage ) ) != 0 )
1001 return ( ret );
1002 break;
1003
1004 case EXT_SUBJECT_ALT_NAME:
1005 /* Parse subject alt name */
1006 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1007 &crt->subject_alt_names ) ) != 0 )
1008 return ( ret );
1009 break;
1010
1011 case EXT_NS_CERT_TYPE:
1012 /* Parse netscape certificate type */
1013 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1014 &crt->ns_cert_type ) ) != 0 )
1015 return ( ret );
1016 break;
1017
1018 default:
1019 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 }
1022
1023 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001024 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001025 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 return( 0 );
1028}
1029
1030/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001031 * X.509 CRL Entries
1032 */
1033static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001034 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001035 x509_crl_entry *entry )
1036{
Paul Bakker23986e52011-04-24 08:57:21 +00001037 int ret;
1038 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001039 x509_crl_entry *cur_entry = entry;
1040
1041 if( *p == end )
1042 return( 0 );
1043
Paul Bakker9be19372009-07-27 20:21:53 +00001044 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001045 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1046 {
1047 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1048 return( 0 );
1049
1050 return( ret );
1051 }
1052
Paul Bakker9be19372009-07-27 20:21:53 +00001053 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001054
1055 while( *p < end )
1056 {
Paul Bakker23986e52011-04-24 08:57:21 +00001057 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001058 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001059
1060 if( ( ret = asn1_get_tag( p, end, &len2,
1061 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1062 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001063 return( ret );
1064 }
1065
Paul Bakker9be19372009-07-27 20:21:53 +00001066 cur_entry->raw.tag = **p;
1067 cur_entry->raw.p = *p;
1068 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001069 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001070
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001071 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001072 return( ret );
1073
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001074 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001075 return( ret );
1076
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001077 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001078 return( ret );
1079
Paul Bakker74111d32011-01-15 16:57:55 +00001080 if ( *p < end )
1081 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001082 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001083
1084 if( cur_entry->next == NULL )
1085 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1086
Paul Bakkerd98030e2009-05-02 15:13:40 +00001087 cur_entry = cur_entry->next;
1088 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1089 }
1090 }
1091
1092 return( 0 );
1093}
1094
Paul Bakkerc70b9822013-04-07 22:00:46 +02001095static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1096 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001097{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001098 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001099
Paul Bakkerc70b9822013-04-07 22:00:46 +02001100 if( ret != 0 )
1101 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001102
Paul Bakkerc70b9822013-04-07 22:00:46 +02001103 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001104}
1105
Paul Bakkerd98030e2009-05-02 15:13:40 +00001106/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001107 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001109int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001110{
Paul Bakker23986e52011-04-24 08:57:21 +00001111 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001112 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001113 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Paul Bakker320a4b52009-03-28 18:52:39 +00001115 /*
1116 * Check for valid input
1117 */
1118 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001119 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001120
Paul Bakker96743fc2011-02-12 14:30:57 +00001121 p = (unsigned char *) malloc( len = buflen );
1122
1123 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001124 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001125
1126 memcpy( p, buf, buflen );
1127
1128 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001129
1130 crt->raw.p = p;
1131 crt->raw.len = len;
1132 end = p + len;
1133
1134 /*
1135 * Certificate ::= SEQUENCE {
1136 * tbsCertificate TBSCertificate,
1137 * signatureAlgorithm AlgorithmIdentifier,
1138 * signatureValue BIT STRING }
1139 */
1140 if( ( ret = asn1_get_tag( &p, end, &len,
1141 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1142 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001143 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001144 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 }
1146
Paul Bakkerb00ca422012-09-25 12:10:00 +00001147 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001149 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001150 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001151 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001152 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001153 crt_end = p + len;
1154
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 /*
1156 * TBSCertificate ::= SEQUENCE {
1157 */
1158 crt->tbs.p = p;
1159
1160 if( ( ret = asn1_get_tag( &p, end, &len,
1161 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1162 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001163 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001164 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 }
1166
1167 end = p + len;
1168 crt->tbs.len = end - crt->tbs.p;
1169
1170 /*
1171 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1172 *
1173 * CertificateSerialNumber ::= INTEGER
1174 *
1175 * signature AlgorithmIdentifier
1176 */
1177 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1178 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1179 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1180 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001181 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 return( ret );
1183 }
1184
1185 crt->version++;
1186
1187 if( crt->version > 3 )
1188 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001189 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001190 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
1192
Paul Bakkerc70b9822013-04-07 22:00:46 +02001193 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1194 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001196 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001197 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 }
1199
1200 /*
1201 * issuer Name
1202 */
1203 crt->issuer_raw.p = p;
1204
1205 if( ( ret = asn1_get_tag( &p, end, &len,
1206 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1207 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001208 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001209 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
1211
1212 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1213 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001214 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 return( ret );
1216 }
1217
1218 crt->issuer_raw.len = p - crt->issuer_raw.p;
1219
1220 /*
1221 * Validity ::= SEQUENCE {
1222 * notBefore Time,
1223 * notAfter Time }
1224 *
1225 */
1226 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1227 &crt->valid_to ) ) != 0 )
1228 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001229 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 return( ret );
1231 }
1232
1233 /*
1234 * subject Name
1235 */
1236 crt->subject_raw.p = p;
1237
1238 if( ( ret = asn1_get_tag( &p, end, &len,
1239 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1240 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001241 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001242 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 }
1244
Paul Bakkercefb3962012-06-27 11:51:09 +00001245 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001247 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 return( ret );
1249 }
1250
1251 crt->subject_raw.len = p - crt->subject_raw.p;
1252
1253 /*
1254 * SubjectPublicKeyInfo ::= SEQUENCE
1255 * algorithm AlgorithmIdentifier,
1256 * subjectPublicKey BIT STRING }
1257 */
1258 if( ( ret = asn1_get_tag( &p, end, &len,
1259 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1260 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001261 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001262 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 }
1264
1265 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1266 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1267 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001268 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 return( ret );
1270 }
1271
1272 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1273 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001274 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 return( ret );
1276 }
1277
1278 crt->rsa.len = mpi_size( &crt->rsa.N );
1279
1280 /*
1281 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1282 * -- If present, version shall be v2 or v3
1283 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1284 * -- If present, version shall be v2 or v3
1285 * extensions [3] EXPLICIT Extensions OPTIONAL
1286 * -- If present, version shall be v3
1287 */
1288 if( crt->version == 2 || crt->version == 3 )
1289 {
1290 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1291 if( ret != 0 )
1292 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001293 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 return( ret );
1295 }
1296 }
1297
1298 if( crt->version == 2 || crt->version == 3 )
1299 {
1300 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1301 if( ret != 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
1308 if( crt->version == 3 )
1309 {
Paul Bakker74111d32011-01-15 16:57:55 +00001310 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 if( ret != 0 )
1312 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001313 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 return( ret );
1315 }
1316 }
1317
1318 if( p != end )
1319 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001320 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001321 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001322 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 }
1324
Paul Bakkerb00ca422012-09-25 12:10:00 +00001325 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
1327 /*
1328 * signatureAlgorithm AlgorithmIdentifier,
1329 * signatureValue BIT STRING
1330 */
1331 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1332 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001333 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 return( ret );
1335 }
1336
Paul Bakker535e97d2012-08-23 10:49:55 +00001337 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1338 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001340 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001341 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 }
1343
1344 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1345 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001346 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 return( ret );
1348 }
1349
1350 if( p != end )
1351 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001352 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001353 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001354 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 }
1356
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001357 return( 0 );
1358}
1359
1360/*
1361 * Parse one or more PEM certificates from a buffer and add them to the chained list
1362 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001363int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001364{
Paul Bakker69e095c2011-12-10 21:55:01 +00001365 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001366 x509_cert *crt, *prev = NULL;
1367 int buf_format = X509_FORMAT_DER;
1368
1369 crt = chain;
1370
1371 /*
1372 * Check for valid input
1373 */
1374 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001375 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001376
1377 while( crt->version != 0 && crt->next != NULL )
1378 {
1379 prev = crt;
1380 crt = crt->next;
1381 }
1382
1383 /*
1384 * Add new certificate on the end of the chain if needed.
1385 */
1386 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001387 {
1388 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1389
Paul Bakker7d06ad22009-05-02 15:53:56 +00001390 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001391 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001392
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001393 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001394 crt = crt->next;
1395 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001396 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001398 /*
1399 * Determine buffer content. Buffer contains either one DER certificate or
1400 * one or more PEM certificates.
1401 */
1402#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001403 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001404 buf_format = X509_FORMAT_PEM;
1405#endif
1406
1407 if( buf_format == X509_FORMAT_DER )
1408 return x509parse_crt_der( crt, buf, buflen );
1409
1410#if defined(POLARSSL_PEM_C)
1411 if( buf_format == X509_FORMAT_PEM )
1412 {
1413 pem_context pem;
1414
1415 while( buflen > 0 )
1416 {
1417 size_t use_len;
1418 pem_init( &pem );
1419
1420 ret = pem_read_buffer( &pem,
1421 "-----BEGIN CERTIFICATE-----",
1422 "-----END CERTIFICATE-----",
1423 buf, NULL, 0, &use_len );
1424
1425 if( ret == 0 )
1426 {
1427 /*
1428 * Was PEM encoded
1429 */
1430 buflen -= use_len;
1431 buf += use_len;
1432 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001433 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1434 {
1435 return( ret );
1436 }
Paul Bakker00b28602013-06-24 13:02:41 +02001437 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001438 {
1439 pem_free( &pem );
1440
Paul Bakker5ed3b342013-06-24 19:05:46 +02001441 /*
1442 * PEM header and footer were found
1443 */
1444 buflen -= use_len;
1445 buf += use_len;
1446
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001447 if( first_error == 0 )
1448 first_error = ret;
1449
1450 continue;
1451 }
1452 else
1453 break;
1454
1455 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1456
1457 pem_free( &pem );
1458
1459 if( ret != 0 )
1460 {
1461 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001462 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001463 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001464 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001465 {
1466 if( prev )
1467 prev->next = NULL;
1468
1469 if( crt != chain )
1470 free( crt );
1471
1472 return( ret );
1473 }
1474
1475 if( first_error == 0 )
1476 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001477
1478 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001479
1480 memset( crt, 0, sizeof( x509_cert ) );
1481 continue;
1482 }
1483
1484 success = 1;
1485
1486 /*
1487 * Add new certificate to the list
1488 */
1489 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1490
1491 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001492 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001493
1494 prev = crt;
1495 crt = crt->next;
1496 memset( crt, 0, sizeof( x509_cert ) );
1497 }
1498 }
1499#endif
1500
1501 if( crt->version == 0 )
1502 {
1503 if( prev )
1504 prev->next = NULL;
1505
1506 if( crt != chain )
1507 free( crt );
1508 }
1509
1510 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001511 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001512 else if( first_error )
1513 return( first_error );
1514 else
1515 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001516}
1517
1518/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001519 * Parse one or more CRLs and add them to the chained list
1520 */
Paul Bakker23986e52011-04-24 08:57:21 +00001521int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001522{
Paul Bakker23986e52011-04-24 08:57:21 +00001523 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001524 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001525 unsigned char *p, *end;
1526 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001527#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001528 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001529 pem_context pem;
1530#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001531
1532 crl = chain;
1533
1534 /*
1535 * Check for valid input
1536 */
1537 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001538 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001539
1540 while( crl->version != 0 && crl->next != NULL )
1541 crl = crl->next;
1542
1543 /*
1544 * Add new CRL on the end of the chain if needed.
1545 */
1546 if ( crl->version != 0 && crl->next == NULL)
1547 {
1548 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1549
Paul Bakker7d06ad22009-05-02 15:53:56 +00001550 if( crl->next == NULL )
1551 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001552 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001553 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001554 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001555
Paul Bakker7d06ad22009-05-02 15:53:56 +00001556 crl = crl->next;
1557 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001558 }
1559
Paul Bakker96743fc2011-02-12 14:30:57 +00001560#if defined(POLARSSL_PEM_C)
1561 pem_init( &pem );
1562 ret = pem_read_buffer( &pem,
1563 "-----BEGIN X509 CRL-----",
1564 "-----END X509 CRL-----",
1565 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001566
Paul Bakker96743fc2011-02-12 14:30:57 +00001567 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001568 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001569 /*
1570 * Was PEM encoded
1571 */
1572 buflen -= use_len;
1573 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001574
1575 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001576 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001577 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001578 p = pem.buf;
1579 pem.buf = NULL;
1580 len = pem.buflen;
1581 pem_free( &pem );
1582 }
Paul Bakker00b28602013-06-24 13:02:41 +02001583 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001584 {
1585 pem_free( &pem );
1586 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587 }
1588 else
1589 {
1590 /*
1591 * nope, copy the raw DER data
1592 */
1593 p = (unsigned char *) malloc( len = buflen );
1594
1595 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001596 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001597
1598 memcpy( p, buf, buflen );
1599
1600 buflen = 0;
1601 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001602#else
1603 p = (unsigned char *) malloc( len = buflen );
1604
1605 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001606 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001607
1608 memcpy( p, buf, buflen );
1609
1610 buflen = 0;
1611#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612
1613 crl->raw.p = p;
1614 crl->raw.len = len;
1615 end = p + len;
1616
1617 /*
1618 * CertificateList ::= SEQUENCE {
1619 * tbsCertList TBSCertList,
1620 * signatureAlgorithm AlgorithmIdentifier,
1621 * signatureValue BIT STRING }
1622 */
1623 if( ( ret = asn1_get_tag( &p, end, &len,
1624 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1625 {
1626 x509_crl_free( crl );
1627 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1628 }
1629
Paul Bakker23986e52011-04-24 08:57:21 +00001630 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631 {
1632 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001633 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1635 }
1636
1637 /*
1638 * TBSCertList ::= SEQUENCE {
1639 */
1640 crl->tbs.p = p;
1641
1642 if( ( ret = asn1_get_tag( &p, end, &len,
1643 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1644 {
1645 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001646 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001647 }
1648
1649 end = p + len;
1650 crl->tbs.len = end - crl->tbs.p;
1651
1652 /*
1653 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1654 * -- if present, MUST be v2
1655 *
1656 * signature AlgorithmIdentifier
1657 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001658 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001659 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1660 {
1661 x509_crl_free( crl );
1662 return( ret );
1663 }
1664
1665 crl->version++;
1666
1667 if( crl->version > 2 )
1668 {
1669 x509_crl_free( crl );
1670 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1671 }
1672
Paul Bakkerc70b9822013-04-07 22:00:46 +02001673 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1674 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001675 {
1676 x509_crl_free( crl );
1677 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1678 }
1679
1680 /*
1681 * issuer Name
1682 */
1683 crl->issuer_raw.p = p;
1684
1685 if( ( ret = asn1_get_tag( &p, end, &len,
1686 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1687 {
1688 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001689 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001690 }
1691
1692 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1693 {
1694 x509_crl_free( crl );
1695 return( ret );
1696 }
1697
1698 crl->issuer_raw.len = p - crl->issuer_raw.p;
1699
1700 /*
1701 * thisUpdate Time
1702 * nextUpdate Time OPTIONAL
1703 */
Paul Bakker91200182010-02-18 21:26:15 +00001704 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001705 {
1706 x509_crl_free( crl );
1707 return( ret );
1708 }
1709
Paul Bakker91200182010-02-18 21:26:15 +00001710 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001711 {
Paul Bakker9d781402011-05-09 16:17:09 +00001712 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001713 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001714 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001715 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001716 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001717 x509_crl_free( crl );
1718 return( ret );
1719 }
1720 }
1721
1722 /*
1723 * revokedCertificates SEQUENCE OF SEQUENCE {
1724 * userCertificate CertificateSerialNumber,
1725 * revocationDate Time,
1726 * crlEntryExtensions Extensions OPTIONAL
1727 * -- if present, MUST be v2
1728 * } OPTIONAL
1729 */
1730 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1731 {
1732 x509_crl_free( crl );
1733 return( ret );
1734 }
1735
1736 /*
1737 * crlExtensions EXPLICIT Extensions OPTIONAL
1738 * -- if present, MUST be v2
1739 */
1740 if( crl->version == 2 )
1741 {
1742 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1743
1744 if( ret != 0 )
1745 {
1746 x509_crl_free( crl );
1747 return( ret );
1748 }
1749 }
1750
1751 if( p != end )
1752 {
1753 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001754 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1756 }
1757
1758 end = crl->raw.p + crl->raw.len;
1759
1760 /*
1761 * signatureAlgorithm AlgorithmIdentifier,
1762 * signatureValue BIT STRING
1763 */
1764 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1765 {
1766 x509_crl_free( crl );
1767 return( ret );
1768 }
1769
Paul Bakker535e97d2012-08-23 10:49:55 +00001770 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1771 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001772 {
1773 x509_crl_free( crl );
1774 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1775 }
1776
1777 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1778 {
1779 x509_crl_free( crl );
1780 return( ret );
1781 }
1782
1783 if( p != end )
1784 {
1785 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001786 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001787 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1788 }
1789
1790 if( buflen > 0 )
1791 {
1792 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1793
Paul Bakker7d06ad22009-05-02 15:53:56 +00001794 if( crl->next == NULL )
1795 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001796 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001797 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001798 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799
Paul Bakker7d06ad22009-05-02 15:53:56 +00001800 crl = crl->next;
1801 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001802
1803 return( x509parse_crl( crl, buf, buflen ) );
1804 }
1805
1806 return( 0 );
1807}
1808
Paul Bakker335db3f2011-04-25 15:28:35 +00001809#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001811 * Load all data from a file into a given buffer.
1812 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001813int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001814{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001816
Paul Bakkerd98030e2009-05-02 15:13:40 +00001817 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001818 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001819
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820 fseek( f, 0, SEEK_END );
1821 *n = (size_t) ftell( f );
1822 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001823
Paul Bakkerd98030e2009-05-02 15:13:40 +00001824 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001825 {
1826 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001827 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001828 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001829
Paul Bakkerd98030e2009-05-02 15:13:40 +00001830 if( fread( *buf, 1, *n, f ) != *n )
1831 {
1832 fclose( f );
1833 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001834 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001835 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001836
Paul Bakkerd98030e2009-05-02 15:13:40 +00001837 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001838
Paul Bakkerd98030e2009-05-02 15:13:40 +00001839 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001840
Paul Bakkerd98030e2009-05-02 15:13:40 +00001841 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001842}
1843
1844/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 * Load one or more certificates and add them to the chained list
1846 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001847int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001848{
1849 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 size_t n;
1851 unsigned char *buf;
1852
Paul Bakker69e095c2011-12-10 21:55:01 +00001853 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1854 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Paul Bakker69e095c2011-12-10 21:55:01 +00001856 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
1858 memset( buf, 0, n + 1 );
1859 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
1861 return( ret );
1862}
1863
Paul Bakker8d914582012-06-04 12:46:42 +00001864int x509parse_crtpath( x509_cert *chain, const char *path )
1865{
1866 int ret = 0;
1867#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001868 int w_ret;
1869 WCHAR szDir[MAX_PATH];
1870 char filename[MAX_PATH];
1871 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001872 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001873
Paul Bakker97872ac2012-11-02 12:53:26 +00001874 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001875 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001876
1877 if( len > MAX_PATH - 3 )
1878 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001879
Paul Bakker3338b792012-10-01 21:13:10 +00001880 memset( szDir, 0, sizeof(szDir) );
1881 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001882 memcpy( filename, path, len );
1883 filename[len++] = '\\';
1884 p = filename + len;
1885 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001886
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001887 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001888
Paul Bakker97872ac2012-11-02 12:53:26 +00001889 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001890 if (hFind == INVALID_HANDLE_VALUE)
1891 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1892
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001893 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001894 do
1895 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001896 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001897
Paul Bakkere4791f32012-06-04 21:29:15 +00001898 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001899 continue;
1900
Paul Bakker3338b792012-10-01 21:13:10 +00001901 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1902 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001903 p, len - 1,
1904 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001905
Paul Bakker3338b792012-10-01 21:13:10 +00001906 w_ret = x509parse_crtfile( chain, filename );
1907 if( w_ret < 0 )
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001908 {
1909 ret = w_ret;
1910 goto cleanup;
1911 }
Paul Bakker3338b792012-10-01 21:13:10 +00001912
1913 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001914 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001915 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001916
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001917 if (GetLastError() != ERROR_NO_MORE_FILES)
1918 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001919
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001920cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001921 FindClose( hFind );
1922#else
1923 int t_ret;
1924 struct dirent *entry;
1925 char entry_name[255];
1926 DIR *dir = opendir( path );
1927
1928 if( dir == NULL)
1929 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1930
1931 while( ( entry = readdir( dir ) ) != NULL )
1932 {
1933 if( entry->d_type != DT_REG )
1934 continue;
1935
1936 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1937 t_ret = x509parse_crtfile( chain, entry_name );
1938 if( t_ret < 0 )
Paul Bakker97872ac2012-11-02 12:53:26 +00001939 {
1940 ret = t_ret;
1941 break;
1942 }
Paul Bakker8d914582012-06-04 12:46:42 +00001943
1944 ret += t_ret;
1945 }
1946 closedir( dir );
1947#endif
1948
1949 return( ret );
1950}
1951
Paul Bakkerd98030e2009-05-02 15:13:40 +00001952/*
1953 * Load one or more CRLs and add them to the chained list
1954 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001955int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001956{
1957 int ret;
1958 size_t n;
1959 unsigned char *buf;
1960
Paul Bakker69e095c2011-12-10 21:55:01 +00001961 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1962 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001963
Paul Bakker27fdf462011-06-09 13:55:13 +00001964 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001965
1966 memset( buf, 0, n + 1 );
1967 free( buf );
1968
1969 return( ret );
1970}
1971
Paul Bakker5121ce52009-01-03 21:22:43 +00001972/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001973 * Load and parse a private RSA key
1974 */
1975int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1976{
1977 int ret;
1978 size_t n;
1979 unsigned char *buf;
1980
Paul Bakker69e095c2011-12-10 21:55:01 +00001981 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1982 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001983
1984 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001985 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001986 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001987 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001988 (unsigned char *) pwd, strlen( pwd ) );
1989
1990 memset( buf, 0, n + 1 );
1991 free( buf );
1992
1993 return( ret );
1994}
1995
1996/*
1997 * Load and parse a public RSA key
1998 */
1999int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2000{
2001 int ret;
2002 size_t n;
2003 unsigned char *buf;
2004
Paul Bakker69e095c2011-12-10 21:55:01 +00002005 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2006 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002007
Paul Bakker27fdf462011-06-09 13:55:13 +00002008 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002009
2010 memset( buf, 0, n + 1 );
2011 free( buf );
2012
2013 return( ret );
2014}
2015#endif /* POLARSSL_FS_IO */
2016
2017/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 * Parse a private RSA key
2019 */
Paul Bakker23986e52011-04-24 08:57:21 +00002020int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2021 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002022{
Paul Bakker23986e52011-04-24 08:57:21 +00002023 int ret;
2024 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002026 unsigned char *p_alt;
2027 x509_buf pk_alg_oid;
2028
Paul Bakker96743fc2011-02-12 14:30:57 +00002029#if defined(POLARSSL_PEM_C)
2030 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
Paul Bakker96743fc2011-02-12 14:30:57 +00002032 pem_init( &pem );
2033 ret = pem_read_buffer( &pem,
2034 "-----BEGIN RSA PRIVATE KEY-----",
2035 "-----END RSA PRIVATE KEY-----",
2036 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
Paul Bakker00b28602013-06-24 13:02:41 +02002038 if( ret == POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkered56b222011-07-13 11:26:43 +00002039 {
2040 ret = pem_read_buffer( &pem,
2041 "-----BEGIN PRIVATE KEY-----",
2042 "-----END PRIVATE KEY-----",
2043 key, pwd, pwdlen, &len );
2044 }
2045
Paul Bakker96743fc2011-02-12 14:30:57 +00002046 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002048 /*
2049 * Was PEM encoded
2050 */
2051 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 }
Paul Bakker00b28602013-06-24 13:02:41 +02002053 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002054 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002055 pem_free( &pem );
2056 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Paul Bakker96743fc2011-02-12 14:30:57 +00002059 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2060#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002061 ((void) pwd);
2062 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002063 p = (unsigned char *) key;
2064#endif
2065 end = p + keylen;
2066
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002068 * Note: Depending on the type of private key file one can expect either a
2069 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2070 *
2071 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002072 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002073 * algorithm AlgorithmIdentifier,
2074 * PrivateKey BIT STRING
2075 * }
2076 *
2077 * AlgorithmIdentifier ::= SEQUENCE {
2078 * algorithm OBJECT IDENTIFIER,
2079 * parameters ANY DEFINED BY algorithm OPTIONAL
2080 * }
2081 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 * RSAPrivateKey ::= SEQUENCE {
2083 * version Version,
2084 * modulus INTEGER, -- n
2085 * publicExponent INTEGER, -- e
2086 * privateExponent INTEGER, -- d
2087 * prime1 INTEGER, -- p
2088 * prime2 INTEGER, -- q
2089 * exponent1 INTEGER, -- d mod (p-1)
2090 * exponent2 INTEGER, -- d mod (q-1)
2091 * coefficient INTEGER, -- (inverse of q) mod p
2092 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2093 * }
2094 */
2095 if( ( ret = asn1_get_tag( &p, end, &len,
2096 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2097 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002098#if defined(POLARSSL_PEM_C)
2099 pem_free( &pem );
2100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002102 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 }
2104
2105 end = p + len;
2106
2107 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2108 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002109#if defined(POLARSSL_PEM_C)
2110 pem_free( &pem );
2111#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002113 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
2115
2116 if( rsa->ver != 0 )
2117 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002118#if defined(POLARSSL_PEM_C)
2119 pem_free( &pem );
2120#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002122 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 }
2124
Paul Bakkered56b222011-07-13 11:26:43 +00002125 p_alt = p;
2126
2127 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2128 {
2129 // Assume that we have the PKCS#1 format if wrong
2130 // tag was encountered
2131 //
2132 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2133 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2134 {
2135#if defined(POLARSSL_PEM_C)
2136 pem_free( &pem );
2137#endif
2138 rsa_free( rsa );
2139 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2140 }
2141 }
2142 else
2143 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002144 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkered56b222011-07-13 11:26:43 +00002145
2146 /*
2147 * only RSA keys handled at this time
2148 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002149 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +00002150 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2151
2152 /*
2153 * Parse the PKCS#8 format
2154 */
2155
2156 p = p_alt;
2157 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2158 {
2159#if defined(POLARSSL_PEM_C)
2160 pem_free( &pem );
2161#endif
2162 rsa_free( rsa );
2163 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2164 }
2165
2166 if( ( end - p ) < 1 )
2167 {
2168#if defined(POLARSSL_PEM_C)
2169 pem_free( &pem );
2170#endif
2171 rsa_free( rsa );
2172 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2173 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2174 }
2175
2176 end = p + len;
2177
2178 if( ( ret = asn1_get_tag( &p, end, &len,
2179 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2180 {
2181#if defined(POLARSSL_PEM_C)
2182 pem_free( &pem );
2183#endif
2184 rsa_free( rsa );
2185 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2186 }
2187
2188 end = p + len;
2189
2190 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2191 {
2192#if defined(POLARSSL_PEM_C)
2193 pem_free( &pem );
2194#endif
2195 rsa_free( rsa );
2196 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2197 }
2198
2199 if( rsa->ver != 0 )
2200 {
2201#if defined(POLARSSL_PEM_C)
2202 pem_free( &pem );
2203#endif
2204 rsa_free( rsa );
2205 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2206 }
2207 }
2208
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2210 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2211 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2212 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2213 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2214 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2215 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2216 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2217 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002218#if defined(POLARSSL_PEM_C)
2219 pem_free( &pem );
2220#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002222 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 }
2224
2225 rsa->len = mpi_size( &rsa->N );
2226
2227 if( p != end )
2228 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002229#if defined(POLARSSL_PEM_C)
2230 pem_free( &pem );
2231#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002233 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002234 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
2236
2237 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2238 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002239#if defined(POLARSSL_PEM_C)
2240 pem_free( &pem );
2241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 rsa_free( rsa );
2243 return( ret );
2244 }
2245
Paul Bakker96743fc2011-02-12 14:30:57 +00002246#if defined(POLARSSL_PEM_C)
2247 pem_free( &pem );
2248#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
2250 return( 0 );
2251}
2252
2253/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002254 * Parse a public RSA key
2255 */
Paul Bakker23986e52011-04-24 08:57:21 +00002256int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002257{
Paul Bakker23986e52011-04-24 08:57:21 +00002258 int ret;
2259 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002260 unsigned char *p, *end;
2261 x509_buf alg_oid;
2262#if defined(POLARSSL_PEM_C)
2263 pem_context pem;
2264
2265 pem_init( &pem );
2266 ret = pem_read_buffer( &pem,
2267 "-----BEGIN PUBLIC KEY-----",
2268 "-----END PUBLIC KEY-----",
2269 key, NULL, 0, &len );
2270
2271 if( ret == 0 )
2272 {
2273 /*
2274 * Was PEM encoded
2275 */
2276 keylen = pem.buflen;
2277 }
Paul Bakker00b28602013-06-24 13:02:41 +02002278 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002279 {
2280 pem_free( &pem );
2281 return( ret );
2282 }
2283
2284 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2285#else
2286 p = (unsigned char *) key;
2287#endif
2288 end = p + keylen;
2289
2290 /*
2291 * PublicKeyInfo ::= SEQUENCE {
2292 * algorithm AlgorithmIdentifier,
2293 * PublicKey BIT STRING
2294 * }
2295 *
2296 * AlgorithmIdentifier ::= SEQUENCE {
2297 * algorithm OBJECT IDENTIFIER,
2298 * parameters ANY DEFINED BY algorithm OPTIONAL
2299 * }
2300 *
2301 * RSAPublicKey ::= SEQUENCE {
2302 * modulus INTEGER, -- n
2303 * publicExponent INTEGER -- e
2304 * }
2305 */
2306
2307 if( ( ret = asn1_get_tag( &p, end, &len,
2308 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2309 {
2310#if defined(POLARSSL_PEM_C)
2311 pem_free( &pem );
2312#endif
2313 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002314 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002315 }
2316
2317 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2318 {
2319#if defined(POLARSSL_PEM_C)
2320 pem_free( &pem );
2321#endif
2322 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002323 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002324 }
2325
2326 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2327 {
2328#if defined(POLARSSL_PEM_C)
2329 pem_free( &pem );
2330#endif
2331 rsa_free( rsa );
2332 return( ret );
2333 }
2334
2335 rsa->len = mpi_size( &rsa->N );
2336
2337#if defined(POLARSSL_PEM_C)
2338 pem_free( &pem );
2339#endif
2340
2341 return( 0 );
2342}
2343
Paul Bakkereaa89f82011-04-04 21:36:15 +00002344#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002345/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002346 * Parse DHM parameters
2347 */
Paul Bakker23986e52011-04-24 08:57:21 +00002348int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002349{
Paul Bakker23986e52011-04-24 08:57:21 +00002350 int ret;
2351 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002352 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002353#if defined(POLARSSL_PEM_C)
2354 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002355
Paul Bakker96743fc2011-02-12 14:30:57 +00002356 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002357
Paul Bakker96743fc2011-02-12 14:30:57 +00002358 ret = pem_read_buffer( &pem,
2359 "-----BEGIN DH PARAMETERS-----",
2360 "-----END DH PARAMETERS-----",
2361 dhmin, NULL, 0, &dhminlen );
2362
2363 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002364 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002365 /*
2366 * Was PEM encoded
2367 */
2368 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002369 }
Paul Bakker00b28602013-06-24 13:02:41 +02002370 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002371 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002372 pem_free( &pem );
2373 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002374 }
2375
Paul Bakker96743fc2011-02-12 14:30:57 +00002376 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2377#else
2378 p = (unsigned char *) dhmin;
2379#endif
2380 end = p + dhminlen;
2381
Paul Bakker1b57b062011-01-06 15:48:19 +00002382 memset( dhm, 0, sizeof( dhm_context ) );
2383
Paul Bakker1b57b062011-01-06 15:48:19 +00002384 /*
2385 * DHParams ::= SEQUENCE {
2386 * prime INTEGER, -- P
2387 * generator INTEGER, -- g
2388 * }
2389 */
2390 if( ( ret = asn1_get_tag( &p, end, &len,
2391 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2392 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002393#if defined(POLARSSL_PEM_C)
2394 pem_free( &pem );
2395#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002396 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002397 }
2398
2399 end = p + len;
2400
2401 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2402 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2403 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002404#if defined(POLARSSL_PEM_C)
2405 pem_free( &pem );
2406#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002407 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002408 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002409 }
2410
2411 if( p != end )
2412 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002413#if defined(POLARSSL_PEM_C)
2414 pem_free( &pem );
2415#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002416 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002417 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002418 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2419 }
2420
Paul Bakker96743fc2011-02-12 14:30:57 +00002421#if defined(POLARSSL_PEM_C)
2422 pem_free( &pem );
2423#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002424
2425 return( 0 );
2426}
2427
Paul Bakker335db3f2011-04-25 15:28:35 +00002428#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002429/*
2430 * Load and parse a private RSA key
2431 */
2432int x509parse_dhmfile( dhm_context *dhm, const char *path )
2433{
2434 int ret;
2435 size_t n;
2436 unsigned char *buf;
2437
Paul Bakker69e095c2011-12-10 21:55:01 +00002438 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2439 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002440
Paul Bakker27fdf462011-06-09 13:55:13 +00002441 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002442
2443 memset( buf, 0, n + 1 );
2444 free( buf );
2445
2446 return( ret );
2447}
Paul Bakker335db3f2011-04-25 15:28:35 +00002448#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002449#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002450
Paul Bakker5121ce52009-01-03 21:22:43 +00002451#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002452#include <stdarg.h>
2453
2454#if !defined vsnprintf
2455#define vsnprintf _vsnprintf
2456#endif // vsnprintf
2457
2458/*
2459 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2460 * Result value is not size of buffer needed, but -1 if no fit is possible.
2461 *
2462 * This fuction tries to 'fix' this by at least suggesting enlarging the
2463 * size by 20.
2464 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002465static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002466{
2467 va_list ap;
2468 int res = -1;
2469
2470 va_start( ap, format );
2471
2472 res = vsnprintf( str, size, format, ap );
2473
2474 va_end( ap );
2475
2476 // No quick fix possible
2477 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002478 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002479
2480 return res;
2481}
2482
2483#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002484#endif
2485
Paul Bakkerd98030e2009-05-02 15:13:40 +00002486#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2487
2488#define SAFE_SNPRINTF() \
2489{ \
2490 if( ret == -1 ) \
2491 return( -1 ); \
2492 \
Paul Bakker23986e52011-04-24 08:57:21 +00002493 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002494 p[n - 1] = '\0'; \
2495 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2496 } \
2497 \
Paul Bakker23986e52011-04-24 08:57:21 +00002498 n -= (unsigned int) ret; \
2499 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002500}
2501
Paul Bakker5121ce52009-01-03 21:22:43 +00002502/*
2503 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002504 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002506int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002507{
Paul Bakker23986e52011-04-24 08:57:21 +00002508 int ret;
2509 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002511 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002512 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 char s[128], *p;
2514
2515 memset( s, 0, sizeof( s ) );
2516
2517 name = dn;
2518 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002519 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
2521 while( name != NULL )
2522 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002523 if( !name->oid.p )
2524 {
2525 name = name->next;
2526 continue;
2527 }
2528
Paul Bakker74111d32011-01-15 16:57:55 +00002529 if( name != dn )
2530 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002531 ret = snprintf( p, n, ", " );
2532 SAFE_SNPRINTF();
2533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Paul Bakkerc70b9822013-04-07 22:00:46 +02002535 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Paul Bakkerc70b9822013-04-07 22:00:46 +02002537 if( ret == 0 )
2538 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002540 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002541 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
2543 for( i = 0; i < name->val.len; i++ )
2544 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002545 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 break;
2547
2548 c = name->val.p[i];
2549 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2550 s[i] = '?';
2551 else s[i] = c;
2552 }
2553 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002554 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002555 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 name = name->next;
2557 }
2558
Paul Bakker23986e52011-04-24 08:57:21 +00002559 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560}
2561
2562/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002563 * Store the serial in printable form into buf; no more
2564 * than size characters will be written
2565 */
2566int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2567{
Paul Bakker23986e52011-04-24 08:57:21 +00002568 int ret;
2569 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002570 char *p;
2571
2572 p = buf;
2573 n = size;
2574
2575 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002576 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002577
2578 for( i = 0; i < nr; i++ )
2579 {
Paul Bakker93048802011-12-05 14:38:06 +00002580 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002581 continue;
2582
Paul Bakkerdd476992011-01-16 21:34:59 +00002583 ret = snprintf( p, n, "%02X%s",
2584 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2585 SAFE_SNPRINTF();
2586 }
2587
Paul Bakker03c7c252011-11-25 12:37:37 +00002588 if( nr != serial->len )
2589 {
2590 ret = snprintf( p, n, "...." );
2591 SAFE_SNPRINTF();
2592 }
2593
Paul Bakker23986e52011-04-24 08:57:21 +00002594 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002595}
2596
2597/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002598 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002600int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2601 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002602{
Paul Bakker23986e52011-04-24 08:57:21 +00002603 int ret;
2604 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002605 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002606 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
2608 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002609 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Paul Bakkerd98030e2009-05-02 15:13:40 +00002611 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002613 SAFE_SNPRINTF();
2614 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002616 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
Paul Bakkerdd476992011-01-16 21:34:59 +00002618 ret = x509parse_serial_gets( p, n, &crt->serial);
2619 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
Paul Bakkerd98030e2009-05-02 15:13:40 +00002621 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2622 SAFE_SNPRINTF();
2623 ret = x509parse_dn_gets( p, n, &crt->issuer );
2624 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Paul Bakkerd98030e2009-05-02 15:13:40 +00002626 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2627 SAFE_SNPRINTF();
2628 ret = x509parse_dn_gets( p, n, &crt->subject );
2629 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Paul Bakkerd98030e2009-05-02 15:13:40 +00002631 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2633 crt->valid_from.year, crt->valid_from.mon,
2634 crt->valid_from.day, crt->valid_from.hour,
2635 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002636 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Paul Bakkerd98030e2009-05-02 15:13:40 +00002638 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2640 crt->valid_to.year, crt->valid_to.mon,
2641 crt->valid_to.day, crt->valid_to.hour,
2642 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002643 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Paul Bakkerc70b9822013-04-07 22:00:46 +02002645 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002646 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Paul Bakkerc70b9822013-04-07 22:00:46 +02002648 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2649 if( ret != 0 )
2650 ret = snprintf( p, n, "???" );
2651 else
2652 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002653 SAFE_SNPRINTF();
2654
2655 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002656 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002657 SAFE_SNPRINTF();
2658
Paul Bakker23986e52011-04-24 08:57:21 +00002659 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002660}
2661
Paul Bakker74111d32011-01-15 16:57:55 +00002662/*
2663 * Return an informational string describing the given OID
2664 */
2665const char *x509_oid_get_description( x509_buf *oid )
2666{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002667 const char *desc = NULL;
2668 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002669
Paul Bakkerc70b9822013-04-07 22:00:46 +02002670 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002671
Paul Bakkerc70b9822013-04-07 22:00:46 +02002672 if( ret != 0 )
2673 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002674
Paul Bakkerc70b9822013-04-07 22:00:46 +02002675 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002676}
2677
2678/* Return the x.y.z.... style numeric string for the given OID */
2679int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2680{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002681 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002682}
2683
Paul Bakkerd98030e2009-05-02 15:13:40 +00002684/*
2685 * Return an informational string about the CRL.
2686 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002687int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2688 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002689{
Paul Bakker23986e52011-04-24 08:57:21 +00002690 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002691 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002692 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002693 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002694 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002695
2696 p = buf;
2697 n = size;
2698
2699 ret = snprintf( p, n, "%sCRL version : %d",
2700 prefix, crl->version );
2701 SAFE_SNPRINTF();
2702
2703 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2704 SAFE_SNPRINTF();
2705 ret = x509parse_dn_gets( p, n, &crl->issuer );
2706 SAFE_SNPRINTF();
2707
2708 ret = snprintf( p, n, "\n%sthis update : " \
2709 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2710 crl->this_update.year, crl->this_update.mon,
2711 crl->this_update.day, crl->this_update.hour,
2712 crl->this_update.min, crl->this_update.sec );
2713 SAFE_SNPRINTF();
2714
2715 ret = snprintf( p, n, "\n%snext update : " \
2716 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2717 crl->next_update.year, crl->next_update.mon,
2718 crl->next_update.day, crl->next_update.hour,
2719 crl->next_update.min, crl->next_update.sec );
2720 SAFE_SNPRINTF();
2721
2722 entry = &crl->entry;
2723
2724 ret = snprintf( p, n, "\n%sRevoked certificates:",
2725 prefix );
2726 SAFE_SNPRINTF();
2727
Paul Bakker9be19372009-07-27 20:21:53 +00002728 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002729 {
2730 ret = snprintf( p, n, "\n%sserial number: ",
2731 prefix );
2732 SAFE_SNPRINTF();
2733
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002734 ret = x509parse_serial_gets( p, n, &entry->serial);
2735 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002736
Paul Bakkerd98030e2009-05-02 15:13:40 +00002737 ret = snprintf( p, n, " revocation date: " \
2738 "%04d-%02d-%02d %02d:%02d:%02d",
2739 entry->revocation_date.year, entry->revocation_date.mon,
2740 entry->revocation_date.day, entry->revocation_date.hour,
2741 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002742 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002743
2744 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 }
2746
Paul Bakkerc70b9822013-04-07 22:00:46 +02002747 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002748 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Paul Bakkerc70b9822013-04-07 22:00:46 +02002750 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2751 if( ret != 0 )
2752 ret = snprintf( p, n, "???" );
2753 else
2754 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002755 SAFE_SNPRINTF();
2756
Paul Bakker1e27bb22009-07-19 20:25:25 +00002757 ret = snprintf( p, n, "\n" );
2758 SAFE_SNPRINTF();
2759
Paul Bakker23986e52011-04-24 08:57:21 +00002760 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761}
2762
2763/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002764 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002766int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002767{
Paul Bakkercce9d772011-11-18 14:26:47 +00002768 int year, mon, day;
2769 int hour, min, sec;
2770
2771#if defined(_WIN32)
2772 SYSTEMTIME st;
2773
2774 GetLocalTime(&st);
2775
2776 year = st.wYear;
2777 mon = st.wMonth;
2778 day = st.wDay;
2779 hour = st.wHour;
2780 min = st.wMinute;
2781 sec = st.wSecond;
2782#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002783 struct tm *lt;
2784 time_t tt;
2785
2786 tt = time( NULL );
2787 lt = localtime( &tt );
2788
Paul Bakkercce9d772011-11-18 14:26:47 +00002789 year = lt->tm_year + 1900;
2790 mon = lt->tm_mon + 1;
2791 day = lt->tm_mday;
2792 hour = lt->tm_hour;
2793 min = lt->tm_min;
2794 sec = lt->tm_sec;
2795#endif
2796
2797 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002798 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
Paul Bakkercce9d772011-11-18 14:26:47 +00002800 if( year == to->year &&
2801 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002802 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Paul Bakkercce9d772011-11-18 14:26:47 +00002804 if( year == to->year &&
2805 mon == to->mon &&
2806 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002807 return( 1 );
2808
Paul Bakkercce9d772011-11-18 14:26:47 +00002809 if( year == to->year &&
2810 mon == to->mon &&
2811 day == to->day &&
2812 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002813 return( 1 );
2814
Paul Bakkercce9d772011-11-18 14:26:47 +00002815 if( year == to->year &&
2816 mon == to->mon &&
2817 day == to->day &&
2818 hour == to->hour &&
2819 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002820 return( 1 );
2821
Paul Bakkercce9d772011-11-18 14:26:47 +00002822 if( year == to->year &&
2823 mon == to->mon &&
2824 day == to->day &&
2825 hour == to->hour &&
2826 min == to->min &&
2827 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002828 return( 1 );
2829
Paul Bakker40ea7de2009-05-03 10:18:48 +00002830 return( 0 );
2831}
2832
2833/*
2834 * Return 1 if the certificate is revoked, or 0 otherwise.
2835 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002836int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002837{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002838 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002839
2840 while( cur != NULL && cur->serial.len != 0 )
2841 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002842 if( crt->serial.len == cur->serial.len &&
2843 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002844 {
2845 if( x509parse_time_expired( &cur->revocation_date ) )
2846 return( 1 );
2847 }
2848
2849 cur = cur->next;
2850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
2852 return( 0 );
2853}
2854
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002855/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002856 * Check that the given certificate is valid accoring to the CRL.
2857 */
2858static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2859 x509_crl *crl_list)
2860{
2861 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002862 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2863 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002864
Paul Bakker915275b2012-09-28 07:10:55 +00002865 if( ca == NULL )
2866 return( flags );
2867
Paul Bakker76fd75a2011-01-16 21:12:10 +00002868 /*
2869 * TODO: What happens if no CRL is present?
2870 * Suggestion: Revocation state should be unknown if no CRL is present.
2871 * For backwards compatibility this is not yet implemented.
2872 */
2873
Paul Bakker915275b2012-09-28 07:10:55 +00002874 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002875 {
Paul Bakker915275b2012-09-28 07:10:55 +00002876 if( crl_list->version == 0 ||
2877 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00002878 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2879 crl_list->issuer_raw.len ) != 0 )
2880 {
2881 crl_list = crl_list->next;
2882 continue;
2883 }
2884
2885 /*
2886 * Check if CRL is correctly signed by the trusted CA
2887 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002888 md_info = md_info_from_type( crl_list->sig_md );
2889 if( md_info == NULL )
2890 {
2891 /*
2892 * Cannot check 'unknown' hash
2893 */
2894 flags |= BADCRL_NOT_TRUSTED;
2895 break;
2896 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002897
Paul Bakkerc70b9822013-04-07 22:00:46 +02002898 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002899
Paul Bakkerc70b9822013-04-07 22:00:46 +02002900 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00002901 0, hash, crl_list->sig.p ) == 0 )
2902 {
2903 /*
2904 * CRL is not trusted
2905 */
2906 flags |= BADCRL_NOT_TRUSTED;
2907 break;
2908 }
2909
2910 /*
2911 * Check for validity of CRL (Do not drop out)
2912 */
2913 if( x509parse_time_expired( &crl_list->next_update ) )
2914 flags |= BADCRL_EXPIRED;
2915
2916 /*
2917 * Check if certificate is revoked
2918 */
2919 if( x509parse_revoked(crt, crl_list) )
2920 {
2921 flags |= BADCERT_REVOKED;
2922 break;
2923 }
2924
2925 crl_list = crl_list->next;
2926 }
2927 return flags;
2928}
2929
Paul Bakker57b12982012-02-11 17:38:38 +00002930int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002931{
2932 size_t i;
2933 size_t cn_idx = 0;
2934
Paul Bakker57b12982012-02-11 17:38:38 +00002935 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002936 return( 0 );
2937
2938 for( i = 0; i < strlen( cn ); ++i )
2939 {
2940 if( cn[i] == '.' )
2941 {
2942 cn_idx = i;
2943 break;
2944 }
2945 }
2946
2947 if( cn_idx == 0 )
2948 return( 0 );
2949
Paul Bakker535e97d2012-08-23 10:49:55 +00002950 if( strlen( cn ) - cn_idx == name->len - 1 &&
2951 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002952 {
2953 return( 1 );
2954 }
2955
2956 return( 0 );
2957}
2958
Paul Bakker915275b2012-09-28 07:10:55 +00002959static int x509parse_verify_top(
2960 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00002961 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00002962 int (*f_vrfy)(void *, x509_cert *, int, int *),
2963 void *p_vrfy )
2964{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002965 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00002966 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002967 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2968 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00002969
2970 if( x509parse_time_expired( &child->valid_to ) )
2971 *flags |= BADCERT_EXPIRED;
2972
2973 /*
2974 * Child is the top of the chain. Check against the trust_ca list.
2975 */
2976 *flags |= BADCERT_NOT_TRUSTED;
2977
2978 while( trust_ca != NULL )
2979 {
2980 if( trust_ca->version == 0 ||
2981 child->issuer_raw.len != trust_ca->subject_raw.len ||
2982 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
2983 child->issuer_raw.len ) != 0 )
2984 {
2985 trust_ca = trust_ca->next;
2986 continue;
2987 }
2988
Paul Bakker9a736322012-11-14 12:39:52 +00002989 /*
2990 * Reduce path_len to check against if top of the chain is
2991 * the same as the trusted CA
2992 */
2993 if( child->subject_raw.len == trust_ca->subject_raw.len &&
2994 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
2995 child->issuer_raw.len ) == 0 )
2996 {
2997 check_path_cnt--;
2998 }
2999
Paul Bakker915275b2012-09-28 07:10:55 +00003000 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003001 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003002 {
3003 trust_ca = trust_ca->next;
3004 continue;
3005 }
3006
Paul Bakkerc70b9822013-04-07 22:00:46 +02003007 md_info = md_info_from_type( child->sig_md );
3008 if( md_info == NULL )
3009 {
3010 /*
3011 * Cannot check 'unknown' hash
3012 */
3013 continue;
3014 }
Paul Bakker915275b2012-09-28 07:10:55 +00003015
Paul Bakkerc70b9822013-04-07 22:00:46 +02003016 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003017
Paul Bakkerc70b9822013-04-07 22:00:46 +02003018 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003019 0, hash, child->sig.p ) != 0 )
3020 {
3021 trust_ca = trust_ca->next;
3022 continue;
3023 }
3024
3025 /*
3026 * Top of chain is signed by a trusted CA
3027 */
3028 *flags &= ~BADCERT_NOT_TRUSTED;
3029 break;
3030 }
3031
Paul Bakker9a736322012-11-14 12:39:52 +00003032 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003033 * If top of chain is not the same as the trusted CA send a verify request
3034 * to the callback for any issues with validity and CRL presence for the
3035 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003036 */
3037 if( trust_ca != NULL &&
3038 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3039 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3040 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003041 {
3042 /* Check trusted CA's CRL for then chain's top crt */
3043 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3044
3045 if( x509parse_time_expired( &trust_ca->valid_to ) )
3046 ca_flags |= BADCERT_EXPIRED;
3047
Paul Bakker915275b2012-09-28 07:10:55 +00003048 if( NULL != f_vrfy )
3049 {
Paul Bakker9a736322012-11-14 12:39:52 +00003050 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003051 return( ret );
3052 }
3053 }
3054
3055 /* Call callback on top cert */
3056 if( NULL != f_vrfy )
3057 {
Paul Bakker9a736322012-11-14 12:39:52 +00003058 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003059 return( ret );
3060 }
3061
Paul Bakker915275b2012-09-28 07:10:55 +00003062 *flags |= ca_flags;
3063
3064 return( 0 );
3065}
3066
3067static int x509parse_verify_child(
3068 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003069 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003070 int (*f_vrfy)(void *, x509_cert *, int, int *),
3071 void *p_vrfy )
3072{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003073 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003074 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003075 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003076 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003077 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003078
3079 if( x509parse_time_expired( &child->valid_to ) )
3080 *flags |= BADCERT_EXPIRED;
3081
Paul Bakkerc70b9822013-04-07 22:00:46 +02003082 md_info = md_info_from_type( child->sig_md );
3083 if( md_info == NULL )
3084 {
3085 /*
3086 * Cannot check 'unknown' hash
3087 */
Paul Bakker915275b2012-09-28 07:10:55 +00003088 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003089 }
3090 else
3091 {
3092 md( md_info, child->tbs.p, child->tbs.len, hash );
3093
3094 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3095 child->sig.p ) != 0 )
3096 *flags |= BADCERT_NOT_TRUSTED;
3097 }
3098
Paul Bakker915275b2012-09-28 07:10:55 +00003099 /* Check trusted CA's CRL for the given crt */
3100 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3101
3102 grandparent = parent->next;
3103
3104 while( grandparent != NULL )
3105 {
3106 if( grandparent->version == 0 ||
3107 grandparent->ca_istrue == 0 ||
3108 parent->issuer_raw.len != grandparent->subject_raw.len ||
3109 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3110 parent->issuer_raw.len ) != 0 )
3111 {
3112 grandparent = grandparent->next;
3113 continue;
3114 }
3115 break;
3116 }
3117
Paul Bakker915275b2012-09-28 07:10:55 +00003118 if( grandparent != NULL )
3119 {
3120 /*
3121 * Part of the chain
3122 */
Paul Bakker9a736322012-11-14 12:39:52 +00003123 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 +00003124 if( ret != 0 )
3125 return( ret );
3126 }
3127 else
3128 {
Paul Bakker9a736322012-11-14 12:39:52 +00003129 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 +00003130 if( ret != 0 )
3131 return( ret );
3132 }
3133
3134 /* child is verified to be a child of the parent, call verify callback */
3135 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003136 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003137 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003138
3139 *flags |= parent_flags;
3140
3141 return( 0 );
3142}
3143
Paul Bakker76fd75a2011-01-16 21:12:10 +00003144/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 * Verify the certificate validity
3146 */
3147int x509parse_verify( x509_cert *crt,
3148 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003149 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003150 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003151 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003152 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003153{
Paul Bakker23986e52011-04-24 08:57:21 +00003154 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003155 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003156 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003157 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003158 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003159 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
Paul Bakker40ea7de2009-05-03 10:18:48 +00003161 *flags = 0;
3162
Paul Bakker5121ce52009-01-03 21:22:43 +00003163 if( cn != NULL )
3164 {
3165 name = &crt->subject;
3166 cn_len = strlen( cn );
3167
Paul Bakker4d2c1242012-05-10 14:12:46 +00003168 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003169 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003170 cur = &crt->subject_alt_names;
3171
3172 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003173 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003174 if( cur->buf.len == cn_len &&
3175 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003176 break;
3177
Paul Bakker535e97d2012-08-23 10:49:55 +00003178 if( cur->buf.len > 2 &&
3179 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003180 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003181 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Paul Bakker4d2c1242012-05-10 14:12:46 +00003183 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003184 }
3185
3186 if( cur == NULL )
3187 *flags |= BADCERT_CN_MISMATCH;
3188 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003189 else
3190 {
3191 while( name != NULL )
3192 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003193 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003194 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003195 if( name->val.len == cn_len &&
3196 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003197 break;
3198
Paul Bakker535e97d2012-08-23 10:49:55 +00003199 if( name->val.len > 2 &&
3200 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003201 x509_wildcard_verify( cn, &name->val ) )
3202 break;
3203 }
3204
3205 name = name->next;
3206 }
3207
3208 if( name == NULL )
3209 *flags |= BADCERT_CN_MISMATCH;
3210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 }
3212
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003214 * Iterate upwards in the given cert chain, to find our crt parent.
3215 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003217 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Paul Bakker76fd75a2011-01-16 21:12:10 +00003219 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003221 if( parent->ca_istrue == 0 ||
3222 crt->issuer_raw.len != parent->subject_raw.len ||
3223 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 crt->issuer_raw.len ) != 0 )
3225 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003226 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 continue;
3228 }
Paul Bakker915275b2012-09-28 07:10:55 +00003229 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003230 }
3231
Paul Bakker915275b2012-09-28 07:10:55 +00003232 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 {
Paul Bakker915275b2012-09-28 07:10:55 +00003234 /*
3235 * Part of the chain
3236 */
Paul Bakker9a736322012-11-14 12:39:52 +00003237 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003238 if( ret != 0 )
3239 return( ret );
3240 }
3241 else
Paul Bakker74111d32011-01-15 16:57:55 +00003242 {
Paul Bakker9a736322012-11-14 12:39:52 +00003243 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003244 if( ret != 0 )
3245 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003246 }
Paul Bakker915275b2012-09-28 07:10:55 +00003247
3248 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003249 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003250
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 return( 0 );
3252}
3253
3254/*
3255 * Unallocate all certificate data
3256 */
3257void x509_free( x509_cert *crt )
3258{
3259 x509_cert *cert_cur = crt;
3260 x509_cert *cert_prv;
3261 x509_name *name_cur;
3262 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003263 x509_sequence *seq_cur;
3264 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003265
3266 if( crt == NULL )
3267 return;
3268
3269 do
3270 {
3271 rsa_free( &cert_cur->rsa );
3272
3273 name_cur = cert_cur->issuer.next;
3274 while( name_cur != NULL )
3275 {
3276 name_prv = name_cur;
3277 name_cur = name_cur->next;
3278 memset( name_prv, 0, sizeof( x509_name ) );
3279 free( name_prv );
3280 }
3281
3282 name_cur = cert_cur->subject.next;
3283 while( name_cur != NULL )
3284 {
3285 name_prv = name_cur;
3286 name_cur = name_cur->next;
3287 memset( name_prv, 0, sizeof( x509_name ) );
3288 free( name_prv );
3289 }
3290
Paul Bakker74111d32011-01-15 16:57:55 +00003291 seq_cur = cert_cur->ext_key_usage.next;
3292 while( seq_cur != NULL )
3293 {
3294 seq_prv = seq_cur;
3295 seq_cur = seq_cur->next;
3296 memset( seq_prv, 0, sizeof( x509_sequence ) );
3297 free( seq_prv );
3298 }
3299
Paul Bakker8afa70d2012-02-11 18:42:45 +00003300 seq_cur = cert_cur->subject_alt_names.next;
3301 while( seq_cur != NULL )
3302 {
3303 seq_prv = seq_cur;
3304 seq_cur = seq_cur->next;
3305 memset( seq_prv, 0, sizeof( x509_sequence ) );
3306 free( seq_prv );
3307 }
3308
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 if( cert_cur->raw.p != NULL )
3310 {
3311 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3312 free( cert_cur->raw.p );
3313 }
3314
3315 cert_cur = cert_cur->next;
3316 }
3317 while( cert_cur != NULL );
3318
3319 cert_cur = crt;
3320 do
3321 {
3322 cert_prv = cert_cur;
3323 cert_cur = cert_cur->next;
3324
3325 memset( cert_prv, 0, sizeof( x509_cert ) );
3326 if( cert_prv != crt )
3327 free( cert_prv );
3328 }
3329 while( cert_cur != NULL );
3330}
3331
Paul Bakkerd98030e2009-05-02 15:13:40 +00003332/*
3333 * Unallocate all CRL data
3334 */
3335void x509_crl_free( x509_crl *crl )
3336{
3337 x509_crl *crl_cur = crl;
3338 x509_crl *crl_prv;
3339 x509_name *name_cur;
3340 x509_name *name_prv;
3341 x509_crl_entry *entry_cur;
3342 x509_crl_entry *entry_prv;
3343
3344 if( crl == NULL )
3345 return;
3346
3347 do
3348 {
3349 name_cur = crl_cur->issuer.next;
3350 while( name_cur != NULL )
3351 {
3352 name_prv = name_cur;
3353 name_cur = name_cur->next;
3354 memset( name_prv, 0, sizeof( x509_name ) );
3355 free( name_prv );
3356 }
3357
3358 entry_cur = crl_cur->entry.next;
3359 while( entry_cur != NULL )
3360 {
3361 entry_prv = entry_cur;
3362 entry_cur = entry_cur->next;
3363 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3364 free( entry_prv );
3365 }
3366
3367 if( crl_cur->raw.p != NULL )
3368 {
3369 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3370 free( crl_cur->raw.p );
3371 }
3372
3373 crl_cur = crl_cur->next;
3374 }
3375 while( crl_cur != NULL );
3376
3377 crl_cur = crl;
3378 do
3379 {
3380 crl_prv = crl_cur;
3381 crl_cur = crl_cur->next;
3382
3383 memset( crl_prv, 0, sizeof( x509_crl ) );
3384 if( crl_prv != crl )
3385 free( crl_prv );
3386 }
3387 while( crl_cur != NULL );
3388}
3389
Paul Bakker40e46942009-01-03 21:51:57 +00003390#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
Paul Bakker40e46942009-01-03 21:51:57 +00003392#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003393
3394/*
3395 * Checkup routine
3396 */
3397int x509_self_test( int verbose )
3398{
Paul Bakker5690efc2011-05-26 13:16:06 +00003399#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003400 int ret;
3401 int flags;
3402 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 x509_cert cacert;
3404 x509_cert clicert;
3405 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003406#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003407 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003408#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003409
3410 if( verbose != 0 )
3411 printf( " X.509 certificate load: " );
3412
3413 memset( &clicert, 0, sizeof( x509_cert ) );
3414
Paul Bakker3c2122f2013-06-24 19:03:14 +02003415 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003416 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 if( ret != 0 )
3418 {
3419 if( verbose != 0 )
3420 printf( "failed\n" );
3421
3422 return( ret );
3423 }
3424
3425 memset( &cacert, 0, sizeof( x509_cert ) );
3426
Paul Bakker3c2122f2013-06-24 19:03:14 +02003427 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003428 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 if( ret != 0 )
3430 {
3431 if( verbose != 0 )
3432 printf( "failed\n" );
3433
3434 return( ret );
3435 }
3436
3437 if( verbose != 0 )
3438 printf( "passed\n X.509 private key load: " );
3439
3440 i = strlen( test_ca_key );
3441 j = strlen( test_ca_pwd );
3442
Paul Bakker66b78b22011-03-25 14:22:50 +00003443 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3444
Paul Bakker5121ce52009-01-03 21:22:43 +00003445 if( ( ret = x509parse_key( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003446 (const unsigned char *) test_ca_key, i,
3447 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 {
3449 if( verbose != 0 )
3450 printf( "failed\n" );
3451
3452 return( ret );
3453 }
3454
3455 if( verbose != 0 )
3456 printf( "passed\n X.509 signature verify: ");
3457
Paul Bakker23986e52011-04-24 08:57:21 +00003458 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 if( ret != 0 )
3460 {
Paul Bakker23986e52011-04-24 08:57:21 +00003461 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 if( verbose != 0 )
3463 printf( "failed\n" );
3464
3465 return( ret );
3466 }
3467
Paul Bakker5690efc2011-05-26 13:16:06 +00003468#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003470 printf( "passed\n X.509 DHM parameter load: " );
3471
3472 i = strlen( test_dhm_params );
3473 j = strlen( test_ca_pwd );
3474
Paul Bakker3c2122f2013-06-24 19:03:14 +02003475 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003476 {
3477 if( verbose != 0 )
3478 printf( "failed\n" );
3479
3480 return( ret );
3481 }
3482
3483 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003485#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003486
3487 x509_free( &cacert );
3488 x509_free( &clicert );
3489 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003490#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003491 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003492#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003493
3494 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003495#else
3496 ((void) verbose);
3497 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3498#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003499}
3500
3501#endif
3502
3503#endif