blob: 7fd167291437e49be8743f0d62d33c7ac553a752 [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/*
26 * The ITU-T X.509 standard defines a certificat format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc2459.txt
29 * http://www.ietf.org/rfc/rfc3279.txt
30 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000043#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/des.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010045#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000046#include "polarssl/md2.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010047#endif
48#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/md4.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010050#endif
51#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/md5.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010053#endif
54#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000055#include "polarssl/sha1.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010056#endif
57#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000058#include "polarssl/sha2.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010059#endif
60#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000061#include "polarssl/sha4.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010062#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000063#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65#include <string.h>
66#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000067#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000068#include <windows.h>
69#else
Paul Bakker5121ce52009-01-03 21:22:43 +000070#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Paul Bakker335db3f2011-04-25 15:28:35 +000073#if defined(POLARSSL_FS_IO)
74#include <stdio.h>
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
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_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
163/*
164 * AlgorithmIdentifier ::= SEQUENCE {
165 * algorithm OBJECT IDENTIFIER,
166 * parameters ANY DEFINED BY algorithm OPTIONAL }
167 */
168static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000169 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 x509_buf *alg )
171{
Paul Bakker23986e52011-04-24 08:57:21 +0000172 int ret;
173 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175 if( ( ret = asn1_get_tag( p, end, &len,
176 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000177 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 end = *p + len;
180 alg->tag = **p;
181
182 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000183 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 alg->p = *p;
186 *p += alg->len;
187
188 if( *p == end )
189 return( 0 );
190
191 /*
192 * assume the algorithm parameters must be NULL
193 */
194 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000195 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000198 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000199 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201 return( 0 );
202}
203
204/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 * AttributeTypeAndValue ::= SEQUENCE {
206 * type AttributeType,
207 * value AttributeValue }
208 *
209 * AttributeType ::= OBJECT IDENTIFIER
210 *
211 * AttributeValue ::= ANY DEFINED BY AttributeType
212 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000213static int x509_get_attr_type_value( unsigned char **p,
214 const unsigned char *end,
215 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216{
Paul Bakker23986e52011-04-24 08:57:21 +0000217 int ret;
218 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 x509_buf *oid;
220 x509_buf *val;
221
222 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 oid = &cur->oid;
227 oid->tag = **p;
228
229 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000230 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232 oid->p = *p;
233 *p += oid->len;
234
235 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000236 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000237 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
240 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
241 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000242 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000243 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245 val = &cur->val;
246 val->tag = *(*p)++;
247
248 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000249 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251 val->p = *p;
252 *p += val->len;
253
254 cur->next = NULL;
255
Paul Bakker400ff6f2011-02-20 10:40:16 +0000256 return( 0 );
257}
258
259/*
260 * RelativeDistinguishedName ::=
261 * SET OF AttributeTypeAndValue
262 *
263 * AttributeTypeAndValue ::= SEQUENCE {
264 * type AttributeType,
265 * value AttributeValue }
266 *
267 * AttributeType ::= OBJECT IDENTIFIER
268 *
269 * AttributeValue ::= ANY DEFINED BY AttributeType
270 */
271static int x509_get_name( unsigned char **p,
272 const unsigned char *end,
273 x509_name *cur )
274{
Paul Bakker23986e52011-04-24 08:57:21 +0000275 int ret;
276 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277 const unsigned char *end2;
278 x509_name *use;
279
280 if( ( ret = asn1_get_tag( p, end, &len,
281 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000282 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000283
284 end2 = end;
285 end = *p + len;
286 use = cur;
287
288 do
289 {
290 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
291 return( ret );
292
293 if( *p != end )
294 {
295 use->next = (x509_name *) malloc(
296 sizeof( x509_name ) );
297
298 if( use->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +0000299 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000300
301 memset( use->next, 0, sizeof( x509_name ) );
302
303 use = use->next;
304 }
305 }
306 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308 /*
309 * recurse until end of SEQUENCE is reached
310 */
311 if( *p == end2 )
312 return( 0 );
313
314 cur->next = (x509_name *) malloc(
315 sizeof( x509_name ) );
316
317 if( cur->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +0000318 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Paul Bakker07156682012-05-30 07:33:30 +0000320 memset( cur->next, 0, sizeof( x509_name ) );
321
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 return( x509_get_name( p, end2, cur->next ) );
323}
324
325/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 * Time ::= CHOICE {
327 * utcTime UTCTime,
328 * generalTime GeneralizedTime }
329 */
Paul Bakker91200182010-02-18 21:26:15 +0000330static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000331 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332 x509_time *time )
333{
Paul Bakker23986e52011-04-24 08:57:21 +0000334 int ret;
335 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000337 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000340 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
341 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000342
Paul Bakker91200182010-02-18 21:26:15 +0000343 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344
Paul Bakker91200182010-02-18 21:26:15 +0000345 if ( tag == ASN1_UTC_TIME )
346 {
347 (*p)++;
348 ret = asn1_get_len( p, end, &len );
349
350 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000351 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000352
Paul Bakker91200182010-02-18 21:26:15 +0000353 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000354 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
355 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000356
Paul Bakker91200182010-02-18 21:26:15 +0000357 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
358 &time->year, &time->mon, &time->day,
359 &time->hour, &time->min, &time->sec ) < 5 )
360 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000361
Paul Bakker400ff6f2011-02-20 10:40:16 +0000362 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000363 time->year += 1900;
364
365 *p += len;
366
367 return( 0 );
368 }
369 else if ( tag == ASN1_GENERALIZED_TIME )
370 {
371 (*p)++;
372 ret = asn1_get_len( p, end, &len );
373
374 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000375 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000376
377 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000378 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
379 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000380
381 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
382 &time->year, &time->mon, &time->day,
383 &time->hour, &time->min, &time->sec ) < 5 )
384 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
385
386 *p += len;
387
388 return( 0 );
389 }
390 else
Paul Bakker9d781402011-05-09 16:17:09 +0000391 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000392}
393
394
395/*
396 * Validity ::= SEQUENCE {
397 * notBefore Time,
398 * notAfter Time }
399 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000400static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000401 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 x509_time *from,
403 x509_time *to )
404{
Paul Bakker23986e52011-04-24 08:57:21 +0000405 int ret;
406 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
408 if( ( ret = asn1_get_tag( p, end, &len,
409 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000410 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 end = *p + len;
413
Paul Bakker91200182010-02-18 21:26:15 +0000414 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000415 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
Paul Bakker91200182010-02-18 21:26:15 +0000417 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000421 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000422 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 return( 0 );
425}
426
427/*
428 * SubjectPublicKeyInfo ::= SEQUENCE {
429 * algorithm AlgorithmIdentifier,
430 * subjectPublicKey BIT STRING }
431 */
432static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000433 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 x509_buf *pk_alg_oid,
435 mpi *N, mpi *E )
436{
Paul Bakker23986e52011-04-24 08:57:21 +0000437 int ret, can_handle;
438 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 unsigned char *end2;
440
441 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
442 return( ret );
443
444 /*
445 * only RSA public keys handled at this time
446 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000447 can_handle = 0;
448
449 if( pk_alg_oid->len == 9 &&
450 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
451 can_handle = 1;
452
453 if( pk_alg_oid->len == 9 &&
454 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
455 {
456 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
457 can_handle = 1;
458
459 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
460 can_handle = 1;
461 }
462
463 if( pk_alg_oid->len == 5 &&
464 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
465 can_handle = 1;
466
467 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000468 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000471 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000474 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000475 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477 end2 = *p + len;
478
479 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000480 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 /*
483 * RSAPublicKey ::= SEQUENCE {
484 * modulus INTEGER, -- n
485 * publicExponent INTEGER -- e
486 * }
487 */
488 if( ( ret = asn1_get_tag( p, end2, &len,
489 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000490 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000493 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000494 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
497 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000498 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000501 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000502 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 return( 0 );
505}
506
507static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000508 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 x509_buf *sig )
510{
Paul Bakker23986e52011-04-24 08:57:21 +0000511 int ret;
512 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514 sig->tag = **p;
515
516 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000517 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Paul Bakker74111d32011-01-15 16:57:55 +0000519
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000521 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523 sig->len = len;
524 sig->p = *p;
525
526 *p += len;
527
528 return( 0 );
529}
530
531/*
532 * X.509 v2/v3 unique identifier (not parsed)
533 */
534static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000535 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 x509_buf *uid, int n )
537{
538 int ret;
539
540 if( *p == end )
541 return( 0 );
542
543 uid->tag = **p;
544
545 if( ( ret = asn1_get_tag( p, end, &uid->len,
546 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
547 {
Paul Bakker40e46942009-01-03 21:51:57 +0000548 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 return( 0 );
550
551 return( ret );
552 }
553
554 uid->p = *p;
555 *p += uid->len;
556
557 return( 0 );
558}
559
560/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000561 * X.509 Extensions (No parsing of extensions, pointer should
562 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 */
564static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000565 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000566 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000567{
Paul Bakker23986e52011-04-24 08:57:21 +0000568 int ret;
569 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571 if( *p == end )
572 return( 0 );
573
574 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000575
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000577 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 ext->p = *p;
581 end = *p + ext->len;
582
583 /*
584 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
585 *
586 * Extension ::= SEQUENCE {
587 * extnID OBJECT IDENTIFIER,
588 * critical BOOLEAN DEFAULT FALSE,
589 * extnValue OCTET STRING }
590 */
591 if( ( ret = asn1_get_tag( p, end, &len,
592 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000593 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000596 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000597 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Paul Bakkerd98030e2009-05-02 15:13:40 +0000599 return( 0 );
600}
601
602/*
603 * X.509 CRL v2 extensions (no extensions parsed yet.)
604 */
605static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000606 const unsigned char *end,
607 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000608{
Paul Bakker23986e52011-04-24 08:57:21 +0000609 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000610 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000611
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000612 /* Get explicit tag */
613 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614 {
615 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
616 return( 0 );
617
618 return( ret );
619 }
620
621 while( *p < end )
622 {
623 if( ( ret = asn1_get_tag( p, end, &len,
624 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000625 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000626
627 *p += len;
628 }
629
630 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000631 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000632 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
633
634 return( 0 );
635}
636
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000637/*
638 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
639 */
640static int x509_get_crl_entry_ext( unsigned char **p,
641 const unsigned char *end,
642 x509_buf *ext )
643{
644 int ret;
645 size_t len = 0;
646
647 /* OPTIONAL */
648 if (end <= *p)
649 return( 0 );
650
651 ext->tag = **p;
652 ext->p = *p;
653
654 /*
655 * Get CRL-entry extension sequence header
656 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
657 */
658 if( ( ret = asn1_get_tag( p, end, &ext->len,
659 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
660 {
661 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
662 {
663 ext->p = NULL;
664 return( 0 );
665 }
666 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
667 }
668
669 end = *p + ext->len;
670
671 if( end != *p + ext->len )
672 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
673 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
674
675 while( *p < end )
676 {
677 if( ( ret = asn1_get_tag( p, end, &len,
678 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
679 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
680
681 *p += len;
682 }
683
684 if( *p != end )
685 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
686 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
687
688 return( 0 );
689}
690
Paul Bakker74111d32011-01-15 16:57:55 +0000691static int x509_get_basic_constraints( unsigned char **p,
692 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000693 int *ca_istrue,
694 int *max_pathlen )
695{
Paul Bakker23986e52011-04-24 08:57:21 +0000696 int ret;
697 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000698
699 /*
700 * BasicConstraints ::= SEQUENCE {
701 * cA BOOLEAN DEFAULT FALSE,
702 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
703 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000704 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000705 *max_pathlen = 0; /* endless */
706
707 if( ( ret = asn1_get_tag( p, end, &len,
708 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000709 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000710
711 if( *p == end )
712 return 0;
713
Paul Bakker3cccddb2011-01-16 21:46:31 +0000714 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000715 {
716 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000717 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000718
719 if( ret != 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
Paul Bakker3cccddb2011-01-16 21:46:31 +0000722 if( *ca_istrue != 0 )
723 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000724 }
725
726 if( *p == end )
727 return 0;
728
729 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000730 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000731
732 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000733 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000734 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
735
736 (*max_pathlen)++;
737
Paul Bakker74111d32011-01-15 16:57:55 +0000738 return 0;
739}
740
741static int x509_get_ns_cert_type( unsigned char **p,
742 const unsigned char *end,
743 unsigned char *ns_cert_type)
744{
745 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000746 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000747
748 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000749 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000750
751 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000752 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000753 POLARSSL_ERR_ASN1_INVALID_LENGTH );
754
755 /* Get actual bitstring */
756 *ns_cert_type = *bs.p;
757 return 0;
758}
759
760static int x509_get_key_usage( unsigned char **p,
761 const unsigned char *end,
762 unsigned char *key_usage)
763{
764 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000765 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000766
767 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000768 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000769
Paul Bakkercebdf172011-11-11 15:01:31 +0000770 if( bs.len > 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000771 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000772 POLARSSL_ERR_ASN1_INVALID_LENGTH );
773
774 /* Get actual bitstring */
775 *key_usage = *bs.p;
776 return 0;
777}
778
Paul Bakkerd98030e2009-05-02 15:13:40 +0000779/*
Paul Bakker74111d32011-01-15 16:57:55 +0000780 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
781 *
782 * KeyPurposeId ::= OBJECT IDENTIFIER
783 */
784static int x509_get_ext_key_usage( unsigned char **p,
785 const unsigned char *end,
786 x509_sequence *ext_key_usage)
787{
788 int ret;
789
790 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000791 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000792
793 /* Sequence length must be >= 1 */
794 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000795 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000796 POLARSSL_ERR_ASN1_INVALID_LENGTH );
797
798 return 0;
799}
800
801/*
802 * X.509 v3 extensions
803 *
804 * TODO: Perform all of the basic constraints tests required by the RFC
805 * TODO: Set values for undetected extensions to a sane default?
806 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000807 */
808static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000809 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000810 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000811{
Paul Bakker23986e52011-04-24 08:57:21 +0000812 int ret;
813 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000814 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000815
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000816 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000817 {
818 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
819 return( 0 );
820
821 return( ret );
822 }
823
Paul Bakker5121ce52009-01-03 21:22:43 +0000824 while( *p < end )
825 {
Paul Bakker74111d32011-01-15 16:57:55 +0000826 /*
827 * Extension ::= SEQUENCE {
828 * extnID OBJECT IDENTIFIER,
829 * critical BOOLEAN DEFAULT FALSE,
830 * extnValue OCTET STRING }
831 */
832 x509_buf extn_oid = {0, 0, NULL};
833 int is_critical = 0; /* DEFAULT FALSE */
834
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 if( ( ret = asn1_get_tag( p, end, &len,
836 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000837 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000839 end_ext_data = *p + len;
840
Paul Bakker74111d32011-01-15 16:57:55 +0000841 /* Get extension ID */
842 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Paul Bakker74111d32011-01-15 16:57:55 +0000844 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Paul Bakker74111d32011-01-15 16:57:55 +0000847 extn_oid.p = *p;
848 *p += extn_oid.len;
849
850 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000852 POLARSSL_ERR_ASN1_OUT_OF_DATA );
853
854 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000855 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000856 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000857 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Paul Bakker74111d32011-01-15 16:57:55 +0000859 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000860 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000862 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000864 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000865
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000866 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000867 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000868 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Paul Bakker74111d32011-01-15 16:57:55 +0000870 /*
871 * Detect supported extensions
872 */
873 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
874 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 {
Paul Bakker74111d32011-01-15 16:57:55 +0000876 /* Parse basic constraints */
877 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000878 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000879 return ( ret );
880 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000881 }
Paul Bakker74111d32011-01-15 16:57:55 +0000882 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
883 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
884 {
885 /* Parse netscape certificate type */
886 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
887 &crt->ns_cert_type ) ) != 0 )
888 return ( ret );
889 crt->ext_types |= EXT_NS_CERT_TYPE;
890 }
891 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
892 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
893 {
894 /* Parse key usage */
895 if( ( ret = x509_get_key_usage( p, end_ext_octet,
896 &crt->key_usage ) ) != 0 )
897 return ( ret );
898 crt->ext_types |= EXT_KEY_USAGE;
899 }
900 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
901 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
902 {
903 /* Parse extended key usage */
904 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
905 &crt->ext_key_usage ) ) != 0 )
906 return ( ret );
907 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
908 }
909 else
910 {
911 /* No parser found, skip extension */
912 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker5c721f92011-07-27 16:51:09 +0000914#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000915 if( is_critical )
916 {
917 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000918 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000919 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
920 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000921#endif
Paul Bakker74111d32011-01-15 16:57:55 +0000922 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 }
924
925 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000926 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000927 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 return( 0 );
930}
931
932/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000933 * X.509 CRL Entries
934 */
935static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000936 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000937 x509_crl_entry *entry )
938{
Paul Bakker23986e52011-04-24 08:57:21 +0000939 int ret;
940 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000941 x509_crl_entry *cur_entry = entry;
942
943 if( *p == end )
944 return( 0 );
945
Paul Bakker9be19372009-07-27 20:21:53 +0000946 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000947 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
948 {
949 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
950 return( 0 );
951
952 return( ret );
953 }
954
Paul Bakker9be19372009-07-27 20:21:53 +0000955 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000956
957 while( *p < end )
958 {
Paul Bakker23986e52011-04-24 08:57:21 +0000959 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000960 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000961
962 if( ( ret = asn1_get_tag( p, end, &len2,
963 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
964 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000965 return( ret );
966 }
967
Paul Bakker9be19372009-07-27 20:21:53 +0000968 cur_entry->raw.tag = **p;
969 cur_entry->raw.p = *p;
970 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000971 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +0000972
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000973 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000974 return( ret );
975
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000976 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000977 return( ret );
978
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000979 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000980 return( ret );
981
Paul Bakker74111d32011-01-15 16:57:55 +0000982 if ( *p < end )
983 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000984 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkere2e36d32012-01-23 09:56:51 +0000985
986 if( cur_entry->next == NULL )
987 return( POLARSSL_ERR_X509_MALLOC_FAILED );
988
Paul Bakkerd98030e2009-05-02 15:13:40 +0000989 cur_entry = cur_entry->next;
990 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
991 }
992 }
993
994 return( 0 );
995}
996
Paul Bakker27d66162010-03-17 06:56:01 +0000997static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
998{
999 if( sig_oid->len == 9 &&
1000 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1001 {
1002 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1003 {
1004 *sig_alg = sig_oid->p[8];
1005 return( 0 );
1006 }
1007
1008 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1009 {
1010 *sig_alg = sig_oid->p[8];
1011 return( 0 );
1012 }
1013
1014 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1015 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001016 if( sig_oid->len == 5 &&
1017 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1018 {
1019 *sig_alg = SIG_RSA_SHA1;
1020 return( 0 );
1021 }
Paul Bakker27d66162010-03-17 06:56:01 +00001022
1023 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1024}
1025
Paul Bakkerd98030e2009-05-02 15:13:40 +00001026/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001027 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001029int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001030{
Paul Bakker23986e52011-04-24 08:57:21 +00001031 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001032 size_t len;
Paul Bakker47f62612013-01-14 16:40:55 +01001033 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
Paul Bakker320a4b52009-03-28 18:52:39 +00001035 /*
1036 * Check for valid input
1037 */
1038 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001039 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001040
Paul Bakker96743fc2011-02-12 14:30:57 +00001041 p = (unsigned char *) malloc( len = buflen );
1042
1043 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001044 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001045
1046 memcpy( p, buf, buflen );
1047
1048 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
1050 crt->raw.p = p;
1051 crt->raw.len = len;
1052 end = p + len;
1053
1054 /*
1055 * Certificate ::= SEQUENCE {
1056 * tbsCertificate TBSCertificate,
1057 * signatureAlgorithm AlgorithmIdentifier,
1058 * signatureValue BIT STRING }
1059 */
1060 if( ( ret = asn1_get_tag( &p, end, &len,
1061 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1062 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001063 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001064 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
1066
Paul Bakker47f62612013-01-14 16:40:55 +01001067 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001069 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001070 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001071 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 }
Paul Bakker47f62612013-01-14 16:40:55 +01001073 crt_end = p + len;
1074
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 /*
1076 * TBSCertificate ::= SEQUENCE {
1077 */
1078 crt->tbs.p = p;
1079
1080 if( ( ret = asn1_get_tag( &p, end, &len,
1081 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1082 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001083 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001084 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 }
1086
1087 end = p + len;
1088 crt->tbs.len = end - crt->tbs.p;
1089
1090 /*
1091 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1092 *
1093 * CertificateSerialNumber ::= INTEGER
1094 *
1095 * signature AlgorithmIdentifier
1096 */
1097 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1098 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1099 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1100 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001101 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 return( ret );
1103 }
1104
1105 crt->version++;
1106
1107 if( crt->version > 3 )
1108 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001109 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001110 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 }
1112
Paul Bakker27d66162010-03-17 06:56:01 +00001113 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001115 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001116 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 }
1118
1119 /*
1120 * issuer Name
1121 */
1122 crt->issuer_raw.p = p;
1123
1124 if( ( ret = asn1_get_tag( &p, end, &len,
1125 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1126 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001127 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001128 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 }
1130
1131 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1132 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001133 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 return( ret );
1135 }
1136
1137 crt->issuer_raw.len = p - crt->issuer_raw.p;
1138
1139 /*
1140 * Validity ::= SEQUENCE {
1141 * notBefore Time,
1142 * notAfter Time }
1143 *
1144 */
1145 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1146 &crt->valid_to ) ) != 0 )
1147 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001148 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 return( ret );
1150 }
1151
1152 /*
1153 * subject Name
1154 */
1155 crt->subject_raw.p = p;
1156
1157 if( ( ret = asn1_get_tag( &p, end, &len,
1158 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1159 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001160 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001161 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 }
1163
1164 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1165 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001166 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 return( ret );
1168 }
1169
1170 crt->subject_raw.len = p - crt->subject_raw.p;
1171
1172 /*
1173 * SubjectPublicKeyInfo ::= SEQUENCE
1174 * algorithm AlgorithmIdentifier,
1175 * subjectPublicKey BIT STRING }
1176 */
1177 if( ( ret = asn1_get_tag( &p, end, &len,
1178 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1179 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001180 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001181 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 }
1183
1184 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1185 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1186 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001187 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 return( ret );
1189 }
1190
1191 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1192 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001193 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 return( ret );
1195 }
1196
1197 crt->rsa.len = mpi_size( &crt->rsa.N );
1198
1199 /*
1200 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1201 * -- If present, version shall be v2 or v3
1202 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1203 * -- If present, version shall be v2 or v3
1204 * extensions [3] EXPLICIT Extensions OPTIONAL
1205 * -- If present, version shall be v3
1206 */
1207 if( crt->version == 2 || crt->version == 3 )
1208 {
1209 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1210 if( ret != 0 )
1211 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001212 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 return( ret );
1214 }
1215 }
1216
1217 if( crt->version == 2 || crt->version == 3 )
1218 {
1219 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1220 if( ret != 0 )
1221 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001222 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 return( ret );
1224 }
1225 }
1226
1227 if( crt->version == 3 )
1228 {
Paul Bakker74111d32011-01-15 16:57:55 +00001229 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 if( ret != 0 )
1231 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001232 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 return( ret );
1234 }
1235 }
1236
1237 if( p != end )
1238 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001239 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001240 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001241 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 }
1243
Paul Bakker47f62612013-01-14 16:40:55 +01001244 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
1246 /*
1247 * signatureAlgorithm AlgorithmIdentifier,
1248 * signatureValue BIT STRING
1249 */
1250 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1251 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001252 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 return( ret );
1254 }
1255
Paul Bakker7261cba2013-01-16 12:39:54 +01001256 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1257 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001259 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001260 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
1262
1263 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 return( ret );
1267 }
1268
1269 if( p != end )
1270 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001271 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001272 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001273 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 }
1275
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001276 return( 0 );
1277}
1278
1279/*
1280 * Parse one or more PEM certificates from a buffer and add them to the chained list
1281 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001282int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001283{
Paul Bakker732e1a82011-12-11 16:35:09 +00001284 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001285 x509_cert *crt, *prev = NULL;
1286 int buf_format = X509_FORMAT_DER;
1287
1288 crt = chain;
1289
1290 /*
1291 * Check for valid input
1292 */
1293 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001294 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001295
1296 while( crt->version != 0 && crt->next != NULL )
1297 {
1298 prev = crt;
1299 crt = crt->next;
1300 }
1301
1302 /*
1303 * Add new certificate on the end of the chain if needed.
1304 */
1305 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001306 {
1307 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1308
Paul Bakker7d06ad22009-05-02 15:53:56 +00001309 if( crt->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001310 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001311
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001312 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001313 crt = crt->next;
1314 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001317 /*
1318 * Determine buffer content. Buffer contains either one DER certificate or
1319 * one or more PEM certificates.
1320 */
1321#if defined(POLARSSL_PEM_C)
1322 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1323 buf_format = X509_FORMAT_PEM;
1324#endif
1325
1326 if( buf_format == X509_FORMAT_DER )
1327 return x509parse_crt_der( crt, buf, buflen );
1328
1329#if defined(POLARSSL_PEM_C)
1330 if( buf_format == X509_FORMAT_PEM )
1331 {
1332 pem_context pem;
1333
1334 while( buflen > 0 )
1335 {
1336 size_t use_len;
1337 pem_init( &pem );
1338
1339 ret = pem_read_buffer( &pem,
1340 "-----BEGIN CERTIFICATE-----",
1341 "-----END CERTIFICATE-----",
1342 buf, NULL, 0, &use_len );
1343
1344 if( ret == 0 )
1345 {
1346 /*
1347 * Was PEM encoded
1348 */
1349 buflen -= use_len;
1350 buf += use_len;
1351 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001352 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001353 {
1354 pem_free( &pem );
1355
1356 if( first_error == 0 )
1357 first_error = ret;
1358
1359 continue;
1360 }
1361 else
1362 break;
1363
1364 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1365
1366 pem_free( &pem );
1367
1368 if( ret != 0 )
1369 {
1370 /*
Paul Bakker732e1a82011-12-11 16:35:09 +00001371 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001372 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001373 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001374 {
1375 if( prev )
1376 prev->next = NULL;
1377
1378 if( crt != chain )
1379 free( crt );
1380
1381 return( ret );
1382 }
1383
1384 if( first_error == 0 )
1385 first_error = ret;
Paul Bakker732e1a82011-12-11 16:35:09 +00001386
1387 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001388
1389 memset( crt, 0, sizeof( x509_cert ) );
1390 continue;
1391 }
1392
1393 success = 1;
1394
1395 /*
1396 * Add new certificate to the list
1397 */
1398 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1399
1400 if( crt->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001401 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001402
1403 prev = crt;
1404 crt = crt->next;
1405 memset( crt, 0, sizeof( x509_cert ) );
1406 }
1407 }
1408#endif
1409
1410 if( crt->version == 0 )
1411 {
1412 if( prev )
1413 prev->next = NULL;
1414
1415 if( crt != chain )
1416 free( crt );
1417 }
1418
1419 if( success )
Paul Bakker732e1a82011-12-11 16:35:09 +00001420 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001421 else if( first_error )
1422 return( first_error );
1423 else
1424 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425}
1426
1427/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001428 * Parse one or more CRLs and add them to the chained list
1429 */
Paul Bakker23986e52011-04-24 08:57:21 +00001430int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001431{
Paul Bakker23986e52011-04-24 08:57:21 +00001432 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001433 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001434 unsigned char *p, *end;
1435 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001436#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001437 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001438 pem_context pem;
1439#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001440
1441 crl = chain;
1442
1443 /*
1444 * Check for valid input
1445 */
1446 if( crl == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001447 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001448
1449 while( crl->version != 0 && crl->next != NULL )
1450 crl = crl->next;
1451
1452 /*
1453 * Add new CRL on the end of the chain if needed.
1454 */
1455 if ( crl->version != 0 && crl->next == NULL)
1456 {
1457 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1458
Paul Bakker7d06ad22009-05-02 15:53:56 +00001459 if( crl->next == NULL )
1460 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001461 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001462 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001463 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001464
Paul Bakker7d06ad22009-05-02 15:53:56 +00001465 crl = crl->next;
1466 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001467 }
1468
Paul Bakker96743fc2011-02-12 14:30:57 +00001469#if defined(POLARSSL_PEM_C)
1470 pem_init( &pem );
1471 ret = pem_read_buffer( &pem,
1472 "-----BEGIN X509 CRL-----",
1473 "-----END X509 CRL-----",
1474 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001475
Paul Bakker96743fc2011-02-12 14:30:57 +00001476 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001477 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001478 /*
1479 * Was PEM encoded
1480 */
1481 buflen -= use_len;
1482 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001483
1484 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001485 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001486 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001487 p = pem.buf;
1488 pem.buf = NULL;
1489 len = pem.buflen;
1490 pem_free( &pem );
1491 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001492 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001493 {
1494 pem_free( &pem );
1495 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001496 }
1497 else
1498 {
1499 /*
1500 * nope, copy the raw DER data
1501 */
1502 p = (unsigned char *) malloc( len = buflen );
1503
1504 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001505 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001506
1507 memcpy( p, buf, buflen );
1508
1509 buflen = 0;
1510 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001511#else
1512 p = (unsigned char *) malloc( len = buflen );
1513
1514 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001515 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001516
1517 memcpy( p, buf, buflen );
1518
1519 buflen = 0;
1520#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001521
1522 crl->raw.p = p;
1523 crl->raw.len = len;
1524 end = p + len;
1525
1526 /*
1527 * CertificateList ::= SEQUENCE {
1528 * tbsCertList TBSCertList,
1529 * signatureAlgorithm AlgorithmIdentifier,
1530 * signatureValue BIT STRING }
1531 */
1532 if( ( ret = asn1_get_tag( &p, end, &len,
1533 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1534 {
1535 x509_crl_free( crl );
1536 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1537 }
1538
Paul Bakker23986e52011-04-24 08:57:21 +00001539 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001540 {
1541 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001542 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001543 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1544 }
1545
1546 /*
1547 * TBSCertList ::= SEQUENCE {
1548 */
1549 crl->tbs.p = p;
1550
1551 if( ( ret = asn1_get_tag( &p, end, &len,
1552 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1553 {
1554 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001555 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001556 }
1557
1558 end = p + len;
1559 crl->tbs.len = end - crl->tbs.p;
1560
1561 /*
1562 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1563 * -- if present, MUST be v2
1564 *
1565 * signature AlgorithmIdentifier
1566 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001567 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001568 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1569 {
1570 x509_crl_free( crl );
1571 return( ret );
1572 }
1573
1574 crl->version++;
1575
1576 if( crl->version > 2 )
1577 {
1578 x509_crl_free( crl );
1579 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1580 }
1581
Paul Bakker27d66162010-03-17 06:56:01 +00001582 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001583 {
1584 x509_crl_free( crl );
1585 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1586 }
1587
1588 /*
1589 * issuer Name
1590 */
1591 crl->issuer_raw.p = p;
1592
1593 if( ( ret = asn1_get_tag( &p, end, &len,
1594 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1595 {
1596 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001597 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001598 }
1599
1600 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1601 {
1602 x509_crl_free( crl );
1603 return( ret );
1604 }
1605
1606 crl->issuer_raw.len = p - crl->issuer_raw.p;
1607
1608 /*
1609 * thisUpdate Time
1610 * nextUpdate Time OPTIONAL
1611 */
Paul Bakker91200182010-02-18 21:26:15 +00001612 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001613 {
1614 x509_crl_free( crl );
1615 return( ret );
1616 }
1617
Paul Bakker91200182010-02-18 21:26:15 +00001618 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001619 {
Paul Bakker9d781402011-05-09 16:17:09 +00001620 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001621 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001622 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001623 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001624 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001625 x509_crl_free( crl );
1626 return( ret );
1627 }
1628 }
1629
1630 /*
1631 * revokedCertificates SEQUENCE OF SEQUENCE {
1632 * userCertificate CertificateSerialNumber,
1633 * revocationDate Time,
1634 * crlEntryExtensions Extensions OPTIONAL
1635 * -- if present, MUST be v2
1636 * } OPTIONAL
1637 */
1638 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1639 {
1640 x509_crl_free( crl );
1641 return( ret );
1642 }
1643
1644 /*
1645 * crlExtensions EXPLICIT Extensions OPTIONAL
1646 * -- if present, MUST be v2
1647 */
1648 if( crl->version == 2 )
1649 {
1650 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1651
1652 if( ret != 0 )
1653 {
1654 x509_crl_free( crl );
1655 return( ret );
1656 }
1657 }
1658
1659 if( p != end )
1660 {
1661 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001662 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001663 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1664 }
1665
1666 end = crl->raw.p + crl->raw.len;
1667
1668 /*
1669 * signatureAlgorithm AlgorithmIdentifier,
1670 * signatureValue BIT STRING
1671 */
1672 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1673 {
1674 x509_crl_free( crl );
1675 return( ret );
1676 }
1677
Paul Bakker7261cba2013-01-16 12:39:54 +01001678 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1679 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001680 {
1681 x509_crl_free( crl );
1682 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1683 }
1684
1685 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1686 {
1687 x509_crl_free( crl );
1688 return( ret );
1689 }
1690
1691 if( p != end )
1692 {
1693 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001694 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001695 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1696 }
1697
1698 if( buflen > 0 )
1699 {
1700 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1701
Paul Bakker7d06ad22009-05-02 15:53:56 +00001702 if( crl->next == NULL )
1703 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001704 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001705 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001706 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001707
Paul Bakker7d06ad22009-05-02 15:53:56 +00001708 crl = crl->next;
1709 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001710
1711 return( x509parse_crl( crl, buf, buflen ) );
1712 }
1713
1714 return( 0 );
1715}
1716
Paul Bakker335db3f2011-04-25 15:28:35 +00001717#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001718/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001719 * Load all data from a file into a given buffer.
1720 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001721int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001722{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001723 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001724
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001726 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001727
Paul Bakkerd98030e2009-05-02 15:13:40 +00001728 fseek( f, 0, SEEK_END );
1729 *n = (size_t) ftell( f );
1730 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001731
Paul Bakkerd98030e2009-05-02 15:13:40 +00001732 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001733 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001734
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 if( fread( *buf, 1, *n, f ) != *n )
1736 {
1737 fclose( f );
1738 free( *buf );
Paul Bakker732e1a82011-12-11 16:35:09 +00001739 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001740 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001741
Paul Bakkerd98030e2009-05-02 15:13:40 +00001742 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001743
Paul Bakkerd98030e2009-05-02 15:13:40 +00001744 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001745
Paul Bakkerd98030e2009-05-02 15:13:40 +00001746 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001747}
1748
1749/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001750 * Load one or more certificates and add them to the chained list
1751 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001752int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001753{
1754 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001755 size_t n;
1756 unsigned char *buf;
1757
Paul Bakker732e1a82011-12-11 16:35:09 +00001758 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1759 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001760
Paul Bakker732e1a82011-12-11 16:35:09 +00001761 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
1763 memset( buf, 0, n + 1 );
1764 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
1766 return( ret );
1767}
1768
Paul Bakkerd98030e2009-05-02 15:13:40 +00001769/*
1770 * Load one or more CRLs and add them to the chained list
1771 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001772int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001773{
1774 int ret;
1775 size_t n;
1776 unsigned char *buf;
1777
Paul Bakker732e1a82011-12-11 16:35:09 +00001778 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1779 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001780
Paul Bakker27fdf462011-06-09 13:55:13 +00001781 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001782
1783 memset( buf, 0, n + 1 );
1784 free( buf );
1785
1786 return( ret );
1787}
1788
Paul Bakker5121ce52009-01-03 21:22:43 +00001789/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001790 * Load and parse a private RSA key
1791 */
1792int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1793{
1794 int ret;
1795 size_t n;
1796 unsigned char *buf;
1797
Paul Bakker732e1a82011-12-11 16:35:09 +00001798 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1799 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001800
1801 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001802 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001803 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001804 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001805 (unsigned char *) pwd, strlen( pwd ) );
1806
1807 memset( buf, 0, n + 1 );
1808 free( buf );
1809
1810 return( ret );
1811}
1812
1813/*
1814 * Load and parse a public RSA key
1815 */
1816int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1817{
1818 int ret;
1819 size_t n;
1820 unsigned char *buf;
1821
Paul Bakker732e1a82011-12-11 16:35:09 +00001822 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1823 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001824
Paul Bakker27fdf462011-06-09 13:55:13 +00001825 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001826
1827 memset( buf, 0, n + 1 );
1828 free( buf );
1829
1830 return( ret );
1831}
1832#endif /* POLARSSL_FS_IO */
1833
1834/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 * Parse a private RSA key
1836 */
Paul Bakker23986e52011-04-24 08:57:21 +00001837int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1838 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001839{
Paul Bakker23986e52011-04-24 08:57:21 +00001840 int ret;
1841 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001843 unsigned char *p_alt;
1844 x509_buf pk_alg_oid;
1845
Paul Bakker96743fc2011-02-12 14:30:57 +00001846#if defined(POLARSSL_PEM_C)
1847 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Paul Bakker96743fc2011-02-12 14:30:57 +00001849 pem_init( &pem );
1850 ret = pem_read_buffer( &pem,
1851 "-----BEGIN RSA PRIVATE KEY-----",
1852 "-----END RSA PRIVATE KEY-----",
1853 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Paul Bakker03a85bc2013-06-19 12:06:00 +02001855 if( ret == POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkered56b222011-07-13 11:26:43 +00001856 {
1857 ret = pem_read_buffer( &pem,
1858 "-----BEGIN PRIVATE KEY-----",
1859 "-----END PRIVATE KEY-----",
1860 key, pwd, pwdlen, &len );
1861 }
1862
Paul Bakker96743fc2011-02-12 14:30:57 +00001863 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001865 /*
1866 * Was PEM encoded
1867 */
1868 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001870 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001871 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001872 pem_free( &pem );
1873 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Paul Bakker96743fc2011-02-12 14:30:57 +00001876 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1877#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001878 ((void) pwd);
1879 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001880 p = (unsigned char *) key;
1881#endif
1882 end = p + keylen;
1883
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001885 * Note: Depending on the type of private key file one can expect either a
1886 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1887 *
1888 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001889 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001890 * algorithm AlgorithmIdentifier,
1891 * PrivateKey BIT STRING
1892 * }
1893 *
1894 * AlgorithmIdentifier ::= SEQUENCE {
1895 * algorithm OBJECT IDENTIFIER,
1896 * parameters ANY DEFINED BY algorithm OPTIONAL
1897 * }
1898 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 * RSAPrivateKey ::= SEQUENCE {
1900 * version Version,
1901 * modulus INTEGER, -- n
1902 * publicExponent INTEGER, -- e
1903 * privateExponent INTEGER, -- d
1904 * prime1 INTEGER, -- p
1905 * prime2 INTEGER, -- q
1906 * exponent1 INTEGER, -- d mod (p-1)
1907 * exponent2 INTEGER, -- d mod (q-1)
1908 * coefficient INTEGER, -- (inverse of q) mod p
1909 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1910 * }
1911 */
1912 if( ( ret = asn1_get_tag( &p, end, &len,
1913 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1914 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001915#if defined(POLARSSL_PEM_C)
1916 pem_free( &pem );
1917#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001919 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 }
1921
1922 end = p + len;
1923
1924 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1925 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001926#if defined(POLARSSL_PEM_C)
1927 pem_free( &pem );
1928#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001930 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 }
1932
1933 if( rsa->ver != 0 )
1934 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001935#if defined(POLARSSL_PEM_C)
1936 pem_free( &pem );
1937#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001939 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 }
1941
Paul Bakkered56b222011-07-13 11:26:43 +00001942 p_alt = p;
1943
1944 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1945 {
1946 // Assume that we have the PKCS#1 format if wrong
1947 // tag was encountered
1948 //
1949 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1950 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1951 {
1952#if defined(POLARSSL_PEM_C)
1953 pem_free( &pem );
1954#endif
1955 rsa_free( rsa );
1956 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1957 }
1958 }
1959 else
1960 {
1961 int can_handle;
1962
1963 /*
1964 * only RSA keys handled at this time
1965 */
1966 can_handle = 0;
1967
1968 if( pk_alg_oid.len == 9 &&
1969 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1970 can_handle = 1;
1971
1972 if( pk_alg_oid.len == 9 &&
1973 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1974 {
1975 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1976 can_handle = 1;
1977
1978 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1979 can_handle = 1;
1980 }
1981
1982 if( pk_alg_oid.len == 5 &&
1983 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
1984 can_handle = 1;
1985
1986 if( can_handle == 0 )
1987 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
1988
1989 /*
1990 * Parse the PKCS#8 format
1991 */
1992
1993 p = p_alt;
1994 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
1995 {
1996#if defined(POLARSSL_PEM_C)
1997 pem_free( &pem );
1998#endif
1999 rsa_free( rsa );
2000 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2001 }
2002
2003 if( ( end - p ) < 1 )
2004 {
2005#if defined(POLARSSL_PEM_C)
2006 pem_free( &pem );
2007#endif
2008 rsa_free( rsa );
2009 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2010 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2011 }
2012
2013 end = p + len;
2014
2015 if( ( ret = asn1_get_tag( &p, end, &len,
2016 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2017 {
2018#if defined(POLARSSL_PEM_C)
2019 pem_free( &pem );
2020#endif
2021 rsa_free( rsa );
2022 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2023 }
2024
2025 end = p + len;
2026
2027 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2028 {
2029#if defined(POLARSSL_PEM_C)
2030 pem_free( &pem );
2031#endif
2032 rsa_free( rsa );
2033 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2034 }
2035
2036 if( rsa->ver != 0 )
2037 {
2038#if defined(POLARSSL_PEM_C)
2039 pem_free( &pem );
2040#endif
2041 rsa_free( rsa );
2042 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2043 }
2044 }
2045
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2047 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2048 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2049 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2050 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2051 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2052 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2053 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2054 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002055#if defined(POLARSSL_PEM_C)
2056 pem_free( &pem );
2057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002059 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 }
2061
2062 rsa->len = mpi_size( &rsa->N );
2063
2064 if( p != end )
2065 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002066#if defined(POLARSSL_PEM_C)
2067 pem_free( &pem );
2068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002070 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002071 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
2073
2074 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2075 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002076#if defined(POLARSSL_PEM_C)
2077 pem_free( &pem );
2078#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 rsa_free( rsa );
2080 return( ret );
2081 }
2082
Paul Bakker96743fc2011-02-12 14:30:57 +00002083#if defined(POLARSSL_PEM_C)
2084 pem_free( &pem );
2085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
2087 return( 0 );
2088}
2089
2090/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002091 * Parse a public RSA key
2092 */
Paul Bakker23986e52011-04-24 08:57:21 +00002093int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002094{
Paul Bakker23986e52011-04-24 08:57:21 +00002095 int ret;
2096 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002097 unsigned char *p, *end;
2098 x509_buf alg_oid;
2099#if defined(POLARSSL_PEM_C)
2100 pem_context pem;
2101
2102 pem_init( &pem );
2103 ret = pem_read_buffer( &pem,
2104 "-----BEGIN PUBLIC KEY-----",
2105 "-----END PUBLIC KEY-----",
2106 key, NULL, 0, &len );
2107
2108 if( ret == 0 )
2109 {
2110 /*
2111 * Was PEM encoded
2112 */
2113 keylen = pem.buflen;
2114 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02002115 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002116 {
2117 pem_free( &pem );
2118 return( ret );
2119 }
2120
2121 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2122#else
2123 p = (unsigned char *) key;
2124#endif
2125 end = p + keylen;
2126
2127 /*
2128 * PublicKeyInfo ::= SEQUENCE {
2129 * algorithm AlgorithmIdentifier,
2130 * PublicKey BIT STRING
2131 * }
2132 *
2133 * AlgorithmIdentifier ::= SEQUENCE {
2134 * algorithm OBJECT IDENTIFIER,
2135 * parameters ANY DEFINED BY algorithm OPTIONAL
2136 * }
2137 *
2138 * RSAPublicKey ::= SEQUENCE {
2139 * modulus INTEGER, -- n
2140 * publicExponent INTEGER -- e
2141 * }
2142 */
2143
2144 if( ( ret = asn1_get_tag( &p, end, &len,
2145 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2146 {
2147#if defined(POLARSSL_PEM_C)
2148 pem_free( &pem );
2149#endif
2150 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002151 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002152 }
2153
2154 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2155 {
2156#if defined(POLARSSL_PEM_C)
2157 pem_free( &pem );
2158#endif
2159 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002160 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002161 }
2162
2163 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2164 {
2165#if defined(POLARSSL_PEM_C)
2166 pem_free( &pem );
2167#endif
2168 rsa_free( rsa );
2169 return( ret );
2170 }
2171
2172 rsa->len = mpi_size( &rsa->N );
2173
2174#if defined(POLARSSL_PEM_C)
2175 pem_free( &pem );
2176#endif
2177
2178 return( 0 );
2179}
2180
Paul Bakkereaa89f82011-04-04 21:36:15 +00002181#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002182/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002183 * Parse DHM parameters
2184 */
Paul Bakker23986e52011-04-24 08:57:21 +00002185int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002186{
Paul Bakker23986e52011-04-24 08:57:21 +00002187 int ret;
2188 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002189 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002190#if defined(POLARSSL_PEM_C)
2191 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002192
Paul Bakker96743fc2011-02-12 14:30:57 +00002193 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002194
Paul Bakker96743fc2011-02-12 14:30:57 +00002195 ret = pem_read_buffer( &pem,
2196 "-----BEGIN DH PARAMETERS-----",
2197 "-----END DH PARAMETERS-----",
2198 dhmin, NULL, 0, &dhminlen );
2199
2200 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002201 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002202 /*
2203 * Was PEM encoded
2204 */
2205 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002206 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02002207 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002208 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002209 pem_free( &pem );
2210 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002211 }
2212
Paul Bakker96743fc2011-02-12 14:30:57 +00002213 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2214#else
2215 p = (unsigned char *) dhmin;
2216#endif
2217 end = p + dhminlen;
2218
Paul Bakker1b57b062011-01-06 15:48:19 +00002219 memset( dhm, 0, sizeof( dhm_context ) );
2220
Paul Bakker1b57b062011-01-06 15:48:19 +00002221 /*
2222 * DHParams ::= SEQUENCE {
2223 * prime INTEGER, -- P
2224 * generator INTEGER, -- g
2225 * }
2226 */
2227 if( ( ret = asn1_get_tag( &p, end, &len,
2228 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2229 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002230#if defined(POLARSSL_PEM_C)
2231 pem_free( &pem );
2232#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002233 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002234 }
2235
2236 end = p + len;
2237
2238 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2239 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2240 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002241#if defined(POLARSSL_PEM_C)
2242 pem_free( &pem );
2243#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002244 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002245 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002246 }
2247
2248 if( p != end )
2249 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002250#if defined(POLARSSL_PEM_C)
2251 pem_free( &pem );
2252#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002253 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002254 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002255 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2256 }
2257
Paul Bakker96743fc2011-02-12 14:30:57 +00002258#if defined(POLARSSL_PEM_C)
2259 pem_free( &pem );
2260#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002261
2262 return( 0 );
2263}
2264
Paul Bakker335db3f2011-04-25 15:28:35 +00002265#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002266/*
2267 * Load and parse a private RSA key
2268 */
2269int x509parse_dhmfile( dhm_context *dhm, const char *path )
2270{
2271 int ret;
2272 size_t n;
2273 unsigned char *buf;
2274
Paul Bakker732e1a82011-12-11 16:35:09 +00002275 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2276 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002277
Paul Bakker27fdf462011-06-09 13:55:13 +00002278 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002279
2280 memset( buf, 0, n + 1 );
2281 free( buf );
2282
2283 return( ret );
2284}
Paul Bakker335db3f2011-04-25 15:28:35 +00002285#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002286#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002287
Paul Bakker5121ce52009-01-03 21:22:43 +00002288#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002289#include <stdarg.h>
2290
2291#if !defined vsnprintf
2292#define vsnprintf _vsnprintf
2293#endif // vsnprintf
2294
2295/*
2296 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2297 * Result value is not size of buffer needed, but -1 if no fit is possible.
2298 *
2299 * This fuction tries to 'fix' this by at least suggesting enlarging the
2300 * size by 20.
2301 */
2302int compat_snprintf(char *str, size_t size, const char *format, ...)
2303{
2304 va_list ap;
2305 int res = -1;
2306
2307 va_start( ap, format );
2308
2309 res = vsnprintf( str, size, format, ap );
2310
2311 va_end( ap );
2312
2313 // No quick fix possible
2314 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002315 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002316
2317 return res;
2318}
2319
2320#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002321#endif
2322
Paul Bakkerd98030e2009-05-02 15:13:40 +00002323#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2324
2325#define SAFE_SNPRINTF() \
2326{ \
2327 if( ret == -1 ) \
2328 return( -1 ); \
2329 \
Paul Bakker23986e52011-04-24 08:57:21 +00002330 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002331 p[n - 1] = '\0'; \
2332 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2333 } \
2334 \
Paul Bakker23986e52011-04-24 08:57:21 +00002335 n -= (unsigned int) ret; \
2336 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002337}
2338
Paul Bakker5121ce52009-01-03 21:22:43 +00002339/*
2340 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002341 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002343int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002344{
Paul Bakker23986e52011-04-24 08:57:21 +00002345 int ret;
2346 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002348 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 char s[128], *p;
2350
2351 memset( s, 0, sizeof( s ) );
2352
2353 name = dn;
2354 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002355 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
2357 while( name != NULL )
2358 {
Paul Bakker74111d32011-01-15 16:57:55 +00002359 if( name != dn )
2360 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002361 ret = snprintf( p, n, ", " );
2362 SAFE_SNPRINTF();
2363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Paul Bakker7261cba2013-01-16 12:39:54 +01002365 if( name->oid.len == 3 &&
2366 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 {
2368 switch( name->oid.p[2] )
2369 {
2370 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002371 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002372
2373 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002374 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
2376 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002377 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
2379 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002380 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
2382 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002383 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
2385 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002386 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
2388 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002389 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 name->oid.p[2] );
2391 break;
2392 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002393 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 }
Paul Bakker7261cba2013-01-16 12:39:54 +01002395 else if( name->oid.len == 9 &&
2396 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 {
2398 switch( name->oid.p[8] )
2399 {
2400 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002401 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
2403 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002404 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 name->oid.p[8] );
2406 break;
2407 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002408 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 }
2410 else
Paul Bakker74111d32011-01-15 16:57:55 +00002411 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002412 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002413 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002414 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
2416 for( i = 0; i < name->val.len; i++ )
2417 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002418 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 break;
2420
2421 c = name->val.p[i];
2422 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2423 s[i] = '?';
2424 else s[i] = c;
2425 }
2426 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002427 ret = snprintf( p, n, "%s", s );
2428 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 name = name->next;
2430 }
2431
Paul Bakker23986e52011-04-24 08:57:21 +00002432 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433}
2434
2435/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002436 * Store the serial in printable form into buf; no more
2437 * than size characters will be written
2438 */
2439int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2440{
Paul Bakker23986e52011-04-24 08:57:21 +00002441 int ret;
2442 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002443 char *p;
2444
2445 p = buf;
2446 n = size;
2447
2448 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002449 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002450
2451 for( i = 0; i < nr; i++ )
2452 {
Paul Bakker93048802011-12-05 14:38:06 +00002453 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002454 continue;
2455
Paul Bakkerdd476992011-01-16 21:34:59 +00002456 ret = snprintf( p, n, "%02X%s",
2457 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2458 SAFE_SNPRINTF();
2459 }
2460
Paul Bakker03c7c252011-11-25 12:37:37 +00002461 if( nr != serial->len )
2462 {
2463 ret = snprintf( p, n, "...." );
2464 SAFE_SNPRINTF();
2465 }
2466
Paul Bakker23986e52011-04-24 08:57:21 +00002467 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002468}
2469
2470/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002471 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002473int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2474 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002475{
Paul Bakker23986e52011-04-24 08:57:21 +00002476 int ret;
2477 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002478 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
2480 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002481 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Paul Bakkerd98030e2009-05-02 15:13:40 +00002483 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002485 SAFE_SNPRINTF();
2486 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002488 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Paul Bakkerdd476992011-01-16 21:34:59 +00002490 ret = x509parse_serial_gets( p, n, &crt->serial);
2491 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Paul Bakkerd98030e2009-05-02 15:13:40 +00002493 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2494 SAFE_SNPRINTF();
2495 ret = x509parse_dn_gets( p, n, &crt->issuer );
2496 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Paul Bakkerd98030e2009-05-02 15:13:40 +00002498 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2499 SAFE_SNPRINTF();
2500 ret = x509parse_dn_gets( p, n, &crt->subject );
2501 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Paul Bakkerd98030e2009-05-02 15:13:40 +00002503 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2505 crt->valid_from.year, crt->valid_from.mon,
2506 crt->valid_from.day, crt->valid_from.hour,
2507 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002508 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Paul Bakkerd98030e2009-05-02 15:13:40 +00002510 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2512 crt->valid_to.year, crt->valid_to.mon,
2513 crt->valid_to.day, crt->valid_to.hour,
2514 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002515 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Paul Bakkerd98030e2009-05-02 15:13:40 +00002517 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2518 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Paul Bakker27d66162010-03-17 06:56:01 +00002520 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002522 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2523 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2524 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2525 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2526 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2527 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2528 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2529 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2530 default: ret = snprintf( p, n, "???" ); break;
2531 }
2532 SAFE_SNPRINTF();
2533
2534 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002535 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002536 SAFE_SNPRINTF();
2537
Paul Bakker23986e52011-04-24 08:57:21 +00002538 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002539}
2540
Paul Bakker74111d32011-01-15 16:57:55 +00002541/* Compare a given OID string with an OID x509_buf * */
2542#define OID_CMP(oid_str, oid_buf) \
2543 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2544 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2545
2546/*
2547 * Return an informational string describing the given OID
2548 */
2549const char *x509_oid_get_description( x509_buf *oid )
2550{
2551 if ( oid == NULL )
2552 return ( NULL );
2553
2554 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2555 return( STRING_SERVER_AUTH );
2556
2557 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2558 return( STRING_CLIENT_AUTH );
2559
2560 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2561 return( STRING_CODE_SIGNING );
2562
2563 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2564 return( STRING_EMAIL_PROTECTION );
2565
2566 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2567 return( STRING_TIME_STAMPING );
2568
2569 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2570 return( STRING_OCSP_SIGNING );
2571
2572 return( NULL );
2573}
2574
2575/* Return the x.y.z.... style numeric string for the given OID */
2576int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2577{
Paul Bakker23986e52011-04-24 08:57:21 +00002578 int ret;
2579 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002580 unsigned int value;
2581 char *p;
2582
2583 p = buf;
2584 n = size;
2585
2586 /* First byte contains first two dots */
2587 if( oid->len > 0 )
2588 {
2589 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2590 SAFE_SNPRINTF();
2591 }
2592
2593 /* TODO: value can overflow in value. */
2594 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002595 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002596 {
2597 value <<= 7;
2598 value += oid->p[i] & 0x7F;
2599
2600 if( !( oid->p[i] & 0x80 ) )
2601 {
2602 /* Last byte */
2603 ret = snprintf( p, n, ".%d", value );
2604 SAFE_SNPRINTF();
2605 value = 0;
2606 }
2607 }
2608
Paul Bakker23986e52011-04-24 08:57:21 +00002609 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002610}
2611
Paul Bakkerd98030e2009-05-02 15:13:40 +00002612/*
2613 * Return an informational string about the CRL.
2614 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002615int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2616 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002617{
Paul Bakker23986e52011-04-24 08:57:21 +00002618 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002619 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002620 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002621 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002622
2623 p = buf;
2624 n = size;
2625
2626 ret = snprintf( p, n, "%sCRL version : %d",
2627 prefix, crl->version );
2628 SAFE_SNPRINTF();
2629
2630 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2631 SAFE_SNPRINTF();
2632 ret = x509parse_dn_gets( p, n, &crl->issuer );
2633 SAFE_SNPRINTF();
2634
2635 ret = snprintf( p, n, "\n%sthis update : " \
2636 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2637 crl->this_update.year, crl->this_update.mon,
2638 crl->this_update.day, crl->this_update.hour,
2639 crl->this_update.min, crl->this_update.sec );
2640 SAFE_SNPRINTF();
2641
2642 ret = snprintf( p, n, "\n%snext update : " \
2643 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2644 crl->next_update.year, crl->next_update.mon,
2645 crl->next_update.day, crl->next_update.hour,
2646 crl->next_update.min, crl->next_update.sec );
2647 SAFE_SNPRINTF();
2648
2649 entry = &crl->entry;
2650
2651 ret = snprintf( p, n, "\n%sRevoked certificates:",
2652 prefix );
2653 SAFE_SNPRINTF();
2654
Paul Bakker9be19372009-07-27 20:21:53 +00002655 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002656 {
2657 ret = snprintf( p, n, "\n%sserial number: ",
2658 prefix );
2659 SAFE_SNPRINTF();
2660
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002661 ret = x509parse_serial_gets( p, n, &entry->serial);
2662 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002663
Paul Bakkerd98030e2009-05-02 15:13:40 +00002664 ret = snprintf( p, n, " revocation date: " \
2665 "%04d-%02d-%02d %02d:%02d:%02d",
2666 entry->revocation_date.year, entry->revocation_date.mon,
2667 entry->revocation_date.day, entry->revocation_date.hour,
2668 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002669 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002670
2671 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 }
2673
Paul Bakkerd98030e2009-05-02 15:13:40 +00002674 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2675 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Paul Bakker27d66162010-03-17 06:56:01 +00002677 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002678 {
2679 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2680 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2681 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2682 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2683 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2684 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2685 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2686 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2687 default: ret = snprintf( p, n, "???" ); break;
2688 }
2689 SAFE_SNPRINTF();
2690
Paul Bakker1e27bb22009-07-19 20:25:25 +00002691 ret = snprintf( p, n, "\n" );
2692 SAFE_SNPRINTF();
2693
Paul Bakker23986e52011-04-24 08:57:21 +00002694 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695}
2696
2697/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002698 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002700int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002701{
Paul Bakkercce9d772011-11-18 14:26:47 +00002702 int year, mon, day;
2703 int hour, min, sec;
2704
2705#if defined(_WIN32)
2706 SYSTEMTIME st;
2707
2708 GetLocalTime(&st);
2709
2710 year = st.wYear;
2711 mon = st.wMonth;
2712 day = st.wDay;
2713 hour = st.wHour;
2714 min = st.wMinute;
2715 sec = st.wSecond;
2716#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 struct tm *lt;
2718 time_t tt;
2719
2720 tt = time( NULL );
2721 lt = localtime( &tt );
2722
Paul Bakkercce9d772011-11-18 14:26:47 +00002723 year = lt->tm_year + 1900;
2724 mon = lt->tm_mon + 1;
2725 day = lt->tm_mday;
2726 hour = lt->tm_hour;
2727 min = lt->tm_min;
2728 sec = lt->tm_sec;
2729#endif
2730
2731 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002732 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Paul Bakkercce9d772011-11-18 14:26:47 +00002734 if( year == to->year &&
2735 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002736 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Paul Bakkercce9d772011-11-18 14:26:47 +00002738 if( year == to->year &&
2739 mon == to->mon &&
2740 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002741 return( 1 );
2742
Paul Bakkercce9d772011-11-18 14:26:47 +00002743 if( year == to->year &&
2744 mon == to->mon &&
2745 day == to->day &&
2746 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002747 return( 1 );
2748
Paul Bakkercce9d772011-11-18 14:26:47 +00002749 if( year == to->year &&
2750 mon == to->mon &&
2751 day == to->day &&
2752 hour == to->hour &&
2753 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002754 return( 1 );
2755
Paul Bakkercce9d772011-11-18 14:26:47 +00002756 if( year == to->year &&
2757 mon == to->mon &&
2758 day == to->day &&
2759 hour == to->hour &&
2760 min == to->min &&
2761 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002762 return( 1 );
2763
Paul Bakker40ea7de2009-05-03 10:18:48 +00002764 return( 0 );
2765}
2766
2767/*
2768 * Return 1 if the certificate is revoked, or 0 otherwise.
2769 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002770int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002771{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002772 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002773
2774 while( cur != NULL && cur->serial.len != 0 )
2775 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002776 if( crt->serial.len == cur->serial.len &&
2777 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002778 {
2779 if( x509parse_time_expired( &cur->revocation_date ) )
2780 return( 1 );
2781 }
2782
2783 cur = cur->next;
2784 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
2786 return( 0 );
2787}
2788
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002789/*
2790 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002791 */
Paul Bakker23986e52011-04-24 08:57:21 +00002792static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 unsigned char *out )
2794{
2795 switch( alg )
2796 {
Paul Bakker40e46942009-01-03 21:51:57 +00002797#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002798 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002799#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002800#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002801 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002802#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002803#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002804 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002805#endif
2806#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002807 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002808#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002809#if defined(POLARSSL_SHA2_C)
2810 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2811 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2812#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002813#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002814 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2815 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002818 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 break;
2820 }
2821}
2822
2823/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002824 * Check that the given certificate is valid accoring to the CRL.
2825 */
2826static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2827 x509_crl *crl_list)
2828{
2829 int flags = 0;
2830 int hash_id;
2831 unsigned char hash[64];
2832
2833 /*
2834 * TODO: What happens if no CRL is present?
2835 * Suggestion: Revocation state should be unknown if no CRL is present.
2836 * For backwards compatibility this is not yet implemented.
2837 */
2838
2839 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2840 {
2841 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2842 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2843 crl_list->issuer_raw.len ) != 0 )
2844 {
2845 crl_list = crl_list->next;
2846 continue;
2847 }
2848
2849 /*
2850 * Check if CRL is correctly signed by the trusted CA
2851 */
2852 hash_id = crl_list->sig_alg;
2853
2854 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2855
2856 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2857 0, hash, crl_list->sig.p ) == 0 )
2858 {
2859 /*
2860 * CRL is not trusted
2861 */
2862 flags |= BADCRL_NOT_TRUSTED;
2863 break;
2864 }
2865
2866 /*
2867 * Check for validity of CRL (Do not drop out)
2868 */
2869 if( x509parse_time_expired( &crl_list->next_update ) )
2870 flags |= BADCRL_EXPIRED;
2871
2872 /*
2873 * Check if certificate is revoked
2874 */
2875 if( x509parse_revoked(crt, crl_list) )
2876 {
2877 flags |= BADCERT_REVOKED;
2878 break;
2879 }
2880
2881 crl_list = crl_list->next;
2882 }
2883 return flags;
2884}
2885
2886/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002887 * Verify the certificate validity
2888 */
2889int x509parse_verify( x509_cert *crt,
2890 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002891 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002892 const char *cn, int *flags,
2893 int (*f_vrfy)(void *, x509_cert *, int, int),
2894 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002895{
Paul Bakker23986e52011-04-24 08:57:21 +00002896 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 int hash_id;
2898 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002899 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002901 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Paul Bakker40ea7de2009-05-03 10:18:48 +00002903 *flags = 0;
2904
2905 if( x509parse_time_expired( &crt->valid_to ) )
2906 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
2908 if( cn != NULL )
2909 {
2910 name = &crt->subject;
2911 cn_len = strlen( cn );
2912
2913 while( name != NULL )
2914 {
Paul Bakker7261cba2013-01-16 12:39:54 +01002915 if( name->oid.len == 3 &&
2916 memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2917 name->val.len == cn_len &&
2918 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 break;
2920
2921 name = name->next;
2922 }
2923
2924 if( name == NULL )
2925 *flags |= BADCERT_CN_MISMATCH;
2926 }
2927
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 /*
2929 * Iterate upwards in the given cert chain,
2930 * ignoring any upper cert with CA != TRUE.
2931 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002932 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
2934 pathlen = 1;
2935
Paul Bakker76fd75a2011-01-16 21:12:10 +00002936 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002938 if( parent->ca_istrue == 0 ||
2939 crt->issuer_raw.len != parent->subject_raw.len ||
2940 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 crt->issuer_raw.len ) != 0 )
2942 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002943 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 continue;
2945 }
2946
Paul Bakker27d66162010-03-17 06:56:01 +00002947 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
2949 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2950
Paul Bakker76fd75a2011-01-16 21:12:10 +00002951 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2952 crt->sig.p ) != 0 )
2953 *flags |= BADCERT_NOT_TRUSTED;
2954
2955 /* Check trusted CA's CRL for the given crt */
2956 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002957
2958 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002959 if( NULL != f_vrfy )
2960 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002961 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002962 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002963 else
2964 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002965 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002966 else if( *flags != 0 )
2967 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
2969 pathlen++;
2970
Paul Bakker76fd75a2011-01-16 21:12:10 +00002971 crt = parent;
2972 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002973 }
2974
2975 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002976 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002978 *flags |= BADCERT_NOT_TRUSTED;
2979
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002980 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 {
2982 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2983 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2984 crt->issuer_raw.len ) != 0 )
2985 {
2986 trust_ca = trust_ca->next;
2987 continue;
2988 }
2989
2990 if( trust_ca->max_pathlen > 0 &&
2991 trust_ca->max_pathlen < pathlen )
2992 break;
2993
Paul Bakker27d66162010-03-17 06:56:01 +00002994 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002995
2996 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2997
2998 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2999 0, hash, crt->sig.p ) == 0 )
3000 {
3001 /*
3002 * cert. is signed by a trusted CA
3003 */
3004 *flags &= ~BADCERT_NOT_TRUSTED;
3005 break;
3006 }
3007
3008 trust_ca = trust_ca->next;
3009 }
3010
Paul Bakker76fd75a2011-01-16 21:12:10 +00003011 /* Check trusted CA's CRL for the given crt */
3012 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003013
3014 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003015 if( NULL != f_vrfy )
3016 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003017 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003018 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003019 else
3020 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003021 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003022 else if( *flags != 0 )
3023 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003024
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 return( 0 );
3026}
3027
3028/*
3029 * Unallocate all certificate data
3030 */
3031void x509_free( x509_cert *crt )
3032{
3033 x509_cert *cert_cur = crt;
3034 x509_cert *cert_prv;
3035 x509_name *name_cur;
3036 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003037 x509_sequence *seq_cur;
3038 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
3040 if( crt == NULL )
3041 return;
3042
3043 do
3044 {
3045 rsa_free( &cert_cur->rsa );
3046
3047 name_cur = cert_cur->issuer.next;
3048 while( name_cur != NULL )
3049 {
3050 name_prv = name_cur;
3051 name_cur = name_cur->next;
3052 memset( name_prv, 0, sizeof( x509_name ) );
3053 free( name_prv );
3054 }
3055
3056 name_cur = cert_cur->subject.next;
3057 while( name_cur != NULL )
3058 {
3059 name_prv = name_cur;
3060 name_cur = name_cur->next;
3061 memset( name_prv, 0, sizeof( x509_name ) );
3062 free( name_prv );
3063 }
3064
Paul Bakker74111d32011-01-15 16:57:55 +00003065 seq_cur = cert_cur->ext_key_usage.next;
3066 while( seq_cur != NULL )
3067 {
3068 seq_prv = seq_cur;
3069 seq_cur = seq_cur->next;
3070 memset( seq_prv, 0, sizeof( x509_sequence ) );
3071 free( seq_prv );
3072 }
3073
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 if( cert_cur->raw.p != NULL )
3075 {
3076 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3077 free( cert_cur->raw.p );
3078 }
3079
3080 cert_cur = cert_cur->next;
3081 }
3082 while( cert_cur != NULL );
3083
3084 cert_cur = crt;
3085 do
3086 {
3087 cert_prv = cert_cur;
3088 cert_cur = cert_cur->next;
3089
3090 memset( cert_prv, 0, sizeof( x509_cert ) );
3091 if( cert_prv != crt )
3092 free( cert_prv );
3093 }
3094 while( cert_cur != NULL );
3095}
3096
Paul Bakkerd98030e2009-05-02 15:13:40 +00003097/*
3098 * Unallocate all CRL data
3099 */
3100void x509_crl_free( x509_crl *crl )
3101{
3102 x509_crl *crl_cur = crl;
3103 x509_crl *crl_prv;
3104 x509_name *name_cur;
3105 x509_name *name_prv;
3106 x509_crl_entry *entry_cur;
3107 x509_crl_entry *entry_prv;
3108
3109 if( crl == NULL )
3110 return;
3111
3112 do
3113 {
3114 name_cur = crl_cur->issuer.next;
3115 while( name_cur != NULL )
3116 {
3117 name_prv = name_cur;
3118 name_cur = name_cur->next;
3119 memset( name_prv, 0, sizeof( x509_name ) );
3120 free( name_prv );
3121 }
3122
3123 entry_cur = crl_cur->entry.next;
3124 while( entry_cur != NULL )
3125 {
3126 entry_prv = entry_cur;
3127 entry_cur = entry_cur->next;
3128 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3129 free( entry_prv );
3130 }
3131
3132 if( crl_cur->raw.p != NULL )
3133 {
3134 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3135 free( crl_cur->raw.p );
3136 }
3137
3138 crl_cur = crl_cur->next;
3139 }
3140 while( crl_cur != NULL );
3141
3142 crl_cur = crl;
3143 do
3144 {
3145 crl_prv = crl_cur;
3146 crl_cur = crl_cur->next;
3147
3148 memset( crl_prv, 0, sizeof( x509_crl ) );
3149 if( crl_prv != crl )
3150 free( crl_prv );
3151 }
3152 while( crl_cur != NULL );
3153}
3154
Paul Bakker40e46942009-01-03 21:51:57 +00003155#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003156
Paul Bakker40e46942009-01-03 21:51:57 +00003157#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
3159/*
3160 * Checkup routine
3161 */
3162int x509_self_test( int verbose )
3163{
Paul Bakker5690efc2011-05-26 13:16:06 +00003164#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003165 int ret;
3166 int flags;
3167 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003168 x509_cert cacert;
3169 x509_cert clicert;
3170 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003171#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003172 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003173#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
3175 if( verbose != 0 )
3176 printf( " X.509 certificate load: " );
3177
3178 memset( &clicert, 0, sizeof( x509_cert ) );
3179
3180 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003181 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003182 if( ret != 0 )
3183 {
3184 if( verbose != 0 )
3185 printf( "failed\n" );
3186
3187 return( ret );
3188 }
3189
3190 memset( &cacert, 0, sizeof( x509_cert ) );
3191
3192 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003193 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 if( ret != 0 )
3195 {
3196 if( verbose != 0 )
3197 printf( "failed\n" );
3198
3199 return( ret );
3200 }
3201
3202 if( verbose != 0 )
3203 printf( "passed\n X.509 private key load: " );
3204
3205 i = strlen( test_ca_key );
3206 j = strlen( test_ca_pwd );
3207
Paul Bakker66b78b22011-03-25 14:22:50 +00003208 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3209
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 if( ( ret = x509parse_key( &rsa,
3211 (unsigned char *) test_ca_key, i,
3212 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3213 {
3214 if( verbose != 0 )
3215 printf( "failed\n" );
3216
3217 return( ret );
3218 }
3219
3220 if( verbose != 0 )
3221 printf( "passed\n X.509 signature verify: ");
3222
Paul Bakker23986e52011-04-24 08:57:21 +00003223 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 if( ret != 0 )
3225 {
Paul Bakker23986e52011-04-24 08:57:21 +00003226 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 if( verbose != 0 )
3228 printf( "failed\n" );
3229
3230 return( ret );
3231 }
3232
Paul Bakker5690efc2011-05-26 13:16:06 +00003233#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003234 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003235 printf( "passed\n X.509 DHM parameter load: " );
3236
3237 i = strlen( test_dhm_params );
3238 j = strlen( test_ca_pwd );
3239
3240 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3241 {
3242 if( verbose != 0 )
3243 printf( "failed\n" );
3244
3245 return( ret );
3246 }
3247
3248 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003250#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
3252 x509_free( &cacert );
3253 x509_free( &clicert );
3254 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003255#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003256 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003257#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
3259 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003260#else
3261 ((void) verbose);
3262 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3263#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003264}
3265
3266#endif
3267
3268#endif