blob: 3e99bdca9a3552771e702acaeb44b91d4f5cd45e [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 Bakker69e095c2011-12-10 21:55:01 +00001815 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001816
Paul Bakkerd98030e2009-05-02 15:13:40 +00001817 if( fread( *buf, 1, *n, f ) != *n )
1818 {
1819 fclose( f );
1820 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001821 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001822 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001823
Paul Bakkerd98030e2009-05-02 15:13:40 +00001824 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001825
Paul Bakkerd98030e2009-05-02 15:13:40 +00001826 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001827
Paul Bakkerd98030e2009-05-02 15:13:40 +00001828 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001829}
1830
1831/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 * Load one or more certificates and add them to the chained list
1833 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001834int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001835{
1836 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 size_t n;
1838 unsigned char *buf;
1839
Paul Bakker69e095c2011-12-10 21:55:01 +00001840 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1841 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Paul Bakker69e095c2011-12-10 21:55:01 +00001843 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001844
1845 memset( buf, 0, n + 1 );
1846 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
1848 return( ret );
1849}
1850
Paul Bakker8d914582012-06-04 12:46:42 +00001851int x509parse_crtpath( x509_cert *chain, const char *path )
1852{
1853 int ret = 0;
1854#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001855 int w_ret;
1856 WCHAR szDir[MAX_PATH];
1857 char filename[MAX_PATH];
1858 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001859 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001860
Paul Bakker97872ac2012-11-02 12:53:26 +00001861 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001862 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001863
1864 if( len > MAX_PATH - 3 )
1865 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001866
Paul Bakker3338b792012-10-01 21:13:10 +00001867 memset( szDir, 0, sizeof(szDir) );
1868 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001869 memcpy( filename, path, len );
1870 filename[len++] = '\\';
1871 p = filename + len;
1872 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001873
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001874 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001875
Paul Bakker97872ac2012-11-02 12:53:26 +00001876 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001877 if (hFind == INVALID_HANDLE_VALUE)
1878 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1879
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001880 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001881 do
1882 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001883 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001884
Paul Bakkere4791f32012-06-04 21:29:15 +00001885 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001886 continue;
1887
Paul Bakker3338b792012-10-01 21:13:10 +00001888 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1889 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001890 p, len - 1,
1891 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001892
Paul Bakker3338b792012-10-01 21:13:10 +00001893 w_ret = x509parse_crtfile( chain, filename );
1894 if( w_ret < 0 )
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001895 {
1896 ret = w_ret;
1897 goto cleanup;
1898 }
Paul Bakker3338b792012-10-01 21:13:10 +00001899
1900 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001901 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001902 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001903
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001904 if (GetLastError() != ERROR_NO_MORE_FILES)
1905 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001906
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001907cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001908 FindClose( hFind );
1909#else
1910 int t_ret;
1911 struct dirent *entry;
1912 char entry_name[255];
1913 DIR *dir = opendir( path );
1914
1915 if( dir == NULL)
1916 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1917
1918 while( ( entry = readdir( dir ) ) != NULL )
1919 {
1920 if( entry->d_type != DT_REG )
1921 continue;
1922
1923 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1924 t_ret = x509parse_crtfile( chain, entry_name );
1925 if( t_ret < 0 )
Paul Bakker97872ac2012-11-02 12:53:26 +00001926 {
1927 ret = t_ret;
1928 break;
1929 }
Paul Bakker8d914582012-06-04 12:46:42 +00001930
1931 ret += t_ret;
1932 }
1933 closedir( dir );
1934#endif
1935
1936 return( ret );
1937}
1938
Paul Bakkerd98030e2009-05-02 15:13:40 +00001939/*
1940 * Load one or more CRLs and add them to the chained list
1941 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001942int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001943{
1944 int ret;
1945 size_t n;
1946 unsigned char *buf;
1947
Paul Bakker69e095c2011-12-10 21:55:01 +00001948 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1949 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001950
Paul Bakker27fdf462011-06-09 13:55:13 +00001951 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001952
1953 memset( buf, 0, n + 1 );
1954 free( buf );
1955
1956 return( ret );
1957}
1958
Paul Bakker5121ce52009-01-03 21:22:43 +00001959/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001960 * Load and parse a private RSA key
1961 */
1962int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1963{
1964 int ret;
1965 size_t n;
1966 unsigned char *buf;
1967
Paul Bakker69e095c2011-12-10 21:55:01 +00001968 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1969 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001970
1971 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001972 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001973 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001974 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001975 (unsigned char *) pwd, strlen( pwd ) );
1976
1977 memset( buf, 0, n + 1 );
1978 free( buf );
1979
1980 return( ret );
1981}
1982
1983/*
1984 * Load and parse a public RSA key
1985 */
1986int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1987{
1988 int ret;
1989 size_t n;
1990 unsigned char *buf;
1991
Paul Bakker69e095c2011-12-10 21:55:01 +00001992 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1993 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001994
Paul Bakker27fdf462011-06-09 13:55:13 +00001995 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001996
1997 memset( buf, 0, n + 1 );
1998 free( buf );
1999
2000 return( ret );
2001}
2002#endif /* POLARSSL_FS_IO */
2003
2004/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 * Parse a private RSA key
2006 */
Paul Bakker23986e52011-04-24 08:57:21 +00002007int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2008 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002009{
Paul Bakker23986e52011-04-24 08:57:21 +00002010 int ret;
2011 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002013 unsigned char *p_alt;
2014 x509_buf pk_alg_oid;
2015
Paul Bakker96743fc2011-02-12 14:30:57 +00002016#if defined(POLARSSL_PEM_C)
2017 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
Paul Bakker96743fc2011-02-12 14:30:57 +00002019 pem_init( &pem );
2020 ret = pem_read_buffer( &pem,
2021 "-----BEGIN RSA PRIVATE KEY-----",
2022 "-----END RSA PRIVATE KEY-----",
2023 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
Paul Bakkered56b222011-07-13 11:26:43 +00002025 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2026 {
2027 ret = pem_read_buffer( &pem,
2028 "-----BEGIN PRIVATE KEY-----",
2029 "-----END PRIVATE KEY-----",
2030 key, pwd, pwdlen, &len );
2031 }
2032
Paul Bakker96743fc2011-02-12 14:30:57 +00002033 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002035 /*
2036 * Was PEM encoded
2037 */
2038 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002040 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002041 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002042 pem_free( &pem );
2043 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Paul Bakker96743fc2011-02-12 14:30:57 +00002046 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2047#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002048 ((void) pwd);
2049 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002050 p = (unsigned char *) key;
2051#endif
2052 end = p + keylen;
2053
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002055 * Note: Depending on the type of private key file one can expect either a
2056 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2057 *
2058 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002059 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002060 * algorithm AlgorithmIdentifier,
2061 * PrivateKey BIT STRING
2062 * }
2063 *
2064 * AlgorithmIdentifier ::= SEQUENCE {
2065 * algorithm OBJECT IDENTIFIER,
2066 * parameters ANY DEFINED BY algorithm OPTIONAL
2067 * }
2068 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 * RSAPrivateKey ::= SEQUENCE {
2070 * version Version,
2071 * modulus INTEGER, -- n
2072 * publicExponent INTEGER, -- e
2073 * privateExponent INTEGER, -- d
2074 * prime1 INTEGER, -- p
2075 * prime2 INTEGER, -- q
2076 * exponent1 INTEGER, -- d mod (p-1)
2077 * exponent2 INTEGER, -- d mod (q-1)
2078 * coefficient INTEGER, -- (inverse of q) mod p
2079 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2080 * }
2081 */
2082 if( ( ret = asn1_get_tag( &p, end, &len,
2083 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2084 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002085#if defined(POLARSSL_PEM_C)
2086 pem_free( &pem );
2087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002089 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 }
2091
2092 end = p + len;
2093
2094 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2095 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002096#if defined(POLARSSL_PEM_C)
2097 pem_free( &pem );
2098#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002100 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
2102
2103 if( rsa->ver != 0 )
2104 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002105#if defined(POLARSSL_PEM_C)
2106 pem_free( &pem );
2107#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002109 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 }
2111
Paul Bakkered56b222011-07-13 11:26:43 +00002112 p_alt = p;
2113
2114 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2115 {
2116 // Assume that we have the PKCS#1 format if wrong
2117 // tag was encountered
2118 //
2119 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2120 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2121 {
2122#if defined(POLARSSL_PEM_C)
2123 pem_free( &pem );
2124#endif
2125 rsa_free( rsa );
2126 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2127 }
2128 }
2129 else
2130 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002131 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkered56b222011-07-13 11:26:43 +00002132
2133 /*
2134 * only RSA keys handled at this time
2135 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002136 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +00002137 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2138
2139 /*
2140 * Parse the PKCS#8 format
2141 */
2142
2143 p = p_alt;
2144 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2145 {
2146#if defined(POLARSSL_PEM_C)
2147 pem_free( &pem );
2148#endif
2149 rsa_free( rsa );
2150 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2151 }
2152
2153 if( ( end - p ) < 1 )
2154 {
2155#if defined(POLARSSL_PEM_C)
2156 pem_free( &pem );
2157#endif
2158 rsa_free( rsa );
2159 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2160 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2161 }
2162
2163 end = p + len;
2164
2165 if( ( ret = asn1_get_tag( &p, end, &len,
2166 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2167 {
2168#if defined(POLARSSL_PEM_C)
2169 pem_free( &pem );
2170#endif
2171 rsa_free( rsa );
2172 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2173 }
2174
2175 end = p + len;
2176
2177 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2178 {
2179#if defined(POLARSSL_PEM_C)
2180 pem_free( &pem );
2181#endif
2182 rsa_free( rsa );
2183 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2184 }
2185
2186 if( rsa->ver != 0 )
2187 {
2188#if defined(POLARSSL_PEM_C)
2189 pem_free( &pem );
2190#endif
2191 rsa_free( rsa );
2192 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2193 }
2194 }
2195
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2197 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2198 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2199 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2200 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2201 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2202 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2203 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2204 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002205#if defined(POLARSSL_PEM_C)
2206 pem_free( &pem );
2207#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002209 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 }
2211
2212 rsa->len = mpi_size( &rsa->N );
2213
2214 if( p != end )
2215 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002216#if defined(POLARSSL_PEM_C)
2217 pem_free( &pem );
2218#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002220 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002221 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 }
2223
2224 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2225 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002226#if defined(POLARSSL_PEM_C)
2227 pem_free( &pem );
2228#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 rsa_free( rsa );
2230 return( ret );
2231 }
2232
Paul Bakker96743fc2011-02-12 14:30:57 +00002233#if defined(POLARSSL_PEM_C)
2234 pem_free( &pem );
2235#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
2237 return( 0 );
2238}
2239
2240/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002241 * Parse a public RSA key
2242 */
Paul Bakker23986e52011-04-24 08:57:21 +00002243int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002244{
Paul Bakker23986e52011-04-24 08:57:21 +00002245 int ret;
2246 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002247 unsigned char *p, *end;
2248 x509_buf alg_oid;
2249#if defined(POLARSSL_PEM_C)
2250 pem_context pem;
2251
2252 pem_init( &pem );
2253 ret = pem_read_buffer( &pem,
2254 "-----BEGIN PUBLIC KEY-----",
2255 "-----END PUBLIC KEY-----",
2256 key, NULL, 0, &len );
2257
2258 if( ret == 0 )
2259 {
2260 /*
2261 * Was PEM encoded
2262 */
2263 keylen = pem.buflen;
2264 }
2265 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2266 {
2267 pem_free( &pem );
2268 return( ret );
2269 }
2270
2271 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2272#else
2273 p = (unsigned char *) key;
2274#endif
2275 end = p + keylen;
2276
2277 /*
2278 * PublicKeyInfo ::= SEQUENCE {
2279 * algorithm AlgorithmIdentifier,
2280 * PublicKey BIT STRING
2281 * }
2282 *
2283 * AlgorithmIdentifier ::= SEQUENCE {
2284 * algorithm OBJECT IDENTIFIER,
2285 * parameters ANY DEFINED BY algorithm OPTIONAL
2286 * }
2287 *
2288 * RSAPublicKey ::= SEQUENCE {
2289 * modulus INTEGER, -- n
2290 * publicExponent INTEGER -- e
2291 * }
2292 */
2293
2294 if( ( ret = asn1_get_tag( &p, end, &len,
2295 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2296 {
2297#if defined(POLARSSL_PEM_C)
2298 pem_free( &pem );
2299#endif
2300 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002301 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002302 }
2303
2304 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2305 {
2306#if defined(POLARSSL_PEM_C)
2307 pem_free( &pem );
2308#endif
2309 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002310 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002311 }
2312
2313 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2314 {
2315#if defined(POLARSSL_PEM_C)
2316 pem_free( &pem );
2317#endif
2318 rsa_free( rsa );
2319 return( ret );
2320 }
2321
2322 rsa->len = mpi_size( &rsa->N );
2323
2324#if defined(POLARSSL_PEM_C)
2325 pem_free( &pem );
2326#endif
2327
2328 return( 0 );
2329}
2330
Paul Bakkereaa89f82011-04-04 21:36:15 +00002331#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002332/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002333 * Parse DHM parameters
2334 */
Paul Bakker23986e52011-04-24 08:57:21 +00002335int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002336{
Paul Bakker23986e52011-04-24 08:57:21 +00002337 int ret;
2338 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002339 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002340#if defined(POLARSSL_PEM_C)
2341 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002342
Paul Bakker96743fc2011-02-12 14:30:57 +00002343 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002344
Paul Bakker96743fc2011-02-12 14:30:57 +00002345 ret = pem_read_buffer( &pem,
2346 "-----BEGIN DH PARAMETERS-----",
2347 "-----END DH PARAMETERS-----",
2348 dhmin, NULL, 0, &dhminlen );
2349
2350 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002351 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002352 /*
2353 * Was PEM encoded
2354 */
2355 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002356 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002357 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002358 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002359 pem_free( &pem );
2360 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002361 }
2362
Paul Bakker96743fc2011-02-12 14:30:57 +00002363 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2364#else
2365 p = (unsigned char *) dhmin;
2366#endif
2367 end = p + dhminlen;
2368
Paul Bakker1b57b062011-01-06 15:48:19 +00002369 memset( dhm, 0, sizeof( dhm_context ) );
2370
Paul Bakker1b57b062011-01-06 15:48:19 +00002371 /*
2372 * DHParams ::= SEQUENCE {
2373 * prime INTEGER, -- P
2374 * generator INTEGER, -- g
2375 * }
2376 */
2377 if( ( ret = asn1_get_tag( &p, end, &len,
2378 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2379 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002380#if defined(POLARSSL_PEM_C)
2381 pem_free( &pem );
2382#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002383 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002384 }
2385
2386 end = p + len;
2387
2388 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2389 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2390 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002391#if defined(POLARSSL_PEM_C)
2392 pem_free( &pem );
2393#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002394 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002395 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002396 }
2397
2398 if( p != end )
2399 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002400#if defined(POLARSSL_PEM_C)
2401 pem_free( &pem );
2402#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002403 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002404 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002405 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2406 }
2407
Paul Bakker96743fc2011-02-12 14:30:57 +00002408#if defined(POLARSSL_PEM_C)
2409 pem_free( &pem );
2410#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002411
2412 return( 0 );
2413}
2414
Paul Bakker335db3f2011-04-25 15:28:35 +00002415#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002416/*
2417 * Load and parse a private RSA key
2418 */
2419int x509parse_dhmfile( dhm_context *dhm, const char *path )
2420{
2421 int ret;
2422 size_t n;
2423 unsigned char *buf;
2424
Paul Bakker69e095c2011-12-10 21:55:01 +00002425 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2426 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002427
Paul Bakker27fdf462011-06-09 13:55:13 +00002428 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002429
2430 memset( buf, 0, n + 1 );
2431 free( buf );
2432
2433 return( ret );
2434}
Paul Bakker335db3f2011-04-25 15:28:35 +00002435#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002436#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002437
Paul Bakker5121ce52009-01-03 21:22:43 +00002438#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002439#include <stdarg.h>
2440
2441#if !defined vsnprintf
2442#define vsnprintf _vsnprintf
2443#endif // vsnprintf
2444
2445/*
2446 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2447 * Result value is not size of buffer needed, but -1 if no fit is possible.
2448 *
2449 * This fuction tries to 'fix' this by at least suggesting enlarging the
2450 * size by 20.
2451 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002452static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002453{
2454 va_list ap;
2455 int res = -1;
2456
2457 va_start( ap, format );
2458
2459 res = vsnprintf( str, size, format, ap );
2460
2461 va_end( ap );
2462
2463 // No quick fix possible
2464 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002465 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002466
2467 return res;
2468}
2469
2470#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002471#endif
2472
Paul Bakkerd98030e2009-05-02 15:13:40 +00002473#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2474
2475#define SAFE_SNPRINTF() \
2476{ \
2477 if( ret == -1 ) \
2478 return( -1 ); \
2479 \
Paul Bakker23986e52011-04-24 08:57:21 +00002480 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002481 p[n - 1] = '\0'; \
2482 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2483 } \
2484 \
Paul Bakker23986e52011-04-24 08:57:21 +00002485 n -= (unsigned int) ret; \
2486 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002487}
2488
Paul Bakker5121ce52009-01-03 21:22:43 +00002489/*
2490 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002491 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002493int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002494{
Paul Bakker23986e52011-04-24 08:57:21 +00002495 int ret;
2496 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002498 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002499 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 char s[128], *p;
2501
2502 memset( s, 0, sizeof( s ) );
2503
2504 name = dn;
2505 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002506 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
2508 while( name != NULL )
2509 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002510 if( !name->oid.p )
2511 {
2512 name = name->next;
2513 continue;
2514 }
2515
Paul Bakker74111d32011-01-15 16:57:55 +00002516 if( name != dn )
2517 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002518 ret = snprintf( p, n, ", " );
2519 SAFE_SNPRINTF();
2520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Paul Bakkerc70b9822013-04-07 22:00:46 +02002522 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Paul Bakkerc70b9822013-04-07 22:00:46 +02002524 if( ret == 0 )
2525 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002527 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002528 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
2530 for( i = 0; i < name->val.len; i++ )
2531 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002532 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 break;
2534
2535 c = name->val.p[i];
2536 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2537 s[i] = '?';
2538 else s[i] = c;
2539 }
2540 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002541 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002542 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 name = name->next;
2544 }
2545
Paul Bakker23986e52011-04-24 08:57:21 +00002546 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002547}
2548
2549/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002550 * Store the serial in printable form into buf; no more
2551 * than size characters will be written
2552 */
2553int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2554{
Paul Bakker23986e52011-04-24 08:57:21 +00002555 int ret;
2556 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002557 char *p;
2558
2559 p = buf;
2560 n = size;
2561
2562 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002563 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002564
2565 for( i = 0; i < nr; i++ )
2566 {
Paul Bakker93048802011-12-05 14:38:06 +00002567 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002568 continue;
2569
Paul Bakkerdd476992011-01-16 21:34:59 +00002570 ret = snprintf( p, n, "%02X%s",
2571 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2572 SAFE_SNPRINTF();
2573 }
2574
Paul Bakker03c7c252011-11-25 12:37:37 +00002575 if( nr != serial->len )
2576 {
2577 ret = snprintf( p, n, "...." );
2578 SAFE_SNPRINTF();
2579 }
2580
Paul Bakker23986e52011-04-24 08:57:21 +00002581 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002582}
2583
2584/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002585 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002587int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2588 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002589{
Paul Bakker23986e52011-04-24 08:57:21 +00002590 int ret;
2591 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002592 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002593 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
2595 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002596 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
Paul Bakkerd98030e2009-05-02 15:13:40 +00002598 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002600 SAFE_SNPRINTF();
2601 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002603 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Paul Bakkerdd476992011-01-16 21:34:59 +00002605 ret = x509parse_serial_gets( p, n, &crt->serial);
2606 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Paul Bakkerd98030e2009-05-02 15:13:40 +00002608 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2609 SAFE_SNPRINTF();
2610 ret = x509parse_dn_gets( p, n, &crt->issuer );
2611 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Paul Bakkerd98030e2009-05-02 15:13:40 +00002613 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2614 SAFE_SNPRINTF();
2615 ret = x509parse_dn_gets( p, n, &crt->subject );
2616 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
Paul Bakkerd98030e2009-05-02 15:13:40 +00002618 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2620 crt->valid_from.year, crt->valid_from.mon,
2621 crt->valid_from.day, crt->valid_from.hour,
2622 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002623 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Paul Bakkerd98030e2009-05-02 15:13:40 +00002625 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2627 crt->valid_to.year, crt->valid_to.mon,
2628 crt->valid_to.day, crt->valid_to.hour,
2629 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002630 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Paul Bakkerc70b9822013-04-07 22:00:46 +02002632 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
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 = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2636 if( ret != 0 )
2637 ret = snprintf( p, n, "???" );
2638 else
2639 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002640 SAFE_SNPRINTF();
2641
2642 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002643 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002644 SAFE_SNPRINTF();
2645
Paul Bakker23986e52011-04-24 08:57:21 +00002646 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002647}
2648
Paul Bakker74111d32011-01-15 16:57:55 +00002649/*
2650 * Return an informational string describing the given OID
2651 */
2652const char *x509_oid_get_description( x509_buf *oid )
2653{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002654 const char *desc = NULL;
2655 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002656
Paul Bakkerc70b9822013-04-07 22:00:46 +02002657 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002658
Paul Bakkerc70b9822013-04-07 22:00:46 +02002659 if( ret != 0 )
2660 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002661
Paul Bakkerc70b9822013-04-07 22:00:46 +02002662 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002663}
2664
2665/* Return the x.y.z.... style numeric string for the given OID */
2666int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2667{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002668 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002669}
2670
Paul Bakkerd98030e2009-05-02 15:13:40 +00002671/*
2672 * Return an informational string about the CRL.
2673 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002674int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2675 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002676{
Paul Bakker23986e52011-04-24 08:57:21 +00002677 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002678 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002679 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002680 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002681 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002682
2683 p = buf;
2684 n = size;
2685
2686 ret = snprintf( p, n, "%sCRL version : %d",
2687 prefix, crl->version );
2688 SAFE_SNPRINTF();
2689
2690 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2691 SAFE_SNPRINTF();
2692 ret = x509parse_dn_gets( p, n, &crl->issuer );
2693 SAFE_SNPRINTF();
2694
2695 ret = snprintf( p, n, "\n%sthis update : " \
2696 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2697 crl->this_update.year, crl->this_update.mon,
2698 crl->this_update.day, crl->this_update.hour,
2699 crl->this_update.min, crl->this_update.sec );
2700 SAFE_SNPRINTF();
2701
2702 ret = snprintf( p, n, "\n%snext update : " \
2703 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2704 crl->next_update.year, crl->next_update.mon,
2705 crl->next_update.day, crl->next_update.hour,
2706 crl->next_update.min, crl->next_update.sec );
2707 SAFE_SNPRINTF();
2708
2709 entry = &crl->entry;
2710
2711 ret = snprintf( p, n, "\n%sRevoked certificates:",
2712 prefix );
2713 SAFE_SNPRINTF();
2714
Paul Bakker9be19372009-07-27 20:21:53 +00002715 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002716 {
2717 ret = snprintf( p, n, "\n%sserial number: ",
2718 prefix );
2719 SAFE_SNPRINTF();
2720
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002721 ret = x509parse_serial_gets( p, n, &entry->serial);
2722 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002723
Paul Bakkerd98030e2009-05-02 15:13:40 +00002724 ret = snprintf( p, n, " revocation date: " \
2725 "%04d-%02d-%02d %02d:%02d:%02d",
2726 entry->revocation_date.year, entry->revocation_date.mon,
2727 entry->revocation_date.day, entry->revocation_date.hour,
2728 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002729 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002730
2731 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 }
2733
Paul Bakkerc70b9822013-04-07 22:00:46 +02002734 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002735 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Paul Bakkerc70b9822013-04-07 22:00:46 +02002737 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2738 if( ret != 0 )
2739 ret = snprintf( p, n, "???" );
2740 else
2741 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002742 SAFE_SNPRINTF();
2743
Paul Bakker1e27bb22009-07-19 20:25:25 +00002744 ret = snprintf( p, n, "\n" );
2745 SAFE_SNPRINTF();
2746
Paul Bakker23986e52011-04-24 08:57:21 +00002747 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748}
2749
2750/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002751 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002753int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002754{
Paul Bakkercce9d772011-11-18 14:26:47 +00002755 int year, mon, day;
2756 int hour, min, sec;
2757
2758#if defined(_WIN32)
2759 SYSTEMTIME st;
2760
2761 GetLocalTime(&st);
2762
2763 year = st.wYear;
2764 mon = st.wMonth;
2765 day = st.wDay;
2766 hour = st.wHour;
2767 min = st.wMinute;
2768 sec = st.wSecond;
2769#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 struct tm *lt;
2771 time_t tt;
2772
2773 tt = time( NULL );
2774 lt = localtime( &tt );
2775
Paul Bakkercce9d772011-11-18 14:26:47 +00002776 year = lt->tm_year + 1900;
2777 mon = lt->tm_mon + 1;
2778 day = lt->tm_mday;
2779 hour = lt->tm_hour;
2780 min = lt->tm_min;
2781 sec = lt->tm_sec;
2782#endif
2783
2784 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002785 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Paul Bakkercce9d772011-11-18 14:26:47 +00002787 if( year == to->year &&
2788 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002789 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
Paul Bakkercce9d772011-11-18 14:26:47 +00002791 if( year == to->year &&
2792 mon == to->mon &&
2793 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002794 return( 1 );
2795
Paul Bakkercce9d772011-11-18 14:26:47 +00002796 if( year == to->year &&
2797 mon == to->mon &&
2798 day == to->day &&
2799 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002800 return( 1 );
2801
Paul Bakkercce9d772011-11-18 14:26:47 +00002802 if( year == to->year &&
2803 mon == to->mon &&
2804 day == to->day &&
2805 hour == to->hour &&
2806 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002807 return( 1 );
2808
Paul Bakkercce9d772011-11-18 14:26:47 +00002809 if( year == to->year &&
2810 mon == to->mon &&
2811 day == to->day &&
2812 hour == to->hour &&
2813 min == to->min &&
2814 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002815 return( 1 );
2816
Paul Bakker40ea7de2009-05-03 10:18:48 +00002817 return( 0 );
2818}
2819
2820/*
2821 * Return 1 if the certificate is revoked, or 0 otherwise.
2822 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002823int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002824{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002825 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002826
2827 while( cur != NULL && cur->serial.len != 0 )
2828 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002829 if( crt->serial.len == cur->serial.len &&
2830 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002831 {
2832 if( x509parse_time_expired( &cur->revocation_date ) )
2833 return( 1 );
2834 }
2835
2836 cur = cur->next;
2837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
2839 return( 0 );
2840}
2841
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002842/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002843 * Check that the given certificate is valid accoring to the CRL.
2844 */
2845static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2846 x509_crl *crl_list)
2847{
2848 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002849 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2850 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002851
Paul Bakker915275b2012-09-28 07:10:55 +00002852 if( ca == NULL )
2853 return( flags );
2854
Paul Bakker76fd75a2011-01-16 21:12:10 +00002855 /*
2856 * TODO: What happens if no CRL is present?
2857 * Suggestion: Revocation state should be unknown if no CRL is present.
2858 * For backwards compatibility this is not yet implemented.
2859 */
2860
Paul Bakker915275b2012-09-28 07:10:55 +00002861 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002862 {
Paul Bakker915275b2012-09-28 07:10:55 +00002863 if( crl_list->version == 0 ||
2864 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00002865 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2866 crl_list->issuer_raw.len ) != 0 )
2867 {
2868 crl_list = crl_list->next;
2869 continue;
2870 }
2871
2872 /*
2873 * Check if CRL is correctly signed by the trusted CA
2874 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002875 md_info = md_info_from_type( crl_list->sig_md );
2876 if( md_info == NULL )
2877 {
2878 /*
2879 * Cannot check 'unknown' hash
2880 */
2881 flags |= BADCRL_NOT_TRUSTED;
2882 break;
2883 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002884
Paul Bakkerc70b9822013-04-07 22:00:46 +02002885 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002886
Paul Bakkerc70b9822013-04-07 22:00:46 +02002887 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00002888 0, hash, crl_list->sig.p ) == 0 )
2889 {
2890 /*
2891 * CRL is not trusted
2892 */
2893 flags |= BADCRL_NOT_TRUSTED;
2894 break;
2895 }
2896
2897 /*
2898 * Check for validity of CRL (Do not drop out)
2899 */
2900 if( x509parse_time_expired( &crl_list->next_update ) )
2901 flags |= BADCRL_EXPIRED;
2902
2903 /*
2904 * Check if certificate is revoked
2905 */
2906 if( x509parse_revoked(crt, crl_list) )
2907 {
2908 flags |= BADCERT_REVOKED;
2909 break;
2910 }
2911
2912 crl_list = crl_list->next;
2913 }
2914 return flags;
2915}
2916
Paul Bakker57b12982012-02-11 17:38:38 +00002917int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002918{
2919 size_t i;
2920 size_t cn_idx = 0;
2921
Paul Bakker57b12982012-02-11 17:38:38 +00002922 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002923 return( 0 );
2924
2925 for( i = 0; i < strlen( cn ); ++i )
2926 {
2927 if( cn[i] == '.' )
2928 {
2929 cn_idx = i;
2930 break;
2931 }
2932 }
2933
2934 if( cn_idx == 0 )
2935 return( 0 );
2936
Paul Bakker535e97d2012-08-23 10:49:55 +00002937 if( strlen( cn ) - cn_idx == name->len - 1 &&
2938 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002939 {
2940 return( 1 );
2941 }
2942
2943 return( 0 );
2944}
2945
Paul Bakker915275b2012-09-28 07:10:55 +00002946static int x509parse_verify_top(
2947 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00002948 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00002949 int (*f_vrfy)(void *, x509_cert *, int, int *),
2950 void *p_vrfy )
2951{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002952 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00002953 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002954 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2955 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00002956
2957 if( x509parse_time_expired( &child->valid_to ) )
2958 *flags |= BADCERT_EXPIRED;
2959
2960 /*
2961 * Child is the top of the chain. Check against the trust_ca list.
2962 */
2963 *flags |= BADCERT_NOT_TRUSTED;
2964
2965 while( trust_ca != NULL )
2966 {
2967 if( trust_ca->version == 0 ||
2968 child->issuer_raw.len != trust_ca->subject_raw.len ||
2969 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
2970 child->issuer_raw.len ) != 0 )
2971 {
2972 trust_ca = trust_ca->next;
2973 continue;
2974 }
2975
Paul Bakker9a736322012-11-14 12:39:52 +00002976 /*
2977 * Reduce path_len to check against if top of the chain is
2978 * the same as the trusted CA
2979 */
2980 if( child->subject_raw.len == trust_ca->subject_raw.len &&
2981 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
2982 child->issuer_raw.len ) == 0 )
2983 {
2984 check_path_cnt--;
2985 }
2986
Paul Bakker915275b2012-09-28 07:10:55 +00002987 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00002988 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00002989 {
2990 trust_ca = trust_ca->next;
2991 continue;
2992 }
2993
Paul Bakkerc70b9822013-04-07 22:00:46 +02002994 md_info = md_info_from_type( child->sig_md );
2995 if( md_info == NULL )
2996 {
2997 /*
2998 * Cannot check 'unknown' hash
2999 */
3000 continue;
3001 }
Paul Bakker915275b2012-09-28 07:10:55 +00003002
Paul Bakkerc70b9822013-04-07 22:00:46 +02003003 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003004
Paul Bakkerc70b9822013-04-07 22:00:46 +02003005 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003006 0, hash, child->sig.p ) != 0 )
3007 {
3008 trust_ca = trust_ca->next;
3009 continue;
3010 }
3011
3012 /*
3013 * Top of chain is signed by a trusted CA
3014 */
3015 *flags &= ~BADCERT_NOT_TRUSTED;
3016 break;
3017 }
3018
Paul Bakker9a736322012-11-14 12:39:52 +00003019 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003020 * If top of chain is not the same as the trusted CA send a verify request
3021 * to the callback for any issues with validity and CRL presence for the
3022 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003023 */
3024 if( trust_ca != NULL &&
3025 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3026 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3027 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003028 {
3029 /* Check trusted CA's CRL for then chain's top crt */
3030 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3031
3032 if( x509parse_time_expired( &trust_ca->valid_to ) )
3033 ca_flags |= BADCERT_EXPIRED;
3034
Paul Bakker915275b2012-09-28 07:10:55 +00003035 if( NULL != f_vrfy )
3036 {
Paul Bakker9a736322012-11-14 12:39:52 +00003037 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003038 return( ret );
3039 }
3040 }
3041
3042 /* Call callback on top cert */
3043 if( NULL != f_vrfy )
3044 {
Paul Bakker9a736322012-11-14 12:39:52 +00003045 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003046 return( ret );
3047 }
3048
Paul Bakker915275b2012-09-28 07:10:55 +00003049 *flags |= ca_flags;
3050
3051 return( 0 );
3052}
3053
3054static int x509parse_verify_child(
3055 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003056 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003057 int (*f_vrfy)(void *, x509_cert *, int, int *),
3058 void *p_vrfy )
3059{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003060 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003061 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003062 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003063 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003064 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003065
3066 if( x509parse_time_expired( &child->valid_to ) )
3067 *flags |= BADCERT_EXPIRED;
3068
Paul Bakkerc70b9822013-04-07 22:00:46 +02003069 md_info = md_info_from_type( child->sig_md );
3070 if( md_info == NULL )
3071 {
3072 /*
3073 * Cannot check 'unknown' hash
3074 */
Paul Bakker915275b2012-09-28 07:10:55 +00003075 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003076 }
3077 else
3078 {
3079 md( md_info, child->tbs.p, child->tbs.len, hash );
3080
3081 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3082 child->sig.p ) != 0 )
3083 *flags |= BADCERT_NOT_TRUSTED;
3084 }
3085
Paul Bakker915275b2012-09-28 07:10:55 +00003086 /* Check trusted CA's CRL for the given crt */
3087 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3088
3089 grandparent = parent->next;
3090
3091 while( grandparent != NULL )
3092 {
3093 if( grandparent->version == 0 ||
3094 grandparent->ca_istrue == 0 ||
3095 parent->issuer_raw.len != grandparent->subject_raw.len ||
3096 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3097 parent->issuer_raw.len ) != 0 )
3098 {
3099 grandparent = grandparent->next;
3100 continue;
3101 }
3102 break;
3103 }
3104
Paul Bakker915275b2012-09-28 07:10:55 +00003105 if( grandparent != NULL )
3106 {
3107 /*
3108 * Part of the chain
3109 */
Paul Bakker9a736322012-11-14 12:39:52 +00003110 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 +00003111 if( ret != 0 )
3112 return( ret );
3113 }
3114 else
3115 {
Paul Bakker9a736322012-11-14 12:39:52 +00003116 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 +00003117 if( ret != 0 )
3118 return( ret );
3119 }
3120
3121 /* child is verified to be a child of the parent, call verify callback */
3122 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003123 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003124 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003125
3126 *flags |= parent_flags;
3127
3128 return( 0 );
3129}
3130
Paul Bakker76fd75a2011-01-16 21:12:10 +00003131/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 * Verify the certificate validity
3133 */
3134int x509parse_verify( x509_cert *crt,
3135 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003136 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003137 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003138 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003139 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003140{
Paul Bakker23986e52011-04-24 08:57:21 +00003141 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003142 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003143 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003144 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003146 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003147
Paul Bakker40ea7de2009-05-03 10:18:48 +00003148 *flags = 0;
3149
Paul Bakker5121ce52009-01-03 21:22:43 +00003150 if( cn != NULL )
3151 {
3152 name = &crt->subject;
3153 cn_len = strlen( cn );
3154
Paul Bakker4d2c1242012-05-10 14:12:46 +00003155 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003156 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003157 cur = &crt->subject_alt_names;
3158
3159 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003160 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003161 if( cur->buf.len == cn_len &&
3162 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003163 break;
3164
Paul Bakker535e97d2012-08-23 10:49:55 +00003165 if( cur->buf.len > 2 &&
3166 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003167 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003168 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003169
Paul Bakker4d2c1242012-05-10 14:12:46 +00003170 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003171 }
3172
3173 if( cur == NULL )
3174 *flags |= BADCERT_CN_MISMATCH;
3175 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003176 else
3177 {
3178 while( name != NULL )
3179 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003180 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003181 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003182 if( name->val.len == cn_len &&
3183 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003184 break;
3185
Paul Bakker535e97d2012-08-23 10:49:55 +00003186 if( name->val.len > 2 &&
3187 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003188 x509_wildcard_verify( cn, &name->val ) )
3189 break;
3190 }
3191
3192 name = name->next;
3193 }
3194
3195 if( name == NULL )
3196 *flags |= BADCERT_CN_MISMATCH;
3197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 }
3199
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003201 * Iterate upwards in the given cert chain, to find our crt parent.
3202 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003204 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003205
Paul Bakker76fd75a2011-01-16 21:12:10 +00003206 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003208 if( parent->ca_istrue == 0 ||
3209 crt->issuer_raw.len != parent->subject_raw.len ||
3210 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 crt->issuer_raw.len ) != 0 )
3212 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003213 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 continue;
3215 }
Paul Bakker915275b2012-09-28 07:10:55 +00003216 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 }
3218
Paul Bakker915275b2012-09-28 07:10:55 +00003219 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 {
Paul Bakker915275b2012-09-28 07:10:55 +00003221 /*
3222 * Part of the chain
3223 */
Paul Bakker9a736322012-11-14 12:39:52 +00003224 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003225 if( ret != 0 )
3226 return( ret );
3227 }
3228 else
Paul Bakker74111d32011-01-15 16:57:55 +00003229 {
Paul Bakker9a736322012-11-14 12:39:52 +00003230 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003231 if( ret != 0 )
3232 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003233 }
Paul Bakker915275b2012-09-28 07:10:55 +00003234
3235 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003236 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003237
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 return( 0 );
3239}
3240
3241/*
3242 * Unallocate all certificate data
3243 */
3244void x509_free( x509_cert *crt )
3245{
3246 x509_cert *cert_cur = crt;
3247 x509_cert *cert_prv;
3248 x509_name *name_cur;
3249 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003250 x509_sequence *seq_cur;
3251 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003252
3253 if( crt == NULL )
3254 return;
3255
3256 do
3257 {
3258 rsa_free( &cert_cur->rsa );
3259
3260 name_cur = cert_cur->issuer.next;
3261 while( name_cur != NULL )
3262 {
3263 name_prv = name_cur;
3264 name_cur = name_cur->next;
3265 memset( name_prv, 0, sizeof( x509_name ) );
3266 free( name_prv );
3267 }
3268
3269 name_cur = cert_cur->subject.next;
3270 while( name_cur != NULL )
3271 {
3272 name_prv = name_cur;
3273 name_cur = name_cur->next;
3274 memset( name_prv, 0, sizeof( x509_name ) );
3275 free( name_prv );
3276 }
3277
Paul Bakker74111d32011-01-15 16:57:55 +00003278 seq_cur = cert_cur->ext_key_usage.next;
3279 while( seq_cur != NULL )
3280 {
3281 seq_prv = seq_cur;
3282 seq_cur = seq_cur->next;
3283 memset( seq_prv, 0, sizeof( x509_sequence ) );
3284 free( seq_prv );
3285 }
3286
Paul Bakker8afa70d2012-02-11 18:42:45 +00003287 seq_cur = cert_cur->subject_alt_names.next;
3288 while( seq_cur != NULL )
3289 {
3290 seq_prv = seq_cur;
3291 seq_cur = seq_cur->next;
3292 memset( seq_prv, 0, sizeof( x509_sequence ) );
3293 free( seq_prv );
3294 }
3295
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 if( cert_cur->raw.p != NULL )
3297 {
3298 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3299 free( cert_cur->raw.p );
3300 }
3301
3302 cert_cur = cert_cur->next;
3303 }
3304 while( cert_cur != NULL );
3305
3306 cert_cur = crt;
3307 do
3308 {
3309 cert_prv = cert_cur;
3310 cert_cur = cert_cur->next;
3311
3312 memset( cert_prv, 0, sizeof( x509_cert ) );
3313 if( cert_prv != crt )
3314 free( cert_prv );
3315 }
3316 while( cert_cur != NULL );
3317}
3318
Paul Bakkerd98030e2009-05-02 15:13:40 +00003319/*
3320 * Unallocate all CRL data
3321 */
3322void x509_crl_free( x509_crl *crl )
3323{
3324 x509_crl *crl_cur = crl;
3325 x509_crl *crl_prv;
3326 x509_name *name_cur;
3327 x509_name *name_prv;
3328 x509_crl_entry *entry_cur;
3329 x509_crl_entry *entry_prv;
3330
3331 if( crl == NULL )
3332 return;
3333
3334 do
3335 {
3336 name_cur = crl_cur->issuer.next;
3337 while( name_cur != NULL )
3338 {
3339 name_prv = name_cur;
3340 name_cur = name_cur->next;
3341 memset( name_prv, 0, sizeof( x509_name ) );
3342 free( name_prv );
3343 }
3344
3345 entry_cur = crl_cur->entry.next;
3346 while( entry_cur != NULL )
3347 {
3348 entry_prv = entry_cur;
3349 entry_cur = entry_cur->next;
3350 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3351 free( entry_prv );
3352 }
3353
3354 if( crl_cur->raw.p != NULL )
3355 {
3356 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3357 free( crl_cur->raw.p );
3358 }
3359
3360 crl_cur = crl_cur->next;
3361 }
3362 while( crl_cur != NULL );
3363
3364 crl_cur = crl;
3365 do
3366 {
3367 crl_prv = crl_cur;
3368 crl_cur = crl_cur->next;
3369
3370 memset( crl_prv, 0, sizeof( x509_crl ) );
3371 if( crl_prv != crl )
3372 free( crl_prv );
3373 }
3374 while( crl_cur != NULL );
3375}
3376
Paul Bakker40e46942009-01-03 21:51:57 +00003377#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003378
Paul Bakker40e46942009-01-03 21:51:57 +00003379#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003380
3381/*
3382 * Checkup routine
3383 */
3384int x509_self_test( int verbose )
3385{
Paul Bakker5690efc2011-05-26 13:16:06 +00003386#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003387 int ret;
3388 int flags;
3389 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 x509_cert cacert;
3391 x509_cert clicert;
3392 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003393#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003394 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003395#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003396
3397 if( verbose != 0 )
3398 printf( " X.509 certificate load: " );
3399
3400 memset( &clicert, 0, sizeof( x509_cert ) );
3401
3402 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003403 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 if( ret != 0 )
3405 {
3406 if( verbose != 0 )
3407 printf( "failed\n" );
3408
3409 return( ret );
3410 }
3411
3412 memset( &cacert, 0, sizeof( x509_cert ) );
3413
3414 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003415 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003416 if( ret != 0 )
3417 {
3418 if( verbose != 0 )
3419 printf( "failed\n" );
3420
3421 return( ret );
3422 }
3423
3424 if( verbose != 0 )
3425 printf( "passed\n X.509 private key load: " );
3426
3427 i = strlen( test_ca_key );
3428 j = strlen( test_ca_pwd );
3429
Paul Bakker66b78b22011-03-25 14:22:50 +00003430 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3431
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 if( ( ret = x509parse_key( &rsa,
3433 (unsigned char *) test_ca_key, i,
3434 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3435 {
3436 if( verbose != 0 )
3437 printf( "failed\n" );
3438
3439 return( ret );
3440 }
3441
3442 if( verbose != 0 )
3443 printf( "passed\n X.509 signature verify: ");
3444
Paul Bakker23986e52011-04-24 08:57:21 +00003445 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003446 if( ret != 0 )
3447 {
Paul Bakker23986e52011-04-24 08:57:21 +00003448 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 if( verbose != 0 )
3450 printf( "failed\n" );
3451
3452 return( ret );
3453 }
3454
Paul Bakker5690efc2011-05-26 13:16:06 +00003455#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003456 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003457 printf( "passed\n X.509 DHM parameter load: " );
3458
3459 i = strlen( test_dhm_params );
3460 j = strlen( test_ca_pwd );
3461
3462 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3463 {
3464 if( verbose != 0 )
3465 printf( "failed\n" );
3466
3467 return( ret );
3468 }
3469
3470 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003472#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003473
3474 x509_free( &cacert );
3475 x509_free( &clicert );
3476 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003477#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003478 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003479#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003480
3481 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003482#else
3483 ((void) verbose);
3484 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3485#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003486}
3487
3488#endif
3489
3490#endif