blob: f561754eb7e2ab653d6035495d516ddcdf384674 [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"
45#include "polarssl/md2.h"
46#include "polarssl/md4.h"
47#include "polarssl/md5.h"
48#include "polarssl/sha1.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000049#include "polarssl/sha2.h"
50#include "polarssl/sha4.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000051#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53#include <string.h>
54#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000055#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000056#include <windows.h>
57#else
Paul Bakker5121ce52009-01-03 21:22:43 +000058#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakker335db3f2011-04-25 15:28:35 +000061#if defined(POLARSSL_FS_IO)
62#include <stdio.h>
63#endif
64
Paul Bakker5121ce52009-01-03 21:22:43 +000065/*
Paul Bakker5121ce52009-01-03 21:22:43 +000066 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
67 */
68static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000069 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000070 int *ver )
71{
Paul Bakker23986e52011-04-24 08:57:21 +000072 int ret;
73 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75 if( ( ret = asn1_get_tag( p, end, &len,
76 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
77 {
Paul Bakker40e46942009-01-03 21:51:57 +000078 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000079 {
80 *ver = 0;
81 return( 0 );
82 }
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84 return( ret );
85 }
86
87 end = *p + len;
88
89 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000090 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +000093 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +000094 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( 0 );
97}
98
99/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000100 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000101 */
102static int x509_crl_get_version( unsigned char **p,
103 const unsigned char *end,
104 int *ver )
105{
106 int ret;
107
108 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
109 {
110 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000111 {
112 *ver = 0;
113 return( 0 );
114 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000115
116 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
117 }
118
119 return( 0 );
120}
121
122/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 * CertificateSerialNumber ::= INTEGER
124 */
125static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000126 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 x509_buf *serial )
128{
129 int ret;
130
131 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000132 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000133 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
136 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000137 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000138 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 serial->tag = *(*p)++;
141
142 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000143 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145 serial->p = *p;
146 *p += serial->len;
147
148 return( 0 );
149}
150
151/*
152 * AlgorithmIdentifier ::= SEQUENCE {
153 * algorithm OBJECT IDENTIFIER,
154 * parameters ANY DEFINED BY algorithm OPTIONAL }
155 */
156static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000157 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 x509_buf *alg )
159{
Paul Bakker23986e52011-04-24 08:57:21 +0000160 int ret;
161 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 if( ( ret = asn1_get_tag( p, end, &len,
164 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000165 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167 end = *p + len;
168 alg->tag = **p;
169
170 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000171 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 alg->p = *p;
174 *p += alg->len;
175
176 if( *p == end )
177 return( 0 );
178
179 /*
180 * assume the algorithm parameters must be NULL
181 */
182 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 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 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000186 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000187 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189 return( 0 );
190}
191
192/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 * AttributeTypeAndValue ::= SEQUENCE {
194 * type AttributeType,
195 * value AttributeValue }
196 *
197 * AttributeType ::= OBJECT IDENTIFIER
198 *
199 * AttributeValue ::= ANY DEFINED BY AttributeType
200 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000201static int x509_get_attr_type_value( unsigned char **p,
202 const unsigned char *end,
203 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204{
Paul Bakker23986e52011-04-24 08:57:21 +0000205 int ret;
206 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 x509_buf *oid;
208 x509_buf *val;
209
210 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000212 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 oid = &cur->oid;
215 oid->tag = **p;
216
217 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000218 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220 oid->p = *p;
221 *p += oid->len;
222
223 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000225 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
228 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
229 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000230 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000231 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 val = &cur->val;
234 val->tag = *(*p)++;
235
236 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000237 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 val->p = *p;
240 *p += val->len;
241
242 cur->next = NULL;
243
Paul Bakker400ff6f2011-02-20 10:40:16 +0000244 return( 0 );
245}
246
247/*
248 * RelativeDistinguishedName ::=
249 * SET OF AttributeTypeAndValue
250 *
251 * AttributeTypeAndValue ::= SEQUENCE {
252 * type AttributeType,
253 * value AttributeValue }
254 *
255 * AttributeType ::= OBJECT IDENTIFIER
256 *
257 * AttributeValue ::= ANY DEFINED BY AttributeType
258 */
259static int x509_get_name( unsigned char **p,
260 const unsigned char *end,
261 x509_name *cur )
262{
Paul Bakker23986e52011-04-24 08:57:21 +0000263 int ret;
264 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000265 const unsigned char *end2;
266 x509_name *use;
267
268 if( ( ret = asn1_get_tag( p, end, &len,
269 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000270 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000271
272 end2 = end;
273 end = *p + len;
274 use = cur;
275
276 do
277 {
278 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
279 return( ret );
280
281 if( *p != end )
282 {
283 use->next = (x509_name *) malloc(
284 sizeof( x509_name ) );
285
286 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000287 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000288
289 memset( use->next, 0, sizeof( x509_name ) );
290
291 use = use->next;
292 }
293 }
294 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296 /*
297 * recurse until end of SEQUENCE is reached
298 */
299 if( *p == end2 )
300 return( 0 );
301
302 cur->next = (x509_name *) malloc(
303 sizeof( x509_name ) );
304
305 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000306 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308 return( x509_get_name( p, end2, cur->next ) );
309}
310
311/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 * Time ::= CHOICE {
313 * utcTime UTCTime,
314 * generalTime GeneralizedTime }
315 */
Paul Bakker91200182010-02-18 21:26:15 +0000316static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000317 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000318 x509_time *time )
319{
Paul Bakker23986e52011-04-24 08:57:21 +0000320 int ret;
321 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000322 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000323 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000324
Paul Bakker91200182010-02-18 21:26:15 +0000325 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000326 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
327 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000328
Paul Bakker91200182010-02-18 21:26:15 +0000329 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330
Paul Bakker91200182010-02-18 21:26:15 +0000331 if ( tag == ASN1_UTC_TIME )
332 {
333 (*p)++;
334 ret = asn1_get_len( p, end, &len );
335
336 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000337 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000340 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
341 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000342
Paul Bakker91200182010-02-18 21:26:15 +0000343 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
344 &time->year, &time->mon, &time->day,
345 &time->hour, &time->min, &time->sec ) < 5 )
346 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000347
Paul Bakker400ff6f2011-02-20 10:40:16 +0000348 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000349 time->year += 1900;
350
351 *p += len;
352
353 return( 0 );
354 }
355 else if ( tag == ASN1_GENERALIZED_TIME )
356 {
357 (*p)++;
358 ret = asn1_get_len( p, end, &len );
359
360 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000361 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000362
363 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000364 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
365 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000366
367 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
368 &time->year, &time->mon, &time->day,
369 &time->hour, &time->min, &time->sec ) < 5 )
370 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
371
372 *p += len;
373
374 return( 0 );
375 }
376 else
Paul Bakker9d781402011-05-09 16:17:09 +0000377 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000378}
379
380
381/*
382 * Validity ::= SEQUENCE {
383 * notBefore Time,
384 * notAfter Time }
385 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000386static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000387 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 x509_time *from,
389 x509_time *to )
390{
Paul Bakker23986e52011-04-24 08:57:21 +0000391 int ret;
392 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394 if( ( ret = asn1_get_tag( p, end, &len,
395 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000396 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
398 end = *p + len;
399
Paul Bakker91200182010-02-18 21:26:15 +0000400 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000401 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
Paul Bakker91200182010-02-18 21:26:15 +0000403 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000407 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000408 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
410 return( 0 );
411}
412
413/*
414 * SubjectPublicKeyInfo ::= SEQUENCE {
415 * algorithm AlgorithmIdentifier,
416 * subjectPublicKey BIT STRING }
417 */
418static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000419 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 x509_buf *pk_alg_oid,
421 mpi *N, mpi *E )
422{
Paul Bakker23986e52011-04-24 08:57:21 +0000423 int ret, can_handle;
424 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 unsigned char *end2;
426
427 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
428 return( ret );
429
430 /*
431 * only RSA public keys handled at this time
432 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000433 can_handle = 0;
434
435 if( pk_alg_oid->len == 9 &&
436 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
437 can_handle = 1;
438
439 if( pk_alg_oid->len == 9 &&
440 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
441 {
442 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
443 can_handle = 1;
444
445 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
446 can_handle = 1;
447 }
448
449 if( pk_alg_oid->len == 5 &&
450 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
451 can_handle = 1;
452
453 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000454 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000457 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000460 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000461 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463 end2 = *p + len;
464
465 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000466 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 /*
469 * RSAPublicKey ::= SEQUENCE {
470 * modulus INTEGER, -- n
471 * publicExponent INTEGER -- e
472 * }
473 */
474 if( ( ret = asn1_get_tag( p, end2, &len,
475 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000476 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000479 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000480 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
483 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 return( 0 );
491}
492
493static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000494 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 x509_buf *sig )
496{
Paul Bakker23986e52011-04-24 08:57:21 +0000497 int ret;
498 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 sig->tag = **p;
501
502 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000503 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Paul Bakker74111d32011-01-15 16:57:55 +0000505
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000507 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 sig->len = len;
510 sig->p = *p;
511
512 *p += len;
513
514 return( 0 );
515}
516
517/*
518 * X.509 v2/v3 unique identifier (not parsed)
519 */
520static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000521 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 x509_buf *uid, int n )
523{
524 int ret;
525
526 if( *p == end )
527 return( 0 );
528
529 uid->tag = **p;
530
531 if( ( ret = asn1_get_tag( p, end, &uid->len,
532 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
533 {
Paul Bakker40e46942009-01-03 21:51:57 +0000534 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 return( 0 );
536
537 return( ret );
538 }
539
540 uid->p = *p;
541 *p += uid->len;
542
543 return( 0 );
544}
545
546/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000547 * X.509 Extensions (No parsing of extensions, pointer should
548 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 */
550static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000551 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000552 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000553{
Paul Bakker23986e52011-04-24 08:57:21 +0000554 int ret;
555 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 if( *p == end )
558 return( 0 );
559
560 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000561
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000563 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 ext->p = *p;
567 end = *p + ext->len;
568
569 /*
570 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
571 *
572 * Extension ::= SEQUENCE {
573 * extnID OBJECT IDENTIFIER,
574 * critical BOOLEAN DEFAULT FALSE,
575 * extnValue OCTET STRING }
576 */
577 if( ( ret = asn1_get_tag( p, end, &len,
578 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000579 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000582 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000583 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Paul Bakkerd98030e2009-05-02 15:13:40 +0000585 return( 0 );
586}
587
588/*
589 * X.509 CRL v2 extensions (no extensions parsed yet.)
590 */
591static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000592 const unsigned char *end,
593 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000594{
Paul Bakker23986e52011-04-24 08:57:21 +0000595 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000596 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000597
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000598 /* Get explicit tag */
599 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000600 {
601 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
602 return( 0 );
603
604 return( ret );
605 }
606
607 while( *p < end )
608 {
609 if( ( ret = asn1_get_tag( p, end, &len,
610 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000611 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000612
613 *p += len;
614 }
615
616 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000617 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000618 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
619
620 return( 0 );
621}
622
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000623/*
624 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
625 */
626static int x509_get_crl_entry_ext( unsigned char **p,
627 const unsigned char *end,
628 x509_buf *ext )
629{
630 int ret;
631 size_t len = 0;
632
633 /* OPTIONAL */
634 if (end <= *p)
635 return( 0 );
636
637 ext->tag = **p;
638 ext->p = *p;
639
640 /*
641 * Get CRL-entry extension sequence header
642 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
643 */
644 if( ( ret = asn1_get_tag( p, end, &ext->len,
645 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
646 {
647 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
648 {
649 ext->p = NULL;
650 return( 0 );
651 }
652 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
653 }
654
655 end = *p + ext->len;
656
657 if( end != *p + ext->len )
658 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
659 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
660
661 while( *p < end )
662 {
663 if( ( ret = asn1_get_tag( p, end, &len,
664 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
665 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
666
667 *p += len;
668 }
669
670 if( *p != end )
671 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
672 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
673
674 return( 0 );
675}
676
Paul Bakker74111d32011-01-15 16:57:55 +0000677static int x509_get_basic_constraints( unsigned char **p,
678 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000679 int *ca_istrue,
680 int *max_pathlen )
681{
Paul Bakker23986e52011-04-24 08:57:21 +0000682 int ret;
683 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000684
685 /*
686 * BasicConstraints ::= SEQUENCE {
687 * cA BOOLEAN DEFAULT FALSE,
688 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
689 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000690 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000691 *max_pathlen = 0; /* endless */
692
693 if( ( ret = asn1_get_tag( p, end, &len,
694 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000695 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000696
697 if( *p == end )
698 return 0;
699
Paul Bakker3cccddb2011-01-16 21:46:31 +0000700 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000701 {
702 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000703 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000704
705 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000706 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000707
Paul Bakker3cccddb2011-01-16 21:46:31 +0000708 if( *ca_istrue != 0 )
709 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000710 }
711
712 if( *p == end )
713 return 0;
714
715 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000716 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000717
718 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000719 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000720 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
721
722 (*max_pathlen)++;
723
Paul Bakker74111d32011-01-15 16:57:55 +0000724 return 0;
725}
726
727static int x509_get_ns_cert_type( unsigned char **p,
728 const unsigned char *end,
729 unsigned char *ns_cert_type)
730{
731 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000732 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000733
734 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000735 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000736
737 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000738 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000739 POLARSSL_ERR_ASN1_INVALID_LENGTH );
740
741 /* Get actual bitstring */
742 *ns_cert_type = *bs.p;
743 return 0;
744}
745
746static int x509_get_key_usage( unsigned char **p,
747 const unsigned char *end,
748 unsigned char *key_usage)
749{
750 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000751 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000752
753 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000754 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000755
Paul Bakkercebdf172011-11-11 15:01:31 +0000756 if( bs.len > 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000757 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000758 POLARSSL_ERR_ASN1_INVALID_LENGTH );
759
760 /* Get actual bitstring */
761 *key_usage = *bs.p;
762 return 0;
763}
764
Paul Bakkerd98030e2009-05-02 15:13:40 +0000765/*
Paul Bakker74111d32011-01-15 16:57:55 +0000766 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
767 *
768 * KeyPurposeId ::= OBJECT IDENTIFIER
769 */
770static int x509_get_ext_key_usage( unsigned char **p,
771 const unsigned char *end,
772 x509_sequence *ext_key_usage)
773{
774 int ret;
775
776 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000777 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000778
779 /* Sequence length must be >= 1 */
780 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000782 POLARSSL_ERR_ASN1_INVALID_LENGTH );
783
784 return 0;
785}
786
787/*
788 * X.509 v3 extensions
789 *
790 * TODO: Perform all of the basic constraints tests required by the RFC
791 * TODO: Set values for undetected extensions to a sane default?
792 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000793 */
794static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000795 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000796 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000797{
Paul Bakker23986e52011-04-24 08:57:21 +0000798 int ret;
799 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000800 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000801
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000802 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000803 {
804 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
805 return( 0 );
806
807 return( ret );
808 }
809
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 while( *p < end )
811 {
Paul Bakker74111d32011-01-15 16:57:55 +0000812 /*
813 * Extension ::= SEQUENCE {
814 * extnID OBJECT IDENTIFIER,
815 * critical BOOLEAN DEFAULT FALSE,
816 * extnValue OCTET STRING }
817 */
818 x509_buf extn_oid = {0, 0, NULL};
819 int is_critical = 0; /* DEFAULT FALSE */
820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 if( ( ret = asn1_get_tag( p, end, &len,
822 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000823 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000825 end_ext_data = *p + len;
826
Paul Bakker74111d32011-01-15 16:57:55 +0000827 /* Get extension ID */
828 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Paul Bakker74111d32011-01-15 16:57:55 +0000830 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000831 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
Paul Bakker74111d32011-01-15 16:57:55 +0000833 extn_oid.p = *p;
834 *p += extn_oid.len;
835
836 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000837 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000838 POLARSSL_ERR_ASN1_OUT_OF_DATA );
839
840 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000841 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000842 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000843 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000844
Paul Bakker74111d32011-01-15 16:57:55 +0000845 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000846 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000847 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000848 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000850 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000851
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000852 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000853 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000854 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Paul Bakker74111d32011-01-15 16:57:55 +0000856 /*
857 * Detect supported extensions
858 */
859 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
860 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 {
Paul Bakker74111d32011-01-15 16:57:55 +0000862 /* Parse basic constraints */
863 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000864 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000865 return ( ret );
866 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 }
Paul Bakker74111d32011-01-15 16:57:55 +0000868 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
869 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
870 {
871 /* Parse netscape certificate type */
872 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
873 &crt->ns_cert_type ) ) != 0 )
874 return ( ret );
875 crt->ext_types |= EXT_NS_CERT_TYPE;
876 }
877 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
878 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
879 {
880 /* Parse key usage */
881 if( ( ret = x509_get_key_usage( p, end_ext_octet,
882 &crt->key_usage ) ) != 0 )
883 return ( ret );
884 crt->ext_types |= EXT_KEY_USAGE;
885 }
886 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
887 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
888 {
889 /* Parse extended key usage */
890 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
891 &crt->ext_key_usage ) ) != 0 )
892 return ( ret );
893 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
894 }
895 else
896 {
897 /* No parser found, skip extension */
898 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Paul Bakker5c721f92011-07-27 16:51:09 +0000900#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000901 if( is_critical )
902 {
903 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000904 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000905 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
906 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000907#endif
Paul Bakker74111d32011-01-15 16:57:55 +0000908 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 }
910
911 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000912 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000913 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 return( 0 );
916}
917
918/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000919 * X.509 CRL Entries
920 */
921static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000922 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000923 x509_crl_entry *entry )
924{
Paul Bakker23986e52011-04-24 08:57:21 +0000925 int ret;
926 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000927 x509_crl_entry *cur_entry = entry;
928
929 if( *p == end )
930 return( 0 );
931
Paul Bakker9be19372009-07-27 20:21:53 +0000932 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000933 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
934 {
935 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
936 return( 0 );
937
938 return( ret );
939 }
940
Paul Bakker9be19372009-07-27 20:21:53 +0000941 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000942
943 while( *p < end )
944 {
Paul Bakker23986e52011-04-24 08:57:21 +0000945 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000946 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000947
948 if( ( ret = asn1_get_tag( p, end, &len2,
949 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
950 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000951 return( ret );
952 }
953
Paul Bakker9be19372009-07-27 20:21:53 +0000954 cur_entry->raw.tag = **p;
955 cur_entry->raw.p = *p;
956 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000957 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +0000958
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000959 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000960 return( ret );
961
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000962 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000963 return( ret );
964
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000965 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000966 return( ret );
967
Paul Bakker74111d32011-01-15 16:57:55 +0000968 if ( *p < end )
969 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000970 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
971 cur_entry = cur_entry->next;
972 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
973 }
974 }
975
976 return( 0 );
977}
978
Paul Bakker27d66162010-03-17 06:56:01 +0000979static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
980{
981 if( sig_oid->len == 9 &&
982 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
983 {
984 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
985 {
986 *sig_alg = sig_oid->p[8];
987 return( 0 );
988 }
989
990 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
991 {
992 *sig_alg = sig_oid->p[8];
993 return( 0 );
994 }
995
996 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
997 }
Paul Bakker400ff6f2011-02-20 10:40:16 +0000998 if( sig_oid->len == 5 &&
999 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1000 {
1001 *sig_alg = SIG_RSA_SHA1;
1002 return( 0 );
1003 }
Paul Bakker27d66162010-03-17 06:56:01 +00001004
1005 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1006}
1007
Paul Bakkerd98030e2009-05-02 15:13:40 +00001008/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001009 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001011int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001012{
Paul Bakker23986e52011-04-24 08:57:21 +00001013 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001014 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Paul Bakker320a4b52009-03-28 18:52:39 +00001017 /*
1018 * Check for valid input
1019 */
1020 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001021 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001022
Paul Bakker96743fc2011-02-12 14:30:57 +00001023 p = (unsigned char *) malloc( len = buflen );
1024
1025 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001026 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001027
1028 memcpy( p, buf, buflen );
1029
1030 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
1032 crt->raw.p = p;
1033 crt->raw.len = len;
1034 end = p + len;
1035
1036 /*
1037 * Certificate ::= SEQUENCE {
1038 * tbsCertificate TBSCertificate,
1039 * signatureAlgorithm AlgorithmIdentifier,
1040 * signatureValue BIT STRING }
1041 */
1042 if( ( ret = asn1_get_tag( &p, end, &len,
1043 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1044 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001045 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001046 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 }
1048
Paul Bakker23986e52011-04-24 08:57:21 +00001049 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001051 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001052 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001053 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 }
1055
1056 /*
1057 * TBSCertificate ::= SEQUENCE {
1058 */
1059 crt->tbs.p = p;
1060
1061 if( ( ret = asn1_get_tag( &p, end, &len,
1062 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1063 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001064 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001065 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 }
1067
1068 end = p + len;
1069 crt->tbs.len = end - crt->tbs.p;
1070
1071 /*
1072 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1073 *
1074 * CertificateSerialNumber ::= INTEGER
1075 *
1076 * signature AlgorithmIdentifier
1077 */
1078 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1079 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1080 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1081 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001082 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 return( ret );
1084 }
1085
1086 crt->version++;
1087
1088 if( crt->version > 3 )
1089 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001090 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001091 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 }
1093
Paul Bakker27d66162010-03-17 06:56:01 +00001094 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001096 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001097 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 }
1099
1100 /*
1101 * issuer Name
1102 */
1103 crt->issuer_raw.p = p;
1104
1105 if( ( ret = asn1_get_tag( &p, end, &len,
1106 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1107 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001108 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001109 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 }
1111
1112 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1113 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001114 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 return( ret );
1116 }
1117
1118 crt->issuer_raw.len = p - crt->issuer_raw.p;
1119
1120 /*
1121 * Validity ::= SEQUENCE {
1122 * notBefore Time,
1123 * notAfter Time }
1124 *
1125 */
1126 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1127 &crt->valid_to ) ) != 0 )
1128 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001129 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 return( ret );
1131 }
1132
1133 /*
1134 * subject Name
1135 */
1136 crt->subject_raw.p = p;
1137
1138 if( ( ret = asn1_get_tag( &p, end, &len,
1139 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1140 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001141 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001142 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143 }
1144
1145 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1146 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001147 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 return( ret );
1149 }
1150
1151 crt->subject_raw.len = p - crt->subject_raw.p;
1152
1153 /*
1154 * SubjectPublicKeyInfo ::= SEQUENCE
1155 * algorithm AlgorithmIdentifier,
1156 * subjectPublicKey BIT STRING }
1157 */
1158 if( ( ret = asn1_get_tag( &p, end, &len,
1159 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1160 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001161 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001162 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 }
1164
1165 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1166 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1167 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001168 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 return( ret );
1170 }
1171
1172 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1173 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001174 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 return( ret );
1176 }
1177
1178 crt->rsa.len = mpi_size( &crt->rsa.N );
1179
1180 /*
1181 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1182 * -- If present, version shall be v2 or v3
1183 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1184 * -- If present, version shall be v2 or v3
1185 * extensions [3] EXPLICIT Extensions OPTIONAL
1186 * -- If present, version shall be v3
1187 */
1188 if( crt->version == 2 || crt->version == 3 )
1189 {
1190 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1191 if( ret != 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
1198 if( crt->version == 2 || crt->version == 3 )
1199 {
1200 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1201 if( ret != 0 )
1202 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001203 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 return( ret );
1205 }
1206 }
1207
1208 if( crt->version == 3 )
1209 {
Paul Bakker74111d32011-01-15 16:57:55 +00001210 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 if( ret != 0 )
1212 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001213 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 return( ret );
1215 }
1216 }
1217
1218 if( p != end )
1219 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001220 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001221 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001222 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 }
1224
1225 end = crt->raw.p + crt->raw.len;
1226
1227 /*
1228 * signatureAlgorithm AlgorithmIdentifier,
1229 * signatureValue BIT STRING
1230 */
1231 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1232 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001233 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 return( ret );
1235 }
1236
Paul Bakker320a4b52009-03-28 18:52:39 +00001237 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001239 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001240 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 }
1242
1243 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1244 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001245 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 return( ret );
1247 }
1248
1249 if( p != end )
1250 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001251 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001252 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001253 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 }
1255
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001256 return( 0 );
1257}
1258
1259/*
1260 * Parse one or more PEM certificates from a buffer and add them to the chained list
1261 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001262int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001263{
Paul Bakker69e095c2011-12-10 21:55:01 +00001264 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001265 x509_cert *crt, *prev = NULL;
1266 int buf_format = X509_FORMAT_DER;
1267
1268 crt = chain;
1269
1270 /*
1271 * Check for valid input
1272 */
1273 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001274 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001275
1276 while( crt->version != 0 && crt->next != NULL )
1277 {
1278 prev = crt;
1279 crt = crt->next;
1280 }
1281
1282 /*
1283 * Add new certificate on the end of the chain if needed.
1284 */
1285 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001286 {
1287 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1288
Paul Bakker7d06ad22009-05-02 15:53:56 +00001289 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001290 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001291
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001292 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001293 crt = crt->next;
1294 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001295 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001297 /*
1298 * Determine buffer content. Buffer contains either one DER certificate or
1299 * one or more PEM certificates.
1300 */
1301#if defined(POLARSSL_PEM_C)
1302 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1303 buf_format = X509_FORMAT_PEM;
1304#endif
1305
1306 if( buf_format == X509_FORMAT_DER )
1307 return x509parse_crt_der( crt, buf, buflen );
1308
1309#if defined(POLARSSL_PEM_C)
1310 if( buf_format == X509_FORMAT_PEM )
1311 {
1312 pem_context pem;
1313
1314 while( buflen > 0 )
1315 {
1316 size_t use_len;
1317 pem_init( &pem );
1318
1319 ret = pem_read_buffer( &pem,
1320 "-----BEGIN CERTIFICATE-----",
1321 "-----END CERTIFICATE-----",
1322 buf, NULL, 0, &use_len );
1323
1324 if( ret == 0 )
1325 {
1326 /*
1327 * Was PEM encoded
1328 */
1329 buflen -= use_len;
1330 buf += use_len;
1331 }
1332 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1333 {
1334 pem_free( &pem );
1335
1336 if( first_error == 0 )
1337 first_error = ret;
1338
1339 continue;
1340 }
1341 else
1342 break;
1343
1344 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1345
1346 pem_free( &pem );
1347
1348 if( ret != 0 )
1349 {
1350 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001351 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001352 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001353 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001354 {
1355 if( prev )
1356 prev->next = NULL;
1357
1358 if( crt != chain )
1359 free( crt );
1360
1361 return( ret );
1362 }
1363
1364 if( first_error == 0 )
1365 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001366
1367 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001368
1369 memset( crt, 0, sizeof( x509_cert ) );
1370 continue;
1371 }
1372
1373 success = 1;
1374
1375 /*
1376 * Add new certificate to the list
1377 */
1378 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1379
1380 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001381 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001382
1383 prev = crt;
1384 crt = crt->next;
1385 memset( crt, 0, sizeof( x509_cert ) );
1386 }
1387 }
1388#endif
1389
1390 if( crt->version == 0 )
1391 {
1392 if( prev )
1393 prev->next = NULL;
1394
1395 if( crt != chain )
1396 free( crt );
1397 }
1398
1399 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001400 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001401 else if( first_error )
1402 return( first_error );
1403 else
1404 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405}
1406
1407/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001408 * Parse one or more CRLs and add them to the chained list
1409 */
Paul Bakker23986e52011-04-24 08:57:21 +00001410int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001411{
Paul Bakker23986e52011-04-24 08:57:21 +00001412 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001413 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001414 unsigned char *p, *end;
1415 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001416#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001417 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001418 pem_context pem;
1419#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001420
1421 crl = chain;
1422
1423 /*
1424 * Check for valid input
1425 */
1426 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001427 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001428
1429 while( crl->version != 0 && crl->next != NULL )
1430 crl = crl->next;
1431
1432 /*
1433 * Add new CRL on the end of the chain if needed.
1434 */
1435 if ( crl->version != 0 && crl->next == NULL)
1436 {
1437 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1438
Paul Bakker7d06ad22009-05-02 15:53:56 +00001439 if( crl->next == NULL )
1440 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001441 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001442 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001443 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001444
Paul Bakker7d06ad22009-05-02 15:53:56 +00001445 crl = crl->next;
1446 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001447 }
1448
Paul Bakker96743fc2011-02-12 14:30:57 +00001449#if defined(POLARSSL_PEM_C)
1450 pem_init( &pem );
1451 ret = pem_read_buffer( &pem,
1452 "-----BEGIN X509 CRL-----",
1453 "-----END X509 CRL-----",
1454 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001455
Paul Bakker96743fc2011-02-12 14:30:57 +00001456 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001457 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001458 /*
1459 * Was PEM encoded
1460 */
1461 buflen -= use_len;
1462 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001463
1464 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001465 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001466 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001467 p = pem.buf;
1468 pem.buf = NULL;
1469 len = pem.buflen;
1470 pem_free( &pem );
1471 }
1472 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1473 {
1474 pem_free( &pem );
1475 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001476 }
1477 else
1478 {
1479 /*
1480 * nope, copy the raw DER data
1481 */
1482 p = (unsigned char *) malloc( len = buflen );
1483
1484 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001485 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001486
1487 memcpy( p, buf, buflen );
1488
1489 buflen = 0;
1490 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001491#else
1492 p = (unsigned char *) malloc( len = buflen );
1493
1494 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001495 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001496
1497 memcpy( p, buf, buflen );
1498
1499 buflen = 0;
1500#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001501
1502 crl->raw.p = p;
1503 crl->raw.len = len;
1504 end = p + len;
1505
1506 /*
1507 * CertificateList ::= SEQUENCE {
1508 * tbsCertList TBSCertList,
1509 * signatureAlgorithm AlgorithmIdentifier,
1510 * signatureValue BIT STRING }
1511 */
1512 if( ( ret = asn1_get_tag( &p, end, &len,
1513 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1514 {
1515 x509_crl_free( crl );
1516 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1517 }
1518
Paul Bakker23986e52011-04-24 08:57:21 +00001519 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001520 {
1521 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001522 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001523 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1524 }
1525
1526 /*
1527 * TBSCertList ::= SEQUENCE {
1528 */
1529 crl->tbs.p = p;
1530
1531 if( ( ret = asn1_get_tag( &p, end, &len,
1532 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1533 {
1534 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001535 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001536 }
1537
1538 end = p + len;
1539 crl->tbs.len = end - crl->tbs.p;
1540
1541 /*
1542 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1543 * -- if present, MUST be v2
1544 *
1545 * signature AlgorithmIdentifier
1546 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001547 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001548 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1549 {
1550 x509_crl_free( crl );
1551 return( ret );
1552 }
1553
1554 crl->version++;
1555
1556 if( crl->version > 2 )
1557 {
1558 x509_crl_free( crl );
1559 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1560 }
1561
Paul Bakker27d66162010-03-17 06:56:01 +00001562 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001563 {
1564 x509_crl_free( crl );
1565 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1566 }
1567
1568 /*
1569 * issuer Name
1570 */
1571 crl->issuer_raw.p = p;
1572
1573 if( ( ret = asn1_get_tag( &p, end, &len,
1574 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1575 {
1576 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001577 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001578 }
1579
1580 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1581 {
1582 x509_crl_free( crl );
1583 return( ret );
1584 }
1585
1586 crl->issuer_raw.len = p - crl->issuer_raw.p;
1587
1588 /*
1589 * thisUpdate Time
1590 * nextUpdate Time OPTIONAL
1591 */
Paul Bakker91200182010-02-18 21:26:15 +00001592 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001593 {
1594 x509_crl_free( crl );
1595 return( ret );
1596 }
1597
Paul Bakker91200182010-02-18 21:26:15 +00001598 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001599 {
Paul Bakker9d781402011-05-09 16:17:09 +00001600 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001601 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001602 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001603 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001604 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001605 x509_crl_free( crl );
1606 return( ret );
1607 }
1608 }
1609
1610 /*
1611 * revokedCertificates SEQUENCE OF SEQUENCE {
1612 * userCertificate CertificateSerialNumber,
1613 * revocationDate Time,
1614 * crlEntryExtensions Extensions OPTIONAL
1615 * -- if present, MUST be v2
1616 * } OPTIONAL
1617 */
1618 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1619 {
1620 x509_crl_free( crl );
1621 return( ret );
1622 }
1623
1624 /*
1625 * crlExtensions EXPLICIT Extensions OPTIONAL
1626 * -- if present, MUST be v2
1627 */
1628 if( crl->version == 2 )
1629 {
1630 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1631
1632 if( ret != 0 )
1633 {
1634 x509_crl_free( crl );
1635 return( ret );
1636 }
1637 }
1638
1639 if( p != end )
1640 {
1641 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001642 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001643 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1644 }
1645
1646 end = crl->raw.p + crl->raw.len;
1647
1648 /*
1649 * signatureAlgorithm AlgorithmIdentifier,
1650 * signatureValue BIT STRING
1651 */
1652 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1653 {
1654 x509_crl_free( crl );
1655 return( ret );
1656 }
1657
1658 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1659 {
1660 x509_crl_free( crl );
1661 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1662 }
1663
1664 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1665 {
1666 x509_crl_free( crl );
1667 return( ret );
1668 }
1669
1670 if( p != end )
1671 {
1672 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001673 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001674 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1675 }
1676
1677 if( buflen > 0 )
1678 {
1679 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1680
Paul Bakker7d06ad22009-05-02 15:53:56 +00001681 if( crl->next == NULL )
1682 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001683 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001684 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001685 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001686
Paul Bakker7d06ad22009-05-02 15:53:56 +00001687 crl = crl->next;
1688 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001689
1690 return( x509parse_crl( crl, buf, buflen ) );
1691 }
1692
1693 return( 0 );
1694}
1695
Paul Bakker335db3f2011-04-25 15:28:35 +00001696#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001697/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001698 * Load all data from a file into a given buffer.
1699 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001700int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001701{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001702 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001703
Paul Bakkerd98030e2009-05-02 15:13:40 +00001704 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001705 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001706
Paul Bakkerd98030e2009-05-02 15:13:40 +00001707 fseek( f, 0, SEEK_END );
1708 *n = (size_t) ftell( f );
1709 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001710
Paul Bakkerd98030e2009-05-02 15:13:40 +00001711 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001712 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001713
Paul Bakkerd98030e2009-05-02 15:13:40 +00001714 if( fread( *buf, 1, *n, f ) != *n )
1715 {
1716 fclose( f );
1717 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001718 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001719 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001720
Paul Bakkerd98030e2009-05-02 15:13:40 +00001721 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001722
Paul Bakkerd98030e2009-05-02 15:13:40 +00001723 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001724
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001726}
1727
1728/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 * Load one or more certificates and add them to the chained list
1730 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001731int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001732{
1733 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001734 size_t n;
1735 unsigned char *buf;
1736
Paul Bakker69e095c2011-12-10 21:55:01 +00001737 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1738 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
Paul Bakker69e095c2011-12-10 21:55:01 +00001740 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
1742 memset( buf, 0, n + 1 );
1743 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
1745 return( ret );
1746}
1747
Paul Bakkerd98030e2009-05-02 15:13:40 +00001748/*
1749 * Load one or more CRLs and add them to the chained list
1750 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001751int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752{
1753 int ret;
1754 size_t n;
1755 unsigned char *buf;
1756
Paul Bakker69e095c2011-12-10 21:55:01 +00001757 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1758 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001759
Paul Bakker27fdf462011-06-09 13:55:13 +00001760 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001761
1762 memset( buf, 0, n + 1 );
1763 free( buf );
1764
1765 return( ret );
1766}
1767
Paul Bakker5121ce52009-01-03 21:22:43 +00001768/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001769 * Load and parse a private RSA key
1770 */
1771int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1772{
1773 int ret;
1774 size_t n;
1775 unsigned char *buf;
1776
Paul Bakker69e095c2011-12-10 21:55:01 +00001777 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1778 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001779
1780 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001781 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001782 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001783 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001784 (unsigned char *) pwd, strlen( pwd ) );
1785
1786 memset( buf, 0, n + 1 );
1787 free( buf );
1788
1789 return( ret );
1790}
1791
1792/*
1793 * Load and parse a public RSA key
1794 */
1795int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1796{
1797 int ret;
1798 size_t n;
1799 unsigned char *buf;
1800
Paul Bakker69e095c2011-12-10 21:55:01 +00001801 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1802 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001803
Paul Bakker27fdf462011-06-09 13:55:13 +00001804 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001805
1806 memset( buf, 0, n + 1 );
1807 free( buf );
1808
1809 return( ret );
1810}
1811#endif /* POLARSSL_FS_IO */
1812
1813/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 * Parse a private RSA key
1815 */
Paul Bakker23986e52011-04-24 08:57:21 +00001816int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1817 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001818{
Paul Bakker23986e52011-04-24 08:57:21 +00001819 int ret;
1820 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001821 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001822 unsigned char *p_alt;
1823 x509_buf pk_alg_oid;
1824
Paul Bakker96743fc2011-02-12 14:30:57 +00001825#if defined(POLARSSL_PEM_C)
1826 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001827
Paul Bakker96743fc2011-02-12 14:30:57 +00001828 pem_init( &pem );
1829 ret = pem_read_buffer( &pem,
1830 "-----BEGIN RSA PRIVATE KEY-----",
1831 "-----END RSA PRIVATE KEY-----",
1832 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Paul Bakkered56b222011-07-13 11:26:43 +00001834 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1835 {
1836 ret = pem_read_buffer( &pem,
1837 "-----BEGIN PRIVATE KEY-----",
1838 "-----END PRIVATE KEY-----",
1839 key, pwd, pwdlen, &len );
1840 }
1841
Paul Bakker96743fc2011-02-12 14:30:57 +00001842 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001844 /*
1845 * Was PEM encoded
1846 */
1847 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001849 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001850 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001851 pem_free( &pem );
1852 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Paul Bakker96743fc2011-02-12 14:30:57 +00001855 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1856#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001857 ((void) pwd);
1858 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001859 p = (unsigned char *) key;
1860#endif
1861 end = p + keylen;
1862
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001864 * Note: Depending on the type of private key file one can expect either a
1865 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1866 *
1867 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001868 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001869 * algorithm AlgorithmIdentifier,
1870 * PrivateKey BIT STRING
1871 * }
1872 *
1873 * AlgorithmIdentifier ::= SEQUENCE {
1874 * algorithm OBJECT IDENTIFIER,
1875 * parameters ANY DEFINED BY algorithm OPTIONAL
1876 * }
1877 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 * RSAPrivateKey ::= SEQUENCE {
1879 * version Version,
1880 * modulus INTEGER, -- n
1881 * publicExponent INTEGER, -- e
1882 * privateExponent INTEGER, -- d
1883 * prime1 INTEGER, -- p
1884 * prime2 INTEGER, -- q
1885 * exponent1 INTEGER, -- d mod (p-1)
1886 * exponent2 INTEGER, -- d mod (q-1)
1887 * coefficient INTEGER, -- (inverse of q) mod p
1888 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1889 * }
1890 */
1891 if( ( ret = asn1_get_tag( &p, end, &len,
1892 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1893 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001894#if defined(POLARSSL_PEM_C)
1895 pem_free( &pem );
1896#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001898 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 }
1900
1901 end = p + len;
1902
1903 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1904 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001905#if defined(POLARSSL_PEM_C)
1906 pem_free( &pem );
1907#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001909 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 }
1911
1912 if( rsa->ver != 0 )
1913 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001914#if defined(POLARSSL_PEM_C)
1915 pem_free( &pem );
1916#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001918 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 }
1920
Paul Bakkered56b222011-07-13 11:26:43 +00001921 p_alt = p;
1922
1923 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1924 {
1925 // Assume that we have the PKCS#1 format if wrong
1926 // tag was encountered
1927 //
1928 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1929 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1930 {
1931#if defined(POLARSSL_PEM_C)
1932 pem_free( &pem );
1933#endif
1934 rsa_free( rsa );
1935 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1936 }
1937 }
1938 else
1939 {
1940 int can_handle;
1941
1942 /*
1943 * only RSA keys handled at this time
1944 */
1945 can_handle = 0;
1946
1947 if( pk_alg_oid.len == 9 &&
1948 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1949 can_handle = 1;
1950
1951 if( pk_alg_oid.len == 9 &&
1952 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1953 {
1954 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1955 can_handle = 1;
1956
1957 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1958 can_handle = 1;
1959 }
1960
1961 if( pk_alg_oid.len == 5 &&
1962 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
1963 can_handle = 1;
1964
1965 if( can_handle == 0 )
1966 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
1967
1968 /*
1969 * Parse the PKCS#8 format
1970 */
1971
1972 p = p_alt;
1973 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
1974 {
1975#if defined(POLARSSL_PEM_C)
1976 pem_free( &pem );
1977#endif
1978 rsa_free( rsa );
1979 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1980 }
1981
1982 if( ( end - p ) < 1 )
1983 {
1984#if defined(POLARSSL_PEM_C)
1985 pem_free( &pem );
1986#endif
1987 rsa_free( rsa );
1988 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
1989 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1990 }
1991
1992 end = p + len;
1993
1994 if( ( ret = asn1_get_tag( &p, end, &len,
1995 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1996 {
1997#if defined(POLARSSL_PEM_C)
1998 pem_free( &pem );
1999#endif
2000 rsa_free( rsa );
2001 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2002 }
2003
2004 end = p + len;
2005
2006 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2007 {
2008#if defined(POLARSSL_PEM_C)
2009 pem_free( &pem );
2010#endif
2011 rsa_free( rsa );
2012 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2013 }
2014
2015 if( rsa->ver != 0 )
2016 {
2017#if defined(POLARSSL_PEM_C)
2018 pem_free( &pem );
2019#endif
2020 rsa_free( rsa );
2021 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2022 }
2023 }
2024
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2026 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2027 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2028 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2029 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2030 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2031 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2032 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2033 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002034#if defined(POLARSSL_PEM_C)
2035 pem_free( &pem );
2036#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002038 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 }
2040
2041 rsa->len = mpi_size( &rsa->N );
2042
2043 if( p != end )
2044 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002045#if defined(POLARSSL_PEM_C)
2046 pem_free( &pem );
2047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002049 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002050 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 }
2052
2053 if( ( ret = rsa_check_privkey( rsa ) ) != 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 );
2059 return( ret );
2060 }
2061
Paul Bakker96743fc2011-02-12 14:30:57 +00002062#if defined(POLARSSL_PEM_C)
2063 pem_free( &pem );
2064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
2066 return( 0 );
2067}
2068
2069/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002070 * Parse a public RSA key
2071 */
Paul Bakker23986e52011-04-24 08:57:21 +00002072int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002073{
Paul Bakker23986e52011-04-24 08:57:21 +00002074 int ret;
2075 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002076 unsigned char *p, *end;
2077 x509_buf alg_oid;
2078#if defined(POLARSSL_PEM_C)
2079 pem_context pem;
2080
2081 pem_init( &pem );
2082 ret = pem_read_buffer( &pem,
2083 "-----BEGIN PUBLIC KEY-----",
2084 "-----END PUBLIC KEY-----",
2085 key, NULL, 0, &len );
2086
2087 if( ret == 0 )
2088 {
2089 /*
2090 * Was PEM encoded
2091 */
2092 keylen = pem.buflen;
2093 }
2094 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2095 {
2096 pem_free( &pem );
2097 return( ret );
2098 }
2099
2100 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2101#else
2102 p = (unsigned char *) key;
2103#endif
2104 end = p + keylen;
2105
2106 /*
2107 * PublicKeyInfo ::= SEQUENCE {
2108 * algorithm AlgorithmIdentifier,
2109 * PublicKey BIT STRING
2110 * }
2111 *
2112 * AlgorithmIdentifier ::= SEQUENCE {
2113 * algorithm OBJECT IDENTIFIER,
2114 * parameters ANY DEFINED BY algorithm OPTIONAL
2115 * }
2116 *
2117 * RSAPublicKey ::= SEQUENCE {
2118 * modulus INTEGER, -- n
2119 * publicExponent INTEGER -- e
2120 * }
2121 */
2122
2123 if( ( ret = asn1_get_tag( &p, end, &len,
2124 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2125 {
2126#if defined(POLARSSL_PEM_C)
2127 pem_free( &pem );
2128#endif
2129 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002130 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002131 }
2132
2133 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2134 {
2135#if defined(POLARSSL_PEM_C)
2136 pem_free( &pem );
2137#endif
2138 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002139 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002140 }
2141
2142 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2143 {
2144#if defined(POLARSSL_PEM_C)
2145 pem_free( &pem );
2146#endif
2147 rsa_free( rsa );
2148 return( ret );
2149 }
2150
2151 rsa->len = mpi_size( &rsa->N );
2152
2153#if defined(POLARSSL_PEM_C)
2154 pem_free( &pem );
2155#endif
2156
2157 return( 0 );
2158}
2159
Paul Bakkereaa89f82011-04-04 21:36:15 +00002160#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002161/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002162 * Parse DHM parameters
2163 */
Paul Bakker23986e52011-04-24 08:57:21 +00002164int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002165{
Paul Bakker23986e52011-04-24 08:57:21 +00002166 int ret;
2167 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002168 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002169#if defined(POLARSSL_PEM_C)
2170 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002171
Paul Bakker96743fc2011-02-12 14:30:57 +00002172 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002173
Paul Bakker96743fc2011-02-12 14:30:57 +00002174 ret = pem_read_buffer( &pem,
2175 "-----BEGIN DH PARAMETERS-----",
2176 "-----END DH PARAMETERS-----",
2177 dhmin, NULL, 0, &dhminlen );
2178
2179 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002180 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002181 /*
2182 * Was PEM encoded
2183 */
2184 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002185 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002186 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002187 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002188 pem_free( &pem );
2189 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002190 }
2191
Paul Bakker96743fc2011-02-12 14:30:57 +00002192 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2193#else
2194 p = (unsigned char *) dhmin;
2195#endif
2196 end = p + dhminlen;
2197
Paul Bakker1b57b062011-01-06 15:48:19 +00002198 memset( dhm, 0, sizeof( dhm_context ) );
2199
Paul Bakker1b57b062011-01-06 15:48:19 +00002200 /*
2201 * DHParams ::= SEQUENCE {
2202 * prime INTEGER, -- P
2203 * generator INTEGER, -- g
2204 * }
2205 */
2206 if( ( ret = asn1_get_tag( &p, end, &len,
2207 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2208 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002209#if defined(POLARSSL_PEM_C)
2210 pem_free( &pem );
2211#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002212 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002213 }
2214
2215 end = p + len;
2216
2217 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2218 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2219 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002220#if defined(POLARSSL_PEM_C)
2221 pem_free( &pem );
2222#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002223 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002224 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002225 }
2226
2227 if( p != end )
2228 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002229#if defined(POLARSSL_PEM_C)
2230 pem_free( &pem );
2231#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002232 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002233 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002234 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2235 }
2236
Paul Bakker96743fc2011-02-12 14:30:57 +00002237#if defined(POLARSSL_PEM_C)
2238 pem_free( &pem );
2239#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002240
2241 return( 0 );
2242}
2243
Paul Bakker335db3f2011-04-25 15:28:35 +00002244#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002245/*
2246 * Load and parse a private RSA key
2247 */
2248int x509parse_dhmfile( dhm_context *dhm, const char *path )
2249{
2250 int ret;
2251 size_t n;
2252 unsigned char *buf;
2253
Paul Bakker69e095c2011-12-10 21:55:01 +00002254 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2255 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002256
Paul Bakker27fdf462011-06-09 13:55:13 +00002257 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002258
2259 memset( buf, 0, n + 1 );
2260 free( buf );
2261
2262 return( ret );
2263}
Paul Bakker335db3f2011-04-25 15:28:35 +00002264#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002265#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002266
Paul Bakker5121ce52009-01-03 21:22:43 +00002267#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002268#include <stdarg.h>
2269
2270#if !defined vsnprintf
2271#define vsnprintf _vsnprintf
2272#endif // vsnprintf
2273
2274/*
2275 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2276 * Result value is not size of buffer needed, but -1 if no fit is possible.
2277 *
2278 * This fuction tries to 'fix' this by at least suggesting enlarging the
2279 * size by 20.
2280 */
2281int compat_snprintf(char *str, size_t size, const char *format, ...)
2282{
2283 va_list ap;
2284 int res = -1;
2285
2286 va_start( ap, format );
2287
2288 res = vsnprintf( str, size, format, ap );
2289
2290 va_end( ap );
2291
2292 // No quick fix possible
2293 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002294 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002295
2296 return res;
2297}
2298
2299#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002300#endif
2301
Paul Bakkerd98030e2009-05-02 15:13:40 +00002302#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2303
2304#define SAFE_SNPRINTF() \
2305{ \
2306 if( ret == -1 ) \
2307 return( -1 ); \
2308 \
Paul Bakker23986e52011-04-24 08:57:21 +00002309 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002310 p[n - 1] = '\0'; \
2311 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2312 } \
2313 \
Paul Bakker23986e52011-04-24 08:57:21 +00002314 n -= (unsigned int) ret; \
2315 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002316}
2317
Paul Bakker5121ce52009-01-03 21:22:43 +00002318/*
2319 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002320 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002322int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002323{
Paul Bakker23986e52011-04-24 08:57:21 +00002324 int ret;
2325 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002327 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 char s[128], *p;
2329
2330 memset( s, 0, sizeof( s ) );
2331
2332 name = dn;
2333 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002334 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
2336 while( name != NULL )
2337 {
Paul Bakker74111d32011-01-15 16:57:55 +00002338 if( name != dn )
2339 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002340 ret = snprintf( p, n, ", " );
2341 SAFE_SNPRINTF();
2342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002343
2344 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2345 {
2346 switch( name->oid.p[2] )
2347 {
2348 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002349 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002350
2351 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002352 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
2354 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002355 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
2357 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002358 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
2360 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002361 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002362
2363 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002364 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
2366 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002367 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002368 name->oid.p[2] );
2369 break;
2370 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002371 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 }
2373 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2374 {
2375 switch( name->oid.p[8] )
2376 {
2377 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002378 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
2380 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002381 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 name->oid.p[8] );
2383 break;
2384 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002385 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 }
2387 else
Paul Bakker74111d32011-01-15 16:57:55 +00002388 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002389 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002390 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
2393 for( i = 0; i < name->val.len; i++ )
2394 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002395 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 break;
2397
2398 c = name->val.p[i];
2399 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2400 s[i] = '?';
2401 else s[i] = c;
2402 }
2403 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002404 ret = snprintf( p, n, "%s", s );
2405 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 name = name->next;
2407 }
2408
Paul Bakker23986e52011-04-24 08:57:21 +00002409 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410}
2411
2412/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002413 * Store the serial in printable form into buf; no more
2414 * than size characters will be written
2415 */
2416int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2417{
Paul Bakker23986e52011-04-24 08:57:21 +00002418 int ret;
2419 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002420 char *p;
2421
2422 p = buf;
2423 n = size;
2424
2425 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002426 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002427
2428 for( i = 0; i < nr; i++ )
2429 {
Paul Bakker93048802011-12-05 14:38:06 +00002430 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002431 continue;
2432
Paul Bakkerdd476992011-01-16 21:34:59 +00002433 ret = snprintf( p, n, "%02X%s",
2434 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2435 SAFE_SNPRINTF();
2436 }
2437
Paul Bakker03c7c252011-11-25 12:37:37 +00002438 if( nr != serial->len )
2439 {
2440 ret = snprintf( p, n, "...." );
2441 SAFE_SNPRINTF();
2442 }
2443
Paul Bakker23986e52011-04-24 08:57:21 +00002444 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002445}
2446
2447/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002448 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002450int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2451 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002452{
Paul Bakker23986e52011-04-24 08:57:21 +00002453 int ret;
2454 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002455 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
2457 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002458 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002459
Paul Bakkerd98030e2009-05-02 15:13:40 +00002460 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002462 SAFE_SNPRINTF();
2463 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002465 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
Paul Bakkerdd476992011-01-16 21:34:59 +00002467 ret = x509parse_serial_gets( p, n, &crt->serial);
2468 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Paul Bakkerd98030e2009-05-02 15:13:40 +00002470 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2471 SAFE_SNPRINTF();
2472 ret = x509parse_dn_gets( p, n, &crt->issuer );
2473 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
Paul Bakkerd98030e2009-05-02 15:13:40 +00002475 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2476 SAFE_SNPRINTF();
2477 ret = x509parse_dn_gets( p, n, &crt->subject );
2478 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Paul Bakkerd98030e2009-05-02 15:13:40 +00002480 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2482 crt->valid_from.year, crt->valid_from.mon,
2483 crt->valid_from.day, crt->valid_from.hour,
2484 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002485 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Paul Bakkerd98030e2009-05-02 15:13:40 +00002487 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2489 crt->valid_to.year, crt->valid_to.mon,
2490 crt->valid_to.day, crt->valid_to.hour,
2491 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002492 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
Paul Bakkerd98030e2009-05-02 15:13:40 +00002494 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2495 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Paul Bakker27d66162010-03-17 06:56:01 +00002497 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002499 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2500 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2501 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2502 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2503 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2504 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2505 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2506 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2507 default: ret = snprintf( p, n, "???" ); break;
2508 }
2509 SAFE_SNPRINTF();
2510
2511 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002512 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002513 SAFE_SNPRINTF();
2514
Paul Bakker23986e52011-04-24 08:57:21 +00002515 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002516}
2517
Paul Bakker74111d32011-01-15 16:57:55 +00002518/* Compare a given OID string with an OID x509_buf * */
2519#define OID_CMP(oid_str, oid_buf) \
2520 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2521 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2522
2523/*
2524 * Return an informational string describing the given OID
2525 */
2526const char *x509_oid_get_description( x509_buf *oid )
2527{
2528 if ( oid == NULL )
2529 return ( NULL );
2530
2531 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2532 return( STRING_SERVER_AUTH );
2533
2534 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2535 return( STRING_CLIENT_AUTH );
2536
2537 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2538 return( STRING_CODE_SIGNING );
2539
2540 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2541 return( STRING_EMAIL_PROTECTION );
2542
2543 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2544 return( STRING_TIME_STAMPING );
2545
2546 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2547 return( STRING_OCSP_SIGNING );
2548
2549 return( NULL );
2550}
2551
2552/* Return the x.y.z.... style numeric string for the given OID */
2553int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2554{
Paul Bakker23986e52011-04-24 08:57:21 +00002555 int ret;
2556 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002557 unsigned int value;
2558 char *p;
2559
2560 p = buf;
2561 n = size;
2562
2563 /* First byte contains first two dots */
2564 if( oid->len > 0 )
2565 {
2566 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2567 SAFE_SNPRINTF();
2568 }
2569
2570 /* TODO: value can overflow in value. */
2571 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002572 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002573 {
2574 value <<= 7;
2575 value += oid->p[i] & 0x7F;
2576
2577 if( !( oid->p[i] & 0x80 ) )
2578 {
2579 /* Last byte */
2580 ret = snprintf( p, n, ".%d", value );
2581 SAFE_SNPRINTF();
2582 value = 0;
2583 }
2584 }
2585
Paul Bakker23986e52011-04-24 08:57:21 +00002586 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002587}
2588
Paul Bakkerd98030e2009-05-02 15:13:40 +00002589/*
2590 * Return an informational string about the CRL.
2591 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002592int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2593 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002594{
Paul Bakker23986e52011-04-24 08:57:21 +00002595 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002596 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002597 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002598 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002599
2600 p = buf;
2601 n = size;
2602
2603 ret = snprintf( p, n, "%sCRL version : %d",
2604 prefix, crl->version );
2605 SAFE_SNPRINTF();
2606
2607 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2608 SAFE_SNPRINTF();
2609 ret = x509parse_dn_gets( p, n, &crl->issuer );
2610 SAFE_SNPRINTF();
2611
2612 ret = snprintf( p, n, "\n%sthis update : " \
2613 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2614 crl->this_update.year, crl->this_update.mon,
2615 crl->this_update.day, crl->this_update.hour,
2616 crl->this_update.min, crl->this_update.sec );
2617 SAFE_SNPRINTF();
2618
2619 ret = snprintf( p, n, "\n%snext update : " \
2620 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2621 crl->next_update.year, crl->next_update.mon,
2622 crl->next_update.day, crl->next_update.hour,
2623 crl->next_update.min, crl->next_update.sec );
2624 SAFE_SNPRINTF();
2625
2626 entry = &crl->entry;
2627
2628 ret = snprintf( p, n, "\n%sRevoked certificates:",
2629 prefix );
2630 SAFE_SNPRINTF();
2631
Paul Bakker9be19372009-07-27 20:21:53 +00002632 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002633 {
2634 ret = snprintf( p, n, "\n%sserial number: ",
2635 prefix );
2636 SAFE_SNPRINTF();
2637
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002638 ret = x509parse_serial_gets( p, n, &entry->serial);
2639 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002640
Paul Bakkerd98030e2009-05-02 15:13:40 +00002641 ret = snprintf( p, n, " revocation date: " \
2642 "%04d-%02d-%02d %02d:%02d:%02d",
2643 entry->revocation_date.year, entry->revocation_date.mon,
2644 entry->revocation_date.day, entry->revocation_date.hour,
2645 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002646 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002647
2648 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 }
2650
Paul Bakkerd98030e2009-05-02 15:13:40 +00002651 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2652 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
Paul Bakker27d66162010-03-17 06:56:01 +00002654 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002655 {
2656 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2657 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2658 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2659 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2660 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2661 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2662 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2663 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2664 default: ret = snprintf( p, n, "???" ); break;
2665 }
2666 SAFE_SNPRINTF();
2667
Paul Bakker1e27bb22009-07-19 20:25:25 +00002668 ret = snprintf( p, n, "\n" );
2669 SAFE_SNPRINTF();
2670
Paul Bakker23986e52011-04-24 08:57:21 +00002671 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672}
2673
2674/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002675 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002677int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002678{
Paul Bakkercce9d772011-11-18 14:26:47 +00002679 int year, mon, day;
2680 int hour, min, sec;
2681
2682#if defined(_WIN32)
2683 SYSTEMTIME st;
2684
2685 GetLocalTime(&st);
2686
2687 year = st.wYear;
2688 mon = st.wMonth;
2689 day = st.wDay;
2690 hour = st.wHour;
2691 min = st.wMinute;
2692 sec = st.wSecond;
2693#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 struct tm *lt;
2695 time_t tt;
2696
2697 tt = time( NULL );
2698 lt = localtime( &tt );
2699
Paul Bakkercce9d772011-11-18 14:26:47 +00002700 year = lt->tm_year + 1900;
2701 mon = lt->tm_mon + 1;
2702 day = lt->tm_mday;
2703 hour = lt->tm_hour;
2704 min = lt->tm_min;
2705 sec = lt->tm_sec;
2706#endif
2707
2708 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002709 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Paul Bakkercce9d772011-11-18 14:26:47 +00002711 if( year == to->year &&
2712 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002713 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Paul Bakkercce9d772011-11-18 14:26:47 +00002715 if( year == to->year &&
2716 mon == to->mon &&
2717 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002718 return( 1 );
2719
Paul Bakkercce9d772011-11-18 14:26:47 +00002720 if( year == to->year &&
2721 mon == to->mon &&
2722 day == to->day &&
2723 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002724 return( 1 );
2725
Paul Bakkercce9d772011-11-18 14:26:47 +00002726 if( year == to->year &&
2727 mon == to->mon &&
2728 day == to->day &&
2729 hour == to->hour &&
2730 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002731 return( 1 );
2732
Paul Bakkercce9d772011-11-18 14:26:47 +00002733 if( year == to->year &&
2734 mon == to->mon &&
2735 day == to->day &&
2736 hour == to->hour &&
2737 min == to->min &&
2738 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002739 return( 1 );
2740
Paul Bakker40ea7de2009-05-03 10:18:48 +00002741 return( 0 );
2742}
2743
2744/*
2745 * Return 1 if the certificate is revoked, or 0 otherwise.
2746 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002747int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002748{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002749 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002750
2751 while( cur != NULL && cur->serial.len != 0 )
2752 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002753 if( crt->serial.len == cur->serial.len &&
2754 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002755 {
2756 if( x509parse_time_expired( &cur->revocation_date ) )
2757 return( 1 );
2758 }
2759
2760 cur = cur->next;
2761 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
2763 return( 0 );
2764}
2765
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002766/*
2767 * Wrapper for x509 hashes.
2768 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002769 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002770 */
Paul Bakker23986e52011-04-24 08:57:21 +00002771static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 unsigned char *out )
2773{
2774 switch( alg )
2775 {
Paul Bakker40e46942009-01-03 21:51:57 +00002776#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002777 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002778#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002779#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002780 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002781#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002782#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002783 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002784#endif
2785#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002786 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002787#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002788#if defined(POLARSSL_SHA2_C)
2789 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2790 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2791#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002792#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002793 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2794 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2795#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002797 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 break;
2799 }
2800}
2801
2802/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002803 * Check that the given certificate is valid accoring to the CRL.
2804 */
2805static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2806 x509_crl *crl_list)
2807{
2808 int flags = 0;
2809 int hash_id;
2810 unsigned char hash[64];
2811
2812 /*
2813 * TODO: What happens if no CRL is present?
2814 * Suggestion: Revocation state should be unknown if no CRL is present.
2815 * For backwards compatibility this is not yet implemented.
2816 */
2817
2818 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2819 {
2820 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2821 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2822 crl_list->issuer_raw.len ) != 0 )
2823 {
2824 crl_list = crl_list->next;
2825 continue;
2826 }
2827
2828 /*
2829 * Check if CRL is correctly signed by the trusted CA
2830 */
2831 hash_id = crl_list->sig_alg;
2832
2833 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2834
2835 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2836 0, hash, crl_list->sig.p ) == 0 )
2837 {
2838 /*
2839 * CRL is not trusted
2840 */
2841 flags |= BADCRL_NOT_TRUSTED;
2842 break;
2843 }
2844
2845 /*
2846 * Check for validity of CRL (Do not drop out)
2847 */
2848 if( x509parse_time_expired( &crl_list->next_update ) )
2849 flags |= BADCRL_EXPIRED;
2850
2851 /*
2852 * Check if certificate is revoked
2853 */
2854 if( x509parse_revoked(crt, crl_list) )
2855 {
2856 flags |= BADCERT_REVOKED;
2857 break;
2858 }
2859
2860 crl_list = crl_list->next;
2861 }
2862 return flags;
2863}
2864
2865/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 * Verify the certificate validity
2867 */
2868int x509parse_verify( x509_cert *crt,
2869 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002870 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002871 const char *cn, int *flags,
2872 int (*f_vrfy)(void *, x509_cert *, int, int),
2873 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002874{
Paul Bakker23986e52011-04-24 08:57:21 +00002875 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002876 int hash_id;
2877 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002878 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002880 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Paul Bakker40ea7de2009-05-03 10:18:48 +00002882 *flags = 0;
2883
2884 if( x509parse_time_expired( &crt->valid_to ) )
2885 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
2887 if( cn != NULL )
2888 {
2889 name = &crt->subject;
2890 cn_len = strlen( cn );
2891
2892 while( name != NULL )
2893 {
2894 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2895 memcmp( name->val.p, cn, cn_len ) == 0 &&
2896 name->val.len == cn_len )
2897 break;
2898
2899 name = name->next;
2900 }
2901
2902 if( name == NULL )
2903 *flags |= BADCERT_CN_MISMATCH;
2904 }
2905
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 /*
2907 * Iterate upwards in the given cert chain,
2908 * ignoring any upper cert with CA != TRUE.
2909 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002910 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
2912 pathlen = 1;
2913
Paul Bakker76fd75a2011-01-16 21:12:10 +00002914 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002916 if( parent->ca_istrue == 0 ||
2917 crt->issuer_raw.len != parent->subject_raw.len ||
2918 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 crt->issuer_raw.len ) != 0 )
2920 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002921 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 continue;
2923 }
2924
Paul Bakker27d66162010-03-17 06:56:01 +00002925 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
2927 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2928
Paul Bakker76fd75a2011-01-16 21:12:10 +00002929 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2930 crt->sig.p ) != 0 )
2931 *flags |= BADCERT_NOT_TRUSTED;
2932
2933 /* Check trusted CA's CRL for the given crt */
2934 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002935
2936 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002937 if( NULL != f_vrfy )
2938 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002939 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002940 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002941 else
2942 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002943 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002944 else if( *flags != 0 )
2945 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
2947 pathlen++;
2948
Paul Bakker76fd75a2011-01-16 21:12:10 +00002949 crt = parent;
2950 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951 }
2952
2953 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002954 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002955 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002956 *flags |= BADCERT_NOT_TRUSTED;
2957
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002958 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 {
2960 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2961 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2962 crt->issuer_raw.len ) != 0 )
2963 {
2964 trust_ca = trust_ca->next;
2965 continue;
2966 }
2967
2968 if( trust_ca->max_pathlen > 0 &&
2969 trust_ca->max_pathlen < pathlen )
2970 break;
2971
Paul Bakker27d66162010-03-17 06:56:01 +00002972 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
2974 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2975
2976 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2977 0, hash, crt->sig.p ) == 0 )
2978 {
2979 /*
2980 * cert. is signed by a trusted CA
2981 */
2982 *flags &= ~BADCERT_NOT_TRUSTED;
2983 break;
2984 }
2985
2986 trust_ca = trust_ca->next;
2987 }
2988
Paul Bakker76fd75a2011-01-16 21:12:10 +00002989 /* Check trusted CA's CRL for the given crt */
2990 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002991
2992 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00002993 if( NULL != f_vrfy )
2994 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002995 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002996 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002997 else
2998 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002999 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003000 else if( *flags != 0 )
3001 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003002
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 return( 0 );
3004}
3005
3006/*
3007 * Unallocate all certificate data
3008 */
3009void x509_free( x509_cert *crt )
3010{
3011 x509_cert *cert_cur = crt;
3012 x509_cert *cert_prv;
3013 x509_name *name_cur;
3014 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003015 x509_sequence *seq_cur;
3016 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
3018 if( crt == NULL )
3019 return;
3020
3021 do
3022 {
3023 rsa_free( &cert_cur->rsa );
3024
3025 name_cur = cert_cur->issuer.next;
3026 while( name_cur != NULL )
3027 {
3028 name_prv = name_cur;
3029 name_cur = name_cur->next;
3030 memset( name_prv, 0, sizeof( x509_name ) );
3031 free( name_prv );
3032 }
3033
3034 name_cur = cert_cur->subject.next;
3035 while( name_cur != NULL )
3036 {
3037 name_prv = name_cur;
3038 name_cur = name_cur->next;
3039 memset( name_prv, 0, sizeof( x509_name ) );
3040 free( name_prv );
3041 }
3042
Paul Bakker74111d32011-01-15 16:57:55 +00003043 seq_cur = cert_cur->ext_key_usage.next;
3044 while( seq_cur != NULL )
3045 {
3046 seq_prv = seq_cur;
3047 seq_cur = seq_cur->next;
3048 memset( seq_prv, 0, sizeof( x509_sequence ) );
3049 free( seq_prv );
3050 }
3051
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 if( cert_cur->raw.p != NULL )
3053 {
3054 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3055 free( cert_cur->raw.p );
3056 }
3057
3058 cert_cur = cert_cur->next;
3059 }
3060 while( cert_cur != NULL );
3061
3062 cert_cur = crt;
3063 do
3064 {
3065 cert_prv = cert_cur;
3066 cert_cur = cert_cur->next;
3067
3068 memset( cert_prv, 0, sizeof( x509_cert ) );
3069 if( cert_prv != crt )
3070 free( cert_prv );
3071 }
3072 while( cert_cur != NULL );
3073}
3074
Paul Bakkerd98030e2009-05-02 15:13:40 +00003075/*
3076 * Unallocate all CRL data
3077 */
3078void x509_crl_free( x509_crl *crl )
3079{
3080 x509_crl *crl_cur = crl;
3081 x509_crl *crl_prv;
3082 x509_name *name_cur;
3083 x509_name *name_prv;
3084 x509_crl_entry *entry_cur;
3085 x509_crl_entry *entry_prv;
3086
3087 if( crl == NULL )
3088 return;
3089
3090 do
3091 {
3092 name_cur = crl_cur->issuer.next;
3093 while( name_cur != NULL )
3094 {
3095 name_prv = name_cur;
3096 name_cur = name_cur->next;
3097 memset( name_prv, 0, sizeof( x509_name ) );
3098 free( name_prv );
3099 }
3100
3101 entry_cur = crl_cur->entry.next;
3102 while( entry_cur != NULL )
3103 {
3104 entry_prv = entry_cur;
3105 entry_cur = entry_cur->next;
3106 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3107 free( entry_prv );
3108 }
3109
3110 if( crl_cur->raw.p != NULL )
3111 {
3112 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3113 free( crl_cur->raw.p );
3114 }
3115
3116 crl_cur = crl_cur->next;
3117 }
3118 while( crl_cur != NULL );
3119
3120 crl_cur = crl;
3121 do
3122 {
3123 crl_prv = crl_cur;
3124 crl_cur = crl_cur->next;
3125
3126 memset( crl_prv, 0, sizeof( x509_crl ) );
3127 if( crl_prv != crl )
3128 free( crl_prv );
3129 }
3130 while( crl_cur != NULL );
3131}
3132
Paul Bakker40e46942009-01-03 21:51:57 +00003133#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003134
Paul Bakker40e46942009-01-03 21:51:57 +00003135#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
3137/*
3138 * Checkup routine
3139 */
3140int x509_self_test( int verbose )
3141{
Paul Bakker5690efc2011-05-26 13:16:06 +00003142#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003143 int ret;
3144 int flags;
3145 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 x509_cert cacert;
3147 x509_cert clicert;
3148 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003149#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003150 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003151#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003152
3153 if( verbose != 0 )
3154 printf( " X.509 certificate load: " );
3155
3156 memset( &clicert, 0, sizeof( x509_cert ) );
3157
3158 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003159 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160 if( ret != 0 )
3161 {
3162 if( verbose != 0 )
3163 printf( "failed\n" );
3164
3165 return( ret );
3166 }
3167
3168 memset( &cacert, 0, sizeof( x509_cert ) );
3169
3170 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003171 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003172 if( ret != 0 )
3173 {
3174 if( verbose != 0 )
3175 printf( "failed\n" );
3176
3177 return( ret );
3178 }
3179
3180 if( verbose != 0 )
3181 printf( "passed\n X.509 private key load: " );
3182
3183 i = strlen( test_ca_key );
3184 j = strlen( test_ca_pwd );
3185
Paul Bakker66b78b22011-03-25 14:22:50 +00003186 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3187
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 if( ( ret = x509parse_key( &rsa,
3189 (unsigned char *) test_ca_key, i,
3190 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3191 {
3192 if( verbose != 0 )
3193 printf( "failed\n" );
3194
3195 return( ret );
3196 }
3197
3198 if( verbose != 0 )
3199 printf( "passed\n X.509 signature verify: ");
3200
Paul Bakker23986e52011-04-24 08:57:21 +00003201 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 if( ret != 0 )
3203 {
Paul Bakker23986e52011-04-24 08:57:21 +00003204 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 if( verbose != 0 )
3206 printf( "failed\n" );
3207
3208 return( ret );
3209 }
3210
Paul Bakker5690efc2011-05-26 13:16:06 +00003211#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003213 printf( "passed\n X.509 DHM parameter load: " );
3214
3215 i = strlen( test_dhm_params );
3216 j = strlen( test_ca_pwd );
3217
3218 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3219 {
3220 if( verbose != 0 )
3221 printf( "failed\n" );
3222
3223 return( ret );
3224 }
3225
3226 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003228#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003229
3230 x509_free( &cacert );
3231 x509_free( &clicert );
3232 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003233#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003234 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003235#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
3237 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003238#else
3239 ((void) verbose);
3240 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003242}
3243
3244#endif
3245
3246#endif