blob: 3ddfb14c6b7536766364c0970e316d0650256545 [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)
1403 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1404 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 }
1433 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1434 {
1435 pem_free( &pem );
1436
1437 if( first_error == 0 )
1438 first_error = ret;
1439
1440 continue;
1441 }
1442 else
1443 break;
1444
1445 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1446
1447 pem_free( &pem );
1448
1449 if( ret != 0 )
1450 {
1451 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001452 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001453 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001454 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001455 {
1456 if( prev )
1457 prev->next = NULL;
1458
1459 if( crt != chain )
1460 free( crt );
1461
1462 return( ret );
1463 }
1464
1465 if( first_error == 0 )
1466 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001467
1468 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001469
1470 memset( crt, 0, sizeof( x509_cert ) );
1471 continue;
1472 }
1473
1474 success = 1;
1475
1476 /*
1477 * Add new certificate to the list
1478 */
1479 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1480
1481 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001482 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001483
1484 prev = crt;
1485 crt = crt->next;
1486 memset( crt, 0, sizeof( x509_cert ) );
1487 }
1488 }
1489#endif
1490
1491 if( crt->version == 0 )
1492 {
1493 if( prev )
1494 prev->next = NULL;
1495
1496 if( crt != chain )
1497 free( crt );
1498 }
1499
1500 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001501 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001502 else if( first_error )
1503 return( first_error );
1504 else
1505 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001506}
1507
1508/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001509 * Parse one or more CRLs and add them to the chained list
1510 */
Paul Bakker23986e52011-04-24 08:57:21 +00001511int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001512{
Paul Bakker23986e52011-04-24 08:57:21 +00001513 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001514 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001515 unsigned char *p, *end;
1516 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001517#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001518 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001519 pem_context pem;
1520#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001521
1522 crl = chain;
1523
1524 /*
1525 * Check for valid input
1526 */
1527 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001528 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001529
1530 while( crl->version != 0 && crl->next != NULL )
1531 crl = crl->next;
1532
1533 /*
1534 * Add new CRL on the end of the chain if needed.
1535 */
1536 if ( crl->version != 0 && crl->next == NULL)
1537 {
1538 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1539
Paul Bakker7d06ad22009-05-02 15:53:56 +00001540 if( crl->next == NULL )
1541 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001542 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001543 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001544 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001545
Paul Bakker7d06ad22009-05-02 15:53:56 +00001546 crl = crl->next;
1547 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001548 }
1549
Paul Bakker96743fc2011-02-12 14:30:57 +00001550#if defined(POLARSSL_PEM_C)
1551 pem_init( &pem );
1552 ret = pem_read_buffer( &pem,
1553 "-----BEGIN X509 CRL-----",
1554 "-----END X509 CRL-----",
1555 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001556
Paul Bakker96743fc2011-02-12 14:30:57 +00001557 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001558 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001559 /*
1560 * Was PEM encoded
1561 */
1562 buflen -= use_len;
1563 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001564
1565 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001566 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001567 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001568 p = pem.buf;
1569 pem.buf = NULL;
1570 len = pem.buflen;
1571 pem_free( &pem );
1572 }
1573 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1574 {
1575 pem_free( &pem );
1576 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001577 }
1578 else
1579 {
1580 /*
1581 * nope, copy the raw DER data
1582 */
1583 p = (unsigned char *) malloc( len = buflen );
1584
1585 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001586 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587
1588 memcpy( p, buf, buflen );
1589
1590 buflen = 0;
1591 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001592#else
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 Bakker96743fc2011-02-12 14:30:57 +00001597
1598 memcpy( p, buf, buflen );
1599
1600 buflen = 0;
1601#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001602
1603 crl->raw.p = p;
1604 crl->raw.len = len;
1605 end = p + len;
1606
1607 /*
1608 * CertificateList ::= SEQUENCE {
1609 * tbsCertList TBSCertList,
1610 * signatureAlgorithm AlgorithmIdentifier,
1611 * signatureValue BIT STRING }
1612 */
1613 if( ( ret = asn1_get_tag( &p, end, &len,
1614 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1615 {
1616 x509_crl_free( crl );
1617 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1618 }
1619
Paul Bakker23986e52011-04-24 08:57:21 +00001620 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001621 {
1622 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001623 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001624 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1625 }
1626
1627 /*
1628 * TBSCertList ::= SEQUENCE {
1629 */
1630 crl->tbs.p = p;
1631
1632 if( ( ret = asn1_get_tag( &p, end, &len,
1633 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1634 {
1635 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001636 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001637 }
1638
1639 end = p + len;
1640 crl->tbs.len = end - crl->tbs.p;
1641
1642 /*
1643 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1644 * -- if present, MUST be v2
1645 *
1646 * signature AlgorithmIdentifier
1647 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001648 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001649 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1650 {
1651 x509_crl_free( crl );
1652 return( ret );
1653 }
1654
1655 crl->version++;
1656
1657 if( crl->version > 2 )
1658 {
1659 x509_crl_free( crl );
1660 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1661 }
1662
Paul Bakkerc70b9822013-04-07 22:00:46 +02001663 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1664 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001665 {
1666 x509_crl_free( crl );
1667 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1668 }
1669
1670 /*
1671 * issuer Name
1672 */
1673 crl->issuer_raw.p = p;
1674
1675 if( ( ret = asn1_get_tag( &p, end, &len,
1676 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1677 {
1678 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001679 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001680 }
1681
1682 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1683 {
1684 x509_crl_free( crl );
1685 return( ret );
1686 }
1687
1688 crl->issuer_raw.len = p - crl->issuer_raw.p;
1689
1690 /*
1691 * thisUpdate Time
1692 * nextUpdate Time OPTIONAL
1693 */
Paul Bakker91200182010-02-18 21:26:15 +00001694 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001695 {
1696 x509_crl_free( crl );
1697 return( ret );
1698 }
1699
Paul Bakker91200182010-02-18 21:26:15 +00001700 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001701 {
Paul Bakker9d781402011-05-09 16:17:09 +00001702 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001703 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001704 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001705 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001706 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001707 x509_crl_free( crl );
1708 return( ret );
1709 }
1710 }
1711
1712 /*
1713 * revokedCertificates SEQUENCE OF SEQUENCE {
1714 * userCertificate CertificateSerialNumber,
1715 * revocationDate Time,
1716 * crlEntryExtensions Extensions OPTIONAL
1717 * -- if present, MUST be v2
1718 * } OPTIONAL
1719 */
1720 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1721 {
1722 x509_crl_free( crl );
1723 return( ret );
1724 }
1725
1726 /*
1727 * crlExtensions EXPLICIT Extensions OPTIONAL
1728 * -- if present, MUST be v2
1729 */
1730 if( crl->version == 2 )
1731 {
1732 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1733
1734 if( ret != 0 )
1735 {
1736 x509_crl_free( crl );
1737 return( ret );
1738 }
1739 }
1740
1741 if( p != end )
1742 {
1743 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001744 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001745 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1746 }
1747
1748 end = crl->raw.p + crl->raw.len;
1749
1750 /*
1751 * signatureAlgorithm AlgorithmIdentifier,
1752 * signatureValue BIT STRING
1753 */
1754 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1755 {
1756 x509_crl_free( crl );
1757 return( ret );
1758 }
1759
Paul Bakker535e97d2012-08-23 10:49:55 +00001760 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1761 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001762 {
1763 x509_crl_free( crl );
1764 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1765 }
1766
1767 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1768 {
1769 x509_crl_free( crl );
1770 return( ret );
1771 }
1772
1773 if( p != end )
1774 {
1775 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001776 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001777 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1778 }
1779
1780 if( buflen > 0 )
1781 {
1782 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1783
Paul Bakker7d06ad22009-05-02 15:53:56 +00001784 if( crl->next == NULL )
1785 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001786 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001787 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001788 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001789
Paul Bakker7d06ad22009-05-02 15:53:56 +00001790 crl = crl->next;
1791 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001792
1793 return( x509parse_crl( crl, buf, buflen ) );
1794 }
1795
1796 return( 0 );
1797}
1798
Paul Bakker335db3f2011-04-25 15:28:35 +00001799#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001800/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001801 * Load all data from a file into a given buffer.
1802 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001803int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001804{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001805 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001806
Paul Bakkerd98030e2009-05-02 15:13:40 +00001807 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001808 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001809
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810 fseek( f, 0, SEEK_END );
1811 *n = (size_t) ftell( f );
1812 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001813
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001815 {
1816 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001817 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001818 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001819
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820 if( fread( *buf, 1, *n, f ) != *n )
1821 {
1822 fclose( f );
1823 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001824 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001825 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001826
Paul Bakkerd98030e2009-05-02 15:13:40 +00001827 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001828
Paul Bakkerd98030e2009-05-02 15:13:40 +00001829 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001830
Paul Bakkerd98030e2009-05-02 15:13:40 +00001831 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001832}
1833
1834/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 * Load one or more certificates and add them to the chained list
1836 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001837int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001838{
1839 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001840 size_t n;
1841 unsigned char *buf;
1842
Paul Bakker69e095c2011-12-10 21:55:01 +00001843 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1844 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Paul Bakker69e095c2011-12-10 21:55:01 +00001846 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
1848 memset( buf, 0, n + 1 );
1849 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
1851 return( ret );
1852}
1853
Paul Bakker8d914582012-06-04 12:46:42 +00001854int x509parse_crtpath( x509_cert *chain, const char *path )
1855{
1856 int ret = 0;
1857#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001858 int w_ret;
1859 WCHAR szDir[MAX_PATH];
1860 char filename[MAX_PATH];
1861 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001862 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001863
Paul Bakker97872ac2012-11-02 12:53:26 +00001864 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001865 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001866
1867 if( len > MAX_PATH - 3 )
1868 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001869
Paul Bakker3338b792012-10-01 21:13:10 +00001870 memset( szDir, 0, sizeof(szDir) );
1871 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001872 memcpy( filename, path, len );
1873 filename[len++] = '\\';
1874 p = filename + len;
1875 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001876
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001877 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001878
Paul Bakker97872ac2012-11-02 12:53:26 +00001879 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001880 if (hFind == INVALID_HANDLE_VALUE)
1881 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1882
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001883 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001884 do
1885 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001886 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001887
Paul Bakkere4791f32012-06-04 21:29:15 +00001888 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001889 continue;
1890
Paul Bakker3338b792012-10-01 21:13:10 +00001891 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1892 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001893 p, len - 1,
1894 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001895
Paul Bakker3338b792012-10-01 21:13:10 +00001896 w_ret = x509parse_crtfile( chain, filename );
1897 if( w_ret < 0 )
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001898 {
1899 ret = w_ret;
1900 goto cleanup;
1901 }
Paul Bakker3338b792012-10-01 21:13:10 +00001902
1903 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001904 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001905 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001906
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001907 if (GetLastError() != ERROR_NO_MORE_FILES)
1908 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001909
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001910cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001911 FindClose( hFind );
1912#else
1913 int t_ret;
1914 struct dirent *entry;
1915 char entry_name[255];
1916 DIR *dir = opendir( path );
1917
1918 if( dir == NULL)
1919 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1920
1921 while( ( entry = readdir( dir ) ) != NULL )
1922 {
1923 if( entry->d_type != DT_REG )
1924 continue;
1925
1926 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1927 t_ret = x509parse_crtfile( chain, entry_name );
1928 if( t_ret < 0 )
Paul Bakker97872ac2012-11-02 12:53:26 +00001929 {
1930 ret = t_ret;
1931 break;
1932 }
Paul Bakker8d914582012-06-04 12:46:42 +00001933
1934 ret += t_ret;
1935 }
1936 closedir( dir );
1937#endif
1938
1939 return( ret );
1940}
1941
Paul Bakkerd98030e2009-05-02 15:13:40 +00001942/*
1943 * Load one or more CRLs and add them to the chained list
1944 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001945int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001946{
1947 int ret;
1948 size_t n;
1949 unsigned char *buf;
1950
Paul Bakker69e095c2011-12-10 21:55:01 +00001951 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1952 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001953
Paul Bakker27fdf462011-06-09 13:55:13 +00001954 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001955
1956 memset( buf, 0, n + 1 );
1957 free( buf );
1958
1959 return( ret );
1960}
1961
Paul Bakker5121ce52009-01-03 21:22:43 +00001962/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001963 * Load and parse a private RSA key
1964 */
1965int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1966{
1967 int ret;
1968 size_t n;
1969 unsigned char *buf;
1970
Paul Bakker69e095c2011-12-10 21:55:01 +00001971 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1972 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001973
1974 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001975 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001976 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001977 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001978 (unsigned char *) pwd, strlen( pwd ) );
1979
1980 memset( buf, 0, n + 1 );
1981 free( buf );
1982
1983 return( ret );
1984}
1985
1986/*
1987 * Load and parse a public RSA key
1988 */
1989int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1990{
1991 int ret;
1992 size_t n;
1993 unsigned char *buf;
1994
Paul Bakker69e095c2011-12-10 21:55:01 +00001995 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1996 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001997
Paul Bakker27fdf462011-06-09 13:55:13 +00001998 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001999
2000 memset( buf, 0, n + 1 );
2001 free( buf );
2002
2003 return( ret );
2004}
2005#endif /* POLARSSL_FS_IO */
2006
2007/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 * Parse a private RSA key
2009 */
Paul Bakker23986e52011-04-24 08:57:21 +00002010int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2011 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002012{
Paul Bakker23986e52011-04-24 08:57:21 +00002013 int ret;
2014 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002016 unsigned char *p_alt;
2017 x509_buf pk_alg_oid;
2018
Paul Bakker96743fc2011-02-12 14:30:57 +00002019#if defined(POLARSSL_PEM_C)
2020 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00002021
Paul Bakker96743fc2011-02-12 14:30:57 +00002022 pem_init( &pem );
2023 ret = pem_read_buffer( &pem,
2024 "-----BEGIN RSA PRIVATE KEY-----",
2025 "-----END RSA PRIVATE KEY-----",
2026 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002027
Paul Bakkered56b222011-07-13 11:26:43 +00002028 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2029 {
2030 ret = pem_read_buffer( &pem,
2031 "-----BEGIN PRIVATE KEY-----",
2032 "-----END PRIVATE KEY-----",
2033 key, pwd, pwdlen, &len );
2034 }
2035
Paul Bakker96743fc2011-02-12 14:30:57 +00002036 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002038 /*
2039 * Was PEM encoded
2040 */
2041 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002043 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002044 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002045 pem_free( &pem );
2046 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002047 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002048
Paul Bakker96743fc2011-02-12 14:30:57 +00002049 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2050#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002051 ((void) pwd);
2052 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002053 p = (unsigned char *) key;
2054#endif
2055 end = p + keylen;
2056
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002058 * Note: Depending on the type of private key file one can expect either a
2059 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2060 *
2061 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002062 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002063 * algorithm AlgorithmIdentifier,
2064 * PrivateKey BIT STRING
2065 * }
2066 *
2067 * AlgorithmIdentifier ::= SEQUENCE {
2068 * algorithm OBJECT IDENTIFIER,
2069 * parameters ANY DEFINED BY algorithm OPTIONAL
2070 * }
2071 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 * RSAPrivateKey ::= SEQUENCE {
2073 * version Version,
2074 * modulus INTEGER, -- n
2075 * publicExponent INTEGER, -- e
2076 * privateExponent INTEGER, -- d
2077 * prime1 INTEGER, -- p
2078 * prime2 INTEGER, -- q
2079 * exponent1 INTEGER, -- d mod (p-1)
2080 * exponent2 INTEGER, -- d mod (q-1)
2081 * coefficient INTEGER, -- (inverse of q) mod p
2082 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2083 * }
2084 */
2085 if( ( ret = asn1_get_tag( &p, end, &len,
2086 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2087 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002088#if defined(POLARSSL_PEM_C)
2089 pem_free( &pem );
2090#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002092 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 }
2094
2095 end = p + len;
2096
2097 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2098 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002099#if defined(POLARSSL_PEM_C)
2100 pem_free( &pem );
2101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002103 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
2105
2106 if( rsa->ver != 0 )
2107 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002108#if defined(POLARSSL_PEM_C)
2109 pem_free( &pem );
2110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002112 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
2114
Paul Bakkered56b222011-07-13 11:26:43 +00002115 p_alt = p;
2116
2117 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2118 {
2119 // Assume that we have the PKCS#1 format if wrong
2120 // tag was encountered
2121 //
2122 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2123 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2124 {
2125#if defined(POLARSSL_PEM_C)
2126 pem_free( &pem );
2127#endif
2128 rsa_free( rsa );
2129 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2130 }
2131 }
2132 else
2133 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002134 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkered56b222011-07-13 11:26:43 +00002135
2136 /*
2137 * only RSA keys handled at this time
2138 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002139 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +00002140 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2141
2142 /*
2143 * Parse the PKCS#8 format
2144 */
2145
2146 p = p_alt;
2147 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2148 {
2149#if defined(POLARSSL_PEM_C)
2150 pem_free( &pem );
2151#endif
2152 rsa_free( rsa );
2153 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2154 }
2155
2156 if( ( end - p ) < 1 )
2157 {
2158#if defined(POLARSSL_PEM_C)
2159 pem_free( &pem );
2160#endif
2161 rsa_free( rsa );
2162 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2163 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2164 }
2165
2166 end = p + len;
2167
2168 if( ( ret = asn1_get_tag( &p, end, &len,
2169 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2170 {
2171#if defined(POLARSSL_PEM_C)
2172 pem_free( &pem );
2173#endif
2174 rsa_free( rsa );
2175 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2176 }
2177
2178 end = p + len;
2179
2180 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2181 {
2182#if defined(POLARSSL_PEM_C)
2183 pem_free( &pem );
2184#endif
2185 rsa_free( rsa );
2186 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2187 }
2188
2189 if( rsa->ver != 0 )
2190 {
2191#if defined(POLARSSL_PEM_C)
2192 pem_free( &pem );
2193#endif
2194 rsa_free( rsa );
2195 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2196 }
2197 }
2198
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2200 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2201 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2202 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2203 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2204 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2205 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2206 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2207 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002208#if defined(POLARSSL_PEM_C)
2209 pem_free( &pem );
2210#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002212 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 }
2214
2215 rsa->len = mpi_size( &rsa->N );
2216
2217 if( p != end )
2218 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002219#if defined(POLARSSL_PEM_C)
2220 pem_free( &pem );
2221#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002223 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002224 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
2226
2227 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
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 );
2233 return( ret );
2234 }
2235
Paul Bakker96743fc2011-02-12 14:30:57 +00002236#if defined(POLARSSL_PEM_C)
2237 pem_free( &pem );
2238#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002239
2240 return( 0 );
2241}
2242
2243/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002244 * Parse a public RSA key
2245 */
Paul Bakker23986e52011-04-24 08:57:21 +00002246int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002247{
Paul Bakker23986e52011-04-24 08:57:21 +00002248 int ret;
2249 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002250 unsigned char *p, *end;
2251 x509_buf alg_oid;
2252#if defined(POLARSSL_PEM_C)
2253 pem_context pem;
2254
2255 pem_init( &pem );
2256 ret = pem_read_buffer( &pem,
2257 "-----BEGIN PUBLIC KEY-----",
2258 "-----END PUBLIC KEY-----",
2259 key, NULL, 0, &len );
2260
2261 if( ret == 0 )
2262 {
2263 /*
2264 * Was PEM encoded
2265 */
2266 keylen = pem.buflen;
2267 }
2268 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2269 {
2270 pem_free( &pem );
2271 return( ret );
2272 }
2273
2274 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2275#else
2276 p = (unsigned char *) key;
2277#endif
2278 end = p + keylen;
2279
2280 /*
2281 * PublicKeyInfo ::= SEQUENCE {
2282 * algorithm AlgorithmIdentifier,
2283 * PublicKey BIT STRING
2284 * }
2285 *
2286 * AlgorithmIdentifier ::= SEQUENCE {
2287 * algorithm OBJECT IDENTIFIER,
2288 * parameters ANY DEFINED BY algorithm OPTIONAL
2289 * }
2290 *
2291 * RSAPublicKey ::= SEQUENCE {
2292 * modulus INTEGER, -- n
2293 * publicExponent INTEGER -- e
2294 * }
2295 */
2296
2297 if( ( ret = asn1_get_tag( &p, end, &len,
2298 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2299 {
2300#if defined(POLARSSL_PEM_C)
2301 pem_free( &pem );
2302#endif
2303 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002304 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002305 }
2306
2307 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2308 {
2309#if defined(POLARSSL_PEM_C)
2310 pem_free( &pem );
2311#endif
2312 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002313 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002314 }
2315
2316 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2317 {
2318#if defined(POLARSSL_PEM_C)
2319 pem_free( &pem );
2320#endif
2321 rsa_free( rsa );
2322 return( ret );
2323 }
2324
2325 rsa->len = mpi_size( &rsa->N );
2326
2327#if defined(POLARSSL_PEM_C)
2328 pem_free( &pem );
2329#endif
2330
2331 return( 0 );
2332}
2333
Paul Bakkereaa89f82011-04-04 21:36:15 +00002334#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002335/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002336 * Parse DHM parameters
2337 */
Paul Bakker23986e52011-04-24 08:57:21 +00002338int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002339{
Paul Bakker23986e52011-04-24 08:57:21 +00002340 int ret;
2341 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002342 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002343#if defined(POLARSSL_PEM_C)
2344 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002345
Paul Bakker96743fc2011-02-12 14:30:57 +00002346 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002347
Paul Bakker96743fc2011-02-12 14:30:57 +00002348 ret = pem_read_buffer( &pem,
2349 "-----BEGIN DH PARAMETERS-----",
2350 "-----END DH PARAMETERS-----",
2351 dhmin, NULL, 0, &dhminlen );
2352
2353 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002354 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002355 /*
2356 * Was PEM encoded
2357 */
2358 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002359 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002360 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002361 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002362 pem_free( &pem );
2363 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002364 }
2365
Paul Bakker96743fc2011-02-12 14:30:57 +00002366 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2367#else
2368 p = (unsigned char *) dhmin;
2369#endif
2370 end = p + dhminlen;
2371
Paul Bakker1b57b062011-01-06 15:48:19 +00002372 memset( dhm, 0, sizeof( dhm_context ) );
2373
Paul Bakker1b57b062011-01-06 15:48:19 +00002374 /*
2375 * DHParams ::= SEQUENCE {
2376 * prime INTEGER, -- P
2377 * generator INTEGER, -- g
2378 * }
2379 */
2380 if( ( ret = asn1_get_tag( &p, end, &len,
2381 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2382 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002383#if defined(POLARSSL_PEM_C)
2384 pem_free( &pem );
2385#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002386 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002387 }
2388
2389 end = p + len;
2390
2391 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2392 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2393 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002394#if defined(POLARSSL_PEM_C)
2395 pem_free( &pem );
2396#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002397 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002398 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002399 }
2400
2401 if( p != end )
2402 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002403#if defined(POLARSSL_PEM_C)
2404 pem_free( &pem );
2405#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002406 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002407 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002408 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2409 }
2410
Paul Bakker96743fc2011-02-12 14:30:57 +00002411#if defined(POLARSSL_PEM_C)
2412 pem_free( &pem );
2413#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002414
2415 return( 0 );
2416}
2417
Paul Bakker335db3f2011-04-25 15:28:35 +00002418#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002419/*
2420 * Load and parse a private RSA key
2421 */
2422int x509parse_dhmfile( dhm_context *dhm, const char *path )
2423{
2424 int ret;
2425 size_t n;
2426 unsigned char *buf;
2427
Paul Bakker69e095c2011-12-10 21:55:01 +00002428 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2429 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002430
Paul Bakker27fdf462011-06-09 13:55:13 +00002431 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002432
2433 memset( buf, 0, n + 1 );
2434 free( buf );
2435
2436 return( ret );
2437}
Paul Bakker335db3f2011-04-25 15:28:35 +00002438#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002439#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002440
Paul Bakker5121ce52009-01-03 21:22:43 +00002441#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002442#include <stdarg.h>
2443
2444#if !defined vsnprintf
2445#define vsnprintf _vsnprintf
2446#endif // vsnprintf
2447
2448/*
2449 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2450 * Result value is not size of buffer needed, but -1 if no fit is possible.
2451 *
2452 * This fuction tries to 'fix' this by at least suggesting enlarging the
2453 * size by 20.
2454 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002455static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002456{
2457 va_list ap;
2458 int res = -1;
2459
2460 va_start( ap, format );
2461
2462 res = vsnprintf( str, size, format, ap );
2463
2464 va_end( ap );
2465
2466 // No quick fix possible
2467 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002468 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002469
2470 return res;
2471}
2472
2473#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002474#endif
2475
Paul Bakkerd98030e2009-05-02 15:13:40 +00002476#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2477
2478#define SAFE_SNPRINTF() \
2479{ \
2480 if( ret == -1 ) \
2481 return( -1 ); \
2482 \
Paul Bakker23986e52011-04-24 08:57:21 +00002483 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002484 p[n - 1] = '\0'; \
2485 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2486 } \
2487 \
Paul Bakker23986e52011-04-24 08:57:21 +00002488 n -= (unsigned int) ret; \
2489 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002490}
2491
Paul Bakker5121ce52009-01-03 21:22:43 +00002492/*
2493 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002494 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002496int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002497{
Paul Bakker23986e52011-04-24 08:57:21 +00002498 int ret;
2499 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002501 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002502 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 char s[128], *p;
2504
2505 memset( s, 0, sizeof( s ) );
2506
2507 name = dn;
2508 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002509 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
2511 while( name != NULL )
2512 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002513 if( !name->oid.p )
2514 {
2515 name = name->next;
2516 continue;
2517 }
2518
Paul Bakker74111d32011-01-15 16:57:55 +00002519 if( name != dn )
2520 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002521 ret = snprintf( p, n, ", " );
2522 SAFE_SNPRINTF();
2523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Paul Bakkerc70b9822013-04-07 22:00:46 +02002525 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Paul Bakkerc70b9822013-04-07 22:00:46 +02002527 if( ret == 0 )
2528 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002530 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002531 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
2533 for( i = 0; i < name->val.len; i++ )
2534 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002535 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 break;
2537
2538 c = name->val.p[i];
2539 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2540 s[i] = '?';
2541 else s[i] = c;
2542 }
2543 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002544 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002545 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 name = name->next;
2547 }
2548
Paul Bakker23986e52011-04-24 08:57:21 +00002549 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550}
2551
2552/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002553 * Store the serial in printable form into buf; no more
2554 * than size characters will be written
2555 */
2556int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2557{
Paul Bakker23986e52011-04-24 08:57:21 +00002558 int ret;
2559 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002560 char *p;
2561
2562 p = buf;
2563 n = size;
2564
2565 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002566 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002567
2568 for( i = 0; i < nr; i++ )
2569 {
Paul Bakker93048802011-12-05 14:38:06 +00002570 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002571 continue;
2572
Paul Bakkerdd476992011-01-16 21:34:59 +00002573 ret = snprintf( p, n, "%02X%s",
2574 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2575 SAFE_SNPRINTF();
2576 }
2577
Paul Bakker03c7c252011-11-25 12:37:37 +00002578 if( nr != serial->len )
2579 {
2580 ret = snprintf( p, n, "...." );
2581 SAFE_SNPRINTF();
2582 }
2583
Paul Bakker23986e52011-04-24 08:57:21 +00002584 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002585}
2586
2587/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002588 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002590int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2591 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002592{
Paul Bakker23986e52011-04-24 08:57:21 +00002593 int ret;
2594 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002595 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002596 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
2598 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002599 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Paul Bakkerd98030e2009-05-02 15:13:40 +00002601 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002603 SAFE_SNPRINTF();
2604 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002606 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Paul Bakkerdd476992011-01-16 21:34:59 +00002608 ret = x509parse_serial_gets( p, n, &crt->serial);
2609 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Paul Bakkerd98030e2009-05-02 15:13:40 +00002611 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2612 SAFE_SNPRINTF();
2613 ret = x509parse_dn_gets( p, n, &crt->issuer );
2614 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Paul Bakkerd98030e2009-05-02 15:13:40 +00002616 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2617 SAFE_SNPRINTF();
2618 ret = x509parse_dn_gets( p, n, &crt->subject );
2619 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
Paul Bakkerd98030e2009-05-02 15:13:40 +00002621 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2623 crt->valid_from.year, crt->valid_from.mon,
2624 crt->valid_from.day, crt->valid_from.hour,
2625 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002626 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Paul Bakkerd98030e2009-05-02 15:13:40 +00002628 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2630 crt->valid_to.year, crt->valid_to.mon,
2631 crt->valid_to.day, crt->valid_to.hour,
2632 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002633 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Paul Bakkerc70b9822013-04-07 22:00:46 +02002635 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002636 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Paul Bakkerc70b9822013-04-07 22:00:46 +02002638 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2639 if( ret != 0 )
2640 ret = snprintf( p, n, "???" );
2641 else
2642 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002643 SAFE_SNPRINTF();
2644
2645 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002646 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002647 SAFE_SNPRINTF();
2648
Paul Bakker23986e52011-04-24 08:57:21 +00002649 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002650}
2651
Paul Bakker74111d32011-01-15 16:57:55 +00002652/*
2653 * Return an informational string describing the given OID
2654 */
2655const char *x509_oid_get_description( x509_buf *oid )
2656{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002657 const char *desc = NULL;
2658 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002659
Paul Bakkerc70b9822013-04-07 22:00:46 +02002660 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002661
Paul Bakkerc70b9822013-04-07 22:00:46 +02002662 if( ret != 0 )
2663 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002664
Paul Bakkerc70b9822013-04-07 22:00:46 +02002665 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002666}
2667
2668/* Return the x.y.z.... style numeric string for the given OID */
2669int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2670{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002671 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002672}
2673
Paul Bakkerd98030e2009-05-02 15:13:40 +00002674/*
2675 * Return an informational string about the CRL.
2676 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002677int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2678 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002679{
Paul Bakker23986e52011-04-24 08:57:21 +00002680 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002681 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002682 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002683 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002684 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002685
2686 p = buf;
2687 n = size;
2688
2689 ret = snprintf( p, n, "%sCRL version : %d",
2690 prefix, crl->version );
2691 SAFE_SNPRINTF();
2692
2693 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2694 SAFE_SNPRINTF();
2695 ret = x509parse_dn_gets( p, n, &crl->issuer );
2696 SAFE_SNPRINTF();
2697
2698 ret = snprintf( p, n, "\n%sthis update : " \
2699 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2700 crl->this_update.year, crl->this_update.mon,
2701 crl->this_update.day, crl->this_update.hour,
2702 crl->this_update.min, crl->this_update.sec );
2703 SAFE_SNPRINTF();
2704
2705 ret = snprintf( p, n, "\n%snext update : " \
2706 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2707 crl->next_update.year, crl->next_update.mon,
2708 crl->next_update.day, crl->next_update.hour,
2709 crl->next_update.min, crl->next_update.sec );
2710 SAFE_SNPRINTF();
2711
2712 entry = &crl->entry;
2713
2714 ret = snprintf( p, n, "\n%sRevoked certificates:",
2715 prefix );
2716 SAFE_SNPRINTF();
2717
Paul Bakker9be19372009-07-27 20:21:53 +00002718 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002719 {
2720 ret = snprintf( p, n, "\n%sserial number: ",
2721 prefix );
2722 SAFE_SNPRINTF();
2723
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002724 ret = x509parse_serial_gets( p, n, &entry->serial);
2725 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002726
Paul Bakkerd98030e2009-05-02 15:13:40 +00002727 ret = snprintf( p, n, " revocation date: " \
2728 "%04d-%02d-%02d %02d:%02d:%02d",
2729 entry->revocation_date.year, entry->revocation_date.mon,
2730 entry->revocation_date.day, entry->revocation_date.hour,
2731 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002732 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002733
2734 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 }
2736
Paul Bakkerc70b9822013-04-07 22:00:46 +02002737 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002738 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Paul Bakkerc70b9822013-04-07 22:00:46 +02002740 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2741 if( ret != 0 )
2742 ret = snprintf( p, n, "???" );
2743 else
2744 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002745 SAFE_SNPRINTF();
2746
Paul Bakker1e27bb22009-07-19 20:25:25 +00002747 ret = snprintf( p, n, "\n" );
2748 SAFE_SNPRINTF();
2749
Paul Bakker23986e52011-04-24 08:57:21 +00002750 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751}
2752
2753/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002754 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002756int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002757{
Paul Bakkercce9d772011-11-18 14:26:47 +00002758 int year, mon, day;
2759 int hour, min, sec;
2760
2761#if defined(_WIN32)
2762 SYSTEMTIME st;
2763
2764 GetLocalTime(&st);
2765
2766 year = st.wYear;
2767 mon = st.wMonth;
2768 day = st.wDay;
2769 hour = st.wHour;
2770 min = st.wMinute;
2771 sec = st.wSecond;
2772#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 struct tm *lt;
2774 time_t tt;
2775
2776 tt = time( NULL );
2777 lt = localtime( &tt );
2778
Paul Bakkercce9d772011-11-18 14:26:47 +00002779 year = lt->tm_year + 1900;
2780 mon = lt->tm_mon + 1;
2781 day = lt->tm_mday;
2782 hour = lt->tm_hour;
2783 min = lt->tm_min;
2784 sec = lt->tm_sec;
2785#endif
2786
2787 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002788 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Paul Bakkercce9d772011-11-18 14:26:47 +00002790 if( year == to->year &&
2791 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002792 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Paul Bakkercce9d772011-11-18 14:26:47 +00002794 if( year == to->year &&
2795 mon == to->mon &&
2796 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002797 return( 1 );
2798
Paul Bakkercce9d772011-11-18 14:26:47 +00002799 if( year == to->year &&
2800 mon == to->mon &&
2801 day == to->day &&
2802 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002803 return( 1 );
2804
Paul Bakkercce9d772011-11-18 14:26:47 +00002805 if( year == to->year &&
2806 mon == to->mon &&
2807 day == to->day &&
2808 hour == to->hour &&
2809 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002810 return( 1 );
2811
Paul Bakkercce9d772011-11-18 14:26:47 +00002812 if( year == to->year &&
2813 mon == to->mon &&
2814 day == to->day &&
2815 hour == to->hour &&
2816 min == to->min &&
2817 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002818 return( 1 );
2819
Paul Bakker40ea7de2009-05-03 10:18:48 +00002820 return( 0 );
2821}
2822
2823/*
2824 * Return 1 if the certificate is revoked, or 0 otherwise.
2825 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002826int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002827{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002828 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002829
2830 while( cur != NULL && cur->serial.len != 0 )
2831 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002832 if( crt->serial.len == cur->serial.len &&
2833 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002834 {
2835 if( x509parse_time_expired( &cur->revocation_date ) )
2836 return( 1 );
2837 }
2838
2839 cur = cur->next;
2840 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
2842 return( 0 );
2843}
2844
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002845/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002846 * Check that the given certificate is valid accoring to the CRL.
2847 */
2848static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2849 x509_crl *crl_list)
2850{
2851 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002852 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2853 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002854
Paul Bakker915275b2012-09-28 07:10:55 +00002855 if( ca == NULL )
2856 return( flags );
2857
Paul Bakker76fd75a2011-01-16 21:12:10 +00002858 /*
2859 * TODO: What happens if no CRL is present?
2860 * Suggestion: Revocation state should be unknown if no CRL is present.
2861 * For backwards compatibility this is not yet implemented.
2862 */
2863
Paul Bakker915275b2012-09-28 07:10:55 +00002864 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002865 {
Paul Bakker915275b2012-09-28 07:10:55 +00002866 if( crl_list->version == 0 ||
2867 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00002868 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2869 crl_list->issuer_raw.len ) != 0 )
2870 {
2871 crl_list = crl_list->next;
2872 continue;
2873 }
2874
2875 /*
2876 * Check if CRL is correctly signed by the trusted CA
2877 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002878 md_info = md_info_from_type( crl_list->sig_md );
2879 if( md_info == NULL )
2880 {
2881 /*
2882 * Cannot check 'unknown' hash
2883 */
2884 flags |= BADCRL_NOT_TRUSTED;
2885 break;
2886 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002887
Paul Bakkerc70b9822013-04-07 22:00:46 +02002888 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002889
Paul Bakkerc70b9822013-04-07 22:00:46 +02002890 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00002891 0, hash, crl_list->sig.p ) == 0 )
2892 {
2893 /*
2894 * CRL is not trusted
2895 */
2896 flags |= BADCRL_NOT_TRUSTED;
2897 break;
2898 }
2899
2900 /*
2901 * Check for validity of CRL (Do not drop out)
2902 */
2903 if( x509parse_time_expired( &crl_list->next_update ) )
2904 flags |= BADCRL_EXPIRED;
2905
2906 /*
2907 * Check if certificate is revoked
2908 */
2909 if( x509parse_revoked(crt, crl_list) )
2910 {
2911 flags |= BADCERT_REVOKED;
2912 break;
2913 }
2914
2915 crl_list = crl_list->next;
2916 }
2917 return flags;
2918}
2919
Paul Bakker57b12982012-02-11 17:38:38 +00002920int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002921{
2922 size_t i;
2923 size_t cn_idx = 0;
2924
Paul Bakker57b12982012-02-11 17:38:38 +00002925 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002926 return( 0 );
2927
2928 for( i = 0; i < strlen( cn ); ++i )
2929 {
2930 if( cn[i] == '.' )
2931 {
2932 cn_idx = i;
2933 break;
2934 }
2935 }
2936
2937 if( cn_idx == 0 )
2938 return( 0 );
2939
Paul Bakker535e97d2012-08-23 10:49:55 +00002940 if( strlen( cn ) - cn_idx == name->len - 1 &&
2941 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002942 {
2943 return( 1 );
2944 }
2945
2946 return( 0 );
2947}
2948
Paul Bakker915275b2012-09-28 07:10:55 +00002949static int x509parse_verify_top(
2950 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00002951 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00002952 int (*f_vrfy)(void *, x509_cert *, int, int *),
2953 void *p_vrfy )
2954{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002955 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00002956 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002957 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2958 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00002959
2960 if( x509parse_time_expired( &child->valid_to ) )
2961 *flags |= BADCERT_EXPIRED;
2962
2963 /*
2964 * Child is the top of the chain. Check against the trust_ca list.
2965 */
2966 *flags |= BADCERT_NOT_TRUSTED;
2967
2968 while( trust_ca != NULL )
2969 {
2970 if( trust_ca->version == 0 ||
2971 child->issuer_raw.len != trust_ca->subject_raw.len ||
2972 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
2973 child->issuer_raw.len ) != 0 )
2974 {
2975 trust_ca = trust_ca->next;
2976 continue;
2977 }
2978
Paul Bakker9a736322012-11-14 12:39:52 +00002979 /*
2980 * Reduce path_len to check against if top of the chain is
2981 * the same as the trusted CA
2982 */
2983 if( child->subject_raw.len == trust_ca->subject_raw.len &&
2984 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
2985 child->issuer_raw.len ) == 0 )
2986 {
2987 check_path_cnt--;
2988 }
2989
Paul Bakker915275b2012-09-28 07:10:55 +00002990 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00002991 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00002992 {
2993 trust_ca = trust_ca->next;
2994 continue;
2995 }
2996
Paul Bakkerc70b9822013-04-07 22:00:46 +02002997 md_info = md_info_from_type( child->sig_md );
2998 if( md_info == NULL )
2999 {
3000 /*
3001 * Cannot check 'unknown' hash
3002 */
3003 continue;
3004 }
Paul Bakker915275b2012-09-28 07:10:55 +00003005
Paul Bakkerc70b9822013-04-07 22:00:46 +02003006 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003007
Paul Bakkerc70b9822013-04-07 22:00:46 +02003008 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003009 0, hash, child->sig.p ) != 0 )
3010 {
3011 trust_ca = trust_ca->next;
3012 continue;
3013 }
3014
3015 /*
3016 * Top of chain is signed by a trusted CA
3017 */
3018 *flags &= ~BADCERT_NOT_TRUSTED;
3019 break;
3020 }
3021
Paul Bakker9a736322012-11-14 12:39:52 +00003022 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003023 * If top of chain is not the same as the trusted CA send a verify request
3024 * to the callback for any issues with validity and CRL presence for the
3025 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003026 */
3027 if( trust_ca != NULL &&
3028 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3029 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3030 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003031 {
3032 /* Check trusted CA's CRL for then chain's top crt */
3033 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3034
3035 if( x509parse_time_expired( &trust_ca->valid_to ) )
3036 ca_flags |= BADCERT_EXPIRED;
3037
Paul Bakker915275b2012-09-28 07:10:55 +00003038 if( NULL != f_vrfy )
3039 {
Paul Bakker9a736322012-11-14 12:39:52 +00003040 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003041 return( ret );
3042 }
3043 }
3044
3045 /* Call callback on top cert */
3046 if( NULL != f_vrfy )
3047 {
Paul Bakker9a736322012-11-14 12:39:52 +00003048 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003049 return( ret );
3050 }
3051
Paul Bakker915275b2012-09-28 07:10:55 +00003052 *flags |= ca_flags;
3053
3054 return( 0 );
3055}
3056
3057static int x509parse_verify_child(
3058 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003059 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003060 int (*f_vrfy)(void *, x509_cert *, int, int *),
3061 void *p_vrfy )
3062{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003063 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003064 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003065 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003066 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003067 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003068
3069 if( x509parse_time_expired( &child->valid_to ) )
3070 *flags |= BADCERT_EXPIRED;
3071
Paul Bakkerc70b9822013-04-07 22:00:46 +02003072 md_info = md_info_from_type( child->sig_md );
3073 if( md_info == NULL )
3074 {
3075 /*
3076 * Cannot check 'unknown' hash
3077 */
Paul Bakker915275b2012-09-28 07:10:55 +00003078 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003079 }
3080 else
3081 {
3082 md( md_info, child->tbs.p, child->tbs.len, hash );
3083
3084 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3085 child->sig.p ) != 0 )
3086 *flags |= BADCERT_NOT_TRUSTED;
3087 }
3088
Paul Bakker915275b2012-09-28 07:10:55 +00003089 /* Check trusted CA's CRL for the given crt */
3090 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3091
3092 grandparent = parent->next;
3093
3094 while( grandparent != NULL )
3095 {
3096 if( grandparent->version == 0 ||
3097 grandparent->ca_istrue == 0 ||
3098 parent->issuer_raw.len != grandparent->subject_raw.len ||
3099 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3100 parent->issuer_raw.len ) != 0 )
3101 {
3102 grandparent = grandparent->next;
3103 continue;
3104 }
3105 break;
3106 }
3107
Paul Bakker915275b2012-09-28 07:10:55 +00003108 if( grandparent != NULL )
3109 {
3110 /*
3111 * Part of the chain
3112 */
Paul Bakker9a736322012-11-14 12:39:52 +00003113 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 +00003114 if( ret != 0 )
3115 return( ret );
3116 }
3117 else
3118 {
Paul Bakker9a736322012-11-14 12:39:52 +00003119 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 +00003120 if( ret != 0 )
3121 return( ret );
3122 }
3123
3124 /* child is verified to be a child of the parent, call verify callback */
3125 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003126 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003127 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003128
3129 *flags |= parent_flags;
3130
3131 return( 0 );
3132}
3133
Paul Bakker76fd75a2011-01-16 21:12:10 +00003134/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003135 * Verify the certificate validity
3136 */
3137int x509parse_verify( x509_cert *crt,
3138 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003139 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003140 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003141 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003142 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003143{
Paul Bakker23986e52011-04-24 08:57:21 +00003144 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003145 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003146 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003147 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003148 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003149 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Paul Bakker40ea7de2009-05-03 10:18:48 +00003151 *flags = 0;
3152
Paul Bakker5121ce52009-01-03 21:22:43 +00003153 if( cn != NULL )
3154 {
3155 name = &crt->subject;
3156 cn_len = strlen( cn );
3157
Paul Bakker4d2c1242012-05-10 14:12:46 +00003158 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003160 cur = &crt->subject_alt_names;
3161
3162 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003163 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003164 if( cur->buf.len == cn_len &&
3165 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003166 break;
3167
Paul Bakker535e97d2012-08-23 10:49:55 +00003168 if( cur->buf.len > 2 &&
3169 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003170 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003171 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003172
Paul Bakker4d2c1242012-05-10 14:12:46 +00003173 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003174 }
3175
3176 if( cur == NULL )
3177 *flags |= BADCERT_CN_MISMATCH;
3178 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003179 else
3180 {
3181 while( name != NULL )
3182 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003183 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003184 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003185 if( name->val.len == cn_len &&
3186 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003187 break;
3188
Paul Bakker535e97d2012-08-23 10:49:55 +00003189 if( name->val.len > 2 &&
3190 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003191 x509_wildcard_verify( cn, &name->val ) )
3192 break;
3193 }
3194
3195 name = name->next;
3196 }
3197
3198 if( name == NULL )
3199 *flags |= BADCERT_CN_MISMATCH;
3200 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 }
3202
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003204 * Iterate upwards in the given cert chain, to find our crt parent.
3205 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003207 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Paul Bakker76fd75a2011-01-16 21:12:10 +00003209 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003211 if( parent->ca_istrue == 0 ||
3212 crt->issuer_raw.len != parent->subject_raw.len ||
3213 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 crt->issuer_raw.len ) != 0 )
3215 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003216 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 continue;
3218 }
Paul Bakker915275b2012-09-28 07:10:55 +00003219 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 }
3221
Paul Bakker915275b2012-09-28 07:10:55 +00003222 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 {
Paul Bakker915275b2012-09-28 07:10:55 +00003224 /*
3225 * Part of the chain
3226 */
Paul Bakker9a736322012-11-14 12:39:52 +00003227 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003228 if( ret != 0 )
3229 return( ret );
3230 }
3231 else
Paul Bakker74111d32011-01-15 16:57:55 +00003232 {
Paul Bakker9a736322012-11-14 12:39:52 +00003233 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003234 if( ret != 0 )
3235 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003236 }
Paul Bakker915275b2012-09-28 07:10:55 +00003237
3238 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003239 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003240
Paul Bakker5121ce52009-01-03 21:22:43 +00003241 return( 0 );
3242}
3243
3244/*
3245 * Unallocate all certificate data
3246 */
3247void x509_free( x509_cert *crt )
3248{
3249 x509_cert *cert_cur = crt;
3250 x509_cert *cert_prv;
3251 x509_name *name_cur;
3252 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003253 x509_sequence *seq_cur;
3254 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003255
3256 if( crt == NULL )
3257 return;
3258
3259 do
3260 {
3261 rsa_free( &cert_cur->rsa );
3262
3263 name_cur = cert_cur->issuer.next;
3264 while( name_cur != NULL )
3265 {
3266 name_prv = name_cur;
3267 name_cur = name_cur->next;
3268 memset( name_prv, 0, sizeof( x509_name ) );
3269 free( name_prv );
3270 }
3271
3272 name_cur = cert_cur->subject.next;
3273 while( name_cur != NULL )
3274 {
3275 name_prv = name_cur;
3276 name_cur = name_cur->next;
3277 memset( name_prv, 0, sizeof( x509_name ) );
3278 free( name_prv );
3279 }
3280
Paul Bakker74111d32011-01-15 16:57:55 +00003281 seq_cur = cert_cur->ext_key_usage.next;
3282 while( seq_cur != NULL )
3283 {
3284 seq_prv = seq_cur;
3285 seq_cur = seq_cur->next;
3286 memset( seq_prv, 0, sizeof( x509_sequence ) );
3287 free( seq_prv );
3288 }
3289
Paul Bakker8afa70d2012-02-11 18:42:45 +00003290 seq_cur = cert_cur->subject_alt_names.next;
3291 while( seq_cur != NULL )
3292 {
3293 seq_prv = seq_cur;
3294 seq_cur = seq_cur->next;
3295 memset( seq_prv, 0, sizeof( x509_sequence ) );
3296 free( seq_prv );
3297 }
3298
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 if( cert_cur->raw.p != NULL )
3300 {
3301 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3302 free( cert_cur->raw.p );
3303 }
3304
3305 cert_cur = cert_cur->next;
3306 }
3307 while( cert_cur != NULL );
3308
3309 cert_cur = crt;
3310 do
3311 {
3312 cert_prv = cert_cur;
3313 cert_cur = cert_cur->next;
3314
3315 memset( cert_prv, 0, sizeof( x509_cert ) );
3316 if( cert_prv != crt )
3317 free( cert_prv );
3318 }
3319 while( cert_cur != NULL );
3320}
3321
Paul Bakkerd98030e2009-05-02 15:13:40 +00003322/*
3323 * Unallocate all CRL data
3324 */
3325void x509_crl_free( x509_crl *crl )
3326{
3327 x509_crl *crl_cur = crl;
3328 x509_crl *crl_prv;
3329 x509_name *name_cur;
3330 x509_name *name_prv;
3331 x509_crl_entry *entry_cur;
3332 x509_crl_entry *entry_prv;
3333
3334 if( crl == NULL )
3335 return;
3336
3337 do
3338 {
3339 name_cur = crl_cur->issuer.next;
3340 while( name_cur != NULL )
3341 {
3342 name_prv = name_cur;
3343 name_cur = name_cur->next;
3344 memset( name_prv, 0, sizeof( x509_name ) );
3345 free( name_prv );
3346 }
3347
3348 entry_cur = crl_cur->entry.next;
3349 while( entry_cur != NULL )
3350 {
3351 entry_prv = entry_cur;
3352 entry_cur = entry_cur->next;
3353 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3354 free( entry_prv );
3355 }
3356
3357 if( crl_cur->raw.p != NULL )
3358 {
3359 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3360 free( crl_cur->raw.p );
3361 }
3362
3363 crl_cur = crl_cur->next;
3364 }
3365 while( crl_cur != NULL );
3366
3367 crl_cur = crl;
3368 do
3369 {
3370 crl_prv = crl_cur;
3371 crl_cur = crl_cur->next;
3372
3373 memset( crl_prv, 0, sizeof( x509_crl ) );
3374 if( crl_prv != crl )
3375 free( crl_prv );
3376 }
3377 while( crl_cur != NULL );
3378}
3379
Paul Bakker40e46942009-01-03 21:51:57 +00003380#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003381
Paul Bakker40e46942009-01-03 21:51:57 +00003382#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003383
3384/*
3385 * Checkup routine
3386 */
3387int x509_self_test( int verbose )
3388{
Paul Bakker5690efc2011-05-26 13:16:06 +00003389#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003390 int ret;
3391 int flags;
3392 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 x509_cert cacert;
3394 x509_cert clicert;
3395 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003396#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003397 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003398#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003399
3400 if( verbose != 0 )
3401 printf( " X.509 certificate load: " );
3402
3403 memset( &clicert, 0, sizeof( x509_cert ) );
3404
3405 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003406 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003407 if( ret != 0 )
3408 {
3409 if( verbose != 0 )
3410 printf( "failed\n" );
3411
3412 return( ret );
3413 }
3414
3415 memset( &cacert, 0, sizeof( x509_cert ) );
3416
3417 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003418 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 if( ret != 0 )
3420 {
3421 if( verbose != 0 )
3422 printf( "failed\n" );
3423
3424 return( ret );
3425 }
3426
3427 if( verbose != 0 )
3428 printf( "passed\n X.509 private key load: " );
3429
3430 i = strlen( test_ca_key );
3431 j = strlen( test_ca_pwd );
3432
Paul Bakker66b78b22011-03-25 14:22:50 +00003433 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3434
Paul Bakker5121ce52009-01-03 21:22:43 +00003435 if( ( ret = x509parse_key( &rsa,
3436 (unsigned char *) test_ca_key, i,
3437 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3438 {
3439 if( verbose != 0 )
3440 printf( "failed\n" );
3441
3442 return( ret );
3443 }
3444
3445 if( verbose != 0 )
3446 printf( "passed\n X.509 signature verify: ");
3447
Paul Bakker23986e52011-04-24 08:57:21 +00003448 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 if( ret != 0 )
3450 {
Paul Bakker23986e52011-04-24 08:57:21 +00003451 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 if( verbose != 0 )
3453 printf( "failed\n" );
3454
3455 return( ret );
3456 }
3457
Paul Bakker5690efc2011-05-26 13:16:06 +00003458#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003460 printf( "passed\n X.509 DHM parameter load: " );
3461
3462 i = strlen( test_dhm_params );
3463 j = strlen( test_ca_pwd );
3464
3465 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3466 {
3467 if( verbose != 0 )
3468 printf( "failed\n" );
3469
3470 return( ret );
3471 }
3472
3473 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003475#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003476
3477 x509_free( &cacert );
3478 x509_free( &clicert );
3479 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003480#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003481 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003482#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
3484 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003485#else
3486 ((void) verbose);
3487 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3488#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003489}
3490
3491#endif
3492
3493#endif