blob: ec4fffc9399e0f332b342243e6e9b92d16b34b82 [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 ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +0000971
972 if( cur_entry->next == NULL )
973 return( POLARSSL_ERR_X509_MALLOC_FAILED );
974
Paul Bakkerd98030e2009-05-02 15:13:40 +0000975 cur_entry = cur_entry->next;
976 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
977 }
978 }
979
980 return( 0 );
981}
982
Paul Bakker27d66162010-03-17 06:56:01 +0000983static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
984{
985 if( sig_oid->len == 9 &&
986 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
987 {
988 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
989 {
990 *sig_alg = sig_oid->p[8];
991 return( 0 );
992 }
993
994 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
995 {
996 *sig_alg = sig_oid->p[8];
997 return( 0 );
998 }
999
1000 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1001 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001002 if( sig_oid->len == 5 &&
1003 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1004 {
1005 *sig_alg = SIG_RSA_SHA1;
1006 return( 0 );
1007 }
Paul Bakker27d66162010-03-17 06:56:01 +00001008
1009 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1010}
1011
Paul Bakkerd98030e2009-05-02 15:13:40 +00001012/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001013 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001015int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001016{
Paul Bakker23986e52011-04-24 08:57:21 +00001017 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001018 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Paul Bakker320a4b52009-03-28 18:52:39 +00001021 /*
1022 * Check for valid input
1023 */
1024 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001025 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001026
Paul Bakker96743fc2011-02-12 14:30:57 +00001027 p = (unsigned char *) malloc( len = buflen );
1028
1029 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001030 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001031
1032 memcpy( p, buf, buflen );
1033
1034 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001035
1036 crt->raw.p = p;
1037 crt->raw.len = len;
1038 end = p + len;
1039
1040 /*
1041 * Certificate ::= SEQUENCE {
1042 * tbsCertificate TBSCertificate,
1043 * signatureAlgorithm AlgorithmIdentifier,
1044 * signatureValue BIT STRING }
1045 */
1046 if( ( ret = asn1_get_tag( &p, end, &len,
1047 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1048 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001049 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001050 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 }
1052
Paul Bakker23986e52011-04-24 08:57:21 +00001053 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001055 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001056 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001057 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 }
1059
1060 /*
1061 * TBSCertificate ::= SEQUENCE {
1062 */
1063 crt->tbs.p = p;
1064
1065 if( ( ret = asn1_get_tag( &p, end, &len,
1066 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1067 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001068 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001069 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 }
1071
1072 end = p + len;
1073 crt->tbs.len = end - crt->tbs.p;
1074
1075 /*
1076 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1077 *
1078 * CertificateSerialNumber ::= INTEGER
1079 *
1080 * signature AlgorithmIdentifier
1081 */
1082 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1083 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1084 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1085 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001086 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 return( ret );
1088 }
1089
1090 crt->version++;
1091
1092 if( crt->version > 3 )
1093 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001094 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001095 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 }
1097
Paul Bakker27d66162010-03-17 06:56:01 +00001098 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001100 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001101 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 }
1103
1104 /*
1105 * issuer Name
1106 */
1107 crt->issuer_raw.p = p;
1108
1109 if( ( ret = asn1_get_tag( &p, end, &len,
1110 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1111 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001112 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001113 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 }
1115
1116 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1117 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001118 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 return( ret );
1120 }
1121
1122 crt->issuer_raw.len = p - crt->issuer_raw.p;
1123
1124 /*
1125 * Validity ::= SEQUENCE {
1126 * notBefore Time,
1127 * notAfter Time }
1128 *
1129 */
1130 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1131 &crt->valid_to ) ) != 0 )
1132 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001133 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 return( ret );
1135 }
1136
1137 /*
1138 * subject Name
1139 */
1140 crt->subject_raw.p = p;
1141
1142 if( ( ret = asn1_get_tag( &p, end, &len,
1143 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1144 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001145 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001146 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 }
1148
1149 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1150 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001151 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001152 return( ret );
1153 }
1154
1155 crt->subject_raw.len = p - crt->subject_raw.p;
1156
1157 /*
1158 * SubjectPublicKeyInfo ::= SEQUENCE
1159 * algorithm AlgorithmIdentifier,
1160 * subjectPublicKey BIT STRING }
1161 */
1162 if( ( ret = asn1_get_tag( &p, end, &len,
1163 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1164 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001165 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001166 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 }
1168
1169 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1170 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1171 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001172 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 return( ret );
1174 }
1175
1176 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1177 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001178 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 return( ret );
1180 }
1181
1182 crt->rsa.len = mpi_size( &crt->rsa.N );
1183
1184 /*
1185 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1186 * -- If present, version shall be v2 or v3
1187 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1188 * -- If present, version shall be v2 or v3
1189 * extensions [3] EXPLICIT Extensions OPTIONAL
1190 * -- If present, version shall be v3
1191 */
1192 if( crt->version == 2 || crt->version == 3 )
1193 {
1194 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1195 if( ret != 0 )
1196 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001197 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 return( ret );
1199 }
1200 }
1201
1202 if( crt->version == 2 || crt->version == 3 )
1203 {
1204 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1205 if( ret != 0 )
1206 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001207 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 return( ret );
1209 }
1210 }
1211
1212 if( crt->version == 3 )
1213 {
Paul Bakker74111d32011-01-15 16:57:55 +00001214 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 if( ret != 0 )
1216 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001217 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 return( ret );
1219 }
1220 }
1221
1222 if( p != end )
1223 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001224 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001225 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001226 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 }
1228
1229 end = crt->raw.p + crt->raw.len;
1230
1231 /*
1232 * signatureAlgorithm AlgorithmIdentifier,
1233 * signatureValue BIT STRING
1234 */
1235 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1236 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001237 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 return( ret );
1239 }
1240
Paul Bakker320a4b52009-03-28 18:52:39 +00001241 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001243 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001244 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 }
1246
1247 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1248 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001249 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 return( ret );
1251 }
1252
1253 if( p != end )
1254 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001255 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001256 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001257 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 }
1259
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001260 return( 0 );
1261}
1262
1263/*
1264 * Parse one or more PEM certificates from a buffer and add them to the chained list
1265 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001266int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001267{
Paul Bakker69e095c2011-12-10 21:55:01 +00001268 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001269 x509_cert *crt, *prev = NULL;
1270 int buf_format = X509_FORMAT_DER;
1271
1272 crt = chain;
1273
1274 /*
1275 * Check for valid input
1276 */
1277 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001278 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001279
1280 while( crt->version != 0 && crt->next != NULL )
1281 {
1282 prev = crt;
1283 crt = crt->next;
1284 }
1285
1286 /*
1287 * Add new certificate on the end of the chain if needed.
1288 */
1289 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001290 {
1291 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1292
Paul Bakker7d06ad22009-05-02 15:53:56 +00001293 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001294 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001295
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001296 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 crt = crt->next;
1298 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001300
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001301 /*
1302 * Determine buffer content. Buffer contains either one DER certificate or
1303 * one or more PEM certificates.
1304 */
1305#if defined(POLARSSL_PEM_C)
1306 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1307 buf_format = X509_FORMAT_PEM;
1308#endif
1309
1310 if( buf_format == X509_FORMAT_DER )
1311 return x509parse_crt_der( crt, buf, buflen );
1312
1313#if defined(POLARSSL_PEM_C)
1314 if( buf_format == X509_FORMAT_PEM )
1315 {
1316 pem_context pem;
1317
1318 while( buflen > 0 )
1319 {
1320 size_t use_len;
1321 pem_init( &pem );
1322
1323 ret = pem_read_buffer( &pem,
1324 "-----BEGIN CERTIFICATE-----",
1325 "-----END CERTIFICATE-----",
1326 buf, NULL, 0, &use_len );
1327
1328 if( ret == 0 )
1329 {
1330 /*
1331 * Was PEM encoded
1332 */
1333 buflen -= use_len;
1334 buf += use_len;
1335 }
1336 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1337 {
1338 pem_free( &pem );
1339
1340 if( first_error == 0 )
1341 first_error = ret;
1342
1343 continue;
1344 }
1345 else
1346 break;
1347
1348 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1349
1350 pem_free( &pem );
1351
1352 if( ret != 0 )
1353 {
1354 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001355 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001356 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001357 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001358 {
1359 if( prev )
1360 prev->next = NULL;
1361
1362 if( crt != chain )
1363 free( crt );
1364
1365 return( ret );
1366 }
1367
1368 if( first_error == 0 )
1369 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001370
1371 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001372
1373 memset( crt, 0, sizeof( x509_cert ) );
1374 continue;
1375 }
1376
1377 success = 1;
1378
1379 /*
1380 * Add new certificate to the list
1381 */
1382 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1383
1384 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001385 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001386
1387 prev = crt;
1388 crt = crt->next;
1389 memset( crt, 0, sizeof( x509_cert ) );
1390 }
1391 }
1392#endif
1393
1394 if( crt->version == 0 )
1395 {
1396 if( prev )
1397 prev->next = NULL;
1398
1399 if( crt != chain )
1400 free( crt );
1401 }
1402
1403 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001404 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001405 else if( first_error )
1406 return( first_error );
1407 else
1408 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409}
1410
1411/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001412 * Parse one or more CRLs and add them to the chained list
1413 */
Paul Bakker23986e52011-04-24 08:57:21 +00001414int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001415{
Paul Bakker23986e52011-04-24 08:57:21 +00001416 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001417 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001418 unsigned char *p, *end;
1419 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001420#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001421 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001422 pem_context pem;
1423#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001424
1425 crl = chain;
1426
1427 /*
1428 * Check for valid input
1429 */
1430 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001431 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001432
1433 while( crl->version != 0 && crl->next != NULL )
1434 crl = crl->next;
1435
1436 /*
1437 * Add new CRL on the end of the chain if needed.
1438 */
1439 if ( crl->version != 0 && crl->next == NULL)
1440 {
1441 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1442
Paul Bakker7d06ad22009-05-02 15:53:56 +00001443 if( crl->next == NULL )
1444 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001445 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001446 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001447 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001448
Paul Bakker7d06ad22009-05-02 15:53:56 +00001449 crl = crl->next;
1450 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001451 }
1452
Paul Bakker96743fc2011-02-12 14:30:57 +00001453#if defined(POLARSSL_PEM_C)
1454 pem_init( &pem );
1455 ret = pem_read_buffer( &pem,
1456 "-----BEGIN X509 CRL-----",
1457 "-----END X509 CRL-----",
1458 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001459
Paul Bakker96743fc2011-02-12 14:30:57 +00001460 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001461 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001462 /*
1463 * Was PEM encoded
1464 */
1465 buflen -= use_len;
1466 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001467
1468 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001469 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001470 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001471 p = pem.buf;
1472 pem.buf = NULL;
1473 len = pem.buflen;
1474 pem_free( &pem );
1475 }
1476 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1477 {
1478 pem_free( &pem );
1479 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001480 }
1481 else
1482 {
1483 /*
1484 * nope, copy the raw DER data
1485 */
1486 p = (unsigned char *) malloc( len = buflen );
1487
1488 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001489 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001490
1491 memcpy( p, buf, buflen );
1492
1493 buflen = 0;
1494 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001495#else
1496 p = (unsigned char *) malloc( len = buflen );
1497
1498 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001499 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001500
1501 memcpy( p, buf, buflen );
1502
1503 buflen = 0;
1504#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001505
1506 crl->raw.p = p;
1507 crl->raw.len = len;
1508 end = p + len;
1509
1510 /*
1511 * CertificateList ::= SEQUENCE {
1512 * tbsCertList TBSCertList,
1513 * signatureAlgorithm AlgorithmIdentifier,
1514 * signatureValue BIT STRING }
1515 */
1516 if( ( ret = asn1_get_tag( &p, end, &len,
1517 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1518 {
1519 x509_crl_free( crl );
1520 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1521 }
1522
Paul Bakker23986e52011-04-24 08:57:21 +00001523 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001524 {
1525 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001526 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001527 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1528 }
1529
1530 /*
1531 * TBSCertList ::= SEQUENCE {
1532 */
1533 crl->tbs.p = p;
1534
1535 if( ( ret = asn1_get_tag( &p, end, &len,
1536 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1537 {
1538 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001539 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001540 }
1541
1542 end = p + len;
1543 crl->tbs.len = end - crl->tbs.p;
1544
1545 /*
1546 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1547 * -- if present, MUST be v2
1548 *
1549 * signature AlgorithmIdentifier
1550 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001551 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001552 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1553 {
1554 x509_crl_free( crl );
1555 return( ret );
1556 }
1557
1558 crl->version++;
1559
1560 if( crl->version > 2 )
1561 {
1562 x509_crl_free( crl );
1563 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1564 }
1565
Paul Bakker27d66162010-03-17 06:56:01 +00001566 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001567 {
1568 x509_crl_free( crl );
1569 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1570 }
1571
1572 /*
1573 * issuer Name
1574 */
1575 crl->issuer_raw.p = p;
1576
1577 if( ( ret = asn1_get_tag( &p, end, &len,
1578 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1579 {
1580 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001581 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001582 }
1583
1584 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1585 {
1586 x509_crl_free( crl );
1587 return( ret );
1588 }
1589
1590 crl->issuer_raw.len = p - crl->issuer_raw.p;
1591
1592 /*
1593 * thisUpdate Time
1594 * nextUpdate Time OPTIONAL
1595 */
Paul Bakker91200182010-02-18 21:26:15 +00001596 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001597 {
1598 x509_crl_free( crl );
1599 return( ret );
1600 }
1601
Paul Bakker91200182010-02-18 21:26:15 +00001602 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001603 {
Paul Bakker9d781402011-05-09 16:17:09 +00001604 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001605 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001606 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001607 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001608 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609 x509_crl_free( crl );
1610 return( ret );
1611 }
1612 }
1613
1614 /*
1615 * revokedCertificates SEQUENCE OF SEQUENCE {
1616 * userCertificate CertificateSerialNumber,
1617 * revocationDate Time,
1618 * crlEntryExtensions Extensions OPTIONAL
1619 * -- if present, MUST be v2
1620 * } OPTIONAL
1621 */
1622 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1623 {
1624 x509_crl_free( crl );
1625 return( ret );
1626 }
1627
1628 /*
1629 * crlExtensions EXPLICIT Extensions OPTIONAL
1630 * -- if present, MUST be v2
1631 */
1632 if( crl->version == 2 )
1633 {
1634 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1635
1636 if( ret != 0 )
1637 {
1638 x509_crl_free( crl );
1639 return( ret );
1640 }
1641 }
1642
1643 if( p != end )
1644 {
1645 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001646 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001647 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1648 }
1649
1650 end = crl->raw.p + crl->raw.len;
1651
1652 /*
1653 * signatureAlgorithm AlgorithmIdentifier,
1654 * signatureValue BIT STRING
1655 */
1656 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1657 {
1658 x509_crl_free( crl );
1659 return( ret );
1660 }
1661
1662 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1663 {
1664 x509_crl_free( crl );
1665 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1666 }
1667
1668 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1669 {
1670 x509_crl_free( crl );
1671 return( ret );
1672 }
1673
1674 if( p != end )
1675 {
1676 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001677 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1679 }
1680
1681 if( buflen > 0 )
1682 {
1683 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1684
Paul Bakker7d06ad22009-05-02 15:53:56 +00001685 if( crl->next == NULL )
1686 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001688 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001689 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001690
Paul Bakker7d06ad22009-05-02 15:53:56 +00001691 crl = crl->next;
1692 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001693
1694 return( x509parse_crl( crl, buf, buflen ) );
1695 }
1696
1697 return( 0 );
1698}
1699
Paul Bakker335db3f2011-04-25 15:28:35 +00001700#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001701/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001702 * Load all data from a file into a given buffer.
1703 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001704int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001705{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001706 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001707
Paul Bakkerd98030e2009-05-02 15:13:40 +00001708 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001709 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001710
Paul Bakkerd98030e2009-05-02 15:13:40 +00001711 fseek( f, 0, SEEK_END );
1712 *n = (size_t) ftell( f );
1713 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001714
Paul Bakkerd98030e2009-05-02 15:13:40 +00001715 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001716 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001717
Paul Bakkerd98030e2009-05-02 15:13:40 +00001718 if( fread( *buf, 1, *n, f ) != *n )
1719 {
1720 fclose( f );
1721 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001722 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001723 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001724
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001726
Paul Bakkerd98030e2009-05-02 15:13:40 +00001727 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001728
Paul Bakkerd98030e2009-05-02 15:13:40 +00001729 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001730}
1731
1732/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001733 * Load one or more certificates and add them to the chained list
1734 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001735int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001736{
1737 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001738 size_t n;
1739 unsigned char *buf;
1740
Paul Bakker69e095c2011-12-10 21:55:01 +00001741 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1742 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Paul Bakker69e095c2011-12-10 21:55:01 +00001744 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
1746 memset( buf, 0, n + 1 );
1747 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
1749 return( ret );
1750}
1751
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752/*
1753 * Load one or more CRLs and add them to the chained list
1754 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001755int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001756{
1757 int ret;
1758 size_t n;
1759 unsigned char *buf;
1760
Paul Bakker69e095c2011-12-10 21:55:01 +00001761 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1762 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001763
Paul Bakker27fdf462011-06-09 13:55:13 +00001764 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001765
1766 memset( buf, 0, n + 1 );
1767 free( buf );
1768
1769 return( ret );
1770}
1771
Paul Bakker5121ce52009-01-03 21:22:43 +00001772/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001773 * Load and parse a private RSA key
1774 */
1775int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1776{
1777 int ret;
1778 size_t n;
1779 unsigned char *buf;
1780
Paul Bakker69e095c2011-12-10 21:55:01 +00001781 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1782 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001783
1784 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001785 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001786 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001787 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001788 (unsigned char *) pwd, strlen( pwd ) );
1789
1790 memset( buf, 0, n + 1 );
1791 free( buf );
1792
1793 return( ret );
1794}
1795
1796/*
1797 * Load and parse a public RSA key
1798 */
1799int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1800{
1801 int ret;
1802 size_t n;
1803 unsigned char *buf;
1804
Paul Bakker69e095c2011-12-10 21:55:01 +00001805 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1806 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001807
Paul Bakker27fdf462011-06-09 13:55:13 +00001808 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001809
1810 memset( buf, 0, n + 1 );
1811 free( buf );
1812
1813 return( ret );
1814}
1815#endif /* POLARSSL_FS_IO */
1816
1817/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 * Parse a private RSA key
1819 */
Paul Bakker23986e52011-04-24 08:57:21 +00001820int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1821 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001822{
Paul Bakker23986e52011-04-24 08:57:21 +00001823 int ret;
1824 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001825 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001826 unsigned char *p_alt;
1827 x509_buf pk_alg_oid;
1828
Paul Bakker96743fc2011-02-12 14:30:57 +00001829#if defined(POLARSSL_PEM_C)
1830 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
Paul Bakker96743fc2011-02-12 14:30:57 +00001832 pem_init( &pem );
1833 ret = pem_read_buffer( &pem,
1834 "-----BEGIN RSA PRIVATE KEY-----",
1835 "-----END RSA PRIVATE KEY-----",
1836 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
Paul Bakkered56b222011-07-13 11:26:43 +00001838 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1839 {
1840 ret = pem_read_buffer( &pem,
1841 "-----BEGIN PRIVATE KEY-----",
1842 "-----END PRIVATE KEY-----",
1843 key, pwd, pwdlen, &len );
1844 }
1845
Paul Bakker96743fc2011-02-12 14:30:57 +00001846 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001848 /*
1849 * Was PEM encoded
1850 */
1851 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001853 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001854 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001855 pem_free( &pem );
1856 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001857 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Paul Bakker96743fc2011-02-12 14:30:57 +00001859 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1860#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001861 ((void) pwd);
1862 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001863 p = (unsigned char *) key;
1864#endif
1865 end = p + keylen;
1866
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001868 * Note: Depending on the type of private key file one can expect either a
1869 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1870 *
1871 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001872 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001873 * algorithm AlgorithmIdentifier,
1874 * PrivateKey BIT STRING
1875 * }
1876 *
1877 * AlgorithmIdentifier ::= SEQUENCE {
1878 * algorithm OBJECT IDENTIFIER,
1879 * parameters ANY DEFINED BY algorithm OPTIONAL
1880 * }
1881 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 * RSAPrivateKey ::= SEQUENCE {
1883 * version Version,
1884 * modulus INTEGER, -- n
1885 * publicExponent INTEGER, -- e
1886 * privateExponent INTEGER, -- d
1887 * prime1 INTEGER, -- p
1888 * prime2 INTEGER, -- q
1889 * exponent1 INTEGER, -- d mod (p-1)
1890 * exponent2 INTEGER, -- d mod (q-1)
1891 * coefficient INTEGER, -- (inverse of q) mod p
1892 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1893 * }
1894 */
1895 if( ( ret = asn1_get_tag( &p, end, &len,
1896 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1897 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001898#if defined(POLARSSL_PEM_C)
1899 pem_free( &pem );
1900#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001902 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 }
1904
1905 end = p + len;
1906
1907 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1908 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001909#if defined(POLARSSL_PEM_C)
1910 pem_free( &pem );
1911#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001913 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 }
1915
1916 if( rsa->ver != 0 )
1917 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001918#if defined(POLARSSL_PEM_C)
1919 pem_free( &pem );
1920#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001922 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 }
1924
Paul Bakkered56b222011-07-13 11:26:43 +00001925 p_alt = p;
1926
1927 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1928 {
1929 // Assume that we have the PKCS#1 format if wrong
1930 // tag was encountered
1931 //
1932 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1933 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1934 {
1935#if defined(POLARSSL_PEM_C)
1936 pem_free( &pem );
1937#endif
1938 rsa_free( rsa );
1939 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1940 }
1941 }
1942 else
1943 {
1944 int can_handle;
1945
1946 /*
1947 * only RSA keys handled at this time
1948 */
1949 can_handle = 0;
1950
1951 if( pk_alg_oid.len == 9 &&
1952 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1953 can_handle = 1;
1954
1955 if( pk_alg_oid.len == 9 &&
1956 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1957 {
1958 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1959 can_handle = 1;
1960
1961 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1962 can_handle = 1;
1963 }
1964
1965 if( pk_alg_oid.len == 5 &&
1966 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
1967 can_handle = 1;
1968
1969 if( can_handle == 0 )
1970 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
1971
1972 /*
1973 * Parse the PKCS#8 format
1974 */
1975
1976 p = p_alt;
1977 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
1978 {
1979#if defined(POLARSSL_PEM_C)
1980 pem_free( &pem );
1981#endif
1982 rsa_free( rsa );
1983 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1984 }
1985
1986 if( ( end - p ) < 1 )
1987 {
1988#if defined(POLARSSL_PEM_C)
1989 pem_free( &pem );
1990#endif
1991 rsa_free( rsa );
1992 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
1993 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1994 }
1995
1996 end = p + len;
1997
1998 if( ( ret = asn1_get_tag( &p, end, &len,
1999 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2000 {
2001#if defined(POLARSSL_PEM_C)
2002 pem_free( &pem );
2003#endif
2004 rsa_free( rsa );
2005 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2006 }
2007
2008 end = p + len;
2009
2010 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2011 {
2012#if defined(POLARSSL_PEM_C)
2013 pem_free( &pem );
2014#endif
2015 rsa_free( rsa );
2016 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2017 }
2018
2019 if( rsa->ver != 0 )
2020 {
2021#if defined(POLARSSL_PEM_C)
2022 pem_free( &pem );
2023#endif
2024 rsa_free( rsa );
2025 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2026 }
2027 }
2028
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2030 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2031 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2032 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2033 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2034 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2035 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2036 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2037 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002038#if defined(POLARSSL_PEM_C)
2039 pem_free( &pem );
2040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002042 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 }
2044
2045 rsa->len = mpi_size( &rsa->N );
2046
2047 if( p != end )
2048 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002049#if defined(POLARSSL_PEM_C)
2050 pem_free( &pem );
2051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002053 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002054 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 }
2056
2057 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2058 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002059#if defined(POLARSSL_PEM_C)
2060 pem_free( &pem );
2061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 rsa_free( rsa );
2063 return( ret );
2064 }
2065
Paul Bakker96743fc2011-02-12 14:30:57 +00002066#if defined(POLARSSL_PEM_C)
2067 pem_free( &pem );
2068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
2070 return( 0 );
2071}
2072
2073/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002074 * Parse a public RSA key
2075 */
Paul Bakker23986e52011-04-24 08:57:21 +00002076int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002077{
Paul Bakker23986e52011-04-24 08:57:21 +00002078 int ret;
2079 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002080 unsigned char *p, *end;
2081 x509_buf alg_oid;
2082#if defined(POLARSSL_PEM_C)
2083 pem_context pem;
2084
2085 pem_init( &pem );
2086 ret = pem_read_buffer( &pem,
2087 "-----BEGIN PUBLIC KEY-----",
2088 "-----END PUBLIC KEY-----",
2089 key, NULL, 0, &len );
2090
2091 if( ret == 0 )
2092 {
2093 /*
2094 * Was PEM encoded
2095 */
2096 keylen = pem.buflen;
2097 }
2098 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2099 {
2100 pem_free( &pem );
2101 return( ret );
2102 }
2103
2104 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2105#else
2106 p = (unsigned char *) key;
2107#endif
2108 end = p + keylen;
2109
2110 /*
2111 * PublicKeyInfo ::= SEQUENCE {
2112 * algorithm AlgorithmIdentifier,
2113 * PublicKey BIT STRING
2114 * }
2115 *
2116 * AlgorithmIdentifier ::= SEQUENCE {
2117 * algorithm OBJECT IDENTIFIER,
2118 * parameters ANY DEFINED BY algorithm OPTIONAL
2119 * }
2120 *
2121 * RSAPublicKey ::= SEQUENCE {
2122 * modulus INTEGER, -- n
2123 * publicExponent INTEGER -- e
2124 * }
2125 */
2126
2127 if( ( ret = asn1_get_tag( &p, end, &len,
2128 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2129 {
2130#if defined(POLARSSL_PEM_C)
2131 pem_free( &pem );
2132#endif
2133 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002134 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002135 }
2136
2137 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2138 {
2139#if defined(POLARSSL_PEM_C)
2140 pem_free( &pem );
2141#endif
2142 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002143 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002144 }
2145
2146 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2147 {
2148#if defined(POLARSSL_PEM_C)
2149 pem_free( &pem );
2150#endif
2151 rsa_free( rsa );
2152 return( ret );
2153 }
2154
2155 rsa->len = mpi_size( &rsa->N );
2156
2157#if defined(POLARSSL_PEM_C)
2158 pem_free( &pem );
2159#endif
2160
2161 return( 0 );
2162}
2163
Paul Bakkereaa89f82011-04-04 21:36:15 +00002164#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002165/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002166 * Parse DHM parameters
2167 */
Paul Bakker23986e52011-04-24 08:57:21 +00002168int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002169{
Paul Bakker23986e52011-04-24 08:57:21 +00002170 int ret;
2171 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002172 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002173#if defined(POLARSSL_PEM_C)
2174 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002175
Paul Bakker96743fc2011-02-12 14:30:57 +00002176 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002177
Paul Bakker96743fc2011-02-12 14:30:57 +00002178 ret = pem_read_buffer( &pem,
2179 "-----BEGIN DH PARAMETERS-----",
2180 "-----END DH PARAMETERS-----",
2181 dhmin, NULL, 0, &dhminlen );
2182
2183 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002184 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002185 /*
2186 * Was PEM encoded
2187 */
2188 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002189 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002190 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002191 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002192 pem_free( &pem );
2193 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002194 }
2195
Paul Bakker96743fc2011-02-12 14:30:57 +00002196 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2197#else
2198 p = (unsigned char *) dhmin;
2199#endif
2200 end = p + dhminlen;
2201
Paul Bakker1b57b062011-01-06 15:48:19 +00002202 memset( dhm, 0, sizeof( dhm_context ) );
2203
Paul Bakker1b57b062011-01-06 15:48:19 +00002204 /*
2205 * DHParams ::= SEQUENCE {
2206 * prime INTEGER, -- P
2207 * generator INTEGER, -- g
2208 * }
2209 */
2210 if( ( ret = asn1_get_tag( &p, end, &len,
2211 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2212 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002213#if defined(POLARSSL_PEM_C)
2214 pem_free( &pem );
2215#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002216 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002217 }
2218
2219 end = p + len;
2220
2221 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2222 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2223 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002224#if defined(POLARSSL_PEM_C)
2225 pem_free( &pem );
2226#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002227 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002228 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002229 }
2230
2231 if( p != end )
2232 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002233#if defined(POLARSSL_PEM_C)
2234 pem_free( &pem );
2235#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002236 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002237 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002238 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2239 }
2240
Paul Bakker96743fc2011-02-12 14:30:57 +00002241#if defined(POLARSSL_PEM_C)
2242 pem_free( &pem );
2243#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002244
2245 return( 0 );
2246}
2247
Paul Bakker335db3f2011-04-25 15:28:35 +00002248#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002249/*
2250 * Load and parse a private RSA key
2251 */
2252int x509parse_dhmfile( dhm_context *dhm, const char *path )
2253{
2254 int ret;
2255 size_t n;
2256 unsigned char *buf;
2257
Paul Bakker69e095c2011-12-10 21:55:01 +00002258 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2259 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002260
Paul Bakker27fdf462011-06-09 13:55:13 +00002261 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002262
2263 memset( buf, 0, n + 1 );
2264 free( buf );
2265
2266 return( ret );
2267}
Paul Bakker335db3f2011-04-25 15:28:35 +00002268#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002269#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002270
Paul Bakker5121ce52009-01-03 21:22:43 +00002271#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002272#include <stdarg.h>
2273
2274#if !defined vsnprintf
2275#define vsnprintf _vsnprintf
2276#endif // vsnprintf
2277
2278/*
2279 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2280 * Result value is not size of buffer needed, but -1 if no fit is possible.
2281 *
2282 * This fuction tries to 'fix' this by at least suggesting enlarging the
2283 * size by 20.
2284 */
2285int compat_snprintf(char *str, size_t size, const char *format, ...)
2286{
2287 va_list ap;
2288 int res = -1;
2289
2290 va_start( ap, format );
2291
2292 res = vsnprintf( str, size, format, ap );
2293
2294 va_end( ap );
2295
2296 // No quick fix possible
2297 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002298 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002299
2300 return res;
2301}
2302
2303#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002304#endif
2305
Paul Bakkerd98030e2009-05-02 15:13:40 +00002306#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2307
2308#define SAFE_SNPRINTF() \
2309{ \
2310 if( ret == -1 ) \
2311 return( -1 ); \
2312 \
Paul Bakker23986e52011-04-24 08:57:21 +00002313 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002314 p[n - 1] = '\0'; \
2315 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2316 } \
2317 \
Paul Bakker23986e52011-04-24 08:57:21 +00002318 n -= (unsigned int) ret; \
2319 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002320}
2321
Paul Bakker5121ce52009-01-03 21:22:43 +00002322/*
2323 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002324 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002326int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002327{
Paul Bakker23986e52011-04-24 08:57:21 +00002328 int ret;
2329 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002331 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 char s[128], *p;
2333
2334 memset( s, 0, sizeof( s ) );
2335
2336 name = dn;
2337 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002338 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
2340 while( name != NULL )
2341 {
Paul Bakker74111d32011-01-15 16:57:55 +00002342 if( name != dn )
2343 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002344 ret = snprintf( p, n, ", " );
2345 SAFE_SNPRINTF();
2346 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002347
2348 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2349 {
2350 switch( name->oid.p[2] )
2351 {
2352 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002353 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002354
2355 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002356 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
2358 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002359 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
2361 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002362 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
2364 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002365 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
2367 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002368 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
2370 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002371 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 name->oid.p[2] );
2373 break;
2374 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002375 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 }
2377 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2378 {
2379 switch( name->oid.p[8] )
2380 {
2381 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002382 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
2384 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002385 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 name->oid.p[8] );
2387 break;
2388 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002389 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 }
2391 else
Paul Bakker74111d32011-01-15 16:57:55 +00002392 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002393 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002394 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
2397 for( i = 0; i < name->val.len; i++ )
2398 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002399 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 break;
2401
2402 c = name->val.p[i];
2403 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2404 s[i] = '?';
2405 else s[i] = c;
2406 }
2407 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002408 ret = snprintf( p, n, "%s", s );
2409 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 name = name->next;
2411 }
2412
Paul Bakker23986e52011-04-24 08:57:21 +00002413 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002414}
2415
2416/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002417 * Store the serial in printable form into buf; no more
2418 * than size characters will be written
2419 */
2420int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2421{
Paul Bakker23986e52011-04-24 08:57:21 +00002422 int ret;
2423 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002424 char *p;
2425
2426 p = buf;
2427 n = size;
2428
2429 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002430 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002431
2432 for( i = 0; i < nr; i++ )
2433 {
Paul Bakker93048802011-12-05 14:38:06 +00002434 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002435 continue;
2436
Paul Bakkerdd476992011-01-16 21:34:59 +00002437 ret = snprintf( p, n, "%02X%s",
2438 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2439 SAFE_SNPRINTF();
2440 }
2441
Paul Bakker03c7c252011-11-25 12:37:37 +00002442 if( nr != serial->len )
2443 {
2444 ret = snprintf( p, n, "...." );
2445 SAFE_SNPRINTF();
2446 }
2447
Paul Bakker23986e52011-04-24 08:57:21 +00002448 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002449}
2450
2451/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002452 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002454int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2455 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002456{
Paul Bakker23986e52011-04-24 08:57:21 +00002457 int ret;
2458 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002459 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
2461 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002462 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
Paul Bakkerd98030e2009-05-02 15:13:40 +00002464 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002466 SAFE_SNPRINTF();
2467 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002469 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Paul Bakkerdd476992011-01-16 21:34:59 +00002471 ret = x509parse_serial_gets( p, n, &crt->serial);
2472 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Paul Bakkerd98030e2009-05-02 15:13:40 +00002474 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2475 SAFE_SNPRINTF();
2476 ret = x509parse_dn_gets( p, n, &crt->issuer );
2477 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Paul Bakkerd98030e2009-05-02 15:13:40 +00002479 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2480 SAFE_SNPRINTF();
2481 ret = x509parse_dn_gets( p, n, &crt->subject );
2482 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Paul Bakkerd98030e2009-05-02 15:13:40 +00002484 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2486 crt->valid_from.year, crt->valid_from.mon,
2487 crt->valid_from.day, crt->valid_from.hour,
2488 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002489 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002490
Paul Bakkerd98030e2009-05-02 15:13:40 +00002491 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2493 crt->valid_to.year, crt->valid_to.mon,
2494 crt->valid_to.day, crt->valid_to.hour,
2495 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002496 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Paul Bakkerd98030e2009-05-02 15:13:40 +00002498 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2499 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Paul Bakker27d66162010-03-17 06:56:01 +00002501 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002503 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2504 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2505 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2506 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2507 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2508 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2509 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2510 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2511 default: ret = snprintf( p, n, "???" ); break;
2512 }
2513 SAFE_SNPRINTF();
2514
2515 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002516 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002517 SAFE_SNPRINTF();
2518
Paul Bakker23986e52011-04-24 08:57:21 +00002519 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002520}
2521
Paul Bakker74111d32011-01-15 16:57:55 +00002522/* Compare a given OID string with an OID x509_buf * */
2523#define OID_CMP(oid_str, oid_buf) \
2524 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2525 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2526
2527/*
2528 * Return an informational string describing the given OID
2529 */
2530const char *x509_oid_get_description( x509_buf *oid )
2531{
2532 if ( oid == NULL )
2533 return ( NULL );
2534
2535 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2536 return( STRING_SERVER_AUTH );
2537
2538 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2539 return( STRING_CLIENT_AUTH );
2540
2541 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2542 return( STRING_CODE_SIGNING );
2543
2544 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2545 return( STRING_EMAIL_PROTECTION );
2546
2547 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2548 return( STRING_TIME_STAMPING );
2549
2550 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2551 return( STRING_OCSP_SIGNING );
2552
2553 return( NULL );
2554}
2555
2556/* Return the x.y.z.... style numeric string for the given OID */
2557int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2558{
Paul Bakker23986e52011-04-24 08:57:21 +00002559 int ret;
2560 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002561 unsigned int value;
2562 char *p;
2563
2564 p = buf;
2565 n = size;
2566
2567 /* First byte contains first two dots */
2568 if( oid->len > 0 )
2569 {
2570 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2571 SAFE_SNPRINTF();
2572 }
2573
2574 /* TODO: value can overflow in value. */
2575 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002576 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002577 {
2578 value <<= 7;
2579 value += oid->p[i] & 0x7F;
2580
2581 if( !( oid->p[i] & 0x80 ) )
2582 {
2583 /* Last byte */
2584 ret = snprintf( p, n, ".%d", value );
2585 SAFE_SNPRINTF();
2586 value = 0;
2587 }
2588 }
2589
Paul Bakker23986e52011-04-24 08:57:21 +00002590 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002591}
2592
Paul Bakkerd98030e2009-05-02 15:13:40 +00002593/*
2594 * Return an informational string about the CRL.
2595 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002596int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2597 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002598{
Paul Bakker23986e52011-04-24 08:57:21 +00002599 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002600 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002601 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002602 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002603
2604 p = buf;
2605 n = size;
2606
2607 ret = snprintf( p, n, "%sCRL version : %d",
2608 prefix, crl->version );
2609 SAFE_SNPRINTF();
2610
2611 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2612 SAFE_SNPRINTF();
2613 ret = x509parse_dn_gets( p, n, &crl->issuer );
2614 SAFE_SNPRINTF();
2615
2616 ret = snprintf( p, n, "\n%sthis update : " \
2617 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2618 crl->this_update.year, crl->this_update.mon,
2619 crl->this_update.day, crl->this_update.hour,
2620 crl->this_update.min, crl->this_update.sec );
2621 SAFE_SNPRINTF();
2622
2623 ret = snprintf( p, n, "\n%snext update : " \
2624 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2625 crl->next_update.year, crl->next_update.mon,
2626 crl->next_update.day, crl->next_update.hour,
2627 crl->next_update.min, crl->next_update.sec );
2628 SAFE_SNPRINTF();
2629
2630 entry = &crl->entry;
2631
2632 ret = snprintf( p, n, "\n%sRevoked certificates:",
2633 prefix );
2634 SAFE_SNPRINTF();
2635
Paul Bakker9be19372009-07-27 20:21:53 +00002636 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002637 {
2638 ret = snprintf( p, n, "\n%sserial number: ",
2639 prefix );
2640 SAFE_SNPRINTF();
2641
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002642 ret = x509parse_serial_gets( p, n, &entry->serial);
2643 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002644
Paul Bakkerd98030e2009-05-02 15:13:40 +00002645 ret = snprintf( p, n, " revocation date: " \
2646 "%04d-%02d-%02d %02d:%02d:%02d",
2647 entry->revocation_date.year, entry->revocation_date.mon,
2648 entry->revocation_date.day, entry->revocation_date.hour,
2649 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002650 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002651
2652 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 }
2654
Paul Bakkerd98030e2009-05-02 15:13:40 +00002655 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2656 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Paul Bakker27d66162010-03-17 06:56:01 +00002658 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002659 {
2660 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2661 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2662 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2663 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2664 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2665 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2666 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2667 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2668 default: ret = snprintf( p, n, "???" ); break;
2669 }
2670 SAFE_SNPRINTF();
2671
Paul Bakker1e27bb22009-07-19 20:25:25 +00002672 ret = snprintf( p, n, "\n" );
2673 SAFE_SNPRINTF();
2674
Paul Bakker23986e52011-04-24 08:57:21 +00002675 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002676}
2677
2678/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002679 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002681int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002682{
Paul Bakkercce9d772011-11-18 14:26:47 +00002683 int year, mon, day;
2684 int hour, min, sec;
2685
2686#if defined(_WIN32)
2687 SYSTEMTIME st;
2688
2689 GetLocalTime(&st);
2690
2691 year = st.wYear;
2692 mon = st.wMonth;
2693 day = st.wDay;
2694 hour = st.wHour;
2695 min = st.wMinute;
2696 sec = st.wSecond;
2697#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 struct tm *lt;
2699 time_t tt;
2700
2701 tt = time( NULL );
2702 lt = localtime( &tt );
2703
Paul Bakkercce9d772011-11-18 14:26:47 +00002704 year = lt->tm_year + 1900;
2705 mon = lt->tm_mon + 1;
2706 day = lt->tm_mday;
2707 hour = lt->tm_hour;
2708 min = lt->tm_min;
2709 sec = lt->tm_sec;
2710#endif
2711
2712 if( year > to->year )
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 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002717 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Paul Bakkercce9d772011-11-18 14:26:47 +00002719 if( year == to->year &&
2720 mon == to->mon &&
2721 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002722 return( 1 );
2723
Paul Bakkercce9d772011-11-18 14:26:47 +00002724 if( year == to->year &&
2725 mon == to->mon &&
2726 day == to->day &&
2727 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002728 return( 1 );
2729
Paul Bakkercce9d772011-11-18 14:26:47 +00002730 if( year == to->year &&
2731 mon == to->mon &&
2732 day == to->day &&
2733 hour == to->hour &&
2734 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002735 return( 1 );
2736
Paul Bakkercce9d772011-11-18 14:26:47 +00002737 if( year == to->year &&
2738 mon == to->mon &&
2739 day == to->day &&
2740 hour == to->hour &&
2741 min == to->min &&
2742 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002743 return( 1 );
2744
Paul Bakker40ea7de2009-05-03 10:18:48 +00002745 return( 0 );
2746}
2747
2748/*
2749 * Return 1 if the certificate is revoked, or 0 otherwise.
2750 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002751int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002752{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002753 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002754
2755 while( cur != NULL && cur->serial.len != 0 )
2756 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002757 if( crt->serial.len == cur->serial.len &&
2758 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002759 {
2760 if( x509parse_time_expired( &cur->revocation_date ) )
2761 return( 1 );
2762 }
2763
2764 cur = cur->next;
2765 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
2767 return( 0 );
2768}
2769
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002770/*
2771 * Wrapper for x509 hashes.
2772 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002773 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002774 */
Paul Bakker23986e52011-04-24 08:57:21 +00002775static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 unsigned char *out )
2777{
2778 switch( alg )
2779 {
Paul Bakker40e46942009-01-03 21:51:57 +00002780#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002781 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002782#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002783#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002784 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002785#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002786#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002787 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002788#endif
2789#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002790 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002791#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002792#if defined(POLARSSL_SHA2_C)
2793 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2794 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2795#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002796#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002797 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2798 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2799#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002801 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 break;
2803 }
2804}
2805
2806/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002807 * Check that the given certificate is valid accoring to the CRL.
2808 */
2809static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2810 x509_crl *crl_list)
2811{
2812 int flags = 0;
2813 int hash_id;
2814 unsigned char hash[64];
2815
2816 /*
2817 * TODO: What happens if no CRL is present?
2818 * Suggestion: Revocation state should be unknown if no CRL is present.
2819 * For backwards compatibility this is not yet implemented.
2820 */
2821
2822 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2823 {
2824 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2825 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2826 crl_list->issuer_raw.len ) != 0 )
2827 {
2828 crl_list = crl_list->next;
2829 continue;
2830 }
2831
2832 /*
2833 * Check if CRL is correctly signed by the trusted CA
2834 */
2835 hash_id = crl_list->sig_alg;
2836
2837 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2838
2839 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2840 0, hash, crl_list->sig.p ) == 0 )
2841 {
2842 /*
2843 * CRL is not trusted
2844 */
2845 flags |= BADCRL_NOT_TRUSTED;
2846 break;
2847 }
2848
2849 /*
2850 * Check for validity of CRL (Do not drop out)
2851 */
2852 if( x509parse_time_expired( &crl_list->next_update ) )
2853 flags |= BADCRL_EXPIRED;
2854
2855 /*
2856 * Check if certificate is revoked
2857 */
2858 if( x509parse_revoked(crt, crl_list) )
2859 {
2860 flags |= BADCERT_REVOKED;
2861 break;
2862 }
2863
2864 crl_list = crl_list->next;
2865 }
2866 return flags;
2867}
2868
2869/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 * Verify the certificate validity
2871 */
2872int x509parse_verify( x509_cert *crt,
2873 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002874 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002875 const char *cn, int *flags,
2876 int (*f_vrfy)(void *, x509_cert *, int, int),
2877 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002878{
Paul Bakker23986e52011-04-24 08:57:21 +00002879 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 int hash_id;
2881 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002882 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002884 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Paul Bakker40ea7de2009-05-03 10:18:48 +00002886 *flags = 0;
2887
2888 if( x509parse_time_expired( &crt->valid_to ) )
2889 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
2891 if( cn != NULL )
2892 {
2893 name = &crt->subject;
2894 cn_len = strlen( cn );
2895
2896 while( name != NULL )
2897 {
2898 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2899 memcmp( name->val.p, cn, cn_len ) == 0 &&
2900 name->val.len == cn_len )
2901 break;
2902
2903 name = name->next;
2904 }
2905
2906 if( name == NULL )
2907 *flags |= BADCERT_CN_MISMATCH;
2908 }
2909
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 /*
2911 * Iterate upwards in the given cert chain,
2912 * ignoring any upper cert with CA != TRUE.
2913 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002914 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
2916 pathlen = 1;
2917
Paul Bakker76fd75a2011-01-16 21:12:10 +00002918 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002920 if( parent->ca_istrue == 0 ||
2921 crt->issuer_raw.len != parent->subject_raw.len ||
2922 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 crt->issuer_raw.len ) != 0 )
2924 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002925 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 continue;
2927 }
2928
Paul Bakker27d66162010-03-17 06:56:01 +00002929 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
2931 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2932
Paul Bakker76fd75a2011-01-16 21:12:10 +00002933 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2934 crt->sig.p ) != 0 )
2935 *flags |= BADCERT_NOT_TRUSTED;
2936
2937 /* Check trusted CA's CRL for the given crt */
2938 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002939
2940 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002941 if( NULL != f_vrfy )
2942 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002943 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002944 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002945 else
2946 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002947 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002948 else if( *flags != 0 )
2949 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
2951 pathlen++;
2952
Paul Bakker76fd75a2011-01-16 21:12:10 +00002953 crt = parent;
2954 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002955 }
2956
2957 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002958 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002960 *flags |= BADCERT_NOT_TRUSTED;
2961
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002962 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002963 {
2964 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2965 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2966 crt->issuer_raw.len ) != 0 )
2967 {
2968 trust_ca = trust_ca->next;
2969 continue;
2970 }
2971
2972 if( trust_ca->max_pathlen > 0 &&
2973 trust_ca->max_pathlen < pathlen )
2974 break;
2975
Paul Bakker27d66162010-03-17 06:56:01 +00002976 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002977
2978 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2979
2980 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2981 0, hash, crt->sig.p ) == 0 )
2982 {
2983 /*
2984 * cert. is signed by a trusted CA
2985 */
2986 *flags &= ~BADCERT_NOT_TRUSTED;
2987 break;
2988 }
2989
2990 trust_ca = trust_ca->next;
2991 }
2992
Paul Bakker76fd75a2011-01-16 21:12:10 +00002993 /* Check trusted CA's CRL for the given crt */
2994 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002995
2996 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00002997 if( NULL != f_vrfy )
2998 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002999 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003000 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003001 else
3002 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003003 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003004 else if( *flags != 0 )
3005 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003006
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 return( 0 );
3008}
3009
3010/*
3011 * Unallocate all certificate data
3012 */
3013void x509_free( x509_cert *crt )
3014{
3015 x509_cert *cert_cur = crt;
3016 x509_cert *cert_prv;
3017 x509_name *name_cur;
3018 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003019 x509_sequence *seq_cur;
3020 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
3022 if( crt == NULL )
3023 return;
3024
3025 do
3026 {
3027 rsa_free( &cert_cur->rsa );
3028
3029 name_cur = cert_cur->issuer.next;
3030 while( name_cur != NULL )
3031 {
3032 name_prv = name_cur;
3033 name_cur = name_cur->next;
3034 memset( name_prv, 0, sizeof( x509_name ) );
3035 free( name_prv );
3036 }
3037
3038 name_cur = cert_cur->subject.next;
3039 while( name_cur != NULL )
3040 {
3041 name_prv = name_cur;
3042 name_cur = name_cur->next;
3043 memset( name_prv, 0, sizeof( x509_name ) );
3044 free( name_prv );
3045 }
3046
Paul Bakker74111d32011-01-15 16:57:55 +00003047 seq_cur = cert_cur->ext_key_usage.next;
3048 while( seq_cur != NULL )
3049 {
3050 seq_prv = seq_cur;
3051 seq_cur = seq_cur->next;
3052 memset( seq_prv, 0, sizeof( x509_sequence ) );
3053 free( seq_prv );
3054 }
3055
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 if( cert_cur->raw.p != NULL )
3057 {
3058 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3059 free( cert_cur->raw.p );
3060 }
3061
3062 cert_cur = cert_cur->next;
3063 }
3064 while( cert_cur != NULL );
3065
3066 cert_cur = crt;
3067 do
3068 {
3069 cert_prv = cert_cur;
3070 cert_cur = cert_cur->next;
3071
3072 memset( cert_prv, 0, sizeof( x509_cert ) );
3073 if( cert_prv != crt )
3074 free( cert_prv );
3075 }
3076 while( cert_cur != NULL );
3077}
3078
Paul Bakkerd98030e2009-05-02 15:13:40 +00003079/*
3080 * Unallocate all CRL data
3081 */
3082void x509_crl_free( x509_crl *crl )
3083{
3084 x509_crl *crl_cur = crl;
3085 x509_crl *crl_prv;
3086 x509_name *name_cur;
3087 x509_name *name_prv;
3088 x509_crl_entry *entry_cur;
3089 x509_crl_entry *entry_prv;
3090
3091 if( crl == NULL )
3092 return;
3093
3094 do
3095 {
3096 name_cur = crl_cur->issuer.next;
3097 while( name_cur != NULL )
3098 {
3099 name_prv = name_cur;
3100 name_cur = name_cur->next;
3101 memset( name_prv, 0, sizeof( x509_name ) );
3102 free( name_prv );
3103 }
3104
3105 entry_cur = crl_cur->entry.next;
3106 while( entry_cur != NULL )
3107 {
3108 entry_prv = entry_cur;
3109 entry_cur = entry_cur->next;
3110 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3111 free( entry_prv );
3112 }
3113
3114 if( crl_cur->raw.p != NULL )
3115 {
3116 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3117 free( crl_cur->raw.p );
3118 }
3119
3120 crl_cur = crl_cur->next;
3121 }
3122 while( crl_cur != NULL );
3123
3124 crl_cur = crl;
3125 do
3126 {
3127 crl_prv = crl_cur;
3128 crl_cur = crl_cur->next;
3129
3130 memset( crl_prv, 0, sizeof( x509_crl ) );
3131 if( crl_prv != crl )
3132 free( crl_prv );
3133 }
3134 while( crl_cur != NULL );
3135}
3136
Paul Bakker40e46942009-01-03 21:51:57 +00003137#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Paul Bakker40e46942009-01-03 21:51:57 +00003139#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
3141/*
3142 * Checkup routine
3143 */
3144int x509_self_test( int verbose )
3145{
Paul Bakker5690efc2011-05-26 13:16:06 +00003146#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003147 int ret;
3148 int flags;
3149 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003150 x509_cert cacert;
3151 x509_cert clicert;
3152 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003153#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003154 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003155#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003156
3157 if( verbose != 0 )
3158 printf( " X.509 certificate load: " );
3159
3160 memset( &clicert, 0, sizeof( x509_cert ) );
3161
3162 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003163 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 if( ret != 0 )
3165 {
3166 if( verbose != 0 )
3167 printf( "failed\n" );
3168
3169 return( ret );
3170 }
3171
3172 memset( &cacert, 0, sizeof( x509_cert ) );
3173
3174 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003175 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 if( ret != 0 )
3177 {
3178 if( verbose != 0 )
3179 printf( "failed\n" );
3180
3181 return( ret );
3182 }
3183
3184 if( verbose != 0 )
3185 printf( "passed\n X.509 private key load: " );
3186
3187 i = strlen( test_ca_key );
3188 j = strlen( test_ca_pwd );
3189
Paul Bakker66b78b22011-03-25 14:22:50 +00003190 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3191
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 if( ( ret = x509parse_key( &rsa,
3193 (unsigned char *) test_ca_key, i,
3194 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3195 {
3196 if( verbose != 0 )
3197 printf( "failed\n" );
3198
3199 return( ret );
3200 }
3201
3202 if( verbose != 0 )
3203 printf( "passed\n X.509 signature verify: ");
3204
Paul Bakker23986e52011-04-24 08:57:21 +00003205 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 if( ret != 0 )
3207 {
Paul Bakker23986e52011-04-24 08:57:21 +00003208 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 if( verbose != 0 )
3210 printf( "failed\n" );
3211
3212 return( ret );
3213 }
3214
Paul Bakker5690efc2011-05-26 13:16:06 +00003215#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003217 printf( "passed\n X.509 DHM parameter load: " );
3218
3219 i = strlen( test_dhm_params );
3220 j = strlen( test_ca_pwd );
3221
3222 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3223 {
3224 if( verbose != 0 )
3225 printf( "failed\n" );
3226
3227 return( ret );
3228 }
3229
3230 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003232#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
3234 x509_free( &cacert );
3235 x509_free( &clicert );
3236 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003237#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003238 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003239#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
3241 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003242#else
3243 ((void) verbose);
3244 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003246}
3247
3248#endif
3249
3250#endif